1 /* boost random/discard_block.hpp header file
3 * Copyright Jens Maurer 2002
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: discard_block.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
16 #ifndef BOOST_RANDOM_DISCARD_BLOCK_HPP
17 #define BOOST_RANDOM_DISCARD_BLOCK_HPP
20 #include <boost/config.hpp>
21 #include <boost/limits.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/random/detail/config.hpp>
30 * The class template \discard_block is a model of
31 * \pseudo_random_number_generator. It modifies
32 * another generator by discarding parts of its output.
33 * Out of every block of @c r results, the first @c p
34 * will be returned and the rest discarded.
36 * Requires: 0 < p <= r
38 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
42 typedef UniformRandomNumberGenerator base_type;
43 typedef typename base_type::result_type result_type;
45 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
46 BOOST_STATIC_CONSTANT(unsigned int, total_block = p);
47 BOOST_STATIC_CONSTANT(unsigned int, returned_block = r);
49 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
50 BOOST_STATIC_ASSERT(total_block >= returned_block);
53 discard_block() : _rng(), _n(0) { }
54 explicit discard_block(const base_type & rng) : _rng(rng), _n(0) { }
55 template<class T> explicit discard_block(T s) : _rng(s), _n(0) {}
56 template<class It> discard_block(It& first, It last)
57 : _rng(first, last), _n(0) { }
58 void seed() { _rng.seed(); _n = 0; }
59 template<class T> void seed(T s) { _rng.seed(s); _n = 0; }
60 template<class It> void seed(It& first, It last)
61 { _n = 0; _rng.seed(first, last); }
63 const base_type& base() const { return _rng; }
65 result_type operator()()
67 if(_n >= returned_block) {
68 // discard values of random number generator
69 for( ; _n < total_block; ++_n)
77 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); }
78 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); }
79 static bool validation(result_type x) { return true; } // dummy
81 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
83 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
84 template<class CharT, class Traits>
85 friend std::basic_ostream<CharT,Traits>&
86 operator<<(std::basic_ostream<CharT,Traits>& os, const discard_block& s)
88 os << s._rng << " " << s._n << " ";
92 template<class CharT, class Traits>
93 friend std::basic_istream<CharT,Traits>&
94 operator>>(std::basic_istream<CharT,Traits>& is, discard_block& s)
96 is >> s._rng >> std::ws >> s._n >> std::ws;
101 friend bool operator==(const discard_block& x, const discard_block& y)
102 { return x._rng == y._rng && x._n == y._n; }
103 friend bool operator!=(const discard_block& x, const discard_block& y)
104 { return !(x == y); }
106 // Use a member function; Streamable concept not supported.
107 bool operator==(const discard_block& rhs) const
108 { return _rng == rhs._rng && _n == rhs._n; }
109 bool operator!=(const discard_block& rhs) const
110 { return !(*this == rhs); }
118 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
119 // A definition is required even for integral static constants
120 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
121 const bool discard_block<UniformRandomNumberGenerator, p, r>::has_fixed_range;
122 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
123 const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::total_block;
124 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
125 const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::returned_block;
128 } // namespace random
132 #endif // BOOST_RANDOM_DISCARD_BLOCK_HPP