annotate DEPENDENCIES/generic/include/boost/random/shuffle_order.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 /* boost random/shuffle_order.hpp header file
Chris@16 2 *
Chris@16 3 * Copyright Jens Maurer 2000-2001
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@101 11 * $Id$
Chris@16 12 *
Chris@16 13 */
Chris@16 14
Chris@16 15 #ifndef BOOST_RANDOM_SHUFFLE_ORDER_HPP
Chris@16 16 #define BOOST_RANDOM_SHUFFLE_ORDER_HPP
Chris@16 17
Chris@16 18 #include <iostream>
Chris@16 19 #include <algorithm> // std::copy
Chris@16 20 #include <cassert>
Chris@16 21 #include <boost/config.hpp>
Chris@16 22 #include <boost/limits.hpp>
Chris@16 23 #include <boost/static_assert.hpp>
Chris@16 24 #include <boost/cstdint.hpp>
Chris@16 25 #include <boost/random/detail/operators.hpp>
Chris@16 26 #include <boost/random/detail/seed.hpp>
Chris@16 27 #include <boost/random/detail/signed_unsigned_tools.hpp>
Chris@16 28 #include <boost/random/linear_congruential.hpp>
Chris@16 29
Chris@16 30 #include <boost/random/detail/disable_warnings.hpp>
Chris@16 31
Chris@16 32 namespace boost {
Chris@16 33 namespace random {
Chris@16 34
Chris@16 35 /**
Chris@16 36 * Instatiations of class template @c shuffle_order_engine model a
Chris@16 37 * \pseudo_random_number_generator. It mixes the output
Chris@16 38 * of some (usually \linear_congruential_engine)
Chris@16 39 * \uniform_random_number_generator to get better statistical properties.
Chris@16 40 * The algorithm is described in
Chris@16 41 *
Chris@16 42 * @blockquote
Chris@16 43 * "Improving a poor random number generator", Carter Bays
Chris@16 44 * and S.D. Durham, ACM Transactions on Mathematical Software,
Chris@16 45 * Vol 2, No. 1, March 1976, pp. 59-64.
Chris@16 46 * http://doi.acm.org/10.1145/355666.355670
Chris@16 47 * @endblockquote
Chris@16 48 *
Chris@16 49 * The output of the base generator is buffered in an array of
Chris@16 50 * length k. Every output X(n) has a second role: It gives an
Chris@16 51 * index into the array where X(n+1) will be retrieved. Used
Chris@16 52 * array elements are replaced with fresh output from the base
Chris@16 53 * generator.
Chris@16 54 *
Chris@16 55 * Template parameters are the base generator and the array
Chris@16 56 * length k, which should be around 100.
Chris@16 57 */
Chris@16 58 template<class UniformRandomNumberGenerator, std::size_t k>
Chris@16 59 class shuffle_order_engine
Chris@16 60 {
Chris@16 61 public:
Chris@16 62 typedef UniformRandomNumberGenerator base_type;
Chris@16 63 typedef typename base_type::result_type result_type;
Chris@16 64
Chris@16 65 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
Chris@16 66 BOOST_STATIC_CONSTANT(std::size_t, buffer_size = k);
Chris@16 67 BOOST_STATIC_CONSTANT(std::size_t, table_size = k);
Chris@16 68
Chris@16 69 BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
Chris@16 70 BOOST_STATIC_ASSERT(k > 0);
Chris@16 71
Chris@16 72 /**
Chris@16 73 * Constructs a @c shuffle_order_engine by invoking the
Chris@16 74 * default constructor of the base generator.
Chris@16 75 *
Chris@16 76 * Complexity: Exactly k+1 invocations of the base generator.
Chris@16 77 */
Chris@16 78 shuffle_order_engine() : _rng() { init(); }
Chris@16 79 /**
Chris@16 80 * Constructs a @c shuffle_output_engine by invoking the one-argument
Chris@16 81 * constructor of the base generator with the parameter seed.
Chris@16 82 *
Chris@16 83 * Complexity: Exactly k+1 invocations of the base generator.
Chris@16 84 */
Chris@16 85 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(shuffle_order_engine,
Chris@16 86 result_type, s)
Chris@16 87 { _rng.seed(s); init(); }
Chris@16 88 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(shuffle_order_engine, SeedSeq, seq)
Chris@16 89 { _rng.seed(seq); init(); }
Chris@16 90 /**
Chris@16 91 * Constructs a @c shuffle_output_engine by using a copy
Chris@16 92 * of the provided generator.
Chris@16 93 *
Chris@16 94 * Precondition: The template argument UniformRandomNumberGenerator
Chris@16 95 * shall denote a CopyConstructible type.
Chris@16 96 *
Chris@16 97 * Complexity: Exactly k+1 invocations of the base generator.
Chris@16 98 */
Chris@16 99 explicit shuffle_order_engine(const base_type & rng) : _rng(rng) { init(); }
Chris@16 100
Chris@101 101 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
Chris@16 102 explicit shuffle_order_engine(base_type&& rng) : _rng(rng) { init(); }
Chris@16 103 #endif
Chris@16 104
Chris@16 105 template<class It> shuffle_order_engine(It& first, It last)
Chris@16 106 : _rng(first, last) { init(); }
Chris@16 107 void seed() { _rng.seed(); init(); }
Chris@16 108 /**
Chris@16 109 * Invokes the one-argument seed method of the base generator
Chris@16 110 * with the parameter seed and re-initializes the internal buffer array.
Chris@16 111 *
Chris@16 112 * Complexity: Exactly k+1 invocations of the base generator.
Chris@16 113 */
Chris@16 114 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(shuffle_order_engine,
Chris@16 115 result_type, seed_arg)
Chris@16 116 { _rng.seed(seed_arg); init(); }
Chris@16 117 /**
Chris@16 118 * Invokes the one-argument seed method of the base generator
Chris@16 119 * with the parameter seq and re-initializes the internal buffer array.
Chris@16 120 *
Chris@16 121 * Complexity: Exactly k+1 invocations of the base generator.
Chris@16 122 */
Chris@16 123 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(shuffle_order_engine, SeedSeq, seq)
Chris@16 124 { _rng.seed(seq); init(); }
Chris@16 125 template<class It> void seed(It& first, It last)
Chris@16 126 { _rng.seed(first, last); init(); }
Chris@16 127
Chris@16 128 const base_type& base() const { return _rng; }
Chris@16 129
Chris@16 130 result_type operator()() {
Chris@16 131 // calculating the range every time may seem wasteful. However, this
Chris@16 132 // makes the information locally available for the optimizer.
Chris@16 133 typedef typename make_unsigned<result_type>::type base_unsigned;
Chris@16 134 const base_unsigned brange =
Chris@16 135 detail::subtract<result_type>()((max)(), (min)());
Chris@16 136 const base_unsigned off =
Chris@16 137 detail::subtract<result_type>()(y, (min)());
Chris@16 138
Chris@16 139 base_unsigned j;
Chris@16 140 if(k == 1) {
Chris@16 141 j = 0;
Chris@16 142 } else if(brange < (std::numeric_limits<base_unsigned>::max)() / k) {
Chris@16 143 // try to do it in the native type if we know that it won't
Chris@16 144 // overflow
Chris@16 145 j = k * off / (brange + 1);
Chris@16 146 } else if(brange < (std::numeric_limits<uintmax_t>::max)() / k) {
Chris@16 147 // Otherwise try to use uint64_t
Chris@16 148 j = static_cast<base_unsigned>(
Chris@16 149 static_cast<uintmax_t>(off) * k /
Chris@16 150 (static_cast<uintmax_t>(brange) + 1));
Chris@16 151 } else {
Chris@16 152 boost::uintmax_t divisor =
Chris@16 153 static_cast<boost::uintmax_t>(brange) + 1;
Chris@16 154 j = static_cast<base_unsigned>(detail::muldiv(off, k, divisor));
Chris@16 155 }
Chris@16 156 // assert(0 <= j && j < k);
Chris@16 157 y = v[j];
Chris@16 158 v[j] = _rng();
Chris@16 159 return y;
Chris@16 160 }
Chris@16 161
Chris@16 162 /** Advances the generator by z steps. */
Chris@16 163 void discard(boost::uintmax_t z)
Chris@16 164 {
Chris@16 165 for(boost::uintmax_t j = 0; j < z; ++j) {
Chris@16 166 (*this)();
Chris@16 167 }
Chris@16 168 }
Chris@16 169
Chris@16 170 /** Fills a range with pseudo-random values. */
Chris@16 171 template<class Iter>
Chris@16 172 void generate(Iter first, Iter last)
Chris@16 173 { detail::generate_from_int(*this, first, last); }
Chris@16 174
Chris@16 175 /** Returns the smallest value that the generator can produce. */
Chris@16 176 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 177 { return (base_type::min)(); }
Chris@16 178 /** Returns the largest value that the generator can produce. */
Chris@16 179 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
Chris@16 180 { return (base_type::max)(); }
Chris@16 181
Chris@16 182 /** Writes a @c shuffle_order_engine to a @c std::ostream. */
Chris@16 183 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, shuffle_order_engine, s)
Chris@16 184 {
Chris@16 185 os << s._rng;
Chris@16 186 for(std::size_t i = 0; i < k; ++i)
Chris@16 187 os << ' ' << s.v[i];
Chris@16 188 os << ' ' << s.y;
Chris@16 189 return os;
Chris@16 190 }
Chris@16 191
Chris@16 192 /** Reads a @c shuffle_order_engine from a @c std::istream. */
Chris@16 193 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, shuffle_order_engine, s)
Chris@16 194 {
Chris@16 195 is >> s._rng;
Chris@16 196 for(std::size_t i = 0; i < k; ++i)
Chris@16 197 is >> std::ws >> s.v[i];
Chris@16 198 is >> std::ws >> s.y;
Chris@16 199 return is;
Chris@16 200 }
Chris@16 201
Chris@16 202 /** Returns true if the two generators will produce identical sequences. */
Chris@16 203 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(shuffle_order_engine, x, y)
Chris@16 204 { return x._rng == y._rng && x.y == y.y && std::equal(x.v, x.v+k, y.v); }
Chris@16 205 /** Returns true if the two generators will produce different sequences. */
Chris@16 206 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(shuffle_order_engine)
Chris@16 207
Chris@16 208 private:
Chris@16 209
Chris@16 210 /// \cond show_private
Chris@16 211
Chris@16 212 void init()
Chris@16 213 {
Chris@16 214 // we cannot use std::generate, because it uses pass-by-value for _rng
Chris@16 215 for(result_type * p = v; p != v+k; ++p)
Chris@16 216 *p = _rng();
Chris@16 217 y = _rng();
Chris@16 218 }
Chris@16 219
Chris@16 220 /// \endcond
Chris@16 221
Chris@16 222 base_type _rng;
Chris@16 223 result_type v[k];
Chris@16 224 result_type y;
Chris@16 225 };
Chris@16 226
Chris@16 227 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
Chris@16 228 // A definition is required even for integral static constants
Chris@16 229 template<class URNG, std::size_t k>
Chris@16 230 const bool shuffle_order_engine<URNG, k>::has_fixed_range;
Chris@16 231 template<class URNG, std::size_t k>
Chris@16 232 const std::size_t shuffle_order_engine<URNG, k>::table_size;
Chris@16 233 template<class URNG, std::size_t k>
Chris@16 234 const std::size_t shuffle_order_engine<URNG, k>::buffer_size;
Chris@16 235 #endif
Chris@16 236
Chris@16 237 /**
Chris@16 238 * According to Harry Erwin (private e-mail), the specialization
Chris@16 239 * @c kreutzer1986 was suggested in:
Chris@16 240 *
Chris@16 241 * @blockquote
Chris@16 242 * "System Simulation: Programming Styles and Languages (International
Chris@16 243 * Computer Science Series)", Wolfgang Kreutzer, Addison-Wesley, December 1986.
Chris@16 244 * @endblockquote
Chris@16 245 */
Chris@16 246 typedef shuffle_order_engine<
Chris@16 247 linear_congruential_engine<uint32_t, 1366, 150889, 714025>,
Chris@16 248 97> kreutzer1986;
Chris@16 249
Chris@16 250 /**
Chris@16 251 * The specialization @c knuth_b is specified by the C++ standard.
Chris@16 252 * It is described in
Chris@16 253 *
Chris@16 254 * @blockquote
Chris@16 255 * "The Art of Computer Programming, Second Edition, Volume 2,
Chris@16 256 * Seminumerical Algorithms", Donald Knuth, Addison-Wesley, 1981.
Chris@16 257 * @endblockquote
Chris@16 258 */
Chris@16 259 typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
Chris@16 260
Chris@16 261 } // namespace random
Chris@16 262
Chris@16 263 using random::kreutzer1986;
Chris@16 264
Chris@16 265 } // namespace boost
Chris@16 266
Chris@16 267 #include <boost/random/detail/enable_warnings.hpp>
Chris@16 268
Chris@16 269 #endif // BOOST_RANDOM_SHUFFLE_OUTPUT_HPP