#ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP #define SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP #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::array make_shuffle_indexes_1( std::ptrdiff_t n, Random const& rnd, sprout::array const& arr, std::ptrdiff_t i ) { return i < n ? sprout::fixed::detail::make_shuffle_indexes_1( n, rnd(), sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()), i + 1 ) : arr ; } template SPROUT_CONSTEXPR inline sprout::array make_shuffle_indexes( std::ptrdiff_t n, UniformRandomNumberGenerator&& g ) { return n > 0 ? sprout::fixed::detail::make_shuffle_indexes_1( n, sprout::random::uniform_int_distribution(0, n - 1)(sprout::forward(g)), sprout::fixed::iota(sprout::null_array >(), 0), 0 ) : sprout::array{{}} ; } template SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type shuffle_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 ) { return sprout::remake_clone( cont, sprout::size(cont), (Indexes >= offset && Indexes < offset + size ? *sprout::next(sprout::begin(cont), shuffled[Indexes - offset]) : *sprout::next(sprout::fixed_begin(cont), Indexes) )... ); } template SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type shuffle_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_impl_1( cont, indexes, sprout::fixed::detail::make_shuffle_indexes::fixed_size>( size, sprout::forward(g) ), offset, size ); } } // namespace detail // // shuffle // template SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm::type shuffle( Container const& cont, UniformRandomNumberGenerator&& g ) { return sprout::fixed::detail::shuffle_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; } // namespace sprout #endif // #ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP