1 /* boost random/discard_block.hpp header file
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)
9 * See http://www.boost.org for most recent version including documentation.
11 * $Id: discard_block.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $
17 #ifndef BOOST_RANDOM_DISCARD_BLOCK_HPP
18 #define BOOST_RANDOM_DISCARD_BLOCK_HPP
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>
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.
39 * Requires: 0 < p <= r
41 template<class UniformRandomNumberGenerator, std::size_t p, std::size_t r>
42 class discard_block_engine
44 typedef typename detail::seed_type<
45 typename UniformRandomNumberGenerator::result_type>::type seed_type;
47 typedef UniformRandomNumberGenerator base_type;
48 typedef typename base_type::result_type result_type;
50 BOOST_STATIC_CONSTANT(std::size_t, block_size = p);
51 BOOST_STATIC_CONSTANT(std::size_t, used_block = r);
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);
57 BOOST_STATIC_ASSERT(total_block >= returned_block);
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) { }
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) { }
70 * Creates a new \discard_block_engine and seeds the underlying
71 * generator with @c value
73 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(discard_block_engine,
75 { _rng.seed(value); _n = 0; }
78 * Creates a new \discard_block_engine and seeds the underlying
79 * generator with @c seq
81 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(discard_block_engine, SeedSeq, seq)
82 { _rng.seed(seq); _n = 0; }
85 * Creates a new \discard_block_engine and seeds the underlying
86 * generator with first and last.
88 template<class It> discard_block_engine(It& first, It last)
89 : _rng(first, last), _n(0) { }
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; }
103 /** Returns the underlying engine. */
104 const base_type& base() const { return _rng; }
106 /** Returns the next value of the generator. */
107 result_type operator()()
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) {
123 void discard(boost::uintmax_t z)
125 for(boost::uintmax_t j = 0; j < z; ++j) {
131 void generate(It first, It last)
132 { detail::generate(*this, first, last); }
135 * Returns the smallest value that the generator can produce.
136 * This is the same as the minimum of the underlying generator.
138 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
139 { return (base_type::min)(); }
141 * Returns the largest value that the generator can produce.
142 * This is the same as the maximum of the underlying generator.
144 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
145 { return (base_type::max)(); }
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)
154 os << s._rng << ' ' << s._n;
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)
163 is >> s._rng >> std::ws >> s._n;
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); }
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;
196 /// \cond \show_deprecated
198 template<class URNG, int p, int r>
199 class discard_block : public discard_block_engine<URNG, p, r>
201 typedef discard_block_engine<URNG, p, r> base_t;
203 typedef typename base_t::result_type result_type;
206 discard_block(T& arg) : base_t(arg) {}
208 discard_block(const T& arg) : base_t(arg) {}
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)(); }
221 template<class Engine>
222 struct generator_bits;
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(); }
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(); }
236 } // namespace random
240 #endif // BOOST_RANDOM_DISCARD_BLOCK_HPP