comparison DEPENDENCIES/generic/include/boost/random/lagged_fibonacci.hpp @ 101:c530137014c0

Update Boost headers (1.58.0)
author Chris Cannam
date Mon, 07 Sep 2015 11:12:49 +0100
parents 2665513ce2d3
children
comparison
equal deleted inserted replaced
100:793467b5e61c 101:c530137014c0
5 * accompanying file LICENSE_1_0.txt or copy at 5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt) 6 * http://www.boost.org/LICENSE_1_0.txt)
7 * 7 *
8 * See http://www.boost.org for most recent version including documentation. 8 * See http://www.boost.org for most recent version including documentation.
9 * 9 *
10 * $Id: lagged_fibonacci.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $ 10 * $Id$
11 * 11 *
12 * Revision history 12 * Revision history
13 * 2013-10-14 fixed some warnings with Wshadow (mgaunard)
13 * 2001-02-18 moved to individual header files 14 * 2001-02-18 moved to individual header files
14 */ 15 */
15 16
16 #ifndef BOOST_RANDOM_LAGGED_FIBONACCI_HPP 17 #ifndef BOOST_RANDOM_LAGGED_FIBONACCI_HPP
17 #define BOOST_RANDOM_LAGGED_FIBONACCI_HPP 18 #define BOOST_RANDOM_LAGGED_FIBONACCI_HPP
33 #include <boost/random/detail/generator_seed_seq.hpp> 34 #include <boost/random/detail/generator_seed_seq.hpp>
34 35
35 namespace boost { 36 namespace boost {
36 namespace random { 37 namespace random {
37 38
38 /** 39 /**
39 * Instantiations of class template \lagged_fibonacci_engine model a 40 * Instantiations of class template \lagged_fibonacci_engine model a
40 * \pseudo_random_number_generator. It uses a lagged Fibonacci 41 * \pseudo_random_number_generator. It uses a lagged Fibonacci
41 * algorithm with two lags @c p and @c q: 42 * algorithm with two lags @c p and @c q:
42 * x(i) = x(i-p) + x(i-q) (mod 2<sup>w</sup>) with p > q. 43 * x(i) = x(i-p) + x(i-q) (mod 2<sup>w</sup>) with p > q.
43 */ 44 */
77 */ 78 */
78 template<class It> lagged_fibonacci_engine(It& first, It last) 79 template<class It> lagged_fibonacci_engine(It& first, It last)
79 { seed(first, last); } 80 { seed(first, last); }
80 81
81 // compiler-generated copy ctor and assignment operator are fine 82 // compiler-generated copy ctor and assignment operator are fine
82 83
83 /** Calls @c seed(default_seed). */ 84 /** Calls @c seed(default_seed). */
84 void seed() { seed(default_seed); } 85 void seed() { seed(default_seed); }
85 86
86 /** 87 /**
87 * Sets the state of the generator to values produced by 88 * Sets the state of the generator to values produced by
121 { 122 {
122 if(i >= long_lag) 123 if(i >= long_lag)
123 fill(); 124 fill();
124 return x[i++]; 125 return x[i++];
125 } 126 }
126 127
127 /** Fills a range with random values */ 128 /** Fills a range with random values */
128 template<class Iter> 129 template<class Iter>
129 void generate(Iter first, Iter last) 130 void generate(Iter first, Iter last)
130 { detail::generate_from_int(*this, first, last); } 131 { detail::generate_from_int(*this, first, last); }
131 132
134 { 135 {
135 for(boost::uintmax_t j = 0; j < z; ++j) { 136 for(boost::uintmax_t j = 0; j < z; ++j) {
136 (*this)(); 137 (*this)();
137 } 138 }
138 } 139 }
139 140
140 /** 141 /**
141 * Writes the textual representation of the generator to a @c std::ostream. 142 * Writes the textual representation of the generator to a @c std::ostream.
142 */ 143 */
143 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lagged_fibonacci_engine, f) 144 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lagged_fibonacci_engine, f)
144 { 145 {
145 os << f.i; 146 os << f.i;
146 for(unsigned int i = 0; i < f.long_lag; ++i) 147 for(unsigned int j = 0; j < f.long_lag; ++j)
147 os << ' ' << f.x[i]; 148 os << ' ' << f.x[j];
148 return os; 149 return os;
149 } 150 }
150 151
151 /** 152 /**
152 * Reads the textual representation of the generator from a @c std::istream. 153 * Reads the textual representation of the generator from a @c std::istream.
153 */ 154 */
154 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lagged_fibonacci_engine, f) 155 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lagged_fibonacci_engine, f)
155 { 156 {
156 is >> f.i >> std::ws; 157 is >> f.i >> std::ws;
157 for(unsigned int i = 0; i < f.long_lag; ++i) 158 for(unsigned int j = 0; j < f.long_lag; ++j)
158 is >> f.x[i] >> std::ws; 159 is >> f.x[j] >> std::ws;
159 return is; 160 return is;
160 } 161 }
161 162
162 /** 163 /**
163 * Returns true if the two generators will produce identical 164 * Returns true if the two generators will produce identical
164 * sequences of outputs. 165 * sequences of outputs.
165 */ 166 */
166 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(lagged_fibonacci_engine, x, y) 167 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(lagged_fibonacci_engine, x_, y_)
167 { return x.i == y.i && std::equal(x.x, x.x+long_lag, y.x); } 168 { return x_.i == y_.i && std::equal(x_.x, x_.x+long_lag, y_.x); }
168 169
169 /** 170 /**
170 * Returns true if the two generators will produce different 171 * Returns true if the two generators will produce different
171 * sequences of outputs. 172 * sequences of outputs.
172 */ 173 */
173 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(lagged_fibonacci_engine) 174 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(lagged_fibonacci_engine)
304 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(lagged_fibonacci_01_engine, SeedSeq, seq) 305 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(lagged_fibonacci_01_engine, SeedSeq, seq)
305 { 306 {
306 detail::seed_array_real<w>(seq, x); 307 detail::seed_array_real<w>(seq, x);
307 i = long_lag; 308 i = long_lag;
308 } 309 }
309 310
310 /** 311 /**
311 * Seeds this @c lagged_fibonacci_01_engine using values from the 312 * Seeds this @c lagged_fibonacci_01_engine using values from the
312 * iterator range [first, last). If there are not enough elements 313 * iterator range [first, last). If there are not enough elements
313 * in the range, throws @c std::invalid_argument. 314 * in the range, throws @c std::invalid_argument.
314 */ 315 */
316 void seed(It& first, It last) 317 void seed(It& first, It last)
317 { 318 {
318 detail::fill_array_real<w>(first, last, x); 319 detail::fill_array_real<w>(first, last, x);
319 i = long_lag; 320 i = long_lag;
320 } 321 }
321 322
322 /** Returns the smallest value that the generator can produce. */ 323 /** Returns the smallest value that the generator can produce. */
323 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return result_type(0); } 324 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return result_type(0); }
324 /** Returns the upper bound of the generators outputs. */ 325 /** Returns the upper bound of the generators outputs. */
325 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return result_type(1); } 326 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return result_type(1); }
326 327
329 { 330 {
330 if(i >= long_lag) 331 if(i >= long_lag)
331 fill(); 332 fill();
332 return x[i++]; 333 return x[i++];
333 } 334 }
334 335
335 /** Fills a range with random values */ 336 /** Fills a range with random values */
336 template<class Iter> 337 template<class Iter>
337 void generate(Iter first, Iter last) 338 void generate(Iter first, Iter last)
338 { return detail::generate_from_real(*this, first, last); } 339 { return detail::generate_from_real(*this, first, last); }
339 340
342 { 343 {
343 for(boost::uintmax_t j = 0; j < z; ++j) { 344 for(boost::uintmax_t j = 0; j < z; ++j) {
344 (*this)(); 345 (*this)();
345 } 346 }
346 } 347 }
347 348
348 /** 349 /**
349 * Writes the textual representation of the generator to a @c std::ostream. 350 * Writes the textual representation of the generator to a @c std::ostream.
350 */ 351 */
351 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lagged_fibonacci_01_engine, f) 352 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, lagged_fibonacci_01_engine, f)
352 { 353 {
353 // allow for Koenig lookup 354 // allow for Koenig lookup
354 using std::pow; 355 using std::pow;
355 os << f.i; 356 os << f.i;
356 std::ios_base::fmtflags oldflags = os.flags(os.dec | os.fixed | os.left); 357 std::ios_base::fmtflags oldflags = os.flags(os.dec | os.fixed | os.left);
357 for(unsigned int i = 0; i < f.long_lag; ++i) 358 for(unsigned int j = 0; j < f.long_lag; ++j)
358 os << ' ' << f.x[i] * f.modulus(); 359 os << ' ' << f.x[j] * f.modulus();
359 os.flags(oldflags); 360 os.flags(oldflags);
360 return os; 361 return os;
361 } 362 }
362 363
363 /** 364 /**
364 * Reads the textual representation of the generator from a @c std::istream. 365 * Reads the textual representation of the generator from a @c std::istream.
365 */ 366 */
366 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lagged_fibonacci_01_engine, f) 367 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lagged_fibonacci_01_engine, f)
367 { 368 {
368 is >> f.i; 369 is >> f.i;
369 for(unsigned int i = 0; i < f.long_lag; ++i) { 370 for(unsigned int j = 0; j < f.long_lag; ++j) {
370 typename lagged_fibonacci_01_engine::result_type value; 371 typename lagged_fibonacci_01_engine::result_type value;
371 is >> std::ws >> value; 372 is >> std::ws >> value;
372 f.x[i] = value / f.modulus(); 373 f.x[j] = value / f.modulus();
373 } 374 }
374 return is; 375 return is;
375 } 376 }
376 377
377 /** 378 /**
378 * Returns true if the two generators will produce identical 379 * Returns true if the two generators will produce identical
379 * sequences of outputs. 380 * sequences of outputs.
380 */ 381 */
381 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(lagged_fibonacci_01_engine, x, y) 382 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(lagged_fibonacci_01_engine, x_, y_)
382 { return x.i == y.i && std::equal(x.x, x.x+long_lag, y.x); } 383 { return x_.i == y_.i && std::equal(x_.x, x_.x+long_lag, y_.x); }
383 384
384 /** 385 /**
385 * Returns true if the two generators will produce different 386 * Returns true if the two generators will produce different
386 * sequences of outputs. 387 * sequences of outputs.
387 */ 388 */
388 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(lagged_fibonacci_01_engine) 389 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(lagged_fibonacci_01_engine)