annotate DEPENDENCIES/generic/include/boost/random/discard_block.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 /* boost random/discard_block.hpp header file
Chris@16 2 *
Chris@16 3 * Copyright Jens Maurer 2002
Chris@16 4 * Copyright Steven Watanabe 2010
Chris@16 5 * Distributed under the Boost Software License, Version 1.0. (See
Chris@16 6 * accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 * http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 *
Chris@16 9 * See http://www.boost.org for most recent version including documentation.
Chris@16 10 *
Chris@16 11 * $Id: discard_block.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $
Chris@16 12 *
Chris@16 13 * Revision history
Chris@16 14 * 2001-03-02 created
Chris@16 15 */
Chris@16 16
Chris@16 17 #ifndef BOOST_RANDOM_DISCARD_BLOCK_HPP
Chris@16 18 #define BOOST_RANDOM_DISCARD_BLOCK_HPP
Chris@16 19
Chris@16 20 #include <iostream>
Chris@16 21 #include <boost/config.hpp>
Chris@16 22 #include <boost/cstdint.hpp>
Chris@16 23 #include <boost/limits.hpp>
Chris@16 24 #include <boost/static_assert.hpp>
Chris@16 25 #include <boost/random/detail/config.hpp>
Chris@16 26 #include <boost/random/detail/seed.hpp>
Chris@16 27
Chris@16 28
Chris@16 29 namespace boost {
Chris@16 30 namespace random {
Chris@16 31
Chris@16 32 /**
Chris@16 33 * The class template \discard_block_engine is a model of
Chris@16 34 * \pseudo_random_number_generator. It modifies
Chris@16 35 * another generator by discarding parts of its output.
Chris@16 36 * Out of every block of @c p results, the first @c r
Chris@16 37 * will be returned and the rest discarded.
Chris@16 38 *
Chris@16 39 * Requires: 0 < p <= r
Chris@16 40 */
Chris@16 41 template<class UniformRandomNumberGenerator, std::size_t p, std::size_t r>
Chris@16 42 class discard_block_engine
Chris@16 43 {
Chris@16 44 typedef typename detail::seed_type<
Chris@16 45 typename UniformRandomNumberGenerator::result_type>::type seed_type;
Chris@16 46 public:
Chris@16 47 typedef UniformRandomNumberGenerator base_type;
Chris@16 48 typedef typename base_type::result_type result_type;
Chris@16 49
Chris@16 50 BOOST_STATIC_CONSTANT(std::size_t, block_size = p);
Chris@16 51 BOOST_STATIC_CONSTANT(std::size_t, used_block = r);
Chris@16 52
Chris@16 53 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
Chris@16 54 BOOST_STATIC_CONSTANT(std::size_t, total_block = p);
Chris@16 55 BOOST_STATIC_CONSTANT(std::size_t, returned_block = r);
Chris@16 56
Chris@16 57 BOOST_STATIC_ASSERT(total_block >= returned_block);
Chris@16 58
Chris@16 59 /** Uses the default seed for the base generator. */
Chris@16 60 discard_block_engine() : _rng(), _n(0) { }
Chris@16 61 /** Constructs a new \discard_block_engine with a copy of rng. */
Chris@16 62 explicit discard_block_engine(const base_type & rng) : _rng(rng), _n(0) { }
Chris@16 63
Chris@16 64 #ifndef BOOST_NO_RVALUE_REFERENCES
Chris@16 65 /** Constructs a new \discard_block_engine with rng. */
Chris@16 66 explicit discard_block_engine(base_type && rng) : _rng(rng), _n(0) { }
Chris@16 67 #endif
Chris@16 68
Chris@16 69 /**
Chris@16 70 * Creates a new \discard_block_engine and seeds the underlying
Chris@16 71 * generator with @c value
Chris@16 72 */
Chris@16 73 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(discard_block_engine,
Chris@16 74 seed_type, value)
Chris@16 75 { _rng.seed(value); _n = 0; }
Chris@16 76
Chris@16 77 /**
Chris@16 78 * Creates a new \discard_block_engine and seeds the underlying
Chris@16 79 * generator with @c seq
Chris@16 80 */
Chris@16 81 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(discard_block_engine, SeedSeq, seq)
Chris@16 82 { _rng.seed(seq); _n = 0; }
Chris@16 83
Chris@16 84 /**
Chris@16 85 * Creates a new \discard_block_engine and seeds the underlying
Chris@16 86 * generator with first and last.
Chris@16 87 */
Chris@16 88 template<class It> discard_block_engine(It& first, It last)
Chris@16 89 : _rng(first, last), _n(0) { }
Chris@16 90
Chris@16 91 /** default seeds the underlying generator. */
Chris@16 92 void seed() { _rng.seed(); _n = 0; }
Chris@16 93 /** Seeds the underlying generator with s. */
Chris@16 94 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(discard_block_engine, seed_type, s)
Chris@16 95 { _rng.seed(s); _n = 0; }
Chris@16 96 /** Seeds the underlying generator with seq. */
Chris@16 97 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(discard_block_engine, SeedSeq, seq)
Chris@16 98 { _rng.seed(seq); _n = 0; }
Chris@16 99 /** Seeds the underlying generator with first and last. */
Chris@16 100 template<class It> void seed(It& first, It last)
Chris@16 101 { _rng.seed(first, last); _n = 0; }
Chris@16 102
Chris@16 103 /** Returns the underlying engine. */
Chris@16 104 const base_type& base() const { return _rng; }
Chris@16 105
Chris@16 106 /** Returns the next value of the generator. */
Chris@16 107 result_type operator()()
Chris@16 108 {
Chris@16 109 if(_n >= returned_block) {
Chris@16 110 // discard values of random number generator
Chris@16 111 // Don't use discard, since we still need to
Chris@16 112 // be somewhat compatible with TR1.
Chris@16 113 // _rng.discard(total_block - _n);
Chris@16 114 for(std::size_t i = 0; i < total_block - _n; ++i) {
Chris@16 115 _rng();
Chris@16 116 }
Chris@16 117 _n = 0;
Chris@16 118 }
Chris@16 119 ++_n;
Chris@16 120 return _rng();
Chris@16 121 }
Chris@16 122
Chris@16 123 void discard(boost::uintmax_t z)
Chris@16 124 {
Chris@16 125 for(boost::uintmax_t j = 0; j < z; ++j) {
Chris@16 126 (*this)();
Chris@16 127 }
Chris@16 128 }
Chris@16 129
Chris@16 130 template<class It>
Chris@16 131 void generate(It first, It last)
Chris@16 132 { detail::generate(*this, first, last); }
Chris@16 133
Chris@16 134 /**
Chris@16 135 * Returns the smallest value that the generator can produce.
Chris@16 136 * This is the same as the minimum of the underlying generator.
Chris@16 137 */
Chris@16 138 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 139 { return (base_type::min)(); }
Chris@16 140 /**
Chris@16 141 * Returns the largest value that the generator can produce.
Chris@16 142 * This is the same as the maximum of the underlying generator.
Chris@16 143 */
Chris@16 144 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 145 { return (base_type::max)(); }
Chris@16 146
Chris@16 147 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
Chris@16 148 /** Writes a \discard_block_engine to a @c std::ostream. */
Chris@16 149 template<class CharT, class Traits>
Chris@16 150 friend std::basic_ostream<CharT,Traits>&
Chris@16 151 operator<<(std::basic_ostream<CharT,Traits>& os,
Chris@16 152 const discard_block_engine& s)
Chris@16 153 {
Chris@16 154 os << s._rng << ' ' << s._n;
Chris@16 155 return os;
Chris@16 156 }
Chris@16 157
Chris@16 158 /** Reads a \discard_block_engine from a @c std::istream. */
Chris@16 159 template<class CharT, class Traits>
Chris@16 160 friend std::basic_istream<CharT,Traits>&
Chris@16 161 operator>>(std::basic_istream<CharT,Traits>& is, discard_block_engine& s)
Chris@16 162 {
Chris@16 163 is >> s._rng >> std::ws >> s._n;
Chris@16 164 return is;
Chris@16 165 }
Chris@16 166 #endif
Chris@16 167
Chris@16 168 /** Returns true if the two generators will produce identical sequences. */
Chris@16 169 friend bool operator==(const discard_block_engine& x,
Chris@16 170 const discard_block_engine& y)
Chris@16 171 { return x._rng == y._rng && x._n == y._n; }
Chris@16 172 /** Returns true if the two generators will produce different sequences. */
Chris@16 173 friend bool operator!=(const discard_block_engine& x,
Chris@16 174 const discard_block_engine& y)
Chris@16 175 { return !(x == y); }
Chris@16 176
Chris@16 177 private:
Chris@16 178 base_type _rng;
Chris@16 179 std::size_t _n;
Chris@16 180 };
Chris@16 181
Chris@16 182 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
Chris@16 183 // A definition is required even for integral static constants
Chris@16 184 template<class URNG, std::size_t p, std::size_t r>
Chris@16 185 const bool discard_block_engine<URNG, p, r>::has_fixed_range;
Chris@16 186 template<class URNG, std::size_t p, std::size_t r>
Chris@16 187 const std::size_t discard_block_engine<URNG, p, r>::total_block;
Chris@16 188 template<class URNG, std::size_t p, std::size_t r>
Chris@16 189 const std::size_t discard_block_engine<URNG, p, r>::returned_block;
Chris@16 190 template<class URNG, std::size_t p, std::size_t r>
Chris@16 191 const std::size_t discard_block_engine<URNG, p, r>::block_size;
Chris@16 192 template<class URNG, std::size_t p, std::size_t r>
Chris@16 193 const std::size_t discard_block_engine<URNG, p, r>::used_block;
Chris@16 194 #endif
Chris@16 195
Chris@16 196 /// \cond \show_deprecated
Chris@16 197
Chris@16 198 template<class URNG, int p, int r>
Chris@16 199 class discard_block : public discard_block_engine<URNG, p, r>
Chris@16 200 {
Chris@16 201 typedef discard_block_engine<URNG, p, r> base_t;
Chris@16 202 public:
Chris@16 203 typedef typename base_t::result_type result_type;
Chris@16 204 discard_block() {}
Chris@16 205 template<class T>
Chris@16 206 discard_block(T& arg) : base_t(arg) {}
Chris@16 207 template<class T>
Chris@16 208 discard_block(const T& arg) : base_t(arg) {}
Chris@16 209 template<class It>
Chris@16 210 discard_block(It& first, It last) : base_t(first, last) {}
Chris@16 211 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 212 { return (this->base().min)(); }
Chris@16 213 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 214 { return (this->base().max)(); }
Chris@16 215 };
Chris@16 216
Chris@16 217 /// \endcond
Chris@16 218
Chris@16 219 namespace detail {
Chris@16 220
Chris@16 221 template<class Engine>
Chris@16 222 struct generator_bits;
Chris@16 223
Chris@16 224 template<class URNG, std::size_t p, std::size_t r>
Chris@16 225 struct generator_bits<discard_block_engine<URNG, p, r> > {
Chris@16 226 static std::size_t value() { return generator_bits<URNG>::value(); }
Chris@16 227 };
Chris@16 228
Chris@16 229 template<class URNG, int p, int r>
Chris@16 230 struct generator_bits<discard_block<URNG, p, r> > {
Chris@16 231 static std::size_t value() { return generator_bits<URNG>::value(); }
Chris@16 232 };
Chris@16 233
Chris@16 234 }
Chris@16 235
Chris@16 236 } // namespace random
Chris@16 237
Chris@16 238 } // namespace boost
Chris@16 239
Chris@16 240 #endif // BOOST_RANDOM_DISCARD_BLOCK_HPP