annotate DEPENDENCIES/generic/include/boost/random/xor_combine.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/xor_combine.hpp header file
Chris@16 2 *
Chris@16 3 * Copyright Jens Maurer 2002
Chris@16 4 * Distributed under the Boost Software License, Version 1.0. (See
Chris@16 5 * accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 * http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7 *
Chris@16 8 * See http://www.boost.org for most recent version including documentation.
Chris@16 9 *
Chris@16 10 * $Id: xor_combine.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
Chris@16 11 *
Chris@16 12 */
Chris@16 13
Chris@16 14 #ifndef BOOST_RANDOM_XOR_COMBINE_HPP
Chris@16 15 #define BOOST_RANDOM_XOR_COMBINE_HPP
Chris@16 16
Chris@16 17 #include <istream>
Chris@16 18 #include <iosfwd>
Chris@16 19 #include <cassert>
Chris@16 20 #include <algorithm> // for std::min and std::max
Chris@16 21 #include <boost/config.hpp>
Chris@16 22 #include <boost/limits.hpp>
Chris@16 23 #include <boost/cstdint.hpp> // uint32_t
Chris@16 24 #include <boost/random/detail/config.hpp>
Chris@16 25 #include <boost/random/detail/seed.hpp>
Chris@16 26 #include <boost/random/detail/seed_impl.hpp>
Chris@16 27
Chris@16 28 namespace boost {
Chris@16 29 namespace random {
Chris@16 30
Chris@16 31 /**
Chris@16 32 * Instantiations of @c xor_combine_engine model a
Chris@16 33 * \pseudo_random_number_generator. To produce its output it
Chris@16 34 * invokes each of the base generators, shifts their results
Chris@16 35 * and xors them together.
Chris@16 36 */
Chris@16 37 template<class URNG1, int s1, class URNG2, int s2>
Chris@16 38 class xor_combine_engine
Chris@16 39 {
Chris@16 40 public:
Chris@16 41 typedef URNG1 base1_type;
Chris@16 42 typedef URNG2 base2_type;
Chris@16 43 typedef typename base1_type::result_type result_type;
Chris@16 44
Chris@16 45 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
Chris@16 46 BOOST_STATIC_CONSTANT(int, shift1 = s1);
Chris@16 47 BOOST_STATIC_CONSTANT(int, shift2 = s2);
Chris@16 48
Chris@16 49 /**
Chris@16 50 * Constructors a @c xor_combine_engine by default constructing
Chris@16 51 * both base generators.
Chris@16 52 */
Chris@16 53 xor_combine_engine() : _rng1(), _rng2() { }
Chris@16 54
Chris@16 55 /** Constructs a @c xor_combine by copying two base generators. */
Chris@16 56 xor_combine_engine(const base1_type & rng1, const base2_type & rng2)
Chris@16 57 : _rng1(rng1), _rng2(rng2) { }
Chris@16 58
Chris@16 59 /**
Chris@16 60 * Constructs a @c xor_combine_engine, seeding both base generators
Chris@16 61 * with @c v.
Chris@16 62 *
Chris@16 63 * @xmlwarning
Chris@16 64 * The exact algorithm used by this function may change in the future.
Chris@16 65 * @endxmlwarning
Chris@16 66 */
Chris@16 67 BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(xor_combine_engine,
Chris@16 68 result_type, v)
Chris@16 69 { seed(v); }
Chris@16 70
Chris@16 71 /**
Chris@16 72 * Constructs a @c xor_combine_engine, seeding both base generators
Chris@16 73 * with values produced by @c seq.
Chris@16 74 */
Chris@16 75 BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(xor_combine_engine,
Chris@16 76 SeedSeq, seq)
Chris@16 77 { seed(seq); }
Chris@16 78
Chris@16 79 /**
Chris@16 80 * Constructs a @c xor_combine_engine, seeding both base generators
Chris@16 81 * with values from the iterator range [first, last) and changes
Chris@16 82 * first to point to the element after the last one used. If there
Chris@16 83 * are not enough elements in the range to seed both generators,
Chris@16 84 * throws @c std::invalid_argument.
Chris@16 85 */
Chris@16 86 template<class It> xor_combine_engine(It& first, It last)
Chris@16 87 : _rng1(first, last), _rng2( /* advanced by other call */ first, last) { }
Chris@16 88
Chris@16 89 /** Calls @c seed() for both base generators. */
Chris@16 90 void seed() { _rng1.seed(); _rng2.seed(); }
Chris@16 91
Chris@16 92 /** @c seeds both base generators with @c v. */
Chris@16 93 BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(xor_combine_engine, result_type, v)
Chris@16 94 { _rng1.seed(v); _rng2.seed(v); }
Chris@16 95
Chris@16 96 /** @c seeds both base generators with values produced by @c seq. */
Chris@16 97 BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(xor_combine_engine, SeedSeq, seq)
Chris@16 98 { _rng1.seed(seq); _rng2.seed(seq); }
Chris@16 99
Chris@16 100 /**
Chris@16 101 * seeds both base generators with values from the iterator
Chris@16 102 * range [first, last) and changes first to point to the element
Chris@16 103 * after the last one used. If there are not enough elements in
Chris@16 104 * the range to seed both generators, throws @c std::invalid_argument.
Chris@16 105 */
Chris@16 106 template<class It> void seed(It& first, It last)
Chris@16 107 {
Chris@16 108 _rng1.seed(first, last);
Chris@16 109 _rng2.seed(first, last);
Chris@16 110 }
Chris@16 111
Chris@16 112 /** Returns the first base generator. */
Chris@16 113 const base1_type& base1() const { return _rng1; }
Chris@16 114
Chris@16 115 /** Returns the second base generator. */
Chris@16 116 const base2_type& base2() const { return _rng2; }
Chris@16 117
Chris@16 118 /** Returns the next value of the generator. */
Chris@16 119 result_type operator()()
Chris@16 120 {
Chris@16 121 return (_rng1() << s1) ^ (_rng2() << s2);
Chris@16 122 }
Chris@16 123
Chris@16 124 /** Fills a range with random values */
Chris@16 125 template<class Iter>
Chris@16 126 void generate(Iter first, Iter last)
Chris@16 127 { detail::generate_from_int(*this, first, last); }
Chris@16 128
Chris@16 129 /** Advances the state of the generator by @c z. */
Chris@16 130 void discard(boost::uintmax_t z)
Chris@16 131 {
Chris@16 132 _rng1.discard(z);
Chris@16 133 _rng2.discard(z);
Chris@16 134 }
Chris@16 135
Chris@16 136 /** Returns the smallest value that the generator can produce. */
Chris@16 137 static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return (std::min)((URNG1::min)(), (URNG2::min)()); }
Chris@16 138 /** Returns the largest value that the generator can produce. */
Chris@16 139 static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () { return (std::max)((URNG1::min)(), (URNG2::max)()); }
Chris@16 140
Chris@16 141 /**
Chris@16 142 * Writes the textual representation of the generator to a @c std::ostream.
Chris@16 143 */
Chris@16 144 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, xor_combine_engine, s)
Chris@16 145 {
Chris@16 146 os << s._rng1 << ' ' << s._rng2;
Chris@16 147 return os;
Chris@16 148 }
Chris@16 149
Chris@16 150 /**
Chris@16 151 * Reads the textual representation of the generator from a @c std::istream.
Chris@16 152 */
Chris@16 153 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, xor_combine_engine, s)
Chris@16 154 {
Chris@16 155 is >> s._rng1 >> std::ws >> s._rng2;
Chris@16 156 return is;
Chris@16 157 }
Chris@16 158
Chris@16 159 /** Returns true if the two generators will produce identical sequences. */
Chris@16 160 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(xor_combine_engine, x, y)
Chris@16 161 { return x._rng1 == y._rng1 && x._rng2 == y._rng2; }
Chris@16 162
Chris@16 163 /** Returns true if the two generators will produce different sequences. */
Chris@16 164 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(xor_combine_engine)
Chris@16 165
Chris@16 166 private:
Chris@16 167 base1_type _rng1;
Chris@16 168 base2_type _rng2;
Chris@16 169 };
Chris@16 170
Chris@16 171 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
Chris@16 172 // A definition is required even for integral static constants
Chris@16 173 template<class URNG1, int s1, class URNG2, int s2>
Chris@16 174 const bool xor_combine_engine<URNG1, s1, URNG2, s2>::has_fixed_range;
Chris@16 175 template<class URNG1, int s1, class URNG2, int s2>
Chris@16 176 const int xor_combine_engine<URNG1, s1, URNG2, s2>::shift1;
Chris@16 177 template<class URNG1, int s1, class URNG2, int s2>
Chris@16 178 const int xor_combine_engine<URNG1, s1, URNG2, s2>::shift2;
Chris@16 179 #endif
Chris@16 180
Chris@16 181 /// \cond show_private
Chris@16 182
Chris@16 183 /** Provided for backwards compatibility. */
Chris@16 184 template<class URNG1, int s1, class URNG2, int s2,
Chris@16 185 typename URNG1::result_type v = 0>
Chris@16 186 class xor_combine : public xor_combine_engine<URNG1, s1, URNG2, s2>
Chris@16 187 {
Chris@16 188 typedef xor_combine_engine<URNG1, s1, URNG2, s2> base_type;
Chris@16 189 public:
Chris@16 190 typedef typename base_type::result_type result_type;
Chris@16 191 xor_combine() {}
Chris@16 192 xor_combine(result_type val) : base_type(val) {}
Chris@16 193 template<class It>
Chris@16 194 xor_combine(It& first, It last) : base_type(first, last) {}
Chris@16 195 xor_combine(const URNG1 & rng1, const URNG2 & rng2)
Chris@16 196 : base_type(rng1, rng2) { }
Chris@16 197
Chris@16 198 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (std::min)((this->base1().min)(), (this->base2().min)()); }
Chris@16 199 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (std::max)((this->base1().min)(), (this->base2().max)()); }
Chris@16 200 };
Chris@16 201
Chris@16 202 /// \endcond
Chris@16 203
Chris@16 204 } // namespace random
Chris@16 205 } // namespace boost
Chris@16 206
Chris@16 207 #endif // BOOST_RANDOM_XOR_COMBINE_HPP