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