/*============================================================================= Copyright (c) 2011-2015 Bolero MURAKAMI https://github.com/bolero-MURAKAMI/Sprout Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #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 #include namespace sprout { namespace fixed { namespace detail { template inline SPROUT_CONSTEXPR sprout::array make_shuffle_indexes_1(std::ptrdiff_t n, Random const& rnd, sprout::array const& arr, std::ptrdiff_t i) { return i < n - 1 ? sprout::fixed::detail::make_shuffle_indexes_1( n, sprout::random::next(rnd)(), sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + sprout::random::result(rnd)), i + 1 ) : sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + sprout::random::result(rnd)) ; } template inline SPROUT_CONSTEXPR sprout::array make_shuffle_indexes(std::ptrdiff_t n, UniformRandomNumberGenerator&& g) { return n > 1 ? sprout::fixed::detail::make_shuffle_indexes_1( n, SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION(0, n - 1)( SPROUT_FORWARD(UniformRandomNumberGenerator, g) ), sprout::fixed::iota(sprout::pit >(), 0), 0 ) : sprout::array{{}} ; } template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type shuffle_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 ) { return sprout::remake( cont, sprout::size(cont), (Indexes >= offset && Indexes < offset + size ? *sprout::next(sprout::begin(cont), shuffled[Indexes - offset]) : *sprout::next(sprout::internal_begin(cont), Indexes) )... ); } template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type shuffle_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_impl_1( cont, indexes, sprout::fixed::detail::make_shuffle_indexes::static_size>( size, SPROUT_FORWARD(UniformRandomNumberGenerator, g) ), offset, size ); } } // namespace detail // // shuffle // template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type shuffle(Container const& cont, UniformRandomNumberGenerator&& g) { return sprout::fixed::detail::shuffle_impl( cont, sprout::container_indexes::make(), SPROUT_FORWARD(UniformRandomNumberGenerator, g), sprout::internal_begin_offset(cont), sprout::size(cont) ); } } // namespace fixed using sprout::fixed::shuffle; } // namespace sprout #endif // #ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP