annotate DEPENDENCIES/generic/include/boost/random/linear_feedback_shift.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 /* boost random/linear_feedback_shift.hpp header file
Chris@16 2 *
Chris@16 3 * Copyright Jens Maurer 2002
Chris@16 4 * Copyright Steven Watanabe 2011
Chris@16 5 * Distributed under the Boost Software License, Version 1.0. (See
Chris@16 6 * accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 * http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 *
Chris@16 9 * See http://www.boost.org for most recent version including documentation.
Chris@16 10 *
Chris@101 11 * $Id$
Chris@16 12 *
Chris@16 13 */
Chris@16 14
Chris@16 15 #ifndef BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
Chris@16 16 #define BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
Chris@16 17
Chris@16 18 #include <iosfwd>
Chris@16 19 #include <stdexcept>
Chris@16 20 #include <boost/config.hpp>
Chris@16 21 #include <boost/cstdint.hpp>
Chris@16 22 #include <boost/static_assert.hpp>
Chris@16 23 #include <boost/integer/integer_mask.hpp>
Chris@16 24 #include <boost/random/detail/config.hpp>
Chris@16 25 #include <boost/random/detail/seed.hpp>
Chris@16 26 #include <boost/random/detail/operators.hpp>
Chris@16 27 #include <boost/random/detail/seed_impl.hpp>
Chris@16 28
Chris@16 29 namespace boost {
Chris@16 30 namespace random {
Chris@16 31
Chris@16 32 /**
Chris@16 33 * Instatiations of @c linear_feedback_shift model a
Chris@16 34 * \pseudo_random_number_generator. It was originally
Chris@16 35 * proposed in
Chris@16 36 *
Chris@16 37 * @blockquote
Chris@16 38 * "Random numbers generated by linear recurrence modulo two.",
Chris@16 39 * Tausworthe, R. C.(1965), Mathematics of Computation 19, 201-209.
Chris@16 40 * @endblockquote
Chris@16 41 */
Chris@16 42 template<class UIntType, int w, int k, int q, int s>
Chris@16 43 class linear_feedback_shift_engine
Chris@16 44 {
Chris@16 45 public:
Chris@16 46 typedef UIntType result_type;
Chris@16 47 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
Chris@16 48 BOOST_STATIC_CONSTANT(int, word_size = w);
Chris@16 49 BOOST_STATIC_CONSTANT(int, exponent1 = k);
Chris@16 50 BOOST_STATIC_CONSTANT(int, exponent2 = q);
Chris@16 51 BOOST_STATIC_CONSTANT(int, step_size = s);
Chris@16 52 BOOST_STATIC_CONSTANT(UIntType, default_seed = 341);
Chris@16 53
Chris@16 54 /** Returns the smallest value that the generator can produce. */
Chris@16 55 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
Chris@16 56 /** Returns the largest value that the generator can produce. */
Chris@16 57 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 58 { return wordmask(); }
Chris@16 59
Chris@16 60 BOOST_STATIC_ASSERT(w > 0);
Chris@16 61 BOOST_STATIC_ASSERT(q > 0);
Chris@16 62 BOOST_STATIC_ASSERT(k < w);
Chris@16 63 BOOST_STATIC_ASSERT(0 < 2*q && 2*q < k);
Chris@16 64 BOOST_STATIC_ASSERT(0 < s && s <= k-q);
Chris@16 65
Chris@16 66 /** Constructs a @c linear_feedback_shift_engine, using the default seed. */
Chris@16 67 linear_feedback_shift_engine() { seed(); }
Chris@16 68
Chris@16 69 /** Constructs a @c linear_feedback_shift_engine, seeding it with s0. */
Chris@16 70 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(linear_feedback_shift_engine,
Chris@16 71 UIntType, s0)
Chris@16 72 { seed(s0); }
Chris@16 73
Chris@16 74 /** Constructs a @c linear_feedback_shift_engine, seeding it with seq. */
Chris@16 75 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(linear_feedback_shift_engine,
Chris@16 76 SeedSeq, seq)
Chris@16 77 { seed(seq); }
Chris@16 78
Chris@16 79 /**
Chris@16 80 * Constructs a @c linear_feedback_shift_engine, seeding it with
Chris@16 81 * values from the range [first, last).
Chris@16 82 */
Chris@16 83 template<class It> linear_feedback_shift_engine(It& first, It last)
Chris@16 84 { seed(first, last); }
Chris@16 85
Chris@16 86 /** Seeds a @c linear_feedback_shift_engine with the default seed. */
Chris@16 87 void seed() { seed(default_seed); }
Chris@16 88
Chris@16 89 /** Seeds a @c linear_feedback_shift_engine with @c s0. */
Chris@16 90 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(linear_feedback_shift_engine,
Chris@16 91 UIntType, s0)
Chris@16 92 {
Chris@16 93 value = s0 & wordmask();
Chris@16 94 if(value < (1 << (w-k))) {
Chris@16 95 value += 1 << (w-k);
Chris@16 96 }
Chris@16 97 }
Chris@16 98
Chris@16 99 /**
Chris@16 100 * Seeds a @c linear_feedback_shift_engine with values
Chris@16 101 * produced by @c seq.generate().
Chris@16 102 */
Chris@16 103 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(linear_feedback_shift_engine,
Chris@16 104 SeedSeq, seq)
Chris@16 105 { seed(detail::seed_one_int<UIntType, (UIntType(2) << (w - 1))>(seq)); }
Chris@16 106
Chris@16 107 /**
Chris@16 108 * Seeds a @c linear_feedback_shift_engine with values
Chris@16 109 * from the range [first, last).
Chris@16 110 */
Chris@16 111 template<class It> void seed(It& first, It last)
Chris@16 112 {
Chris@16 113 seed(detail::get_one_int<UIntType, (UIntType(2) << (w - 1))>(first, last));
Chris@16 114 }
Chris@16 115
Chris@16 116 /** Returns the next value of the generator. */
Chris@16 117 result_type operator()()
Chris@16 118 {
Chris@16 119 const UIntType b = (((value << q) ^ value) & wordmask()) >> (k-s);
Chris@16 120 const UIntType mask = (wordmask() << (w-k)) & wordmask();
Chris@16 121 value = ((value & mask) << s) ^ b;
Chris@16 122 return value;
Chris@16 123 }
Chris@16 124
Chris@16 125 /** Fills a range with random values */
Chris@16 126 template<class Iter>
Chris@16 127 void generate(Iter first, Iter last)
Chris@16 128 { detail::generate_from_int(*this, first, last); }
Chris@16 129
Chris@16 130 /** Advances the state of the generator by @c z. */
Chris@16 131 void discard(boost::uintmax_t z)
Chris@16 132 {
Chris@16 133 for(boost::uintmax_t j = 0; j < z; ++j) {
Chris@16 134 (*this)();
Chris@16 135 }
Chris@16 136 }
Chris@16 137
Chris@16 138 /**
Chris@16 139 * Writes the textual representation of the generator to a @c std::ostream.
Chris@16 140 */
Chris@16 141 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, linear_feedback_shift_engine, x)
Chris@16 142 {
Chris@16 143 os << x.value;
Chris@16 144 return os;
Chris@16 145 }
Chris@16 146
Chris@16 147 /**
Chris@16 148 * Reads the textual representation of the generator from a @c std::istream.
Chris@16 149 */
Chris@16 150 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, linear_feedback_shift_engine, x)
Chris@16 151 {
Chris@16 152 is >> x.value;
Chris@16 153 return is;
Chris@16 154 }
Chris@16 155
Chris@16 156 /**
Chris@16 157 * Returns true if the two generators will produce identical
Chris@16 158 * sequences of outputs.
Chris@16 159 */
Chris@16 160 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(linear_feedback_shift_engine, x, y)
Chris@16 161 { return x.value == y.value; }
Chris@16 162
Chris@16 163 /**
Chris@16 164 * Returns true if the two generators will produce different
Chris@16 165 * sequences of outputs.
Chris@16 166 */
Chris@16 167 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(linear_feedback_shift_engine)
Chris@16 168
Chris@16 169 private:
Chris@16 170 /// \cond show_private
Chris@16 171 static UIntType wordmask() { return boost::low_bits_mask_t<w>::sig_bits; }
Chris@16 172 /// \endcond
Chris@16 173 UIntType value;
Chris@16 174 };
Chris@16 175
Chris@16 176 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
Chris@16 177 // A definition is required even for integral static constants
Chris@16 178 template<class UIntType, int w, int k, int q, int s>
Chris@16 179 const bool linear_feedback_shift_engine<UIntType, w, k, q, s>::has_fixed_range;
Chris@16 180 template<class UIntType, int w, int k, int q, int s>
Chris@16 181 const int linear_feedback_shift_engine<UIntType, w, k, q, s>::word_size;
Chris@16 182 template<class UIntType, int w, int k, int q, int s>
Chris@16 183 const int linear_feedback_shift_engine<UIntType, w, k, q, s>::exponent1;
Chris@16 184 template<class UIntType, int w, int k, int q, int s>
Chris@16 185 const int linear_feedback_shift_engine<UIntType, w, k, q, s>::exponent2;
Chris@16 186 template<class UIntType, int w, int k, int q, int s>
Chris@16 187 const int linear_feedback_shift_engine<UIntType, w, k, q, s>::step_size;
Chris@16 188 template<class UIntType, int w, int k, int q, int s>
Chris@16 189 const UIntType linear_feedback_shift_engine<UIntType, w, k, q, s>::default_seed;
Chris@16 190 #endif
Chris@16 191
Chris@16 192 /// \cond show_deprecated
Chris@16 193
Chris@16 194 /** Provided for backwards compatibility. */
Chris@16 195 template<class UIntType, int w, int k, int q, int s, UIntType v = 0>
Chris@16 196 class linear_feedback_shift :
Chris@16 197 public linear_feedback_shift_engine<UIntType, w, k, q, s>
Chris@16 198 {
Chris@16 199 typedef linear_feedback_shift_engine<UIntType, w, k, q, s> base_type;
Chris@16 200 public:
Chris@16 201 linear_feedback_shift() {}
Chris@16 202 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(linear_feedback_shift,
Chris@16 203 SeedSeq, seq)
Chris@16 204 { seed(seq); }
Chris@16 205 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(linear_feedback_shift,
Chris@16 206 UIntType, val)
Chris@16 207 { seed(val); }
Chris@16 208 template<class It>
Chris@16 209 linear_feedback_shift(It& first, It last) : base_type(first, last) {}
Chris@16 210 };
Chris@16 211
Chris@16 212 /// \endcond
Chris@16 213
Chris@16 214 } // namespace random
Chris@16 215 } // namespace boost
Chris@16 216
Chris@16 217 #endif // BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP