1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-07-02 14:04:09 +00:00

fix next|prev_permutation implementations

This commit is contained in:
bolero-MURAKAMI 2014-04-14 15:16:05 +09:00
parent 057dec630c
commit 2c0b1d4513
8 changed files with 150 additions and 141 deletions

View file

@ -43,35 +43,34 @@ namespace testspr {
} }
// 順列 // 順列
// 範囲の切り出し // 範囲の切り出し
// !!! {
// { SPROUT_STATIC_CONSTEXPR auto permutation = sprout::next_permutation(
// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::next_permutation( sprout::sub(arr1, 1, 4)
// sprout::sub(arr1, 2, 4) );
// ); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( permutation.first,
// permutation.first, array<int, 3>{{2, 4, 3}}
// array<int, 3>{{2, 4, 3}} ));
// )); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( sprout::get_internal(permutation.first),
// sprout::get_internal(permutation.first), array<int, 5>{{1, 2, 4, 3, 5}}
// array<int, 5>{{1, 2, 4, 3, 5}} ));
// )); TESTSPR_BOTH_ASSERT(permutation.second);
// TESTSPR_BOTH_ASSERT(permutation.second); }
// } {
// { SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::next_permutation(
// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::next_permutation( sprout::sub(arr1, 1, 4)
// sprout::sub(arr1, 2, 4) );
// ); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( permutation.first,
// permutation.first, array<int, 3>{{2, 4, 3}}
// array<int, 3>{{2, 4, 3}} ));
// )); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( sprout::get_internal(permutation.first),
// sprout::get_internal(permutation.first), array<int, 5>{{1, 2, 4, 3, 5}}
// array<int, 5>{{1, 2, 4, 3, 5}} ));
// )); TESTSPR_BOTH_ASSERT(permutation.second);
// TESTSPR_BOTH_ASSERT(permutation.second); }
// }
} }
{ {
SPROUT_STATIC_CONSTEXPR auto arr1 = array<int, 5>{{1, 2, 3, 4, 5}}; SPROUT_STATIC_CONSTEXPR auto arr1 = array<int, 5>{{1, 2, 3, 4, 5}};
@ -101,37 +100,36 @@ namespace testspr {
} }
// 順列 // 順列
// 範囲の切り出し // 範囲の切り出し
// !!! {
// { SPROUT_STATIC_CONSTEXPR auto permutation = sprout::next_permutation(
// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::next_permutation( sprout::sub(arr1, 1, 4),
// sprout::sub(arr1, 2, 4), testspr::less<int>()
// testspr::less<int>() );
// ); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( permutation.first,
// permutation.first, array<int, 3>{{2, 4, 3}}
// array<int, 3>{{2, 4, 3}} ));
// )); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( sprout::get_internal(permutation.first),
// sprout::get_internal(permutation.first), array<int, 5>{{1, 2, 4, 3, 5}}
// array<int, 5>{{1, 2, 4, 3, 5}} ));
// )); TESTSPR_BOTH_ASSERT(permutation.second);
// TESTSPR_BOTH_ASSERT(permutation.second); }
// } {
// { SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::next_permutation(
// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::next_permutation( sprout::sub(arr1, 1, 4),
// sprout::sub(arr1, 2, 4), testspr::less<int>()
// testspr::less<int>() );
// ); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( permutation.first,
// permutation.first, array<int, 3>{{2, 4, 3}}
// array<int, 3>{{2, 4, 3}} ));
// )); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( sprout::get_internal(permutation.first),
// sprout::get_internal(permutation.first), array<int, 5>{{1, 2, 4, 3, 5}}
// array<int, 5>{{1, 2, 4, 3, 5}} ));
// )); TESTSPR_BOTH_ASSERT(permutation.second);
// TESTSPR_BOTH_ASSERT(permutation.second); }
// }
} }
} }
} // namespace testspr } // namespace testspr

View file

@ -43,35 +43,34 @@ namespace testspr {
} }
// 順列 // 順列
// 範囲の切り出し // 範囲の切り出し
// !!! {
// { SPROUT_STATIC_CONSTEXPR auto permutation = sprout::prev_permutation(
// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::prev_permutation( sprout::sub(arr1, 1, 4)
// sprout::sub(arr1, 2, 4) );
// ); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( permutation.first,
// permutation.first, array<int, 3>{{4, 2, 3}}
// array<int, 3>{{4, 2, 3}} ));
// )); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( sprout::get_internal(permutation.first),
// sprout::get_internal(permutation.first), array<int, 5>{{5, 4, 2, 3, 1}}
// array<int, 5>{{5, 4, 2, 3, 1}} ));
// )); TESTSPR_BOTH_ASSERT(permutation.second);
// TESTSPR_BOTH_ASSERT(permutation.second); }
// } {
// { SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::prev_permutation(
// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::prev_permutation( sprout::sub(arr1, 1, 4)
// sprout::sub(arr1, 2, 4) );
// ); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( permutation.first,
// permutation.first, array<int, 3>{{4, 2, 3}}
// array<int, 3>{{4, 2, 3}} ));
// )); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( sprout::get_internal(permutation.first),
// sprout::get_internal(permutation.first), array<int, 5>{{5, 4, 2, 3, 1}}
// array<int, 5>{{5, 4, 2, 3, 1}} ));
// )); TESTSPR_BOTH_ASSERT(permutation.second);
// TESTSPR_BOTH_ASSERT(permutation.second); }
// }
} }
{ {
SPROUT_STATIC_CONSTEXPR auto arr1 = array<int, 5>{{5, 4, 3, 2, 1}}; SPROUT_STATIC_CONSTEXPR auto arr1 = array<int, 5>{{5, 4, 3, 2, 1}};
@ -101,37 +100,36 @@ namespace testspr {
} }
// 順列 // 順列
// 範囲の切り出し // 範囲の切り出し
// !!! {
// { SPROUT_STATIC_CONSTEXPR auto permutation = sprout::prev_permutation(
// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::prev_permutation( sprout::sub(arr1, 1, 4),
// sprout::sub(arr1, 2, 4), testspr::less<int>()
// testspr::less<int>() );
// ); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( permutation.first,
// permutation.first, array<int, 3>{{4, 2, 3}}
// array<int, 3>{{4, 2, 3}} ));
// )); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( sprout::get_internal(permutation.first),
// sprout::get_internal(permutation.first), array<int, 5>{{5, 4, 2, 3, 1}}
// array<int, 5>{{5, 4, 2, 3, 1}} ));
// )); TESTSPR_BOTH_ASSERT(permutation.second);
// TESTSPR_BOTH_ASSERT(permutation.second); }
// } {
// { SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::prev_permutation(
// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::prev_permutation( sprout::sub(arr1, 1, 4),
// sprout::sub(arr1, 2, 4), testspr::less<int>()
// testspr::less<int>() );
// ); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( permutation.first,
// permutation.first, array<int, 3>{{4, 2, 3}}
// array<int, 3>{{4, 2, 3}} ));
// )); TESTSPR_BOTH_ASSERT(testspr::equal(
// TESTSPR_BOTH_ASSERT(testspr::equal( sprout::get_internal(permutation.first),
// sprout::get_internal(permutation.first), array<int, 5>{{5, 4, 2, 3, 1}}
// array<int, 5>{{5, 4, 2, 3, 1}} ));
// )); TESTSPR_BOTH_ASSERT(permutation.second);
// TESTSPR_BOTH_ASSERT(permutation.second); }
// }
} }
} }
} // namespace testspr } // namespace testspr

View file

