2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2011-2013 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)
|
|
|
|
=============================================================================*/
|
2011-10-18 08:13:16 +00:00
|
|
|
#ifndef SPROUT_RANDOM_SHUFFLE_ORDER_HPP
|
|
|
|
#define SPROUT_RANDOM_SHUFFLE_ORDER_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <ios>
|
|
|
|
#include <istream>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/array/array.hpp>
|
2013-10-05 10:27:07 +00:00
|
|
|
#include <sprout/array/comparison.hpp>
|
2011-10-18 08:13:16 +00:00
|
|
|
#include <sprout/operation/fixed/set.hpp>
|
2012-12-21 14:21:50 +00:00
|
|
|
#include <sprout/math/compare.hpp>
|
2011-10-18 08:13:16 +00:00
|
|
|
#include <sprout/random/random_result.hpp>
|
|
|
|
#include <sprout/random/linear_congruential.hpp>
|
|
|
|
#include <sprout/random/detail/signed_unsigned_tools.hpp>
|
2013-03-18 10:12:21 +00:00
|
|
|
#include <sprout/assert.hpp>
|
2011-10-18 08:13:16 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace random {
|
|
|
|
namespace detail {
|
|
|
|
template<typename UniformRandomNumberGenerator, std::size_t k>
|
|
|
|
class shuffle_order_engine_member {
|
|
|
|
protected:
|
|
|
|
typedef UniformRandomNumberGenerator base_type;
|
|
|
|
typedef typename base_type::result_type result_type;
|
|
|
|
public:
|
|
|
|
base_type rng_;
|
|
|
|
sprout::array<result_type, k> v_;
|
|
|
|
result_type y_;
|
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// shuffle_order_engine
|
|
|
|
//
|
|
|
|
template<typename UniformRandomNumberGenerator, std::size_t k>
|
|
|
|
class shuffle_order_engine
|
|
|
|
: private sprout::random::detail::shuffle_order_engine_member<UniformRandomNumberGenerator, k>
|
|
|
|
{
|
2012-11-16 04:40:19 +00:00
|
|
|
static_assert(
|
2013-08-06 15:15:09 +00:00
|
|
|
sprout::numeric_limits<typename UniformRandomNumberGenerator::result_type>::is_integer,
|
|
|
|
"sprout::numeric_limits<typename UniformRandomNumberGenerator::result_type>::is_integer"
|
2012-11-16 04:40:19 +00:00
|
|
|
);
|
|
|
|
static_assert(k > 0, "k > 0");
|
2011-10-18 08:13:16 +00:00
|
|
|
private:
|
|
|
|
typedef sprout::random::detail::shuffle_order_engine_member<UniformRandomNumberGenerator, k> member_type;
|
|
|
|
public:
|
|
|
|
typedef UniformRandomNumberGenerator base_type;
|
|
|
|
typedef typename base_type::result_type result_type;
|
|
|
|
private:
|
2013-08-09 13:14:43 +00:00
|
|
|
struct private_construct_t {};
|
2011-10-18 08:13:16 +00:00
|
|
|
public:
|
|
|
|
SPROUT_STATIC_CONSTEXPR std::size_t buffer_size = k;
|
|
|
|
SPROUT_STATIC_CONSTEXPR std::size_t table_size = k;
|
|
|
|
public:
|
2012-11-16 04:40:19 +00:00
|
|
|
static SPROUT_CONSTEXPR result_type static_min() SPROUT_NOEXCEPT {
|
|
|
|
return base_type::static_min();
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR result_type static_max() SPROUT_NOEXCEPT {
|
|
|
|
return base_type::static_max();
|
|
|
|
}
|
2011-10-18 08:13:16 +00:00
|
|
|
private:
|
|
|
|
template<typename Random, typename... Args>
|
|
|
|
static SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sizeof...(Args) == k,
|
|
|
|
member_type
|
|
|
|
>::type init_member_1(Random const& rnd, Args const&... args) {
|
|
|
|
return member_type{
|
|
|
|
rnd.engine(),
|
|
|
|
sprout::array<result_type, k>{{args...}},
|
|
|
|
rnd.result()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
template<typename Random, typename... Args>
|
|
|
|
static SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sizeof...(Args) < k,
|
|
|
|
member_type
|
|
|
|
>::type init_member_1(Random const& rnd, Args const&... args) {
|
|
|
|
return init_member_1(rnd(), args..., rnd.result());
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR member_type init_member(base_type const& rng) {
|
|
|
|
return init_member_1(rng());
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
using member_type::rng_;
|
|
|
|
using member_type::v_;
|
|
|
|
using member_type::y_;
|
|
|
|
private:
|
|
|
|
SPROUT_CONSTEXPR shuffle_order_engine(
|
|
|
|
base_type const& rng,
|
|
|
|
sprout::array<result_type, k> const& v,
|
|
|
|
result_type const& y,
|
2013-08-09 13:14:43 +00:00
|
|
|
private_construct_t
|
2011-10-18 08:13:16 +00:00
|
|
|
)
|
|
|
|
: member_type{rng, v, y}
|
|
|
|
{}
|
|
|
|
template<typename Random, typename BaseUnsigned>
|
2012-11-14 02:22:05 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<shuffle_order_engine>
|
|
|
|
generate_1(Random const& rnd, BaseUnsigned j) const {
|
2011-10-18 08:13:16 +00:00
|
|
|
return sprout::random::random_result<shuffle_order_engine>(
|
|
|
|
v_[j],
|
|
|
|
shuffle_order_engine(
|
|
|
|
rnd.engine(),
|
|
|
|
sprout::fixed::set(v_, j, rnd.result()),
|
|
|
|
v_[j],
|
2013-08-09 13:14:43 +00:00
|
|
|
private_construct_t()
|
2011-10-18 08:13:16 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename BaseUnsigned>
|
2012-11-14 02:22:05 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<shuffle_order_engine>
|
|
|
|
generate(BaseUnsigned brange, BaseUnsigned off) const {
|
2011-10-18 08:13:16 +00:00
|
|
|
return generate_1(
|
|
|
|
rng_(),
|
|
|
|
k == 1 ? BaseUnsigned(0)
|
2013-08-06 15:15:09 +00:00
|
|
|
: sprout::math::less(brange, sprout::numeric_limits<BaseUnsigned>::max() / k) ? BaseUnsigned(k * off / (brange + 1))
|
|
|
|
: sprout::math::less(brange, sprout::numeric_limits<std::uintmax_t>::max() / k)
|
2012-11-14 02:22:05 +00:00
|
|
|
? static_cast<BaseUnsigned>(static_cast<std::uintmax_t>(off) * k / (static_cast<std::uintmax_t>(brange) + 1))
|
2013-11-05 08:39:11 +00:00
|
|
|
// : static_cast<BaseUnsigned>(sprout::random::detail::muldiv(off, k, static_cast<std::uintmax_t>(brange) + 1)) // ???
|
2013-03-18 10:12:21 +00:00
|
|
|
: (SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), sprout::random::random_result<shuffle_order_engine>())
|
2011-10-18 08:13:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR shuffle_order_engine()
|
|
|
|
: member_type(init_member(base_type()))
|
|
|
|
{}
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR shuffle_order_engine(result_type const& s)
|
2011-10-18 08:13:16 +00:00
|
|
|
: member_type(init_member(base_type(s)))
|
|
|
|
{}
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR shuffle_order_engine(base_type const& rng)
|
2011-10-18 08:13:16 +00:00
|
|
|
: member_type(init_member(rng))
|
|
|
|
{}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
|
2011-10-18 08:13:16 +00:00
|
|
|
return rng_.min();
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
|
2011-10-18 08:13:16 +00:00
|
|
|
return rng_.max();
|
|
|
|
}
|
2013-11-05 08:39:11 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR result_type operator()() {
|
|
|
|
typedef typename std::make_unsigned<result_type>::type base_unsigned;
|
|
|
|
base_unsigned const brange = sprout::random::detail::subtract<result_type>()(max(), min());
|
|
|
|
base_unsigned const off = sprout::random::detail::subtract<result_type>()(y_, min());
|
|
|
|
base_unsigned j = k == 1 ? base_unsigned(0)
|
|
|
|
: sprout::math::less(brange, sprout::numeric_limits<base_unsigned>::max() / k) ? base_unsigned(k * off / (brange + 1))
|
|
|
|
: sprout::math::less(brange, sprout::numeric_limits<std::uintmax_t>::max() / k)
|
|
|
|
? static_cast<base_unsigned>(static_cast<std::uintmax_t>(off) * k / (static_cast<std::uintmax_t>(brange) + 1))
|
|
|
|
// : static_cast<base_unsigned>(sprout::random::detail::muldiv(off, k, static_cast<std::uintmax_t>(brange) + 1)) // ???
|
|
|
|
: (SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), base_unsigned(0))
|
|
|
|
;
|
|
|
|
y_ = v_[j];
|
|
|
|
v_[j] = static_cast<result_type>(rng_());
|
|
|
|
return y_;
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<shuffle_order_engine> const operator()() const {
|
2011-10-18 08:13:16 +00:00
|
|
|
typedef typename std::make_unsigned<result_type>::type base_unsigned;
|
|
|
|
return generate(
|
|
|
|
base_unsigned(sprout::random::detail::subtract<result_type>()(max(), min())),
|
|
|
|
base_unsigned(sprout::random::detail::subtract<result_type>()(y_, min()))
|
|
|
|
);
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR base_type const& base() const SPROUT_NOEXCEPT {
|
|
|
|
return rng_;
|
|
|
|
}
|
|
|
|
friend SPROUT_CONSTEXPR bool operator==(shuffle_order_engine const& lhs, shuffle_order_engine const& rhs) SPROUT_NOEXCEPT {
|
2011-10-18 08:13:16 +00:00
|
|
|
return lhs.rng_ == rhs.rng_ && lhs.y_ == rhs.y_ && lhs.v_ == rhs.v_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator!=(shuffle_order_engine const& lhs, shuffle_order_engine const& rhs) SPROUT_NOEXCEPT {
|
2011-10-18 08:13:16 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
template<typename Elem, typename Traits>
|
2013-11-02 09:28:18 +00:00
|
|
|
friend SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>& operator>>(
|
2011-10-18 08:13:16 +00:00
|
|
|
std::basic_istream<Elem, Traits>& lhs,
|
|
|
|
shuffle_order_engine& rhs
|
|
|
|
)
|
|
|
|
{
|
2011-10-18 13:28:32 +00:00
|
|
|
lhs >> rhs.rng_;
|
2011-10-18 08:13:16 +00:00
|
|
|
for (std::size_t i = 0; i < k; ++i) {
|
2011-10-18 13:28:32 +00:00
|
|
|
lhs >> std::ws >> rhs.v_[i];
|
2011-10-18 08:13:16 +00:00
|
|
|
}
|
2011-10-18 13:28:32 +00:00
|
|
|
lhs >> std::ws >> rhs.y_;
|
2011-10-18 08:13:16 +00:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
template<typename Elem, typename Traits>
|
2013-11-02 09:28:18 +00:00
|
|
|
friend SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>& operator<<(
|
2011-10-18 08:13:16 +00:00
|
|
|
std::basic_ostream<Elem, Traits>& lhs,
|
|
|
|
shuffle_order_engine const& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
lhs << rhs.rng_;
|
|
|
|
for (std::size_t i = 0; i < k; ++i) {
|
|
|
|
lhs << ' ' << rhs.v_[i];
|
|
|
|
}
|
|
|
|
lhs << ' ' << rhs.y_;
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename UniformRandomNumberGenerator, std::size_t k>
|
2012-06-15 15:08:42 +00:00
|
|
|
SPROUT_CONSTEXPR_OR_CONST std::size_t sprout::random::shuffle_order_engine<UniformRandomNumberGenerator, k>::buffer_size;
|
2011-10-18 08:13:16 +00:00
|
|
|
template<typename UniformRandomNumberGenerator, std::size_t k>
|
2012-06-15 15:08:42 +00:00
|
|
|
SPROUT_CONSTEXPR_OR_CONST std::size_t sprout::random::shuffle_order_engine<UniformRandomNumberGenerator, k>::table_size;
|
2011-10-18 08:13:16 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// knuth_b
|
|
|
|
//
|
2012-04-14 03:31:47 +00:00
|
|
|
typedef sprout::random::shuffle_order_engine<sprout::random::minstd_rand0, 256> knuth_b;
|
2011-10-18 08:13:16 +00:00
|
|
|
//
|
|
|
|
// kreutzer1986
|
|
|
|
//
|
|
|
|
typedef sprout::random::shuffle_order_engine<sprout::random::linear_congruential_engine<std::uint32_t, 1366, 150889, 714025>, 97> kreutzer1986;
|
2013-03-22 05:24:19 +00:00
|
|
|
} // namespace random
|
2011-10-18 08:13:16 +00:00
|
|
|
|
2012-04-14 03:31:47 +00:00
|
|
|
using sprout::random::shuffle_order_engine;
|
2011-10-18 08:13:16 +00:00
|
|
|
using sprout::random::knuth_b;
|
|
|
|
using sprout::random::kreutzer1986;
|
2013-03-22 05:24:19 +00:00
|
|
|
} // namespace sprout
|
2011-10-18 08:13:16 +00:00
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
#endif // #ifndef SPROUT_RANDOM_SHUFFLE_ORDER_HPP
|