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. */ discard_block_engine()61 discard_block_engine() : _rng(), _n(0) { } 62 /** Constructs a new \discard_block_engine with a copy of rng. */ discard_block_engine(const base_type & 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. */ discard_block_engine(base_type && 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 */ BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(discard_block_engine,seed_type,value)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 */ BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(discard_block_engine,SeedSeq,seq)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 */ discard_block_engine(It & first,It last)89 template<class It> discard_block_engine(It& first, It last) 90 : _rng(first, last), _n(0) { } 91 92 /** default seeds the underlying generator. */ seed()93 void seed() { _rng.seed(); _n = 0; } 94 /** Seeds the underlying generator with s. */ BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(discard_block_engine,seed_type,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. */ BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(discard_block_engine,SeedSeq,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. */ seed(It & first,It last)101 template<class It> void seed(It& first, It last) 102 { _rng.seed(first, last); _n = 0; } 103 104 /** Returns the underlying engine. */ base() const105 const base_type& base() const { return _rng; } 106 107 /** Returns the next value of the generator. */ operator ()()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 discard(boost::uintmax_t z)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> generate(It first,It last)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 */ BOOST_PREVENT_MACRO_SUBSTITUTION()139 static BOOST_CONSTEXPR 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 */ BOOST_PREVENT_MACRO_SUBSTITUTION()145 static BOOST_CONSTEXPR 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>& operator <<(std::basic_ostream<CharT,Traits> & os,const discard_block_engine & s)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>& operator >>(std::basic_istream<CharT,Traits> & is,discard_block_engine & s)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. */ operator ==(const discard_block_engine & x,const discard_block_engine & y)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. */ operator !=(const discard_block_engine & x,const discard_block_engine & y)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; discard_block()205 discard_block() {} 206 template<class T> discard_block(T & arg)207 discard_block(T& arg) : base_t(arg) {} 208 template<class T> discard_block(const T & arg)209 discard_block(const T& arg) : base_t(arg) {} 210 template<class It> discard_block(It & first,It last)211 discard_block(It& first, It last) : base_t(first, last) {} BOOST_PREVENT_MACRO_SUBSTITUTION()212 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () 213 { return (this->base().min)(); } BOOST_PREVENT_MACRO_SUBSTITUTION()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> > { valueboost::random::detail::generator_bits227 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> > { valueboost::random::detail::generator_bits232 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 242