Chris@16
|
1 /* boost random/discard_block.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Jens Maurer 2002
|
Chris@16
|
4 * Copyright Steven Watanabe 2010
|
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 * Revision history
|
Chris@16
|
14 * 2001-03-02 created
|
Chris@16
|
15 */
|
Chris@16
|
16
|
Chris@16
|
17 #ifndef BOOST_RANDOM_DISCARD_BLOCK_HPP
|
Chris@16
|
18 #define BOOST_RANDOM_DISCARD_BLOCK_HPP
|
Chris@16
|
19
|
Chris@16
|
20 #include <iostream>
|
Chris@16
|
21 #include <boost/config.hpp>
|
Chris@16
|
22 #include <boost/cstdint.hpp>
|
Chris@16
|
23 #include <boost/limits.hpp>
|
Chris@16
|
24 #include <boost/static_assert.hpp>
|
Chris@16
|
25 #include <boost/random/detail/config.hpp>
|
Chris@16
|
26 #include <boost/random/detail/seed.hpp>
|
Chris@101
|
27 #include <boost/random/detail/seed_impl.hpp>
|
Chris@16
|
28
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31 namespace random {
|
Chris@16
|
32
|
Chris@16
|
33 /**
|
Chris@16
|
34 * The class template \discard_block_engine is a model of
|
Chris@16
|
35 * \pseudo_random_number_generator. It modifies
|
Chris@16
|
36 * another generator by discarding parts of its output.
|
Chris@16
|
37 * Out of every block of @c p results, the first @c r
|
Chris@16
|
38 * will be returned and the rest discarded.
|
Chris@16
|
39 *
|
Chris@16
|
40 * Requires: 0 < p <= r
|
Chris@16
|
41 */
|
Chris@16
|
42 template<class UniformRandomNumberGenerator, std::size_t p, std::size_t r>
|
Chris@16
|
43 class discard_block_engine
|
Chris@16
|
44 {
|
Chris@16
|
45 typedef typename detail::seed_type<
|
Chris@16
|
46 typename UniformRandomNumberGenerator::result_type>::type seed_type;
|
Chris@16
|
47 public:
|
Chris@16
|
48 typedef UniformRandomNumberGenerator base_type;
|
Chris@16
|
49 typedef typename base_type::result_type result_type;
|
Chris@16
|
50
|
Chris@16
|
51 BOOST_STATIC_CONSTANT(std::size_t, block_size = p);
|
Chris@16
|
52 BOOST_STATIC_CONSTANT(std::size_t, used_block = r);
|
Chris@16
|
53
|
Chris@16
|
54 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
|
Chris@16
|
55 BOOST_STATIC_CONSTANT(std::size_t, total_block = p);
|
Chris@16
|
56 BOOST_STATIC_CONSTANT(std::size_t, returned_block = r);
|
Chris@16
|
57
|
Chris@16
|
58 BOOST_STATIC_ASSERT(total_block >= returned_block);
|
Chris@16
|
59
|
Chris@16
|
60 /** Uses the default seed for the base generator. */
|
Chris@16
|
61 discard_block_engine() : _rng(), _n(0) { }
|
Chris@16
|
62 /** Constructs a new \discard_block_engine with a copy of rng. */
|
Chris@16
|
63 explicit discard_block_engine(const base_type & rng) : _rng(rng), _n(0) { }
|
Chris@16
|
64
|
Chris@101
|
65 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
Chris@16
|
66 /** Constructs a new \discard_block_engine with rng. */
|
Chris@16
|
67 explicit discard_block_engine(base_type && rng) : _rng(rng), _n(0) { }
|
Chris@16
|
68 #endif
|
Chris@16
|
69
|
Chris@16
|
70 /**
|
Chris@16
|
71 * Creates a new \discard_block_engine and seeds the underlying
|
Chris@16
|
72 * generator with @c value
|
Chris@16
|
73 */
|
Chris@16
|
74 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(discard_block_engine,
|
Chris@16
|
75 seed_type, value)
|
Chris@16
|
76 { _rng.seed(value); _n = 0; }
|
Chris@16
|
77
|
Chris@16
|
78 /**
|
Chris@16
|
79 * Creates a new \discard_block_engine and seeds the underlying
|
Chris@16
|
80 * generator with @c seq
|
Chris@16
|
81 */
|
Chris@16
|
82 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(discard_block_engine, SeedSeq, seq)
|
Chris@16
|
83 { _rng.seed(seq); _n = 0; }
|
Chris@16
|
84
|
Chris@16
|
85 /**
|
Chris@16
|
86 * Creates a new \discard_block_engine and seeds the underlying
|
Chris@16
|
87 * generator with first and last.
|
Chris@16
|
88 */
|
Chris@16
|
89 template<class It> discard_block_engine(It& first, It last)
|
Chris@16
|
90 : _rng(first, last), _n(0) { }
|
Chris@16
|
91
|
Chris@16
|
92 /** default seeds the underlying generator. */
|
Chris@16
|
93 void seed() { _rng.seed(); _n = 0; }
|
Chris@16
|
94 /** Seeds the underlying generator with s. */
|
Chris@16
|
95 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(discard_block_engine, seed_type, s)
|
Chris@16
|
96 { _rng.seed(s); _n = 0; }
|
Chris@16
|
97 /** Seeds the underlying generator with seq. */
|
Chris@16
|
98 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(discard_block_engine, SeedSeq, seq)
|
Chris@16
|
99 { _rng.seed(seq); _n = 0; }
|
Chris@16
|
100 /** Seeds the underlying generator with first and last. */
|
Chris@16
|
101 template<class It> void seed(It& first, It last)
|
Chris@16
|
102 { _rng.seed(first, last); _n = 0; }
|
Chris@16
|
103
|
Chris@16
|
104 /** Returns the underlying engine. */
|
Chris@16
|
105 const base_type& base() const { return _rng; }
|
Chris@16
|
106
|
Chris@16
|
107 /** Returns the next value of the generator. */
|
Chris@16
|
108 result_type operator()()
|
Chris@16
|
109 {
|
Chris@16
|
110 if(_n >= returned_block) {
|
Chris@16
|
111 // discard values of random number generator
|
Chris@16
|
112 // Don't use discard, since we still need to
|
Chris@16
|
113 // be somewhat compatible with TR1.
|
Chris@16
|
114 // _rng.discard(total_block - _n);
|
Chris@16
|
115 for(std::size_t i = 0; i < total_block - _n; ++i) {
|
Chris@16
|
116 _rng();
|
Chris@16
|
117 }
|
Chris@16
|
118 _n = 0;
|
Chris@16
|
119 }
|
Chris@16
|
120 ++_n;
|
Chris@16
|
121 return _rng();
|
Chris@16
|
122 }
|
Chris@16
|
123
|
Chris@16
|
124 void discard(boost::uintmax_t z)
|
Chris@16
|
125 {
|
Chris@16
|
126 for(boost::uintmax_t j = 0; j < z; ++j) {
|
Chris@16
|
127 (*this)();
|
Chris@16
|
128 }
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 template<class It>
|
Chris@16
|
132 void generate(It first, It last)
|
Chris@16
|
133 { detail::generate(*this, first, last); }
|
Chris@16
|
134
|
Chris@16
|
135 /**
|
Chris@16
|
136 * Returns the smallest value that the generator can produce.
|
Chris@16
|
137 * This is the same as the minimum of the underlying generator.
|
Chris@16
|
138 */
|
Chris@16
|
139 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
Chris@16
|
140 { return (base_type::min)(); }
|
Chris@16
|
141 /**
|
Chris@16
|
142 * Returns the largest value that the generator can produce.
|
Chris@16
|
143 * This is the same as the maximum of the underlying generator.
|
Chris@16
|
144 */
|
Chris@16
|
145 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
Chris@16
|
146 { return (base_type::max)(); }
|
Chris@16
|
147
|
Chris@16
|
148 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
|
Chris@16
|
149 /** Writes a \discard_block_engine to a @c std::ostream. */
|
Chris@16
|
150 template<class CharT, class Traits>
|
Chris@16
|
151 friend std::basic_ostream<CharT,Traits>&
|
Chris@16
|
152 operator<<(std::basic_ostream<CharT,Traits>& os,
|
Chris@16
|
153 const discard_block_engine& s)
|
Chris@16
|
154 {
|
Chris@16
|
155 os << s._rng << ' ' << s._n;
|
Chris@16
|
156 return os;
|
Chris@16
|
157 }
|
Chris@16
|
158
|
Chris@16
|
159 /** Reads a \discard_block_engine from a @c std::istream. */
|
Chris@16
|
160 template<class CharT, class Traits>
|
Chris@16
|
161 friend std::basic_istream<CharT,Traits>&
|
Chris@16
|
162 operator>>(std::basic_istream<CharT,Traits>& is, discard_block_engine& s)
|
Chris@16
|
163 {
|
Chris@16
|
164 is >> s._rng >> std::ws >> s._n;
|
Chris@16
|
165 return is;
|
Chris@16
|
166 }
|
Chris@16
|
167 #endif
|
Chris@16
|
168
|
Chris@16
|
169 /** Returns true if the two generators will produce identical sequences. */
|
Chris@16
|
170 friend bool operator==(const discard_block_engine& x,
|
Chris@16
|
171 const discard_block_engine& y)
|
Chris@16
|
172 { return x._rng == y._rng && x._n == y._n; }
|
Chris@16
|
173 /** Returns true if the two generators will produce different sequences. */
|
Chris@16
|
174 friend bool operator!=(const discard_block_engine& x,
|
Chris@16
|
175 const discard_block_engine& y)
|
Chris@16
|
176 { return !(x == y); }
|
Chris@16
|
177
|
Chris@16
|
178 private:
|
Chris@16
|
179 base_type _rng;
|
Chris@16
|
180 std::size_t _n;
|
Chris@16
|
181 };
|
Chris@16
|
182
|
Chris@16
|
183 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
Chris@16
|
184 // A definition is required even for integral static constants
|
Chris@16
|
185 template<class URNG, std::size_t p, std::size_t r>
|
Chris@16
|
186 const bool discard_block_engine<URNG, p, r>::has_fixed_range;
|
Chris@16
|
187 template<class URNG, std::size_t p, std::size_t r>
|
Chris@16
|
188 const std::size_t discard_block_engine<URNG, p, r>::total_block;
|
Chris@16
|
189 template<class URNG, std::size_t p, std::size_t r>
|
Chris@16
|
190 const std::size_t discard_block_engine<URNG, p, r>::returned_block;
|
Chris@16
|
191 template<class URNG, std::size_t p, std::size_t r>
|
Chris@16
|
192 const std::size_t discard_block_engine<URNG, p, r>::block_size;
|
Chris@16
|
193 template<class URNG, std::size_t p, std::size_t r>
|
Chris@16
|
194 const std::size_t discard_block_engine<URNG, p, r>::used_block;
|
Chris@16
|
195 #endif
|
Chris@16
|
196
|
Chris@16
|
197 /// \cond \show_deprecated
|
Chris@16
|
198
|
Chris@16
|
199 template<class URNG, int p, int r>
|
Chris@16
|
200 class discard_block : public discard_block_engine<URNG, p, r>
|
Chris@16
|
201 {
|
Chris@16
|
202 typedef discard_block_engine<URNG, p, r> base_t;
|
Chris@16
|
203 public:
|
Chris@16
|
204 typedef typename base_t::result_type result_type;
|
Chris@16
|
205 discard_block() {}
|
Chris@16
|
206 template<class T>
|
Chris@16
|
207 discard_block(T& arg) : base_t(arg) {}
|
Chris@16
|
208 template<class T>
|
Chris@16
|
209 discard_block(const T& arg) : base_t(arg) {}
|
Chris@16
|
210 template<class It>
|
Chris@16
|
211 discard_block(It& first, It last) : base_t(first, last) {}
|
Chris@16
|
212 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
Chris@16
|
213 { return (this->base().min)(); }
|
Chris@16
|
214 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
|
Chris@16
|
215 { return (this->base().max)(); }
|
Chris@16
|
216 };
|
Chris@16
|
217
|
Chris@16
|
218 /// \endcond
|
Chris@16
|
219
|
Chris@16
|
220 namespace detail {
|
Chris@16
|
221
|
Chris@16
|
222 template<class Engine>
|
Chris@16
|
223 struct generator_bits;
|
Chris@16
|
224
|
Chris@16
|
225 template<class URNG, std::size_t p, std::size_t r>
|
Chris@16
|
226 struct generator_bits<discard_block_engine<URNG, p, r> > {
|
Chris@16
|
227 static std::size_t value() { return generator_bits<URNG>::value(); }
|
Chris@16
|
228 };
|
Chris@16
|
229
|
Chris@16
|
230 template<class URNG, int p, int r>
|
Chris@16
|
231 struct generator_bits<discard_block<URNG, p, r> > {
|
Chris@16
|
232 static std::size_t value() { return generator_bits<URNG>::value(); }
|
Chris@16
|
233 };
|
Chris@16
|
234
|
Chris@16
|
235 }
|
Chris@16
|
236
|
Chris@16
|
237 } // namespace random
|
Chris@16
|
238
|
Chris@16
|
239 } // namespace boost
|
Chris@16
|
240
|
Chris@16
|
241 #endif // BOOST_RANDOM_DISCARD_BLOCK_HPP
|