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