diff --git a/libs/algorithm/test/bozo_sort.cpp b/libs/algorithm/test/bozo_sort.cpp new file mode 100644 index 00000000..2b78ea86 --- /dev/null +++ b/libs/algorithm/test/bozo_sort.cpp @@ -0,0 +1,137 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_BOZO_SORT_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_BOZO_SORT_CPP + +#include +#include +#include +#include +#include +#include +#include + +namespace testspr { + static void algorithm_bozo_sort_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{5, 1, 4, 2, 3}}; + + // ソート + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::bozo_sort( + arr1, + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED) + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sorted, + array{{1, 2, 3, 4, 5}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::fit::bozo_sort( + arr1, + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED) + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sorted, + array{{1, 2, 3, 4, 5}} + )); + } + // ソート + // 範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::bozo_sort( + sprout::sub(arr1, 1, 4), + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED) + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sorted, + array{{1, 2, 4}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(sorted), + array{{5, 1, 2, 4, 3}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::fit::bozo_sort( + sprout::sub(arr1, 1, 4), + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED) + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sorted, + array{{1, 2, 4}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(sorted), + array{{5, 1, 2, 4, 3}} + )); + } + } + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{5, 1, 4, 2, 3}}; + + // ソート + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::bozo_sort( + arr1, + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED), + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sorted, + array{{1, 2, 3, 4, 5}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::fit::bozo_sort( + arr1, + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED), + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sorted, + array{{1, 2, 3, 4, 5}} + )); + } + // ソート + // 範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::bozo_sort( + sprout::sub(arr1, 1, 4), + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED), + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sorted, + array{{1, 2, 4}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(sorted), + array{{5, 1, 2, 4, 3}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::fit::bozo_sort( + sprout::sub(arr1, 1, 4), + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED), + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sorted, + array{{1, 2, 4}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(sorted), + array{{5, 1, 2, 4, 3}} + )); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_bozo_sort_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_BOZO_SORT_CPP diff --git a/libs/algorithm/test/bozo_sort_result.cpp b/libs/algorithm/test/bozo_sort_result.cpp new file mode 100644 index 00000000..843a3782 --- /dev/null +++ b/libs/algorithm/test/bozo_sort_result.cpp @@ -0,0 +1,179 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_BOZO_SORT_RESULT_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_BOZO_SORT_RESULT_CPP + +#include +#include +#include +#include +#include +#include +#include + +namespace testspr { + static void algorithm_bozo_sort_result_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{5, 1, 4, 2, 3}}; + SPROUT_STATIC_CONSTEXPR auto g = sprout::random::default_random_engine(SPROUT_UNIQUE_SEED); + + // ソート + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::bozo_sort_result( + arr1, + g + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get<0>(sorted), + array{{1, 2, 3, 4, 5}} + )); + SPROUT_STATIC_CONSTEXPR auto sorted2 = sprout::bozo_sort_result( + sprout::get<0>(sorted), + sprout::get<1>(sorted) + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(sorted) == sprout::get<1>(sorted2)); + } + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::fit::bozo_sort_result( + arr1, + g + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get<0>(sorted), + array{{1, 2, 3, 4, 5}} + )); + SPROUT_STATIC_CONSTEXPR auto sorted2 = sprout::bozo_sort_result( + sprout::get<0>(sorted), + sprout::get<1>(sorted) + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(sorted) == sprout::get<1>(sorted2)); + } + // ソート + // 範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::bozo_sort_result( + sprout::sub(arr1, 1, 4), + g + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get<0>(sorted), + array{{1, 2, 4}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(sprout::get<0>(sorted)), + array{{5, 1, 2, 4, 3}} + )); + SPROUT_STATIC_CONSTEXPR auto sorted2 = sprout::bozo_sort_result( + sprout::get<0>(sorted), + sprout::get<1>(sorted) + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(sorted) == sprout::get<1>(sorted2)); + } + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::fit::bozo_sort_result( + sprout::sub(arr1, 1, 4), + g + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get<0>(sorted), + array{{1, 2, 4}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(sprout::get<0>(sorted)), + array{{5, 1, 2, 4, 3}} + )); + SPROUT_STATIC_CONSTEXPR auto sorted2 = sprout::bozo_sort_result( + sprout::get<0>(sorted), + sprout::get<1>(sorted) + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(sorted) == sprout::get<1>(sorted2)); + } + } + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{5, 1, 4, 2, 3}}; + SPROUT_STATIC_CONSTEXPR auto g = sprout::random::default_random_engine(SPROUT_UNIQUE_SEED); + + // ソート + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::bozo_sort_result( + arr1, + g, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get<0>(sorted), + array{{1, 2, 3, 4, 5}} + )); + SPROUT_STATIC_CONSTEXPR auto sorted2 = sprout::bozo_sort_result( + sprout::get<0>(sorted), + sprout::get<1>(sorted) + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(sorted) == sprout::get<1>(sorted2)); + } + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::fit::bozo_sort_result( + arr1, + g, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get<0>(sorted), + array{{1, 2, 3, 4, 5}} + )); + SPROUT_STATIC_CONSTEXPR auto sorted2 = sprout::bozo_sort_result( + sprout::get<0>(sorted), + sprout::get<1>(sorted) + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(sorted) == sprout::get<1>(sorted2)); + } + // ソート + // 範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::bozo_sort_result( + sprout::sub(arr1, 1, 4), + g, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get<0>(sorted), + array{{1, 2, 4}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(sprout::get<0>(sorted)), + array{{5, 1, 2, 4, 3}} + )); + SPROUT_STATIC_CONSTEXPR auto sorted2 = sprout::bozo_sort_result( + sprout::get<0>(sorted), + sprout::get<1>(sorted) + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(sorted) == sprout::get<1>(sorted2)); + } + { + SPROUT_STATIC_CONSTEXPR auto sorted = sprout::fit::bozo_sort_result( + sprout::sub(arr1, 1, 4), + g, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get<0>(sorted), + array{{1, 2, 4}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(sprout::get<0>(sorted)), + array{{5, 1, 2, 4, 3}} + )); + SPROUT_STATIC_CONSTEXPR auto sorted2 = sprout::bozo_sort_result( + sprout::get<0>(sorted), + sprout::get<1>(sorted) + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(sorted) == sprout::get<1>(sorted2)); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_bozo_sort_result_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_BOZO_SORT_RESULT_CPP diff --git a/libs/algorithm/test/modifying.cpp b/libs/algorithm/test/modifying.cpp index 0716a198..8ada63f8 100644 --- a/libs/algorithm/test/modifying.cpp +++ b/libs/algorithm/test/modifying.cpp @@ -54,8 +54,12 @@ #include "./prev_permutation.cpp" #include "./swap_element.cpp" #include "./swap_element_copy.cpp" +#include "./random_swap.cpp" +#include "./random_swap_result.cpp" #include "./bogo_sort.cpp" #include "./bogo_sort_result.cpp" +#include "./bozo_sort.cpp" +#include "./bozo_sort_result.cpp" #ifdef TESTSPR_CPP_INCLUDE_DISABLE_SPROUT_LIBS_ALGORITHM_TEST_MODIFYIING_CPP # undef TESTSPR_CPP_INCLUDE @@ -111,8 +115,12 @@ namespace testspr { testspr::algorithm_prev_permutation_test(); testspr::algorithm_swap_element_test(); testspr::algorithm_swap_element_copy_test(); + testspr::algorithm_random_swap_test(); + testspr::algorithm_random_swap_result_test(); testspr::algorithm_bogo_sort_test(); testspr::algorithm_bogo_sort_result_test(); + testspr::algorithm_bozo_sort_test(); + testspr::algorithm_bozo_sort_result_test(); } } // namespace testspr diff --git a/libs/algorithm/test/random_swap.cpp b/libs/algorithm/test/random_swap.cpp new file mode 100644 index 00000000..a7857af5 --- /dev/null +++ b/libs/algorithm/test/random_swap.cpp @@ -0,0 +1,78 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_RANDOM_SWAP_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_RANDOM_SWAP_CPP + +#include +#include +#include +#include +#include +#include +#include + +namespace testspr { + static void algorithm_random_swap_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; + + // ランダムスワップ + { + SPROUT_STATIC_CONSTEXPR auto random_swapped = sprout::random_swap( + arr1, + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED) + ); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + random_swapped, + arr1 + )); + } + { + SPROUT_STATIC_CONSTEXPR auto random_swapped = sprout::fit::random_swap( + arr1, + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED) + ); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + random_swapped, + arr1 + )); + } + // ランダムスワップ + // 範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto random_swapped = sprout::random_swap( + sprout::sub(arr1, 2, 8), + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED) + ); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + random_swapped, + sprout::sub(arr1, 2, 8) + )); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + sprout::get_internal(random_swapped), + arr1 + )); + } + { + SPROUT_STATIC_CONSTEXPR auto random_swapped = sprout::fit::random_swap( + sprout::sub(arr1, 2, 8), + sprout::random::default_random_engine(SPROUT_UNIQUE_SEED) + ); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + random_swapped, + sprout::sub(arr1, 2, 8) + )); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + sprout::get_internal(random_swapped), + arr1 + )); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_random_swap_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_RANDOM_SWAP_CPP diff --git a/libs/algorithm/test/random_swap_result.cpp b/libs/algorithm/test/random_swap_result.cpp new file mode 100644 index 00000000..bb41dcd4 --- /dev/null +++ b/libs/algorithm/test/random_swap_result.cpp @@ -0,0 +1,99 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_RANDOM_SWAP_RESULT_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_RANDOM_SWAP_RESULT_CPP + +#include +#include +#include +#include +#include +#include +#include + +namespace testspr { + static void algorithm_random_swap_result_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; + SPROUT_STATIC_CONSTEXPR auto g = sprout::random::default_random_engine(SPROUT_UNIQUE_SEED); + + // ランダムスワップ + { + SPROUT_STATIC_CONSTEXPR auto random_swapped = sprout::random_swap_result( + arr1, + g + ); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + sprout::get<0>(random_swapped), + arr1 + )); + SPROUT_STATIC_CONSTEXPR auto random_swapped2 = sprout::random_swap_result( + sprout::get<0>(random_swapped), + g + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(random_swapped) == sprout::get<1>(random_swapped2)); + } + { + SPROUT_STATIC_CONSTEXPR auto random_swapped = sprout::fit::random_swap_result( + arr1, + g + ); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + sprout::get<0>(random_swapped), + arr1 + )); + SPROUT_STATIC_CONSTEXPR auto random_swapped2 = sprout::random_swap_result( + sprout::get<0>(random_swapped), + g + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(random_swapped) == sprout::get<1>(random_swapped2)); + } + // ランダムスワップ + // 範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto random_swapped = sprout::random_swap_result( + sprout::sub(arr1, 2, 8), + g + ); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + sprout::get<0>(random_swapped), + sprout::sub(arr1, 2, 8) + )); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + sprout::get_internal(sprout::get<0>(random_swapped)), + arr1 + )); + SPROUT_STATIC_CONSTEXPR auto random_swapped2 = sprout::random_swap_result( + sprout::get<0>(random_swapped), + g + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(random_swapped) == sprout::get<1>(random_swapped2)); + } + { + SPROUT_STATIC_CONSTEXPR auto random_swapped = sprout::fit::random_swap_result( + sprout::sub(arr1, 2, 8), + g + ); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + sprout::get<0>(random_swapped), + sprout::sub(arr1, 2, 8) + )); + TESTSPR_BOTH_ASSERT(testspr::is_permutation( + sprout::get_internal(sprout::get<0>(random_swapped)), + arr1 + )); + SPROUT_STATIC_CONSTEXPR auto random_swapped2 = sprout::random_swap_result( + sprout::get<0>(random_swapped), + g + ); + TESTSPR_BOTH_ASSERT(sprout::get<1>(random_swapped) == sprout::get<1>(random_swapped2)); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_random_swap_result_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_RANDOM_SWAP_RESULT_CPP diff --git a/sprout/algorithm/bozo_sort.hpp b/sprout/algorithm/bozo_sort.hpp new file mode 100644 index 00000000..1885131a --- /dev/null +++ b/sprout/algorithm/bozo_sort.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ALGORITHM_BOZO_SORT_HPP +#define SPROUT_ALGORITHM_BOZO_SORT_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_BOZO_SORT_HPP diff --git a/sprout/algorithm/bozo_sort_result.hpp b/sprout/algorithm/bozo_sort_result.hpp new file mode 100644 index 00000000..ac08cc96 --- /dev/null +++ b/sprout/algorithm/bozo_sort_result.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ALGORITHM_BOZO_SORT_RESULT_HPP +#define SPROUT_ALGORITHM_BOZO_SORT_RESULT_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_BOZO_SORT_RESULT_HPP diff --git a/sprout/algorithm/fit.hpp b/sprout/algorithm/fit.hpp index 1e1d4750..ab342abd 100644 --- a/sprout/algorithm/fit.hpp +++ b/sprout/algorithm/fit.hpp @@ -48,9 +48,11 @@ #include #include #include -#include -#include +#include +#include #include #include +#include +#include #endif // #ifndef SPROUT_ALGORITHM_FIT_HPP diff --git a/sprout/algorithm/fit/bozo_sort.hpp b/sprout/algorithm/fit/bozo_sort.hpp new file mode 100644 index 00000000..846265cd --- /dev/null +++ b/sprout/algorithm/fit/bozo_sort.hpp @@ -0,0 +1,80 @@ +#ifndef SPROUT_ALGORITHM_FIT_BOZO_SORT_HPP +#define SPROUT_ALGORITHM_FIT_BOZO_SORT_HPP + +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fit { + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type bozo_sort_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_internal(sprout::fixed::bozo_sort(cont, sprout::forward(g), comp)), + offset, + offset + sprout::size(cont) + ); + } + } // namespace detail + // + // bozo_sort + // + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type bozo_sort( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return sprout::fit::detail::bozo_sort_impl( + cont, + sprout::forward(g), + comp, + sprout::internal_begin_offset(cont) + ); + } + + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type bozo_sort_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_internal(sprout::fixed::bozo_sort(cont, sprout::forward(g))), + offset, + offset + sprout::size(cont) + ); + } + } // namespace detail + // + // bozo_sort + // + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type bozo_sort( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fit::detail::bozo_sort_impl( + cont, + sprout::forward(g), + sprout::internal_begin_offset(cont) + ); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIT_BOZO_SORT_HPP diff --git a/sprout/algorithm/fit/bozo_sort_result.hpp b/sprout/algorithm/fit/bozo_sort_result.hpp new file mode 100644 index 00000000..cab77654 --- /dev/null +++ b/sprout/algorithm/fit/bozo_sort_result.hpp @@ -0,0 +1,114 @@ +#ifndef SPROUT_ALGORITHM_FIT_BOZO_SORT_RESULT_HPP +#define SPROUT_ALGORITHM_FIT_BOZO_SORT_RESULT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fit { + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bozo_sort_result_impl_1( + Sorted const& sorted, + typename sprout::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_internal(sprout::tuples::get<0>(sorted)), + offset, + offset + sprout::size(sprout::tuples::get<0>(sorted)) + ), + sprout::tuples::get<1>(sorted) + ); + } + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bozo_sort_result_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::fit::detail::bozo_sort_result_impl_1( + sprout::fixed::bozo_sort_result(cont, sprout::forward(g), comp), + offset + ); + } + } // namespace detail + // + // bozo_sort_result + // + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bozo_sort_result( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return sprout::fit::detail::bozo_sort_result_impl( + cont, + sprout::forward(g), + comp, + sprout::internal_begin_offset(cont) + ); + } + + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bozo_sort_result_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::fit::detail::bozo_sort_result_impl_1( + sprout::fixed::bozo_sort_result(cont, sprout::forward(g)), + offset + ); + } + } // namespace detail + // + // bozo_sort_result + // + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > bozo_sort_result( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fit::detail::bozo_sort_result_impl( + cont, + sprout::forward(g), + sprout::internal_begin_offset(cont) + ); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIT_BOZO_SORT_RESULT_HPP diff --git a/sprout/algorithm/fit/random_swap.hpp b/sprout/algorithm/fit/random_swap.hpp new file mode 100644 index 00000000..d1ea906e --- /dev/null +++ b/sprout/algorithm/fit/random_swap.hpp @@ -0,0 +1,47 @@ +#ifndef SPROUT_ALGORITHM_FIT_RANDOM_SWAP_HPP +#define SPROUT_ALGORITHM_FIT_RANDOM_SWAP_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fit { + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type random_swap_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_internal(sprout::fixed::random_swap(cont, sprout::forward(g))), + offset, + offset + sprout::size(cont) + ); + } + } // namespace detail + // + // random_swap + // + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type random_swap( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fit::detail::random_swap_impl( + cont, + sprout::forward(g), + sprout::internal_begin_offset(cont) + ); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIT_RANDOM_SWAP_HPP diff --git a/sprout/algorithm/fit/random_swap_result.hpp b/sprout/algorithm/fit/random_swap_result.hpp new file mode 100644 index 00000000..33bdf659 --- /dev/null +++ b/sprout/algorithm/fit/random_swap_result.hpp @@ -0,0 +1,75 @@ +#ifndef SPROUT_ALGORITHM_FIT_RANDOM_SWAP_RESULT_HPP +#define SPROUT_ALGORITHM_FIT_RANDOM_SWAP_RESULT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fit { + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > random_swap_result_impl_1( + Shuffled const& random_swapd, + typename sprout::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_internal(sprout::tuples::get<0>(random_swapd)), + offset, + offset + sprout::size(sprout::tuples::get<0>(random_swapd)) + ), + sprout::tuples::get<1>(random_swapd) + ); + } + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > random_swap_result_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::fit::detail::random_swap_result_impl_1( + sprout::fixed::random_swap_result(cont, sprout::forward(g)), + offset + ); + } + } // namespace detail + // + // random_swap_result + // + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fit::result_of::algorithm::type, + typename std::decay::type + > random_swap_result( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fit::detail::random_swap_result_impl( + cont, + sprout::forward(g), + sprout::internal_begin_offset(cont) + ); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIT_RANDOM_SWAP_RESULT_HPP diff --git a/sprout/algorithm/fixed.hpp b/sprout/algorithm/fixed.hpp index 874ed13b..3a994b92 100644 --- a/sprout/algorithm/fixed.hpp +++ b/sprout/algorithm/fixed.hpp @@ -50,7 +50,11 @@ #include #include #include +#include +#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 index fada647e..a04f96ac 100644 --- a/sprout/algorithm/fixed/bogo_sort.hpp +++ b/sprout/algorithm/fixed/bogo_sort.hpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT diff --git a/sprout/algorithm/fixed/bogo_sort_result.hpp b/sprout/algorithm/fixed/bogo_sort_result.hpp index ecb9b4c8..fea734a8 100644 --- a/sprout/algorithm/fixed/bogo_sort_result.hpp +++ b/sprout/algorithm/fixed/bogo_sort_result.hpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT diff --git a/sprout/algorithm/fixed/bozo_sort.hpp b/sprout/algorithm/fixed/bozo_sort.hpp new file mode 100644 index 00000000..6924b658 --- /dev/null +++ b/sprout/algorithm/fixed/bozo_sort.hpp @@ -0,0 +1,98 @@ +#ifndef SPROUT_ALGORITHM_FIXED_BOZO_SORT_HPP +#define SPROUT_ALGORITHM_FIXED_BOZO_SORT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + namespace fixed { + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type bozo_sort_impl_1( + Shuffled const& shuffled, + Compare comp + ) + { + return NS_SSCRISK_CEL_OR_SPROUT::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::bozo_sort_impl_1( + sprout::fixed::random_swap_result( + sprout::tuples::get<0>(shuffled), + sprout::tuples::get<1>(shuffled) + ), + comp + ) + ; + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type bozo_sort_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return NS_SSCRISK_CEL_OR_SPROUT::is_sorted( + sprout::begin(cont), + sprout::end(cont), + comp + ) + ? sprout::deep_copy(cont) + : sprout::fixed::detail::bozo_sort_impl_1( + sprout::fixed::random_swap_result( + cont, + sprout::forward(g) + ), + comp + ) + ; + } + } // namespace detail + // + // bozo_sort + // + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type bozo_sort( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return sprout::fixed::detail::bozo_sort_impl( + cont, + sprout::forward(g), + comp + ); + } + // + // bozo_sort + // + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type bozo_sort( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fixed::detail::bozo_sort_impl( + cont, + sprout::forward(g), + NS_SSCRISK_CEL_OR_SPROUT::less::value_type>() + ); + } + } // namespace fixed + + using sprout::fixed::bozo_sort; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_BOZO_SORT_HPP diff --git a/sprout/algorithm/fixed/bozo_sort_result.hpp b/sprout/algorithm/fixed/bozo_sort_result.hpp new file mode 100644 index 00000000..c9411c68 --- /dev/null +++ b/sprout/algorithm/fixed/bozo_sort_result.hpp @@ -0,0 +1,118 @@ +#ifndef SPROUT_ALGORITHM_FIXED_BOZO_SORT_RESULT_HPP +#define SPROUT_ALGORITHM_FIXED_BOZO_SORT_RESULT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + namespace fixed { + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > bozo_sort_result_impl_1( + Shuffled const& shuffled, + Compare comp + ) + { + return NS_SSCRISK_CEL_OR_SPROUT::is_sorted( + sprout::begin(sprout::tuples::get<0>(shuffled)), + sprout::end(sprout::tuples::get<0>(shuffled)), + comp + ) + ? shuffled + : sprout::fixed::detail::bozo_sort_result_impl_1( + sprout::fixed::random_swap_result( + sprout::tuples::get<0>(shuffled), + sprout::tuples::get<1>(shuffled) + ), + comp + ) + ; + } + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > bozo_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::is_sorted( + sprout::begin(cont), + sprout::end(cont), + comp + ) + ? result_type( + sprout::deep_copy(cont), + sprout::forward(g) + ) + : sprout::fixed::detail::bozo_sort_result_impl_1( + sprout::fixed::random_swap_result( + cont, + sprout::forward(g) + ), + comp + ) + ; + } + } // namespace detail + // + // bozo_sort_result + // + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > bozo_sort_result( + Container const& cont, + UniformRandomNumberGenerator&& g, + Compare comp + ) + { + return sprout::fixed::detail::bozo_sort_result_impl( + cont, + sprout::forward(g), + comp + ); + } + // + // bozo_sort_result + // + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > bozo_sort_result( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fixed::detail::bozo_sort_result_impl( + cont, + sprout::forward(g), + NS_SSCRISK_CEL_OR_SPROUT::less::value_type>() + ); + } + } // namespace fixed + + using sprout::fixed::bozo_sort_result; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_BOZO_SORT_RESULT_HPP diff --git a/sprout/algorithm/fixed/random_swap.hpp b/sprout/algorithm/fixed/random_swap.hpp new file mode 100644 index 00000000..e7f09f91 --- /dev/null +++ b/sprout/algorithm/fixed/random_swap.hpp @@ -0,0 +1,100 @@ +#ifndef SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_HPP +#define SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fixed { + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::array make_random_swap_indexes_2( + Random const& rnd, + std::ptrdiff_t i0 + ) + { + return sprout::array{{i0, rnd.result()}}; + } + template + inline SPROUT_CONSTEXPR sprout::array make_random_swap_indexes_1( + Random const& rnd + ) + { + return sprout::fixed::detail::make_random_swap_indexes_2( + rnd(), + rnd.result() + ); + } + template + inline SPROUT_CONSTEXPR sprout::array make_random_swap_indexes( + std::ptrdiff_t n, + UniformRandomNumberGenerator&& g + ) + { + return n > 1 + ? sprout::fixed::detail::make_random_swap_indexes_1( + sprout::random::uniform_int_distribution(0, n - 1)(sprout::forward(g)) + ) + : sprout::array{{}} + ; + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type random_swap_impl_1( + Container const& cont, + RandomSwapped const& random_swapped + ) + { + return random_swapped[0] == random_swapped[1] + ? sprout::deep_copy(cont) + : sprout::fixed::swap_element( + cont, + sprout::next(sprout::begin(cont), random_swapped[0]), + sprout::next(sprout::begin(cont), random_swapped[1]) + ) + ; + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type random_swap_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + typename sprout::container_traits::size_type size + ) + { + return sprout::fixed::detail::random_swap_impl_1( + cont, + sprout::fixed::detail::make_random_swap_indexes( + size, + sprout::forward(g) + ) + ); + } + } // namespace detail + // + // random_swap + // + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type random_swap( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fixed::detail::random_swap_impl( + cont, + sprout::forward(g), + sprout::size(cont) + ); + } + } // namespace fixed + + using sprout::fixed::random_swap; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_HPP diff --git a/sprout/algorithm/fixed/random_swap_result.hpp b/sprout/algorithm/fixed/random_swap_result.hpp new file mode 100644 index 00000000..eb926803 --- /dev/null +++ b/sprout/algorithm/fixed/random_swap_result.hpp @@ -0,0 +1,144 @@ +#ifndef SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_RESULT_HPP +#define SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_RESULT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fixed { + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + sprout::array, + typename std::decay::type + > make_random_swap_result_indexes_2( + Random const& rnd, + std::ptrdiff_t i0 + ) + { + typedef sprout::tuples::tuple< + sprout::array, + typename std::decay::type + > result_type; + return result_type( + sprout::array{{i0, rnd.result()}}, + rnd.engine() + ); + } + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + sprout::array, + typename std::decay::type + > make_random_swap_result_indexes_1( + Random const& rnd + ) + { + return sprout::fixed::detail::make_random_swap_result_indexes_2( + rnd(), + rnd.result() + ); + } + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + sprout::array, + typename std::decay::type + > make_random_swap_result_indexes( + std::ptrdiff_t n, + UniformRandomNumberGenerator&& g + ) + { + typedef sprout::tuples::tuple< + sprout::array, + typename std::decay::type + > result_type; + return n > 1 + ? sprout::fixed::detail::make_random_swap_result_indexes_1( + sprout::random::uniform_int_distribution(0, n - 1)(sprout::forward(g)) + ) + : result_type( + sprout::array{{}}, + sprout::forward(g) + ) + ; + } + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > random_swap_result_impl_1( + Container const& cont, + RandomSwapped const& random_swapped + ) + { + typedef sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > result_type; + return sprout::tuples::get<0>(random_swapped)[0] == sprout::tuples::get<0>(random_swapped)[1] + ? result_type( + sprout::deep_copy(cont), + sprout::tuples::get<1>(random_swapped) + ) + : result_type( + sprout::fixed::swap_element( + cont, + sprout::next(sprout::begin(cont), sprout::tuples::get<0>(random_swapped)[0]), + sprout::next(sprout::begin(cont), sprout::tuples::get<0>(random_swapped)[1]) + ), + sprout::tuples::get<1>(random_swapped) + ) + ; + } + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > random_swap_result_impl( + Container const& cont, + UniformRandomNumberGenerator&& g, + typename sprout::container_traits::size_type size + ) + { + return sprout::fixed::detail::random_swap_result_impl_1( + cont, + sprout::fixed::detail::make_random_swap_result_indexes( + size, + sprout::forward(g) + ) + ); + } + } // namespace detail + // + // random_swap_result + // + template + inline SPROUT_CONSTEXPR sprout::tuples::tuple< + typename sprout::fixed::result_of::algorithm::type, + typename std::decay::type + > random_swap_result( + Container const& cont, + UniformRandomNumberGenerator&& g + ) + { + return sprout::fixed::detail::random_swap_result_impl( + cont, + sprout::forward(g), + sprout::size(cont) + ); + } + } // namespace fixed + + using sprout::fixed::random_swap_result; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_RESULT_HPP diff --git a/sprout/algorithm/fixed/shuffle.hpp b/sprout/algorithm/fixed/shuffle.hpp index 13f29726..5977b1f3 100644 --- a/sprout/algorithm/fixed/shuffle.hpp +++ b/sprout/algorithm/fixed/shuffle.hpp @@ -2,7 +2,6 @@ #define SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP #include -#include #include #include #include @@ -43,7 +42,7 @@ namespace sprout { UniformRandomNumberGenerator&& g ) { - return n > 0 + return n > 1 ? sprout::fixed::detail::make_shuffle_indexes_1( n, sprout::random::uniform_int_distribution(0, n - 1)(sprout::forward(g)), diff --git a/sprout/algorithm/fixed/shuffle_result.hpp b/sprout/algorithm/fixed/shuffle_result.hpp index c7163f17..8a06f3b3 100644 --- a/sprout/algorithm/fixed/shuffle_result.hpp +++ b/sprout/algorithm/fixed/shuffle_result.hpp @@ -61,7 +61,7 @@ namespace sprout { sprout::array, typename std::decay::type > result_type; - return n > 0 + return n > 1 ? sprout::fixed::detail::make_shuffle_result_indexes_1( n, sprout::random::uniform_int_distribution(0, n - 1)(sprout::forward(g)), diff --git a/sprout/algorithm/random_swap.hpp b/sprout/algorithm/random_swap.hpp new file mode 100644 index 00000000..e4273eb4 --- /dev/null +++ b/sprout/algorithm/random_swap.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ALGORITHM_RANDOM_SWAP_HPP +#define SPROUT_ALGORITHM_RANDOM_SWAP_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_RANDOM_SWAP_HPP diff --git a/sprout/algorithm/random_swap_result.hpp b/sprout/algorithm/random_swap_result.hpp new file mode 100644 index 00000000..eb1b0e67 --- /dev/null +++ b/sprout/algorithm/random_swap_result.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ALGORITHM_RANDOM_SWAP_RESULT_HPP +#define SPROUT_ALGORITHM_RANDOM_SWAP_RESULT_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_RANDOM_SWAP_RESULT_HPP diff --git a/sprout/breed/breed_fwd.hpp b/sprout/breed/breed_fwd.hpp index 44b93632..3b15b1cc 100644 --- a/sprout/breed/breed_fwd.hpp +++ b/sprout/breed/breed_fwd.hpp @@ -24,7 +24,7 @@ namespace sprout { struct private_type_ { public: - SPROUT_CONSTEXPR private_type_ operator,(int) const; //!!!! + SPROUT_CONSTEXPR private_type_ operator,(int) const; //!!! }; template