#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 #ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE # include # define SPROUT_WORKAROUND_UNIFORM_INT_DISTRIBUTION sprout::random::uniform_smallint #else # include # define SPROUT_WORKAROUND_UNIFORM_INT_DISTRIBUTION sprout::random::uniform_int_distribution #endif 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_WORKAROUND_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 #undef SPROUT_WORKAROUND_UNIFORM_INT_DISTRIBUTION #endif // #ifndef SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_RESULT_HPP