#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 inline SPROUT_CONSTEXPR sprout::pair, 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::pair, 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 inline SPROUT_CONSTEXPR sprout::pair, typename std::decay::type> make_shuffle_result_indexes(std::ptrdiff_t n, UniformRandomNumberGenerator&& g) { typedef sprout::pair, typename std::decay::type> result_type; return n > 1 ? sprout::fixed::detail::make_shuffle_result_indexes_1( n, SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION(0, n - 1)( sprout::forward(g) ), sprout::fixed::iota(sprout::pit >(), 0), 0 ) : result_type( sprout::array{{}}, sprout::forward(g) ) ; } template inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::shuffle::type shuffle_result_impl_1( Container const& cont, sprout::index_tuple, Shuffled const& shuffled, typename sprout::container_traits::difference_type offset, typename sprout::container_traits::size_type size ) { typedef typename sprout::fixed::result_of::shuffle::type result_type; return result_type( sprout::remake( cont, sprout::size(cont), (Indexes >= offset && Indexes < offset + size ? *sprout::next(sprout::begin(cont), sprout::first(shuffled)[Indexes - offset]) : *sprout::next(sprout::internal_begin(cont), Indexes) )... ), sprout::second(shuffled) ); } template inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::shuffle::type shuffle_result_impl( Container const& cont, sprout::index_tuple indexes, UniformRandomNumberGenerator&& g, typename sprout::container_traits::difference_type offset, typename sprout::container_traits::size_type size ) { return sprout::fixed::detail::shuffle_result_impl_1( cont, indexes, sprout::fixed::detail::make_shuffle_result_indexes::static_size>( size, sprout::forward(g) ), offset, size ); } } // namespace detail // // shuffle_result // template inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::shuffle::type shuffle_result(Container const& cont, UniformRandomNumberGenerator&& g) { return sprout::fixed::detail::shuffle_result_impl( cont, sprout::index_range<0, sprout::container_traits::static_size>::make(), sprout::forward(g), sprout::internal_begin_offset(cont), sprout::size(cont) ); } } // namespace fixed using sprout::fixed::shuffle_result; } // namespace sprout #endif // #ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP