comparison DEPENDENCIES/generic/include/boost/random/discard_block.hpp @ 16:2665513ce2d3

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