2011-12-22 12:52:59 +00:00
|
|
|
#ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP
|
|
|
|
#define SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-04-06 04:06:51 +00:00
|
|
|
#include <sprout/index_tuple/metafunction.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/array/array.hpp>
|
|
|
|
#include <sprout/pit/pit.hpp>
|
2012-03-31 07:24:13 +00:00
|
|
|
#include <sprout/container/traits.hpp>
|
|
|
|
#include <sprout/container/functions.hpp>
|
2013-03-31 06:14:10 +00:00
|
|
|
#include <sprout/container/indexes.hpp>
|
2011-12-22 12:52:59 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
|
|
|
#include <sprout/utility/forward.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2013-02-07 15:49:47 +00:00
|
|
|
#include <sprout/utility/pair/access.hpp>
|
2011-12-22 12:52:59 +00:00
|
|
|
#include <sprout/algorithm/fixed/result_of.hpp>
|
|
|
|
#include <sprout/algorithm/fixed/swap_element.hpp>
|
|
|
|
#include <sprout/numeric/fixed/iota.hpp>
|
2013-02-03 16:10:26 +00:00
|
|
|
#include <sprout/workaround/detail/uniform_int_distribution.hpp>
|
2011-12-22 12:52:59 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace fixed {
|
|
|
|
namespace detail {
|
|
|
|
template<typename UniformRandomNumberGenerator, std::size_t N, typename Random>
|
2013-02-03 16:10:26 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<sprout::array<std::ptrdiff_t, N>, typename std::decay<UniformRandomNumberGenerator>::type>
|
2012-09-29 08:10:46 +00:00
|
|
|
make_shuffle_result_indexes_1(std::ptrdiff_t n, Random const& rnd, sprout::array<std::ptrdiff_t, N> const& arr, std::ptrdiff_t i) {
|
2013-02-03 16:10:26 +00:00
|
|
|
typedef sprout::pair<sprout::array<std::ptrdiff_t, N>, typename std::decay<UniformRandomNumberGenerator>::type> result_type;
|
2011-12-22 12:52:59 +00:00
|
|
|
return i < n - 1
|
|
|
|
? sprout::fixed::detail::make_shuffle_result_indexes_1<UniformRandomNumberGenerator>(
|
|
|
|
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<std::size_t N, typename UniformRandomNumberGenerator>
|
2013-02-03 16:10:26 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<sprout::array<std::ptrdiff_t, N>, typename std::decay<UniformRandomNumberGenerator>::type>
|
2012-09-29 08:10:46 +00:00
|
|
|
make_shuffle_result_indexes(std::ptrdiff_t n, UniformRandomNumberGenerator&& g) {
|
2013-02-03 16:10:26 +00:00
|
|
|
typedef sprout::pair<sprout::array<std::ptrdiff_t, N>, typename std::decay<UniformRandomNumberGenerator>::type> result_type;
|
2012-07-25 14:10:01 +00:00
|
|
|
return n > 1
|
2011-12-22 12:52:59 +00:00
|
|
|
? sprout::fixed::detail::make_shuffle_result_indexes_1<UniformRandomNumberGenerator>(
|
|
|
|
n,
|
2013-02-03 16:10:26 +00:00
|
|
|
SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION<std::ptrdiff_t>(0, n - 1)(
|
|
|
|
sprout::forward<UniformRandomNumberGenerator>(g)
|
|
|
|
),
|
2011-12-22 12:52:59 +00:00
|
|
|
sprout::fixed::iota(sprout::pit<sprout::array<std::ptrdiff_t, N> >(), 0),
|
|
|
|
0
|
|
|
|
)
|
|
|
|
: result_type(
|
|
|
|
sprout::array<std::ptrdiff_t, N>{{}},
|
|
|
|
sprout::forward<UniformRandomNumberGenerator>(g)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2012-02-28 01:46:39 +00:00
|
|
|
template<typename UniformRandomNumberGenerator, typename Container, typename Shuffled, sprout::index_t... Indexes>
|
2013-02-03 16:10:26 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::shuffle<Container, UniformRandomNumberGenerator>::type
|
2012-09-29 08:10:46 +00:00
|
|
|
shuffle_result_impl_1(
|
2011-12-22 12:52:59 +00:00
|
|
|
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-12-22 12:52:59 +00:00
|
|
|
)
|
|
|
|
{
|
2013-02-03 16:10:26 +00:00
|
|
|
typedef typename sprout::fixed::result_of::shuffle<Container, UniformRandomNumberGenerator>::type result_type;
|
2011-12-22 12:52:59 +00:00
|
|
|
return result_type(
|
2012-03-31 07:24:13 +00:00
|
|
|
sprout::remake<Container>(
|
2011-12-22 12:52:59 +00:00
|
|
|
cont,
|
|
|
|
sprout::size(cont),
|
|
|
|
(Indexes >= offset && Indexes < offset + size
|
2013-02-03 16:10:26 +00:00
|
|
|
? *sprout::next(sprout::begin(cont), sprout::first(shuffled)[Indexes - offset])
|
2012-03-31 07:24:13 +00:00
|
|
|
: *sprout::next(sprout::internal_begin(cont), Indexes)
|
2011-12-22 12:52:59 +00:00
|
|
|
)...
|
|
|
|
),
|
2013-02-03 16:10:26 +00:00
|
|
|
sprout::second(shuffled)
|
2011-12-22 12:52:59 +00:00
|
|
|
);
|
|
|
|
}
|
2012-02-28 01:46:39 +00:00
|
|
|
template<typename Container, typename UniformRandomNumberGenerator, sprout::index_t... Indexes>
|
2013-02-03 16:10:26 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::shuffle<Container, UniformRandomNumberGenerator>::type
|
2012-09-29 08:10:46 +00:00
|
|
|
shuffle_result_impl(
|
2011-12-22 12:52:59 +00:00
|
|
|
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-12-22 12:52:59 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::fixed::detail::shuffle_result_impl_1<UniformRandomNumberGenerator>(
|
|
|
|
cont,
|
|
|
|
indexes,
|
2012-03-31 07:24:13 +00:00
|
|
|
sprout::fixed::detail::make_shuffle_result_indexes<sprout::container_traits<Container>::static_size>(
|
2011-12-22 12:52:59 +00:00
|
|
|
size,
|
|
|
|
sprout::forward<UniformRandomNumberGenerator>(g)
|
|
|
|
),
|
|
|
|
offset,
|
|
|
|
size
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// shuffle_result
|
|
|
|
//
|
|
|
|
template<typename Container, typename UniformRandomNumberGenerator>
|
2013-02-03 16:10:26 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::shuffle<Container, UniformRandomNumberGenerator>::type
|
2012-09-29 08:10:46 +00:00
|
|
|
shuffle_result(Container const& cont, UniformRandomNumberGenerator&& g) {
|
2011-12-22 12:52:59 +00:00
|
|
|
return sprout::fixed::detail::shuffle_result_impl(
|
|
|
|
cont,
|
2013-03-31 06:14:10 +00:00
|
|
|
sprout::container_indexes<Container>::make(),
|
2011-12-22 12:52:59 +00:00
|
|
|
sprout::forward<UniformRandomNumberGenerator>(g),
|
2012-03-31 07:24:13 +00:00
|
|
|
sprout::internal_begin_offset(cont),
|
2011-12-22 12:52:59 +00:00
|
|
|
sprout::size(cont)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace fixed
|
|
|
|
|
|
|
|
using sprout::fixed::shuffle_result;
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP
|