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