|
1 /* boost random/discard_block.hpp header file |
|
2 * |
|
3 * Copyright Jens Maurer 2002 |
|
4 * Distributed under the Boost Software License, Version 1.0. (See |
|
5 * accompanying file LICENSE_1_0.txt or copy at |
|
6 * http://www.boost.org/LICENSE_1_0.txt) |
|
7 * |
|
8 * See http://www.boost.org for most recent version including documentation. |
|
9 * |
|
10 * $Id: discard_block.hpp,v 1.12 2005/05/21 15:57:00 dgregor Exp $ |
|
11 * |
|
12 * Revision history |
|
13 * 2001-03-02 created |
|
14 */ |
|
15 |
|
16 #ifndef BOOST_RANDOM_DISCARD_BLOCK_HPP |
|
17 #define BOOST_RANDOM_DISCARD_BLOCK_HPP |
|
18 |
|
19 #include <iostream> |
|
20 #include <boost/config.hpp> |
|
21 #include <boost/limits.hpp> |
|
22 #include <boost/static_assert.hpp> |
|
23 |
|
24 |
|
25 namespace boost { |
|
26 namespace random { |
|
27 |
|
28 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r> |
|
29 class discard_block |
|
30 { |
|
31 public: |
|
32 typedef UniformRandomNumberGenerator base_type; |
|
33 typedef typename base_type::result_type result_type; |
|
34 |
|
35 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false); |
|
36 BOOST_STATIC_CONSTANT(unsigned int, total_block = p); |
|
37 BOOST_STATIC_CONSTANT(unsigned int, returned_block = r); |
|
38 |
|
39 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
|
40 BOOST_STATIC_ASSERT(total_block >= returned_block); |
|
41 #endif |
|
42 |
|
43 discard_block() : _rng(), _n(0) { } |
|
44 explicit discard_block(const base_type & rng) : _rng(rng), _n(0) { } |
|
45 template<class It> discard_block(It& first, It last) |
|
46 : _rng(first, last), _n(0) { } |
|
47 void seed() { _rng.seed(); _n = 0; } |
|
48 template<class T> void seed(T s) { _rng.seed(s); _n = 0; } |
|
49 template<class It> void seed(It& first, It last) |
|
50 { _n = 0; _rng.seed(first, last); } |
|
51 |
|
52 const base_type& base() const { return _rng; } |
|
53 |
|
54 result_type operator()() |
|
55 { |
|
56 if(_n >= returned_block) { |
|
57 // discard values of random number generator |
|
58 for( ; _n < total_block; ++_n) |
|
59 _rng(); |
|
60 _n = 0; |
|
61 } |
|
62 ++_n; |
|
63 return _rng(); |
|
64 } |
|
65 |
|
66 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); } |
|
67 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); } |
|
68 static bool validation(result_type x) { return true; } // dummy |
|
69 |
|
70 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE |
|
71 |
|
72 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS |
|
73 template<class CharT, class Traits> |
|
74 friend std::basic_ostream<CharT,Traits>& |
|
75 operator<<(std::basic_ostream<CharT,Traits>& os, const discard_block& s) |
|
76 { |
|
77 os << s._rng << " " << s._n << " "; |
|
78 return os; |
|
79 } |
|
80 |
|
81 template<class CharT, class Traits> |
|
82 friend std::basic_istream<CharT,Traits>& |
|
83 operator>>(std::basic_istream<CharT,Traits>& is, discard_block& s) |
|
84 { |
|
85 is >> s._rng >> std::ws >> s._n >> std::ws; |
|
86 return is; |
|
87 } |
|
88 #endif |
|
89 |
|
90 friend bool operator==(const discard_block& x, const discard_block& y) |
|
91 { return x._rng == y._rng && x._n == y._n; } |
|
92 friend bool operator!=(const discard_block& x, const discard_block& y) |
|
93 { return !(x == y); } |
|
94 #else |
|
95 // Use a member function; Streamable concept not supported. |
|
96 bool operator==(const discard_block& rhs) const |
|
97 { return _rng == rhs._rng && _n == rhs._n; } |
|
98 bool operator!=(const discard_block& rhs) const |
|
99 { return !(*this == rhs); } |
|
100 #endif |
|
101 |
|
102 private: |
|
103 base_type _rng; |
|
104 unsigned int _n; |
|
105 }; |
|
106 |
|
107 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION |
|
108 // A definition is required even for integral static constants |
|
109 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r> |
|
110 const bool discard_block<UniformRandomNumberGenerator, p, r>::has_fixed_range; |
|
111 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r> |
|
112 const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::total_block; |
|
113 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r> |
|
114 const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::returned_block; |
|
115 #endif |
|
116 |
|
117 } // namespace random |
|
118 |
|
119 } // namespace boost |
|
120 |
|
121 #endif // BOOST_RANDOM_DISCARD_BLOCK_HPP |