#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