From 2c0b1d451388eda5cce490112a944ec5491b4217 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Mon, 14 Apr 2014 15:16:05 +0900 Subject: [PATCH] fix next|prev_permutation implementations --- libs/algorithm/test/next_permutation.cpp | 118 +++++++++--------- libs/algorithm/test/prev_permutation.cpp | 118 +++++++++--------- sprout/algorithm/cxx14/detail/heap_tool.hpp | 1 - sprout/algorithm/fixed/next_permutation.hpp | 16 ++- sprout/algorithm/fixed/prev_permutation.hpp | 16 ++- sprout/bit/parity.hpp | 6 - sprout/detail/container_complate_backward.hpp | 8 +- tools/testspr/test.sh | 8 +- 8 files changed, 150 insertions(+), 141 deletions(-) diff --git a/libs/algorithm/test/next_permutation.cpp b/libs/algorithm/test/next_permutation.cpp index 76c1cbda..d2801330 100644 --- a/libs/algorithm/test/next_permutation.cpp +++ b/libs/algorithm/test/next_permutation.cpp @@ -43,35 +43,34 @@ namespace testspr { } // 順列 // 範囲の切り出し - // !!! -// { -// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::next_permutation( -// sprout::sub(arr1, 2, 4) -// ); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// permutation.first, -// array{{2, 4, 3}} -// )); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// sprout::get_internal(permutation.first), -// array{{1, 2, 4, 3, 5}} -// )); -// TESTSPR_BOTH_ASSERT(permutation.second); -// } -// { -// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::next_permutation( -// sprout::sub(arr1, 2, 4) -// ); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// permutation.first, -// array{{2, 4, 3}} -// )); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// sprout::get_internal(permutation.first), -// array{{1, 2, 4, 3, 5}} -// )); -// TESTSPR_BOTH_ASSERT(permutation.second); -// } + { + SPROUT_STATIC_CONSTEXPR auto permutation = sprout::next_permutation( + sprout::sub(arr1, 1, 4) + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + permutation.first, + array{{2, 4, 3}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(permutation.first), + array{{1, 2, 4, 3, 5}} + )); + TESTSPR_BOTH_ASSERT(permutation.second); + } + { + SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::next_permutation( + sprout::sub(arr1, 1, 4) + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + permutation.first, + array{{2, 4, 3}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(permutation.first), + array{{1, 2, 4, 3, 5}} + )); + TESTSPR_BOTH_ASSERT(permutation.second); + } } { SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 2, 3, 4, 5}}; @@ -101,37 +100,36 @@ namespace testspr { } // 順列 // 範囲の切り出し - // !!! -// { -// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::next_permutation( -// sprout::sub(arr1, 2, 4), -// testspr::less() -// ); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// permutation.first, -// array{{2, 4, 3}} -// )); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// sprout::get_internal(permutation.first), -// array{{1, 2, 4, 3, 5}} -// )); -// TESTSPR_BOTH_ASSERT(permutation.second); -// } -// { -// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::next_permutation( -// sprout::sub(arr1, 2, 4), -// testspr::less() -// ); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// permutation.first, -// array{{2, 4, 3}} -// )); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// sprout::get_internal(permutation.first), -// array{{1, 2, 4, 3, 5}} -// )); -// TESTSPR_BOTH_ASSERT(permutation.second); -// } + { + SPROUT_STATIC_CONSTEXPR auto permutation = sprout::next_permutation( + sprout::sub(arr1, 1, 4), + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + permutation.first, + array{{2, 4, 3}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(permutation.first), + array{{1, 2, 4, 3, 5}} + )); + TESTSPR_BOTH_ASSERT(permutation.second); + } + { + SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::next_permutation( + sprout::sub(arr1, 1, 4), + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + permutation.first, + array{{2, 4, 3}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(permutation.first), + array{{1, 2, 4, 3, 5}} + )); + TESTSPR_BOTH_ASSERT(permutation.second); + } } } } // namespace testspr diff --git a/libs/algorithm/test/prev_permutation.cpp b/libs/algorithm/test/prev_permutation.cpp index 425fd9f7..115c492b 100644 --- a/libs/algorithm/test/prev_permutation.cpp +++ b/libs/algorithm/test/prev_permutation.cpp @@ -43,35 +43,34 @@ namespace testspr { } // 順列 // 範囲の切り出し - // !!! -// { -// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::prev_permutation( -// sprout::sub(arr1, 2, 4) -// ); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// permutation.first, -// array{{4, 2, 3}} -// )); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// sprout::get_internal(permutation.first), -// array{{5, 4, 2, 3, 1}} -// )); -// TESTSPR_BOTH_ASSERT(permutation.second); -// } -// { -// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::prev_permutation( -// sprout::sub(arr1, 2, 4) -// ); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// permutation.first, -// array{{4, 2, 3}} -// )); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// sprout::get_internal(permutation.first), -// array{{5, 4, 2, 3, 1}} -// )); -// TESTSPR_BOTH_ASSERT(permutation.second); -// } + { + SPROUT_STATIC_CONSTEXPR auto permutation = sprout::prev_permutation( + sprout::sub(arr1, 1, 4) + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + permutation.first, + array{{4, 2, 3}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(permutation.first), + array{{5, 4, 2, 3, 1}} + )); + TESTSPR_BOTH_ASSERT(permutation.second); + } + { + SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::prev_permutation( + sprout::sub(arr1, 1, 4) + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + permutation.first, + array{{4, 2, 3}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(permutation.first), + array{{5, 4, 2, 3, 1}} + )); + TESTSPR_BOTH_ASSERT(permutation.second); + } } { SPROUT_STATIC_CONSTEXPR auto arr1 = array{{5, 4, 3, 2, 1}}; @@ -101,37 +100,36 @@ namespace testspr { } // 順列 // 範囲の切り出し - // !!! -// { -// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::prev_permutation( -// sprout::sub(arr1, 2, 4), -// testspr::less() -// ); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// permutation.first, -// array{{4, 2, 3}} -// )); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// sprout::get_internal(permutation.first), -// array{{5, 4, 2, 3, 1}} -// )); -// TESTSPR_BOTH_ASSERT(permutation.second); -// } -// { -// SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::prev_permutation( -// sprout::sub(arr1, 2, 4), -// testspr::less() -// ); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// permutation.first, -// array{{4, 2, 3}} -// )); -// TESTSPR_BOTH_ASSERT(testspr::equal( -// sprout::get_internal(permutation.first), -// array{{5, 4, 2, 3, 1}} -// )); -// TESTSPR_BOTH_ASSERT(permutation.second); -// } + { + SPROUT_STATIC_CONSTEXPR auto permutation = sprout::prev_permutation( + sprout::sub(arr1, 1, 4), + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + permutation.first, + array{{4, 2, 3}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(permutation.first), + array{{5, 4, 2, 3, 1}} + )); + TESTSPR_BOTH_ASSERT(permutation.second); + } + { + SPROUT_STATIC_CONSTEXPR auto permutation = sprout::fit::prev_permutation( + sprout::sub(arr1, 1, 4), + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + permutation.first, + array{{4, 2, 3}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(permutation.first), + array{{5, 4, 2, 3, 1}} + )); + TESTSPR_BOTH_ASSERT(permutation.second); + } } } } // namespace testspr diff --git a/sprout/algorithm/cxx14/detail/heap_tool.hpp b/sprout/algorithm/cxx14/detail/heap_tool.hpp index d31dc293..1c5ec0db 100644 --- a/sprout/algorithm/cxx14/detail/heap_tool.hpp +++ b/sprout/algorithm/cxx14/detail/heap_tool.hpp @@ -59,7 +59,6 @@ namespace sprout { typename std::iterator_traits::difference_type len ) { - typedef typename std::iterator_traits::difference_type difference_type; typedef typename std::iterator_traits::value_type value_type; if (len > 1) { len = (len - 2) / 2; diff --git a/sprout/algorithm/fixed/next_permutation.hpp b/sprout/algorithm/fixed/next_permutation.hpp index 89fca590..f2cc63d0 100644 --- a/sprout/algorithm/fixed/next_permutation.hpp +++ b/sprout/algorithm/fixed/next_permutation.hpp @@ -15,8 +15,14 @@ #include #include #include -#include -#include +#include +#if SPROUT_GCC_EARLIER(4, 8, 2) +# include +# include +#else +# include +# include +#endif #include #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT @@ -27,7 +33,11 @@ namespace sprout { inline SPROUT_CONSTEXPR Result next_permutation_impl_4(Container const& cont, Difference d) { return Result( - sprout::get_internal(sprout::fixed::reverse(sprout::sub_array(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 ); } diff --git a/sprout/algorithm/fixed/prev_permutation.hpp b/sprout/algorithm/fixed/prev_permutation.hpp index 3b1e5b50..a90b7622 100644 --- a/sprout/algorithm/fixed/prev_permutation.hpp +++ b/sprout/algorithm/fixed/prev_permutation.hpp @@ -15,8 +15,14 @@ #include #include #include -#include -#include +#include +#if SPROUT_GCC_EARLIER(4, 8, 2) +# include +# include +#else +# include +# include +#endif #include #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT @@ -27,7 +33,11 @@ namespace sprout { inline SPROUT_CONSTEXPR Result prev_permutation_impl_4(Container const& cont, Difference d) { return Result( - sprout::get_internal(sprout::fixed::reverse(sprout::sub_array(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 ); } diff --git a/sprout/bit/parity.hpp b/sprout/bit/parity.hpp index 344ab11a..b72a502b 100644 --- a/sprout/bit/parity.hpp +++ b/sprout/bit/parity.hpp @@ -12,7 +12,6 @@ #include #include #include -//#include #include namespace sprout { @@ -31,11 +30,6 @@ namespace sprout { return __builtin_parityll(x); } # endif -// template -// inline SPROUT_CONSTEXPR int -// parity(Integral x) { -// return sprout::popcount(x) & 1; -// } template inline SPROUT_CONSTEXPR typename std::enable_if< (N == 1), diff --git a/sprout/detail/container_complate_backward.hpp b/sprout/detail/container_complate_backward.hpp index b7330a60..c54f7388 100644 --- a/sprout/detail/container_complate_backward.hpp +++ b/sprout/detail/container_complate_backward.hpp @@ -31,7 +31,7 @@ namespace sprout { typename sprout::fixed::results::algorithm::type >::type 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 inline SPROUT_CONSTEXPR typename std::enable_if< @@ -58,8 +58,8 @@ namespace sprout { ) { return remain != 0 - ? 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_1(result, remain - 1, args..., *sprout::prev(sprout::internal_end(result), remain)) + : sprout::detail::container_complate_backward_2(result, args...) ; } template @@ -76,7 +76,7 @@ namespace sprout { typename sprout::fixed::results::algorithm::type >::type 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 sprout diff --git a/tools/testspr/test.sh b/tools/testspr/test.sh index 6a2e2c2c..9884c997 100755 --- a/tools/testspr/test.sh +++ b/tools/testspr/test.sh @@ -148,16 +148,16 @@ fi if [ -z "${clang_version}" ]; then clang_version=" " fi -for user_macro in ${user_macros}; do +for user_macro in ${user_macros[*]}; do define_options="${define_options} -D${user_macro}" done -for include_path in ${include_paths}; do +for include_path in ${include_paths[*]}; do include_options="${include_options} -I${include_path}" done all_options="-v -Wall -pedantic ${define_options} ${include_options} ${common_options[*]}" vo=0 vkey="" -for option in ${compiler_options}; do +for option in ${compiler_options[*]}; do if [ ${vo} -eq 0 ]; then vkey=${option} vo=1 @@ -166,7 +166,7 @@ for option in ${compiler_options}; do vo=0 fi done -for option in ${version_options}; do +for option in ${version_options[*]}; do if [ ${vo} -eq 0 ]; then vkey=${option} vo=1