1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/random/linear_feedback_shift.hpp

171 lines
7.4 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2016-02-25 09:48:28 +00:00
Copyright (c) 2011-2016 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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 15:18:58 +00:00
#ifndef SPROUT_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
#define SPROUT_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
2014-11-28 07:19:07 +00:00
#include <istream>
#include <ostream>
2011-10-18 15:18:58 +00:00
#include <sprout/config.hpp>
#include <sprout/detail/integer/integer_mask.hpp>
#include <sprout/random/detail/seed_impl.hpp>
2011-10-18 15:18:58 +00:00
#include <sprout/random/random_result.hpp>
2013-11-10 11:50:16 +00:00
#include <sprout/random/type_traits.hpp>
#include <sprout/type_traits/enabler_if.hpp>
2011-10-18 15:18:58 +00:00
namespace sprout {
namespace random {
//
// linear_feedback_shift_engine
//
template<typename UIntType, int w, int k, int q, int s>
class linear_feedback_shift_engine {
public:
typedef UIntType result_type;
private:
2013-08-09 13:14:43 +00:00
struct private_construct_t {};
2011-10-18 15:18:58 +00:00
public:
SPROUT_STATIC_CONSTEXPR int word_size = w;
SPROUT_STATIC_CONSTEXPR int exponent1 = k;
SPROUT_STATIC_CONSTEXPR int exponent2 = q;
SPROUT_STATIC_CONSTEXPR int step_size = s;
SPROUT_STATIC_CONSTEXPR result_type default_seed = 341;
2011-10-18 15:18:58 +00:00
public:
static_assert(w > 0, "w > 0");
static_assert(q > 0, "q > 0");
static_assert(k < w, "k < w");
static_assert(0 < 2 * q && 2 * q < k, "0 < 2 * q && 2 * q < k");
static_assert(0 < s && s <= k - q, "0 < s && s <= k - q");
2012-11-16 04:40:19 +00:00
public:
static SPROUT_CONSTEXPR result_type static_min() SPROUT_NOEXCEPT {
2011-10-18 15:18:58 +00:00
return 0;
}
2012-11-16 04:40:19 +00:00
static SPROUT_CONSTEXPR result_type static_max() SPROUT_NOEXCEPT {
2011-10-18 15:18:58 +00:00
return wordmask();
}
2012-11-16 04:40:19 +00:00
private:
static SPROUT_CONSTEXPR result_type wordmask() {
2012-11-16 04:40:19 +00:00
return sprout::detail::low_bits_mask_t<w>::sig_bits;
}
static SPROUT_CONSTEXPR result_type init_seed_1(result_type x0) {
2011-10-18 15:18:58 +00:00
return x0 < (1 << (w - k)) ? x0 + (1 << (w - k)) : x0;
}
static SPROUT_CONSTEXPR result_type init_seed(result_type x0 = default_seed) {
2011-10-18 15:18:58 +00:00
return init_seed_1(x0 & wordmask());
}
2013-11-10 11:50:16 +00:00
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
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));
}
2013-11-10 11:50:16 +00:00
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
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));
}
2011-10-18 15:18:58 +00:00
private:
result_type x_;
2011-10-18 15:18:58 +00:00
private:
SPROUT_CONSTEXPR linear_feedback_shift_engine(result_type x, private_construct_t)
2011-10-18 15:18:58 +00:00
: x_(x)
{}
SPROUT_CONSTEXPR sprout::random::random_result<linear_feedback_shift_engine> generate(result_type result) const {
return sprout::random::random_result<linear_feedback_shift_engine>(
result,
2013-08-09 13:14:43 +00:00
linear_feedback_shift_engine(result, private_construct_t())
2011-10-18 15:18:58 +00:00
);
}
public:
SPROUT_CONSTEXPR linear_feedback_shift_engine()
: x_(init_seed(default_seed))
{}
2014-08-01 06:24:00 +00:00
linear_feedback_shift_engine(linear_feedback_shift_engine const&) = default;
explicit SPROUT_CONSTEXPR linear_feedback_shift_engine(result_type x0)
2011-10-18 15:18:58 +00:00
: x_(init_seed(x0))
{}
2013-11-10 11:50:16 +00:00
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
explicit SPROUT_CXX14_CONSTEXPR linear_feedback_shift_engine(Sseq& seq)
: x_(init_seed(seq))
{}
2013-11-10 11:50:16 +00:00
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
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);
}
2013-11-10 11:50:16 +00:00
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
SPROUT_CXX14_CONSTEXPR void seed(Sseq& seq) {
x_ = init_seed(seq);
}
2013-11-10 11:50:16 +00:00
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
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);
}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
2011-10-18 15:18:58 +00:00
return static_min();
}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
2011-10-18 15:18:58 +00:00
return static_max();
}
SPROUT_CXX14_CONSTEXPR result_type operator()() {
x_ = ((x_ & ((wordmask() << (w - k)) & wordmask())) << s) ^ ((((x_ << q) ^ x_) & wordmask()) >> (k - s));
return x_;
}
SPROUT_CONSTEXPR sprout::random::random_result<linear_feedback_shift_engine> const operator()() const {
2011-10-18 15:18:58 +00:00
return generate(((x_ & ((wordmask() << (w - k)) & wordmask())) << s) ^ ((((x_ << q) ^ x_) & wordmask()) >> (k - s)));
}
2012-11-16 04:40:19 +00:00
friend SPROUT_CONSTEXPR bool operator==(linear_feedback_shift_engine const& lhs, linear_feedback_shift_engine const& rhs) SPROUT_NOEXCEPT {
2011-10-18 15:18:58 +00:00
return lhs.x_ == rhs.x_;
}
2012-11-16 04:40:19 +00:00
friend SPROUT_CONSTEXPR bool operator!=(linear_feedback_shift_engine const& lhs, linear_feedback_shift_engine const& rhs) SPROUT_NOEXCEPT {
2011-10-18 15:18:58 +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 15:18:58 +00:00
std::basic_istream<Elem, Traits>& lhs,
linear_feedback_shift_engine& rhs
)
{
return lhs >> rhs.x_;
}
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 15:18:58 +00:00
std::basic_ostream<Elem, Traits>& lhs,
linear_feedback_shift_engine const& rhs
)
{
return lhs << rhs.x_;
}
};
template<typename UIntType, int w, int k, int q, int s>
SPROUT_CONSTEXPR_OR_CONST int sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::word_size;
2011-10-18 15:18:58 +00:00
template<typename UIntType, int w, int k, int q, int s>
SPROUT_CONSTEXPR_OR_CONST int sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::exponent1;
2011-10-18 15:18:58 +00:00
template<typename UIntType, int w, int k, int q, int s>
SPROUT_CONSTEXPR_OR_CONST int sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::exponent2;
2011-10-18 15:18:58 +00:00
template<typename UIntType, int w, int k, int q, int s>
SPROUT_CONSTEXPR_OR_CONST int sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::step_size;
2011-10-18 15:18:58 +00:00
template<typename UIntType, int w, int k, int q, int s>
SPROUT_CONSTEXPR_OR_CONST UIntType sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::default_seed;
2013-03-22 05:24:19 +00:00
} // namespace random
using sprout::random::linear_feedback_shift_engine;
2013-03-22 05:24:19 +00:00
} // namespace sprout
2011-10-18 15:18:58 +00:00
2013-03-22 05:24:19 +00:00
#endif // #ifndef SPROUT_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP