2011-10-19 13:47:59 +00:00
|
|
|
#ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP
|
|
|
|
#define SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/index_tuple.hpp>
|
|
|
|
#include <sprout/array.hpp>
|
2011-10-25 09:16:27 +00:00
|
|
|
#include <sprout/pit.hpp>
|
2012-03-31 07:24:13 +00:00
|
|
|
#include <sprout/container/traits.hpp>
|
|
|
|
#include <sprout/container/functions.hpp>
|
2011-10-19 13:47:59 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
|
|
|
#include <sprout/utility/forward.hpp>
|
|
|
|
#include <sprout/algorithm/fixed/result_of.hpp>
|
|
|
|
#include <sprout/algorithm/fixed/swap_element.hpp>
|
|
|
|
#include <sprout/numeric/fixed/iota.hpp>
|
|
|
|
#include <sprout/random/uniform_int_distribution.hpp>
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace fixed {
|
|
|
|
namespace detail {
|
|
|
|
template<std::size_t N, typename Random>
|
|
|
|
SPROUT_CONSTEXPR inline sprout::array<std::ptrdiff_t, N> make_shuffle_indexes_1(
|
|
|
|
std::ptrdiff_t n,
|
|
|
|
Random const& rnd,
|
|
|
|
sprout::array<std::ptrdiff_t, N> const& arr,
|
|
|
|
std::ptrdiff_t i
|
|
|
|
)
|
|
|
|
{
|
2011-12-22 12:52:59 +00:00
|
|
|
return i < n - 1
|
2011-10-19 13:47:59 +00:00
|
|
|
? sprout::fixed::detail::make_shuffle_indexes_1(
|
|
|
|
n,
|
|
|
|
rnd(),
|
|
|
|
sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()),
|
|
|
|
i + 1
|
|
|
|
)
|
2011-12-22 12:52:59 +00:00
|
|
|
: sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result())
|
2011-10-19 13:47:59 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<std::size_t N, typename UniformRandomNumberGenerator>
|
|
|
|
SPROUT_CONSTEXPR inline sprout::array<std::ptrdiff_t, N> 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<std::ptrdiff_t>(0, n - 1)(sprout::forward<UniformRandomNumberGenerator>(g)),
|
2011-10-25 09:16:27 +00:00
|
|
|
sprout::fixed::iota(sprout::pit<sprout::array<std::ptrdiff_t, N> >(), 0),
|
2011-10-19 13:47:59 +00:00
|
|
|
0
|
|
|
|
)
|
|
|
|
: sprout::array<std::ptrdiff_t, N>{{}}
|
|
|
|
;
|
|
|
|
}
|
2012-02-28 01:46:39 +00:00
|
|
|
template<typename Container, typename Shuffled, sprout::index_t... Indexes>
|
2011-10-19 13:47:59 +00:00
|
|
|
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type shuffle_impl_1(
|
|
|
|
Container const& cont,
|
|
|
|
sprout::index_tuple<Indexes...>,
|
|
|
|
Shuffled const& shuffled,
|
2012-03-31 07:24:13 +00:00
|
|
|
typename sprout::container_traits<Container>::difference_type offset,
|
|
|
|
typename sprout::container_traits<Container>::size_type size
|
2011-10-19 13:47:59 +00:00
|
|
|
)
|
|
|
|
{
|
2012-03-31 07:24:13 +00:00
|
|
|
return sprout::remake<Container>(
|
2011-10-19 13:47:59 +00:00
|
|
|
cont,
|
|
|
|
sprout::size(cont),
|
|
|
|
(Indexes >= offset && Indexes < offset + size
|
|
|
|
? *sprout::next(sprout::begin(cont), shuffled[Indexes - offset])
|
2012-03-31 07:24:13 +00:00
|
|
|
: *sprout::next(sprout::internal_begin(cont), Indexes)
|
2011-10-19 13:47:59 +00:00
|
|
|
)...
|
|
|
|
);
|
|
|
|
}
|
2012-02-28 01:46:39 +00:00
|
|
|
template<typename Container, typename UniformRandomNumberGenerator, sprout::index_t... Indexes>
|
2011-10-19 13:47:59 +00:00
|
|
|
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type shuffle_impl(
|
|
|
|
Container const& cont,
|
|
|
|
sprout::index_tuple<Indexes...> indexes,
|
|
|
|
UniformRandomNumberGenerator&& g,
|
2012-03-31 07:24:13 +00:00
|
|
|
typename sprout::container_traits<Container>::difference_type offset,
|
|
|
|
typename sprout::container_traits<Container>::size_type size
|
2011-10-19 13:47:59 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::fixed::detail::shuffle_impl_1(
|
|
|
|
cont,
|
|
|
|
indexes,
|
2012-03-31 07:24:13 +00:00
|
|
|
sprout::fixed::detail::make_shuffle_indexes<sprout::container_traits<Container>::static_size>(
|
2011-10-19 13:47:59 +00:00
|
|
|
size,
|
|
|
|
sprout::forward<UniformRandomNumberGenerator>(g)
|
|
|
|
),
|
|
|
|
offset,
|
|
|
|
size
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// shuffle
|
|
|
|
//
|
|
|
|
template<typename Container, typename UniformRandomNumberGenerator>
|
|
|
|
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type shuffle(
|
|
|
|
Container const& cont,
|
|
|
|
UniformRandomNumberGenerator&& g
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::fixed::detail::shuffle_impl(
|
|
|
|
cont,
|
2012-03-31 07:24:13 +00:00
|
|
|
typename sprout::index_range<0, sprout::container_traits<Container>::static_size>::type(),
|
2011-10-19 13:47:59 +00:00
|
|
|
sprout::forward<UniformRandomNumberGenerator>(g),
|
2012-03-31 07:24:13 +00:00
|
|
|
sprout::internal_begin_offset(cont),
|
2011-10-19 13:47:59 +00:00
|
|
|
sprout::size(cont)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace fixed
|
|
|
|
|
|
|
|
using sprout::fixed::shuffle;
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_HPP
|