From 01e458886ae644e695edff733d81ccb423ea145f Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 22 Dec 2011 21:52:59 +0900 Subject: [PATCH] =?UTF-8?q?sprout/algorithm/bogo=5Fsort.hpp=20=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=20sprout/algorithm/bogo=5Fsort=5Fresult.hpp=20?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=20sprout/algorithm/shuffle=5Fresult.hpp=20?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sprout/algorithm/bogo_sort.hpp | 8 + sprout/algorithm/bogo_sort_result.hpp | 8 + sprout/algorithm/fit.hpp | 3 + sprout/algorithm/fit/bogo_sort.hpp | 80 ++++++++++ sprout/algorithm/fit/bogo_sort_result.hpp | 114 +++++++++++++++ sprout/algorithm/fit/shuffle.hpp | 6 +- sprout/algorithm/fit/shuffle_result.hpp | 75 ++++++++++ sprout/algorithm/fixed.hpp | 3 + sprout/algorithm/fixed/bogo_sort.hpp | 98 +++++++++++++ sprout/algorithm/fixed/bogo_sort_result.hpp | 118 +++++++++++++++ sprout/algorithm/fixed/shuffle.hpp | 4 +- sprout/algorithm/fixed/shuffle_result.hpp | 154 ++++++++++++++++++++ sprout/algorithm/shuffle_result.hpp | 8 + sprout/detail/algorithm.hpp | 30 ++++ 14 files changed, 706 insertions(+), 3 deletions(-) create mode 100644 sprout/algorithm/bogo_sort.hpp create mode 100644 sprout/algorithm/bogo_sort_result.hpp create mode 100644 sprout/algorithm/fit/bogo_sort.hpp create mode 100644 sprout/algorithm/fit/bogo_sort_result.hpp create mode 100644 sprout/algorithm/fit/shuffle_result.hpp create mode 100644 sprout/algorithm/fixed/bogo_sort.hpp create mode 100644 sprout/algorithm/fixed/bogo_sort_result.hpp create mode 100644 sprout/algorithm/fixed/shuffle_result.hpp create mode 100644 sprout/algorithm/shuffle_result.hpp diff --git a/sprout/algorithm/bogo_sort.hpp b/sprout/algorithm/bogo_sort.hpp new file mode 100644 index 00000000..e60ca9e7 --- /dev/null +++ b/sprout/algorithm/bogo_sort.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ALGORITHM_BOGO_SORT_HPP +#define SPROUT_ALGORITHM_BOGO_SORT_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_BOGO_SORT_HPP diff --git a/sprout/algorithm/bogo_sort_result.hpp b/sprout/algorithm/bogo_sort_result.hpp new file mode 100644 index 00000000..a36d9e6e --- /dev/null +++ b/sprout/algorithm/bogo_sort_result.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ALGORITHM_BOGO_SORT_RESULT_HPP +#define SPROUT_ALGORITHM_BOGO_SORT_RESULT_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_BOGO_SORT_RESULT_HPP diff --git a/sprout/algorithm/fit.hpp b/sprout/algorithm/fit.hpp index f6530e11..e3721477 100644 --- a/sprout/algorithm/fit.hpp +++ b/sprout/algorithm/fit.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -47,5 +48,7 @@ #include #include #include +#include +#include #endif // #ifndef SPROUT_ALGORITHM_FIT_HPP diff --git a/sprout/algorithm/fit/bogo_sort.hpp b/sprout/algorithm/fit/bogo_sort.hpp new file mode 100644 index 00000000..cee00095 --- /dev/null +++ b/sprout/algorithm/fit/bogo_sort.hpp @@ -0,0 +1,80 @@ +#ifndef SPROUT_ALGORITHM_FIT_BOGO_SORT_HPP +#define SPROUT_ALGORITHM_FIT_BOGO_SORT_HPP + +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fit { + namespace detail { + template + SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm::type bogo_sort_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp, + typename sprout::fixed_container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_fixed(sprout::fixed::bogo_sort(cont, sprout::forward(g), comp)), + offset, + offset + sprout::size(cont) + ); + } + } // namespace detail + // + // bogo_sort + // + template + SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm::type bogo_sort( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return sprout::fit::detail::bogo_sort_impl( + cont, + sprout::forward(g), + comp, + sprout::fixed_begin_offset(cont) + ); + } + + namespace detail { + template + SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm::type bogo_sort_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + typename sprout::fixed_container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_fixed(sprout::fixed::bogo_sort(cont, sprout::forward(g))), + offset, + offset + sprout::size(cont) + ); + } + } // namespace detail + // + // bogo_sort + // + template + SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm::type bogo_sort( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fit::detail::bogo_sort_impl( + cont, + sprout::forward(g), + sprout::fixed_begin_offset(cont) + ); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIT_BOGO_SORT_HPP diff --git a/sprout/algorithm/fit/bogo_sort_result.hpp b/sprout/algorithm/fit/bogo_sort_result.hpp new file mode 100644 index 00000000..f088fd08 --- /dev/null +++ b/sprout/algorithm/fit/bogo_sort_result.hpp @@ -0,0 +1,114 @@ +#ifndef SPROUT_ALGORITHM_FIT_BOGO_SORT_RESULT_HPP +#define SPROUT_ALGORITHM_FIT_BOGO_SORT_RESULT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fit { + namespace detail { + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bogo_sort_result_impl_1( + Sorted const& sorted, + typename sprout::fixed_container_traits::difference_type offset + ) + { + typedef sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > result_type; + return result_type( + sprout::sub_copy( + sprout::get_fixed(sprout::tuples::get<0>(sorted)), + offset, + offset + sprout::size(sprout::tuples::get<0>(sorted)) + ), + sprout::tuples::get<1>(sorted) + ); + } + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bogo_sort_result_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp, + typename sprout::fixed_container_traits::difference_type offset + ) + { + return sprout::fit::detail::bogo_sort_result_impl_1( + sprout::fixed::bogo_sort_result(cont, sprout::forward(g), comp), + offset + ); + } + } // namespace detail + // + // bogo_sort_result + // + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bogo_sort_result( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return sprout::fit::detail::bogo_sort_result_impl( + cont, + sprout::forward(g), + comp, + sprout::fixed_begin_offset(cont) + ); + } + + namespace detail { + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bogo_sort_result_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + typename sprout::fixed_container_traits::difference_type offset + ) + { + return sprout::fit::detail::bogo_sort_result_impl_1( + sprout::fixed::bogo_sort_result(cont, sprout::forward(g)), + offset + ); + } + } // namespace detail + // + // bogo_sort_result + // + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bogo_sort_result( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fit::detail::bogo_sort_result_impl( + cont, + sprout::forward(g), + sprout::fixed_begin_offset(cont) + ); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIT_BOGO_SORT_RESULT_HPP diff --git a/sprout/algorithm/fit/shuffle.hpp b/sprout/algorithm/fit/shuffle.hpp index af64c5ab..0e33bd44 100644 --- a/sprout/algorithm/fit/shuffle.hpp +++ b/sprout/algorithm/fit/shuffle.hpp @@ -35,7 +35,11 @@ namespace sprout { UniformRandomNumberGenerator&& g ) { - return sprout::fit::detail::shuffle_impl(cont, sprout::forward(g), sprout::fixed_begin_offset(cont)); + return sprout::fit::detail::shuffle_impl( + cont, + sprout::forward(g), + sprout::fixed_begin_offset(cont) + ); } } // namespace fit } // namespace sprout diff --git a/sprout/algorithm/fit/shuffle_result.hpp b/sprout/algorithm/fit/shuffle_result.hpp new file mode 100644 index 00000000..b54aa6c0 --- /dev/null +++ b/sprout/algorithm/fit/shuffle_result.hpp @@ -0,0 +1,75 @@ +#ifndef SPROUT_ALGORITHM_FIT_SHUFFLE_RESULT_HPP +#define SPROUT_ALGORITHM_FIT_SHUFFLE_RESULT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fit { + namespace detail { + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > shuffle_result_impl_1( + Shuffled const& shuffled, + typename sprout::fixed_container_traits::difference_type offset + ) + { + typedef sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > result_type; + return result_type( + sprout::sub_copy( + sprout::get_fixed(sprout::tuples::get<0>(shuffled)), + offset, + offset + sprout::size(sprout::tuples::get<0>(shuffled)) + ), + sprout::tuples::get<1>(shuffled) + ); + } + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > shuffle_result_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + typename sprout::fixed_container_traits::difference_type offset + ) + { + return sprout::fit::detail::shuffle_result_impl_1( + sprout::fixed::shuffle_result(cont, sprout::forward(g)), + offset + ); + } + } // namespace detail + // + // shuffle_result + // + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > shuffle_result( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fit::detail::shuffle_result_impl( + cont, + sprout::forward(g), + sprout::fixed_begin_offset(cont) + ); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIT_SHUFFLE_RESULT_HPP diff --git a/sprout/algorithm/fixed.hpp b/sprout/algorithm/fixed.hpp index 384293b7..75554868 100644 --- a/sprout/algorithm/fixed.hpp +++ b/sprout/algorithm/fixed.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -47,5 +48,7 @@ #include #include #include +#include +#include #endif // #ifndef SPROUT_ALGORITHM_FIXED_HPP diff --git a/sprout/algorithm/fixed/bogo_sort.hpp b/sprout/algorithm/fixed/bogo_sort.hpp new file mode 100644 index 00000000..f952fcbf --- /dev/null +++ b/sprout/algorithm/fixed/bogo_sort.hpp @@ -0,0 +1,98 @@ +#ifndef SPROUT_ALGORITHM_FIXED_BOGO_SORT_HPP +#define SPROUT_ALGORITHM_FIXED_BOGO_SORT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT_DETAIL + +namespace sprout { + namespace fixed { + namespace detail { + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type bogo_sort_impl_1( + Shuffled const& shuffled, + Compare comp + ) + { + return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::is_sorted( + sprout::begin(sprout::tuples::get<0>(shuffled)), + sprout::end(sprout::tuples::get<0>(shuffled)), + comp + ) + ? sprout::tuples::get<0>(shuffled) + : sprout::fixed::detail::bogo_sort_impl_1( + sprout::fixed::shuffle_result( + sprout::tuples::get<0>(shuffled), + sprout::tuples::get<1>(shuffled) + ), + comp + ) + ; + } + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type bogo_sort_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::is_sorted( + sprout::begin(cont), + sprout::end(cont), + comp + ) + ? sprout::clone(cont) + : sprout::fixed::detail::bogo_sort_impl_1( + sprout::fixed::shuffle_result( + cont, + sprout::forward(g) + ), + comp + ) + ; + } + } // namespace detail + // + // bogo_sort + // + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type bogo_sort( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return sprout::fixed::detail::bogo_sort_impl( + cont, + sprout::forward(g), + comp + ); + } + // + // bogo_sort + // + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type bogo_sort( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fixed::detail::bogo_sort_impl( + cont, + sprout::forward(g), + NS_SSCRISK_CEL_OR_SPROUT_DETAIL::less::value_type>() + ); + } + } // namespace fixed + + using sprout::fixed::bogo_sort; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_BOGO_SORT_HPP diff --git a/sprout/algorithm/fixed/bogo_sort_result.hpp b/sprout/algorithm/fixed/bogo_sort_result.hpp new file mode 100644 index 00000000..3cc91703 --- /dev/null +++ b/sprout/algorithm/fixed/bogo_sort_result.hpp @@ -0,0 +1,118 @@ +#ifndef SPROUT_ALGORITHM_FIXED_BOGO_SORT_RESULT_HPP +#define SPROUT_ALGORITHM_FIXED_BOGO_SORT_RESULT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT_DETAIL + +namespace sprout { + namespace fixed { + namespace detail { + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > bogo_sort_result_impl_1( + Shuffled const& shuffled, + Compare comp + ) + { + return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::is_sorted( + sprout::begin(sprout::tuples::get<0>(shuffled)), + sprout::end(sprout::tuples::get<0>(shuffled)), + comp + ) + ? shuffled + : sprout::fixed::detail::bogo_sort_result_impl_1( + sprout::fixed::shuffle_result( + sprout::tuples::get<0>(shuffled), + sprout::tuples::get<1>(shuffled) + ), + comp + ) + ; + } + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > bogo_sort_result_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + typedef sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > result_type; + return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::is_sorted( + sprout::begin(cont), + sprout::end(cont), + comp + ) + ? result_type( + sprout::clone(cont), + sprout::forward(g) + ) + : sprout::fixed::detail::bogo_sort_result_impl_1( + sprout::fixed::shuffle_result( + cont, + sprout::forward(g) + ), + comp + ) + ; + } + } // namespace detail + // + // bogo_sort_result + // + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > bogo_sort_result( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return sprout::fixed::detail::bogo_sort_result_impl( + cont, + sprout::forward(g), + comp + ); + } + // + // bogo_sort_result + // + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > bogo_sort_result( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fixed::detail::bogo_sort_result_impl( + cont, + sprout::forward(g), + NS_SSCRISK_CEL_OR_SPROUT_DETAIL::less::value_type>() + ); + } + } // namespace fixed + + using sprout::fixed::bogo_sort_result; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_BOGO_SORT_RESULT_HPP diff --git a/sprout/algorithm/fixed/shuffle.hpp b/sprout/algorithm/fixed/shuffle.hpp index 110537d3..319b53f8 100644 --- a/sprout/algorithm/fixed/shuffle.hpp +++ b/sprout/algorithm/fixed/shuffle.hpp @@ -27,14 +27,14 @@ namespace sprout { std::ptrdiff_t i ) { - return i < n + return i < n - 1 ? sprout::fixed::detail::make_shuffle_indexes_1( n, rnd(), sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()), i + 1 ) - : arr + : sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()) ; } template diff --git a/sprout/algorithm/fixed/shuffle_result.hpp b/sprout/algorithm/fixed/shuffle_result.hpp new file mode 100644 index 00000000..520edfc2 --- /dev/null +++ b/sprout/algorithm/fixed/shuffle_result.hpp @@ -0,0 +1,154 @@ +#ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP +#define SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fixed { + namespace detail { + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + sprout::array, + typename std::decay::type + > make_shuffle_result_indexes_1( + std::ptrdiff_t n, + Random const& rnd, + sprout::array const& arr, + std::ptrdiff_t i + ) + { + typedef sprout::tuples::tuple< + sprout::array, + typename std::decay::type + > result_type; + return i < n - 1 + ? sprout::fixed::detail::make_shuffle_result_indexes_1( + n, + rnd(), + sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()), + i + 1 + ) + : result_type( + sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()), + rnd.engine() + ) + ; + } + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + sprout::array, + typename std::decay::type + > make_shuffle_result_indexes( + std::ptrdiff_t n, + UniformRandomNumberGenerator&& g + ) + { + typedef sprout::tuples::tuple< + sprout::array, + typename std::decay::type + > result_type; + return n > 0 + ? sprout::fixed::detail::make_shuffle_result_indexes_1( + n, + sprout::random::uniform_int_distribution(0, n - 1)(sprout::forward(g)), + sprout::fixed::iota(sprout::pit >(), 0), + 0 + ) + : result_type( + sprout::array{{}}, + sprout::forward(g) + ) + ; + } + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > shuffle_result_impl_1( + Container const& cont, + sprout::index_tuple, + Shuffled const& shuffled, + typename sprout::fixed_container_traits::difference_type offset, + typename sprout::fixed_container_traits::size_type size + ) + { + typedef sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > result_type; + return result_type( + sprout::remake_clone( + cont, + sprout::size(cont), + (Indexes >= offset && Indexes < offset + size + ? *sprout::next(sprout::begin(cont), sprout::tuples::get<0>(shuffled)[Indexes - offset]) + : *sprout::next(sprout::fixed_begin(cont), Indexes) + )... + ), + sprout::tuples::get<1>(shuffled) + ); + } + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > shuffle_result_impl( + Container const& cont, + sprout::index_tuple indexes, + UniformRandomNumberGenerator&& g, + typename sprout::fixed_container_traits::difference_type offset, + typename sprout::fixed_container_traits::size_type size + ) + { + return sprout::fixed::detail::shuffle_result_impl_1( + cont, + indexes, + sprout::fixed::detail::make_shuffle_result_indexes::fixed_size>( + size, + sprout::forward(g) + ), + offset, + size + ); + } + } // namespace detail + // + // shuffle_result + // + template + SPROUT_CONSTEXPR inline sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > shuffle_result( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fixed::detail::shuffle_result_impl( + cont, + typename sprout::index_range<0, sprout::fixed_container_traits::fixed_size>::type(), + sprout::forward(g), + sprout::fixed_begin_offset(cont), + sprout::size(cont) + ); + } + } // namespace fixed + + using sprout::fixed::shuffle_result; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP diff --git a/sprout/algorithm/shuffle_result.hpp b/sprout/algorithm/shuffle_result.hpp new file mode 100644 index 00000000..419c0157 --- /dev/null +++ b/sprout/algorithm/shuffle_result.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ALGORITHM_SHUFFLE_RESULT_HPP +#define SPROUT_ALGORITHM_SHUFFLE_RESULT_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_SHUFFLE_RESULT_HPP diff --git a/sprout/detail/algorithm.hpp b/sprout/detail/algorithm.hpp index a55d0c1b..214da234 100644 --- a/sprout/detail/algorithm.hpp +++ b/sprout/detail/algorithm.hpp @@ -141,6 +141,36 @@ namespace sprout { : sprout::detail::find(sprout::next(first), last, value) ; } + + // + // is_sorted_until + // + template + SPROUT_CONSTEXPR InputIterator is_sorted_until(InputIterator first, InputIterator last) { + return first == last || sprout::next(first) == last ? last + : *sprout::next(first) < *first ? sprout::next(first) + : sprout::detail::is_sorted_until(sprout::next(first), last) + ; + } + template + SPROUT_CONSTEXPR InputIterator is_sorted_until(InputIterator first, InputIterator last, Compare comp) { + return first == last || sprout::next(first) == last ? last + : comp(*sprout::next(first), *first) != false ? sprout::next(first) + : sprout::detail::is_sorted_until(sprout::next(first), last) + ; + } + + // + // is_sorted + // + template + SPROUT_CONSTEXPR bool is_sorted(InputIterator first, InputIterator last) { + return sprout::detail::is_sorted_until(first, last) == last; + } + template + SPROUT_CONSTEXPR bool is_sorted(InputIterator first, InputIterator last, Compare comp) { + return sprout::detail::is_sorted_until(first, last, comp) == last; + } } // namespace detail } // namespace sprout