From b8c536cce270ccafcc5193756adb8e9a28b81a0c Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Fri, 28 Oct 2011 00:16:46 +0900 Subject: [PATCH] =?UTF-8?q?sprout/algorithm/fixed/stable=5Fsort.hpp=20?= =?UTF-8?q?=E5=AE=9F=E8=A3=85=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sprout/algorithm/fixed/stable_sort.hpp | 260 ++++++++++--------------- 1 file changed, 105 insertions(+), 155 deletions(-) diff --git a/sprout/algorithm/fixed/stable_sort.hpp b/sprout/algorithm/fixed/stable_sort.hpp index 371ed858..dcbcb2dc 100644 --- a/sprout/algorithm/fixed/stable_sort.hpp +++ b/sprout/algorithm/fixed/stable_sort.hpp @@ -3,200 +3,150 @@ #include #include -#include #include #include -#include +#include #include -#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT_DETAIL +#include namespace sprout { namespace fixed { namespace detail { - template - SPROUT_CONSTEXPR inline typename std::enable_if< - sizeof...(Indexes) != 0 && sizeof...(SortedIndexes) != 0, - typename sprout::fixed::result_of::algorithm::type - >::type stable_sort_impl_4( + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type stable_sort_impl_restart( Container const& cont, Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple + typename sprout::fixed_container_traits::size_type size, + typename sprout::fixed_container_traits::value_type const& prev_value, + Args const&... args ); - template - SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type stable_sort_impl_finish( - Container const& cont, - sprout::index_tuple - ) - { - return sprout::remake_clone(cont, sprout::size(cont), (*sprout::next(sprout::fixed_begin(cont), Indexes))...); - } - template + template SPROUT_CONSTEXPR inline typename std::enable_if< - sizeof...(Indexes) == 0 && sizeof...(SortedIndexes) == 0, + sprout::fixed_container_traits::fixed_size == sizeof...(Args), typename sprout::fixed::result_of::algorithm::type - >::type stable_sort_impl_4( + >::type stable_sort_impl_synonym( Container const& cont, Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple + typename sprout::fixed_container_traits::size_type size, + typename sprout::fixed_container_traits::const_iterator first, + typename sprout::fixed_container_traits::const_iterator last, + typename sprout::fixed_container_traits::value_type const& value, + typename sprout::fixed_container_traits::size_type count, + Args const&... args ) { - return comp(*sprout::next(sprout::fixed_begin(cont), I1), *sprout::next(sprout::fixed_begin(cont), I2)) - ? stable_sort_impl_finish(cont, sprout::index_tuple()) - : stable_sort_impl_finish(cont, sprout::index_tuple()) + return sprout::remake_clone(cont, sprout::size(cont), args...); + } + template + SPROUT_CONSTEXPR inline typename std::enable_if< + sprout::fixed_container_traits::fixed_size != sizeof...(Args), + typename sprout::fixed::result_of::algorithm::type + >::type stable_sort_impl_synonym( + Container const& cont, + Compare comp, + typename sprout::fixed_container_traits::size_type size, + typename sprout::fixed_container_traits::const_iterator first, + typename sprout::fixed_container_traits::const_iterator last, + typename sprout::fixed_container_traits::value_type const& value, + typename sprout::fixed_container_traits::size_type count, + Args const&... args + ) + { + return first != last && count != 0 + ? !comp(*first, value) && !comp(value, *first) + ? sprout::fixed::detail::stable_sort_impl_synonym(cont, comp, size, sprout::next(first), last, value, count - 1, args..., *first) + : sprout::fixed::detail::stable_sort_impl_synonym(cont, comp, size, sprout::next(first), last, value, count, args...) + : sprout::fixed::detail::stable_sort_impl_restart(cont, comp, size, value, args...) ; } - template - SPROUT_CONSTEXPR inline typename std::enable_if< - sizeof...(Indexes) == 0 && sizeof...(SortedIndexes) != 0, - typename sprout::fixed::result_of::algorithm::type - >::type stable_sort_impl_4( + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type stable_sort_impl_restart_1( Container const& cont, Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple + typename sprout::fixed_container_traits::size_type size, + typename sprout::fixed_container_traits::const_iterator first, + typename sprout::fixed_container_traits::const_iterator last, + typename sprout::fixed_container_traits::value_type const& value, + typename sprout::fixed_container_traits::size_type count, + typename sprout::fixed_container_traits::const_iterator current, + typename sprout::fixed_container_traits::value_type const& prev_value, + Args const&... args ) { - return comp(*sprout::next(sprout::fixed_begin(cont), I1), *sprout::next(sprout::fixed_begin(cont), I2)) - ? stable_sort_impl_finish(cont, sprout::index_tuple()) - : stable_sort_impl_4(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple()) + return first != last + ? comp(prev_value, *first) + ? count == 0 || comp(*first, value) + ? sprout::fixed::detail::stable_sort_impl_restart_1(cont, comp, size, sprout::next(first), last, *first, 1, first, prev_value, args...) + : comp(value, *first) + ? sprout::fixed::detail::stable_sort_impl_restart_1(cont, comp, size, sprout::next(first), last, value, count, current, prev_value, args...) + : sprout::fixed::detail::stable_sort_impl_restart_1(cont, comp, size, sprout::next(first), last, value, count + 1, current, prev_value, args...) + : sprout::fixed::detail::stable_sort_impl_restart_1(cont, comp, size, sprout::next(first), last, value, count, current, prev_value, args...) + : sprout::fixed::detail::stable_sort_impl_synonym(cont, comp, size, sprout::next(current), last, value, count - 1, args..., value) ; } - template - SPROUT_CONSTEXPR inline typename std::enable_if< - sizeof...(Indexes) != 0 && sizeof...(SortedIndexes) == 0, - typename sprout::fixed::result_of::algorithm::type - >::type stable_sort_impl_4( + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type stable_sort_impl_restart( Container const& cont, Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple + typename sprout::fixed_container_traits::size_type size, + typename sprout::fixed_container_traits::value_type const& prev_value, + Args const&... args ) { - return comp(*sprout::next(sprout::fixed_begin(cont), I1), *sprout::next(sprout::fixed_begin(cont), I2)) - ? stable_sort_impl_4(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple<>(), sprout::index_tuple(), sprout::index_tuple()) - : stable_sort_impl_4(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple<>(), sprout::index_tuple(), sprout::index_tuple()) + return sizeof...(Args) < size + ? sprout::fixed::detail::stable_sort_impl_restart_1( + cont, + comp, + size, + sprout::begin(cont), + sprout::end(cont), + *sprout::begin(cont), + 0, + sprout::begin(cont), + prev_value, + args... + ) + : sprout::detail::container_complate(cont, args...) ; } - template - SPROUT_CONSTEXPR inline typename std::enable_if< - sizeof...(Indexes) != 0 && sizeof...(SortedIndexes) != 0, - typename sprout::fixed::result_of::algorithm::type - >::type stable_sort_impl_4( + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type stable_sort_impl_1( Container const& cont, Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple + typename sprout::fixed_container_traits::size_type size, + typename sprout::fixed_container_traits::const_iterator first, + typename sprout::fixed_container_traits::const_iterator last, + typename sprout::fixed_container_traits::value_type const& value, + typename sprout::fixed_container_traits::size_type count, + typename sprout::fixed_container_traits::const_iterator current ) { - return comp(*sprout::next(sprout::fixed_begin(cont), I1), *sprout::next(sprout::fixed_begin(cont), I2)) - ? stable_sort_impl_4(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple<>(), sprout::index_tuple(), sprout::index_tuple()) - : stable_sort_impl_4(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple()) + return first != last + ? comp(*first, value) + ? sprout::fixed::detail::stable_sort_impl_1(cont, comp, size, sprout::next(first), last, *first, 1, first) + : comp(value, *first) + ? sprout::fixed::detail::stable_sort_impl_1(cont, comp, size, sprout::next(first), last, value, count, current) + : sprout::fixed::detail::stable_sort_impl_1(cont, comp, size, sprout::next(first), last, value, count + 1, current) + : sprout::fixed::detail::stable_sort_impl_synonym(cont, comp, size, sprout::next(current), last, value, count - 1, value) ; } - template - SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type stable_sort_impl_3( - Container const& cont, - Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple - ) - { - return stable_sort_impl_4(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple<>(), sprout::index_tuple(), sprout::index_tuple()); - } - template - SPROUT_CONSTEXPR inline typename std::enable_if< - (sizeof...(Indexes) <= 1), - typename sprout::fixed::result_of::algorithm::type - >::type stable_sort_impl_2( - Container const& cont, - Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple - ) - { - return stable_sort_impl_finish(cont, sprout::index_tuple()); - } - template - SPROUT_CONSTEXPR inline typename std::enable_if< - (sizeof...(Indexes) > 1), - typename sprout::fixed::result_of::algorithm::type - >::type stable_sort_impl_2( - Container const& cont, - Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple - ) - { - return stable_sort_impl_3(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple()); - } - template - SPROUT_CONSTEXPR inline typename std::enable_if< - sizeof...(Indexes) == 0, - typename sprout::fixed::result_of::algorithm::type - >::type stable_sort_impl_1( - Container const& cont, - Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple - ) - { - return I < sprout::fixed_begin_offset(cont) - ? stable_sort_impl_2(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple()) - : I >= sprout::fixed_end_offset(cont) - ? stable_sort_impl_2(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple()) - : stable_sort_impl_2(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple()) - ; - } - template - SPROUT_CONSTEXPR inline typename std::enable_if< - sizeof...(Indexes) != 0, - typename sprout::fixed::result_of::algorithm::type - >::type stable_sort_impl_1( - Container const& cont, - Compare comp, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple, - sprout::index_tuple - ) - { - return I < sprout::fixed_begin_offset(cont) - ? stable_sort_impl_1(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple()) - : I >= sprout::fixed_end_offset(cont) - ? stable_sort_impl_1(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple()) - : stable_sort_impl_1(cont, comp, sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple(), sprout::index_tuple()) - ; - } - template + template SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type stable_sort_impl( Container const& cont, Compare comp, - sprout::index_tuple + typename sprout::fixed_container_traits::size_type size ) { - return stable_sort_impl_1(cont, comp, sprout::index_tuple(), sprout::index_tuple<>(), sprout::index_tuple<>(), sprout::index_tuple<>()); + return sprout::fixed::detail::stable_sort_impl_1( + cont, + comp, + size, + sprout::next(sprout::begin(cont)), + sprout::end(cont), + *sprout::begin(cont), + 1, + sprout::begin(cont) + ); } template SPROUT_CONSTEXPR inline typename std::enable_if< @@ -221,7 +171,7 @@ namespace sprout { return sprout::fixed::detail::stable_sort_impl( cont, comp, - typename sprout::index_range<0, sprout::fixed_container_traits::fixed_size>::type() + sprout::size(cont) ); } template @@ -245,7 +195,7 @@ namespace sprout { return sprout::fixed::detail::stable_sort_impl( cont, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::less::value_type>(), - typename sprout::index_range<0, sprout::fixed_container_traits::fixed_size>::type() + sprout::size(cont) ); } } // namespace detail