annotate DEPENDENCIES/generic/include/boost/random/independent_bits.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/independent_bits.hpp header file
Chris@16 2 *
Chris@16 3 * Copyright Steven Watanabe 2011
Chris@16 4 * Distributed under the Boost Software License, Version 1.0. (See
Chris@16 5 * accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 * http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7 *
Chris@16 8 * See http://www.boost.org for most recent version including documentation.
Chris@16 9 *
Chris@101 10 * $Id$
Chris@16 11 *
Chris@16 12 */
Chris@16 13
Chris@16 14 #ifndef BOOST_RANDOM_INDEPENDENT_BITS_HPP
Chris@16 15 #define BOOST_RANDOM_INDEPENDENT_BITS_HPP
Chris@16 16
Chris@16 17 #include <istream>
Chris@16 18 #include <iosfwd>
Chris@16 19 #include <boost/assert.hpp>
Chris@16 20 #include <boost/limits.hpp>
Chris@16 21 #include <boost/config.hpp>
Chris@16 22 #include <boost/cstdint.hpp>
Chris@16 23 #include <boost/integer/integer_mask.hpp>
Chris@16 24 #include <boost/type_traits/make_unsigned.hpp>
Chris@16 25 #include <boost/random/detail/config.hpp>
Chris@16 26 #include <boost/random/detail/integer_log2.hpp>
Chris@16 27 #include <boost/random/detail/operators.hpp>
Chris@16 28 #include <boost/random/detail/seed.hpp>
Chris@16 29 #include <boost/random/detail/seed_impl.hpp>
Chris@16 30 #include <boost/random/detail/signed_unsigned_tools.hpp>
Chris@16 31
Chris@16 32 namespace boost {
Chris@16 33 namespace random {
Chris@16 34
Chris@16 35 /**
Chris@16 36 * An instantiation of class template @c independent_bits_engine
Chris@16 37 * model a \pseudo_random_number_generator. It generates random
Chris@16 38 * numbers distributed between [0, 2^w) by combining one or
Chris@16 39 * more invocations of the base engine.
Chris@16 40 *
Chris@16 41 * Requires: 0 < w <= std::numeric_limits<UIntType>::digits
Chris@16 42 */
Chris@16 43 template<class Engine, std::size_t w, class UIntType>
Chris@16 44 class independent_bits_engine
Chris@16 45 {
Chris@16 46 public:
Chris@16 47 typedef Engine base_type;
Chris@16 48 typedef UIntType result_type;
Chris@16 49
Chris@16 50 // Required by old Boost.Random concept
Chris@16 51 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
Chris@16 52
Chris@16 53 /** Returns the smallest value that the generator can produce. */
Chris@16 54 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 55 { 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 boost::low_bits_mask_t<w>::sig_bits; }
Chris@16 59
Chris@16 60 /**
Chris@16 61 * Constructs an @c independent_bits_engine using the
Chris@16 62 * default constructor of the base generator.
Chris@16 63 */
Chris@16 64 independent_bits_engine() { }
Chris@16 65
Chris@16 66 /**
Chris@16 67 * Constructs an @c independent_bits_engine, using seed as
Chris@16 68 * the constructor argument for both base generators.
Chris@16 69 */
Chris@16 70 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(independent_bits_engine,
Chris@16 71 result_type, seed_arg)
Chris@16 72 {
Chris@16 73 _base.seed(seed_arg);
Chris@16 74 }
Chris@16 75
Chris@16 76 /**
Chris@16 77 * Constructs an @c independent_bits_engine, using seq as
Chris@16 78 * the constructor argument for the base generator.
Chris@16 79 */
Chris@16 80 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(independent_bits_engine,
Chris@16 81 SeedSeq, seq)
Chris@16 82 { _base.seed(seq); }
Chris@16 83
Chris@16 84 /** Constructs an @c independent_bits_engine by copying @c base. */
Chris@16 85 independent_bits_engine(const base_type& base_arg) : _base(base_arg) {}
Chris@16 86
Chris@16 87 /**
Chris@16 88 * Contructs an @c independent_bits_engine with
Chris@16 89 * values from the range defined by the input iterators first
Chris@16 90 * and last. first will be modified to point to the element
Chris@16 91 * after the last one used.
Chris@16 92 *
Chris@16 93 * Throws: @c std::invalid_argument if the input range is too small.
Chris@16 94 *
Chris@16 95 * Exception Safety: Basic
Chris@16 96 */
Chris@16 97 template<class It>
Chris@16 98 independent_bits_engine(It& first, It last) : _base(first, last) { }
Chris@16 99
Chris@16 100 /**
Chris@16 101 * Seeds an @c independent_bits_engine using the default
Chris@16 102 * seed of the base generator.
Chris@16 103 */
Chris@16 104 void seed() { _base.seed(); }
Chris@16 105
Chris@16 106 /**
Chris@16 107 * Seeds an @c independent_bits_engine, using @c seed as the
Chris@16 108 * seed for the base generator.
Chris@16 109 */
Chris@16 110 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(independent_bits_engine,
Chris@16 111 result_type, seed_arg)
Chris@16 112 { _base.seed(seed_arg); }
Chris@16 113
Chris@16 114 /**
Chris@16 115 * Seeds an @c independent_bits_engine, using @c seq to
Chris@16 116 * seed the base generator.
Chris@16 117 */
Chris@16 118 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(independent_bits_engine,
Chris@16 119 SeedSeq, seq)
Chris@16 120 { _base.seed(seq); }
Chris@16 121
Chris@16 122 /**
Chris@16 123 * Seeds an @c independent_bits_engine with
Chris@16 124 * values from the range defined by the input iterators first
Chris@16 125 * and last. first will be modified to point to the element
Chris@16 126 * after the last one used.
Chris@16 127 *
Chris@16 128 * Throws: @c std::invalid_argument if the input range is too small.
Chris@16 129 *
Chris@16 130 * Exception Safety: Basic
Chris@16 131 */
Chris@16 132 template<class It> void seed(It& first, It last)
Chris@16 133 { _base.seed(first, last); }
Chris@16 134
Chris@16 135 /** Returns the next value of the generator. */
Chris@16 136 result_type operator()()
Chris@16 137 {
Chris@16 138 // While it may seem wasteful to recalculate this
Chris@16 139 // every time, both msvc and gcc can propagate
Chris@16 140 // constants, resolving this at compile time.
Chris@16 141 base_unsigned range =
Chris@16 142 detail::subtract<base_result>()((_base.max)(), (_base.min)());
Chris@16 143 std::size_t m =
Chris@16 144 (range == (std::numeric_limits<base_unsigned>::max)()) ?
Chris@16 145 std::numeric_limits<base_unsigned>::digits :
Chris@16 146 detail::integer_log2(range + 1);
Chris@16 147 std::size_t n = (w + m - 1) / m;
Chris@16 148 std::size_t w0, n0;
Chris@16 149 base_unsigned y0, y1;
Chris@16 150 base_unsigned y0_mask, y1_mask;
Chris@16 151 calc_params(n, range, w0, n0, y0, y1, y0_mask, y1_mask);
Chris@16 152 if(base_unsigned(range - y0 + 1) > y0 / n) {
Chris@16 153 // increment n and try again.
Chris@16 154 ++n;
Chris@16 155 calc_params(n, range, w0, n0, y0, y1, y0_mask, y1_mask);
Chris@16 156 }
Chris@16 157
Chris@16 158 BOOST_ASSERT(n0*w0 + (n - n0)*(w0 + 1) == w);
Chris@16 159
Chris@16 160 result_type S = 0;
Chris@16 161 for(std::size_t k = 0; k < n0; ++k) {
Chris@16 162 base_unsigned u;
Chris@16 163 do {
Chris@16 164 u = detail::subtract<base_result>()(_base(), (_base.min)());
Chris@16 165 } while(u > base_unsigned(y0 - 1));
Chris@16 166 S = (S << w0) + (u & y0_mask);
Chris@16 167 }
Chris@16 168 for(std::size_t k = 0; k < (n - n0); ++k) {
Chris@16 169 base_unsigned u;
Chris@16 170 do {
Chris@16 171 u = detail::subtract<base_result>()(_base(), (_base.min)());
Chris@16 172 } while(u > base_unsigned(y1 - 1));
Chris@16 173 S = (S << (w0 + 1)) + (u & y1_mask);
Chris@16 174 }
Chris@16 175 return S;
Chris@16 176 }
Chris@16 177
Chris@16 178 /** Fills a range with random values */
Chris@16 179 template<class Iter>
Chris@16 180 void generate(Iter first, Iter last)
Chris@16 181 { detail::generate_from_int(*this, first, last); }
Chris@16 182
Chris@16 183 /** Advances the state of the generator by @c z. */
Chris@16 184 void discard(boost::uintmax_t z)
Chris@16 185 {
Chris@16 186 for(boost::uintmax_t i = 0; i < z; ++i) {
Chris@16 187 (*this)();
Chris@16 188 }
Chris@16 189 }
Chris@16 190
Chris@16 191 const base_type& base() const { return _base; }
Chris@16 192
Chris@16 193 /**
Chris@16 194 * Writes the textual representation if the generator to a @c std::ostream.
Chris@16 195 * The textual representation of the engine is the textual representation
Chris@16 196 * of the base engine.
Chris@16 197 */
Chris@16 198 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, independent_bits_engine, r)
Chris@16 199 {
Chris@16 200 os << r._base;
Chris@16 201 return os;
Chris@16 202 }
Chris@16 203
Chris@16 204 /**
Chris@16 205 * Reads the state of an @c independent_bits_engine from a
Chris@16 206 * @c std::istream.
Chris@16 207 */
Chris@16 208 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, independent_bits_engine, r)
Chris@16 209 {
Chris@16 210 is >> r._base;
Chris@16 211 return is;
Chris@16 212 }
Chris@16 213
Chris@16 214 /**
Chris@16 215 * Returns: true iff the two @c independent_bits_engines will
Chris@16 216 * produce the same sequence of values.
Chris@16 217 */
Chris@16 218 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(independent_bits_engine, x, y)
Chris@16 219 { return x._base == y._base; }
Chris@16 220 /**
Chris@16 221 * Returns: true iff the two @c independent_bits_engines will
Chris@16 222 * produce different sequences of values.
Chris@16 223 */
Chris@16 224 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(independent_bits_engine)
Chris@16 225
Chris@16 226 private:
Chris@16 227
Chris@16 228 /// \cond show_private
Chris@16 229 typedef typename base_type::result_type base_result;
Chris@16 230 typedef typename make_unsigned<base_result>::type base_unsigned;
Chris@16 231
Chris@16 232 void calc_params(
Chris@16 233 std::size_t n, base_unsigned range,
Chris@16 234 std::size_t& w0, std::size_t& n0,
Chris@16 235 base_unsigned& y0, base_unsigned& y1,
Chris@16 236 base_unsigned& y0_mask, base_unsigned& y1_mask)
Chris@16 237 {
Chris@16 238 BOOST_ASSERT(w >= n);
Chris@16 239 w0 = w/n;
Chris@16 240 n0 = n - w % n;
Chris@16 241 y0_mask = (base_unsigned(2) << (w0 - 1)) - 1;
Chris@16 242 y1_mask = (y0_mask << 1) | 1;
Chris@16 243 y0 = (range + 1) & ~y0_mask;
Chris@16 244 y1 = (range + 1) & ~y1_mask;
Chris@16 245 BOOST_ASSERT(y0 != 0 || base_unsigned(range + 1) == 0);
Chris@16 246 }
Chris@16 247 /// \endcond
Chris@16 248
Chris@16 249 Engine _base;
Chris@16 250 };
Chris@16 251
Chris@16 252 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
Chris@16 253 template<class Engine, std::size_t w, class UIntType>
Chris@16 254 const bool independent_bits_engine<Engine, w, UIntType>::has_fixed_range;
Chris@16 255 #endif
Chris@16 256
Chris@16 257 } // namespace random
Chris@16 258 } // namespace boost
Chris@16 259
Chris@16 260 #endif // BOOST_RANDOM_INDEPENDENT_BITS_HPP