[sprout.random] add Sseq version: constructor, seed

This commit is contained in:
bolero-MURAKAMI 2013-11-09 17:39:57 +09:00
parent ab0731c4fc
commit b60a7aca9c
10 changed files with 495 additions and 152 deletions

View file

@ -11,6 +11,7 @@
#include <ios>
#include <sprout/config.hpp>
#include <sprout/detail/integer/integer_mask.hpp>
#include <sprout/random/detail/seed_impl.hpp>
#include <sprout/random/random_result.hpp>
namespace sprout {
@ -29,7 +30,7 @@ namespace sprout {
SPROUT_STATIC_CONSTEXPR int exponent1 = k;
SPROUT_STATIC_CONSTEXPR int exponent2 = q;
SPROUT_STATIC_CONSTEXPR int step_size = s;
SPROUT_STATIC_CONSTEXPR UIntType default_seed = 341;
SPROUT_STATIC_CONSTEXPR result_type default_seed = 341;
public:
static_assert(w > 0, "w > 0");
static_assert(q > 0, "q > 0");
@ -44,19 +45,31 @@ namespace sprout {
return wordmask();
}
private:
static SPROUT_CONSTEXPR UIntType wordmask() {
static SPROUT_CONSTEXPR result_type wordmask() {
return sprout::detail::low_bits_mask_t<w>::sig_bits;
}
static SPROUT_CONSTEXPR UIntType init_seed_1(UIntType const& x0) {
static SPROUT_CONSTEXPR result_type init_seed_1(result_type x0) {
return x0 < (1 << (w - k)) ? x0 + (1 << (w - k)) : x0;
}
static SPROUT_CONSTEXPR UIntType init_seed(UIntType const& x0) {
static SPROUT_CONSTEXPR result_type init_seed(result_type x0 = default_seed) {
return init_seed_1(x0 & wordmask());
}
template<typename Sseq>
static SPROUT_CXX14_CONSTEXPR result_type init_seed(Sseq& seq) {
return init_seed(sprout::random::detail::seed_one_int<result_type, (result_type(2) << (w - 1))>(seq));
}
template<typename Sseq>
static SPROUT_CONSTEXPR result_type init_seed(Sseq const& seq) {
return init_seed(sprout::random::detail::seed_one_int<result_type, (result_type(2) << (w - 1))>(seq));
}
template<typename InputIterator>
static SPROUT_CONSTEXPR result_type init_seed(InputIterator first, InputIterator last) {
return init_seed(sprout::random::detail::get_one_int<result_type, (result_type(2) << (w - 1))>(first, last));
}
private:
UIntType x_;
result_type x_;
private:
SPROUT_CONSTEXPR linear_feedback_shift_engine(UIntType const& x, private_construct_t)
SPROUT_CONSTEXPR linear_feedback_shift_engine(result_type x, private_construct_t)
: x_(x)
{}
SPROUT_CONSTEXPR sprout::random::random_result<linear_feedback_shift_engine> generate(result_type result) const {
@ -69,9 +82,36 @@ namespace sprout {
SPROUT_CONSTEXPR linear_feedback_shift_engine()
: x_(init_seed(default_seed))
{}
explicit SPROUT_CONSTEXPR linear_feedback_shift_engine(UIntType const& x0)
explicit SPROUT_CONSTEXPR linear_feedback_shift_engine(result_type x0)
: x_(init_seed(x0))
{}
template<typename Sseq>
explicit SPROUT_CXX14_CONSTEXPR linear_feedback_shift_engine(Sseq& seq)
: x_(init_seed(seq))
{}
template<typename Sseq>
explicit SPROUT_CONSTEXPR linear_feedback_shift_engine(Sseq const& seq)
: x_(init_seed(seq))
{}
template<typename InputIterator>
SPROUT_CONSTEXPR linear_feedback_shift_engine(InputIterator first, InputIterator last)
: x_(init_seed(first, last))
{}
SPROUT_CXX14_CONSTEXPR void seed(result_type x0 = default_seed) {
x_ = init_seed(x0);
}
template<typename Sseq>
SPROUT_CXX14_CONSTEXPR void seed(Sseq& seq) {
x_ = init_seed(seq);
}
template<typename Sseq>
SPROUT_CXX14_CONSTEXPR void seed(Sseq const& seq) {
x_ = init_seed(seq);
}
template<typename InputIterator>
SPROUT_CXX14_CONSTEXPR void seed(InputIterator first, InputIterator last) {
x_ = init_seed(first, last);
}
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
return static_min();
}