@ -59,7 +59,6 @@ namespace sprout {
typename std::iterator_traits<RandomAccessIterator>::difference_type len typename std::iterator_traits<RandomAccessIterator>::difference_type len
) )
{ {
typedef typename std::iterator_traits<RandomAccessIterator>::difference_type difference_type;
typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type; typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type;
if (len > 1) { if (len > 1) {
len = (len - 2) / 2; len = (len - 2) / 2;

View file

@ -15,8 +15,14 @@
#include <sprout/algorithm/fixed/results.hpp> #include <sprout/algorithm/fixed/results.hpp>
#include <sprout/algorithm/fixed/swap_element.hpp> #include <sprout/algorithm/fixed/swap_element.hpp>
#include <sprout/algorithm/fixed/reverse_copy.hpp> #include <sprout/algorithm/fixed/reverse_copy.hpp>
#include <sprout/algorithm/fixed/reverse.hpp> #include <sprout/detail/predef.hpp>
#include <sprout/sub_array/sub_array.hpp> #if SPROUT_GCC_EARLIER(4, 8, 2)
# include <sprout/algorithm/fit/reverse_copy.hpp>
# include <sprout/range/algorithm/fixed/copy_backward.hpp>
#else
# include <sprout/iterator/reverse_iterator.hpp>
# include <sprout/algorithm/fixed/copy_backward.hpp>
#endif
#include <sprout/utility/pair/pair.hpp> #include <sprout/utility/pair/pair.hpp>
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
@ -27,7 +33,11 @@ namespace sprout {
inline SPROUT_CONSTEXPR Result inline SPROUT_CONSTEXPR Result
next_permutation_impl_4(Container const& cont, Difference d) { next_permutation_impl_4(Container const& cont, Difference d) {
return Result( return Result(
sprout::get_internal(sprout::fixed::reverse(sprout::sub_array<Container const&>(cont, d, sprout::size(cont)))), #if SPROUT_GCC_EARLIER(4, 8, 2)
sprout::range::fixed::copy_backward(sprout::fit::reverse_copy(sprout::next(sprout::begin(cont), d), sprout::end(cont), cont), cont),
#else
sprout::fixed::copy_backward(sprout::make_reverse_iterator(sprout::end(cont)), sprout::make_reverse_iterator(sprout::next(sprout::begin(cont), d)), cont),
#endif
true true
); );
} }

View file

@ -15,8 +15,14 @@
#include <sprout/algorithm/fixed/results.hpp> #include <sprout/algorithm/fixed/results.hpp>
#include <sprout/algorithm/fixed/swap_element.hpp> #include <sprout/algorithm/fixed/swap_element.hpp>
#include <sprout/algorithm/fixed/reverse_copy.hpp> #include <sprout/algorithm/fixed/reverse_copy.hpp>
#include <sprout/algorithm/fixed/reverse.hpp> #include <sprout/detail/predef.hpp>
#include <sprout/sub_array/sub_array.hpp> #if SPROUT_GCC_EARLIER(4, 8, 2)
# include <sprout/algorithm/fit/reverse_copy.hpp>
# include <sprout/range/algorithm/fixed/copy_backward.hpp>
#else
# include <sprout/iterator/reverse_iterator.hpp>
# include <sprout/algorithm/fixed/copy_backward.hpp>
#endif
#include <sprout/utility/pair/pair.hpp> #include <sprout/utility/pair/pair.hpp>
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
@ -27,7 +33,11 @@ namespace sprout {
inline SPROUT_CONSTEXPR Result inline SPROUT_CONSTEXPR Result
prev_permutation_impl_4(Container const& cont, Difference d) { prev_permutation_impl_4(Container const& cont, Difference d) {
return Result( return Result(
sprout::get_internal(sprout::fixed::reverse(sprout::sub_array<Container const&>(cont, d, sprout::size(cont)))), #if SPROUT_GCC_EARLIER(4, 8, 2)
sprout::range::fixed::copy_backward(sprout::fit::reverse_copy(sprout::next(sprout::begin(cont), d), sprout::end(cont), cont), cont),
#else
sprout::fixed::copy_backward(sprout::make_reverse_iterator(sprout::end(cont)), sprout::make_reverse_iterator(sprout::next(sprout::begin(cont), d)), cont),
#endif
true true
); );
} }

View file

@ -12,7 +12,6 @@
#include <climits> #include <climits>
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
//#include <sprout/bit/popcount.hpp>
#include <sprout/bit/shlr.hpp> #include <sprout/bit/shlr.hpp>
namespace sprout { namespace sprout {
@ -31,11 +30,6 @@ namespace sprout {
return __builtin_parityll(x); return __builtin_parityll(x);
} }
# endif # endif
// template<typename Integral>
// inline SPROUT_CONSTEXPR int
// parity(Integral x) {
// return sprout::popcount(x) & 1;
// }
template<std::size_t N, typename Integral> template<std::size_t N, typename Integral>
inline SPROUT_CONSTEXPR typename std::enable_if< inline SPROUT_CONSTEXPR typename std::enable_if<
(N == 1), (N == 1),

View file

@ -31,7 +31,7 @@ namespace sprout {
typename sprout::fixed::results::algorithm<Result>::type typename sprout::fixed::results::algorithm<Result>::type
>::type >::type
container_complate_backward_2(Result const& result, Args const&... args) { container_complate_backward_2(Result const& result, Args const&... args) {
return container_complate_backward_2(result, *sprout::prev(sprout::internal_end(result), sizeof...(Args) + 1), args...); return sprout::detail::container_complate_backward_2(result, *sprout::prev(sprout::internal_end(result), sizeof...(Args) + 1), args...);
} }
template<typename Result, typename... Args> template<typename Result, typename... Args>
inline SPROUT_CONSTEXPR typename std::enable_if< inline SPROUT_CONSTEXPR typename std::enable_if<
@ -58,8 +58,8 @@ namespace sprout {
) )
{ {
return remain != 0 return remain != 0
? container_complate_backward_1(result, remain - 1, args..., *sprout::prev(sprout::internal_end(result), remain)) ? sprout::detail::container_complate_backward_1(result, remain - 1, args..., *sprout::prev(sprout::internal_end(result), remain))
: container_complate_backward_2(result, args...) : sprout::detail::container_complate_backward_2(result, args...)
; ;
} }
template<typename Result, typename... Args> template<typename Result, typename... Args>
@ -76,7 +76,7 @@ namespace sprout {
typename sprout::fixed::results::algorithm<Result>::type typename sprout::fixed::results::algorithm<Result>::type
>::type >::type
container_complate_backward(Result const& result, Args const&... args) { container_complate_backward(Result const& result, Args const&... args) {
return container_complate_backward_1(result, sprout::internal_end_offset_backward(result), args...); return sprout::detail::container_complate_backward_1(result, sprout::internal_end_offset_backward(result), args...);
} }
} // namespace detail } // namespace detail
} // namespace sprout } // namespace sprout

View file

@ -148,16 +148,16 @@ fi
if [ -z "${clang_version}" ]; then if [ -z "${clang_version}" ]; then
clang_version=" " clang_version=" "
fi fi
for user_macro in ${user_macros}; do for user_macro in ${user_macros[*]}; do
define_options="${define_options} -D${user_macro}" define_options="${define_options} -D${user_macro}"
done done
for include_path in ${include_paths}; do for include_path in ${include_paths[*]}; do
include_options="${include_options} -I${include_path}" include_options="${include_options} -I${include_path}"
done done
all_options="-v -Wall -pedantic ${define_options} ${include_options} ${common_options[*]}" all_options="-v -Wall -pedantic ${define_options} ${include_options} ${common_options[*]}"
vo=0 vo=0
vkey="" vkey=""
for option in ${compiler_options}; do for option in ${compiler_options[*]}; do
if [ ${vo} -eq 0 ]; then if [ ${vo} -eq 0 ]; then
vkey=${option} vkey=${option}
vo=1 vo=1
@ -166,7 +166,7 @@ for option in ${compiler_options}; do
vo=0 vo=0
fi fi
done done
for option in ${version_options}; do for option in ${version_options[*]}; do
if [ ${vo} -eq 0 ]; then if [ ${vo} -eq 0 ]; then
vkey=${option} vkey=${option}
vo=1 vo=1