diff --git a/sprout/algorithm/fixed/deprecated/sort.hpp b/sprout/algorithm/fixed/deprecated/sort.hpp new file mode 100644 index 00000000..5ffb82b0 --- /dev/null +++ b/sprout/algorithm/fixed/deprecated/sort.hpp @@ -0,0 +1,256 @@ +#ifndef SPROUT_ALGORITHM_FIXED_DEPRECATED_SORT_HPP +#define SPROUT_ALGORITHM_FIXED_DEPRECATED_SORT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + namespace fixed { + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort_lr( + Container const& cont, + typename sprout::container_traits::difference_type start, + typename sprout::container_traits::difference_type end, + Compare comp, + typename sprout::container_traits::difference_type l, + typename sprout::container_traits::difference_type r, + typename sprout::container_traits::value_type const& p + ); + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort_start( + Container const& cont, + typename sprout::container_traits::difference_type start, + typename sprout::container_traits::difference_type end, + Compare comp + ); + + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::value_type const& + sort_select_pivot( + Iterator origin, + typename sprout::container_traits::difference_type start, + typename sprout::container_traits::difference_type end + ) + { // pivot を選ぶ(中央の要素) + return *sprout::next(origin, (end + start) / 2); + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::difference_type + sort_find_l( + Iterator origin, + Compare comp, + typename sprout::container_traits::difference_type l, + typename sprout::container_traits::value_type const& p + ) + { // left を見つける + return comp(*sprout::next(origin, l), p) + ? sprout::fixed::detail::sort_find_l(origin, comp, l + 1, p) + : l + ; + } + template + inline SPROUT_CONSTEXPR typename sprout::container_traits::difference_type + sort_find_r( + Iterator origin, + Compare comp, + typename sprout::container_traits::difference_type r, + typename sprout::container_traits::value_type const& p + ) + { // right を見つける + return comp(p, *sprout::next(origin, r)) + ? sprout::fixed::detail::sort_find_r(origin, comp, r - 1, p) + : r + ; + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort_part_l( + Container const& cont, + typename sprout::container_traits::difference_type start, + Compare comp, + typename sprout::container_traits::difference_type l + ) + { // 左側をソート + return start < l - 1 + ? sprout::fixed::detail::sort_start(cont, start, l - 1, comp) + : sprout::deep_copy(cont) + ; + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort_part_r( + Container const& cont, + typename sprout::container_traits::difference_type end, + Compare comp, + typename sprout::container_traits::difference_type r + ) + { // 右側をソート + return r + 1 < end + ? sprout::fixed::detail::sort_start(cont, r + 1, end, comp) + : sprout::deep_copy(cont) + ; + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort_part_lr( + Container const& cont, + typename sprout::container_traits::difference_type start, + typename sprout::container_traits::difference_type end, + Compare comp, + typename sprout::container_traits::difference_type l, + typename sprout::container_traits::difference_type r + ) + { // 左右に分けてソート + return sprout::fixed::detail::sort_part_r( + sprout::fixed::detail::sort_part_l(cont, start, comp, l), + end, + comp, + r + ); + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort_next( + Container const& cont, + typename sprout::container_traits::difference_type start, + typename sprout::container_traits::difference_type end, + Compare comp, + typename sprout::container_traits::difference_type l, + typename sprout::container_traits::difference_type r, + typename sprout::container_traits::value_type const& p + ) + { // left と right 比較して、左右に分けてソートするか、またはスワップしてこの範囲のソートを続ける + return l >= r + ? sprout::fixed::detail::sort_part_lr(cont, start, end, comp, l, r) + : sprout::fixed::detail::sort_lr( + sprout::fixed::swap_element( + cont, + sprout::next(sprout::internal_begin(cont), l), + sprout::next(sprout::internal_begin(cont), r) + ), + start, + end, + comp, + l + 1, + r - 1, + p + ) + ; + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort_lr( + Container const& cont, + typename sprout::container_traits::difference_type start, + typename sprout::container_traits::difference_type end, + Compare comp, + typename sprout::container_traits::difference_type l, + typename sprout::container_traits::difference_type r, + typename sprout::container_traits::value_type const& p + ) + { // left と right を検索 + return sprout::fixed::detail::sort_next( + cont, + start, + end, + comp, + sprout::fixed::detail::sort_find_l(sprout::internal_begin(cont), comp, l, p), + sprout::fixed::detail::sort_find_r(sprout::internal_begin(cont), comp, r, p), + p + ); + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort_start( + Container const& cont, + typename sprout::container_traits::difference_type start, + typename sprout::container_traits::difference_type end, + Compare comp + ) + { // pivot を選択してソートを開始 + return sprout::fixed::detail::sort_lr( + cont, + start, + end, + comp, + start, + end, + sprout::fixed::detail::sort_select_pivot(sprout::internal_begin(cont), start, end) + ); + } + template + inline SPROUT_CONSTEXPR typename std::enable_if< + (sprout::container_traits::static_size <= 1), + typename sprout::fixed::result_of::algorithm::type + >::type + sort(Container const& cont, Compare comp) { + return sprout::deep_copy(cont); + } + template + inline SPROUT_CONSTEXPR typename std::enable_if< + (sprout::container_traits::static_size > 1), + typename sprout::fixed::result_of::algorithm::type + >::type + sort(Container const& cont, Compare comp) { + return sprout::fixed::detail::sort_start( + cont, + sprout::internal_begin_offset(cont), + NS_SSCRISK_CEL_OR_SPROUT::distance(sprout::internal_begin(cont), sprout::end(cont) - 1), + comp + ); + } + template + inline SPROUT_CONSTEXPR typename std::enable_if< + (sprout::container_traits::static_size <= 1), + typename sprout::fixed::result_of::algorithm::type + >::type + sort(Container const& cont) { + return sprout::deep_copy(cont); + } + template + inline SPROUT_CONSTEXPR typename std::enable_if< + (sprout::container_traits::static_size > 1), + typename sprout::fixed::result_of::algorithm::type + >::type + sort(Container const& cont) { + return sprout::fixed::detail::sort_start( + cont, + sprout::internal_begin_offset(cont), + NS_SSCRISK_CEL_OR_SPROUT::distance(sprout::internal_begin(cont), sprout::end(cont) - 1), + NS_SSCRISK_CEL_OR_SPROUT::less::value_type>() + ); + } + } // namespace detail + namespace deprecated { + // + // sort + // + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort(Container const& cont, Compare comp) { + return sprout::fixed::detail::sort(cont, comp); + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + sort(Container const& cont) { + return sprout::fixed::detail::sort(cont); + } + } // namespace deprecated + } // namespace fixed + + namespace deprecated { + using sprout::fixed::deprecated::sort; + } // namespace deprecated +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_DEPRECATED_SORT_HPP diff --git a/sprout/algorithm/fixed/sort.hpp b/sprout/algorithm/fixed/sort.hpp index 04d2aa54..07dc3afc 100644 --- a/sprout/algorithm/fixed/sort.hpp +++ b/sprout/algorithm/fixed/sort.hpp @@ -1,248 +1,24 @@ #ifndef SPROUT_ALGORITHM_FIXED_SORT_HPP #define SPROUT_ALGORITHM_FIXED_SORT_HPP -#include #include -#include -#include -#include #include -#include -#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT -#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT +#include namespace sprout { namespace fixed { - namespace detail { - template - inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type - sort_lr( - Container const& cont, - typename sprout::container_traits::difference_type start, - typename sprout::container_traits::difference_type end, - Compare comp, - typename sprout::container_traits::difference_type l, - typename sprout::container_traits::difference_type r, - typename sprout::container_traits::value_type const& p - ); - template - inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type - sort_start( - Container const& cont, - typename sprout::container_traits::difference_type start, - typename sprout::container_traits::difference_type end, - Compare comp - ); - - template - inline SPROUT_CONSTEXPR typename sprout::container_traits::value_type const& - sort_select_pivot( - Iterator origin, - typename sprout::container_traits::difference_type start, - typename sprout::container_traits::difference_type end - ) - { // pivot を選ぶ(中央の要素) - return *sprout::next(origin, (end + start) / 2); - } - template - inline SPROUT_CONSTEXPR typename sprout::container_traits::difference_type - sort_find_l( - Iterator origin, - Compare comp, - typename sprout::container_traits::difference_type l, - typename sprout::container_traits::value_type const& p - ) - { // left を見つける - return comp(*sprout::next(origin, l), p) - ? sprout::fixed::detail::sort_find_l(origin, comp, l + 1, p) - : l - ; - } - template - inline SPROUT_CONSTEXPR typename sprout::container_traits::difference_type - sort_find_r( - Iterator origin, - Compare comp, - typename sprout::container_traits::difference_type r, - typename sprout::container_traits::value_type const& p - ) - { // right を見つける - return comp(p, *sprout::next(origin, r)) - ? sprout::fixed::detail::sort_find_r(origin, comp, r - 1, p) - : r - ; - } - template - inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type - sort_part_l( - Container const& cont, - typename sprout::container_traits::difference_type start, - Compare comp, - typename sprout::container_traits::difference_type l - ) - { // 左側をソート - return start < l - 1 - ? sprout::fixed::detail::sort_start(cont, start, l - 1, comp) - : sprout::deep_copy(cont) - ; - } - template - inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type - sort_part_r( - Container const& cont, - typename sprout::container_traits::difference_type end, - Compare comp, - typename sprout::container_traits::difference_type r - ) - { // 右側をソート - return r + 1 < end - ? sprout::fixed::detail::sort_start(cont, r + 1, end, comp) - : sprout::deep_copy(cont) - ; - } - template - inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type - sort_part_lr( - Container const& cont, - typename sprout::container_traits::difference_type start, - typename sprout::container_traits::difference_type end, - Compare comp, - typename sprout::container_traits::difference_type l, - typename sprout::container_traits::difference_type r - ) - { // 左右に分けてソート - return sprout::fixed::detail::sort_part_r( - sprout::fixed::detail::sort_part_l(cont, start, comp, l), - end, - comp, - r - ); - } - template - inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type - sort_next( - Container const& cont, - typename sprout::container_traits::difference_type start, - typename sprout::container_traits::difference_type end, - Compare comp, - typename sprout::container_traits::difference_type l, - typename sprout::container_traits::difference_type r, - typename sprout::container_traits::value_type const& p - ) - { // left と right 比較して、左右に分けてソートするか、またはスワップしてこの範囲のソートを続ける - return l >= r - ? sprout::fixed::detail::sort_part_lr(cont, start, end, comp, l, r) - : sprout::fixed::detail::sort_lr( - sprout::fixed::swap_element( - cont, - sprout::next(sprout::internal_begin(cont), l), - sprout::next(sprout::internal_begin(cont), r) - ), - start, - end, - comp, - l + 1, - r - 1, - p - ) - ; - } - template - inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type - sort_lr( - Container const& cont, - typename sprout::container_traits::difference_type start, - typename sprout::container_traits::difference_type end, - Compare comp, - typename sprout::container_traits::difference_type l, - typename sprout::container_traits::difference_type r, - typename sprout::container_traits::value_type const& p - ) - { // left と right を検索 - return sprout::fixed::detail::sort_next( - cont, - start, - end, - comp, - sprout::fixed::detail::sort_find_l(sprout::internal_begin(cont), comp, l, p), - sprout::fixed::detail::sort_find_r(sprout::internal_begin(cont), comp, r, p), - p - ); - } - template - inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type - sort_start( - Container const& cont, - typename sprout::container_traits::difference_type start, - typename sprout::container_traits::difference_type end, - Compare comp - ) - { // pivot を選択してソートを開始 - return sprout::fixed::detail::sort_lr( - cont, - start, - end, - comp, - start, - end, - sprout::fixed::detail::sort_select_pivot(sprout::internal_begin(cont), start, end) - ); - } - template - inline SPROUT_CONSTEXPR typename std::enable_if< - (sprout::container_traits::static_size <= 1), - typename sprout::fixed::result_of::algorithm::type - >::type - sort(Container const& cont, Compare comp) { - return sprout::deep_copy(cont); - } - template - inline SPROUT_CONSTEXPR typename std::enable_if< - (sprout::container_traits::static_size > 1), - typename sprout::fixed::result_of::algorithm::type - >::type - sort(Container const& cont, Compare comp) { - return sprout::fixed::detail::sort_start( - cont, - sprout::internal_begin_offset(cont), - NS_SSCRISK_CEL_OR_SPROUT::distance(sprout::internal_begin(cont), sprout::end(cont) - 1), - comp - ); - } - template - inline SPROUT_CONSTEXPR typename std::enable_if< - (sprout::container_traits::static_size <= 1), - typename sprout::fixed::result_of::algorithm::type - >::type - sort(Container const& cont) { - return sprout::deep_copy(cont); - } - template - inline SPROUT_CONSTEXPR typename std::enable_if< - (sprout::container_traits::static_size > 1), - typename sprout::fixed::result_of::algorithm::type - >::type - sort(Container const& cont) { - return sprout::fixed::detail::sort_start( - cont, - sprout::internal_begin_offset(cont), - NS_SSCRISK_CEL_OR_SPROUT::distance(sprout::internal_begin(cont), sprout::end(cont) - 1), - NS_SSCRISK_CEL_OR_SPROUT::less::value_type>() - ); - } - } // namespace detail // // sort // template inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type sort(Container const& cont, Compare comp) { - return sprout::fixed::detail::sort(cont, comp); + return sprout::fixed::stable_sort(cont, comp); } template inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type sort(Container const& cont) { - return sprout::fixed::detail::sort(cont); + return sprout::fixed::stable_sort(cont); } } // namespace fixed