williamr@2
|
1 |
/* boost random/subtract_with_carry.hpp header file
|
williamr@2
|
2 |
*
|
williamr@2
|
3 |
* Copyright Jens Maurer 2002
|
williamr@2
|
4 |
* Distributed under the Boost Software License, Version 1.0. (See
|
williamr@2
|
5 |
* accompanying file LICENSE_1_0.txt or copy at
|
williamr@2
|
6 |
* http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
7 |
*
|
williamr@2
|
8 |
* See http://www.boost.org for most recent version including documentation.
|
williamr@2
|
9 |
*
|
williamr@2
|
10 |
* $Id: subtract_with_carry.hpp,v 1.24 2005/05/21 15:57:00 dgregor Exp $
|
williamr@2
|
11 |
*
|
williamr@2
|
12 |
* Revision history
|
williamr@2
|
13 |
* 2002-03-02 created
|
williamr@2
|
14 |
*/
|
williamr@2
|
15 |
|
williamr@2
|
16 |
#ifndef BOOST_RANDOM_SUBTRACT_WITH_CARRY_HPP
|
williamr@2
|
17 |
#define BOOST_RANDOM_SUBTRACT_WITH_CARRY_HPP
|
williamr@2
|
18 |
|
williamr@2
|
19 |
#include <cmath>
|
williamr@2
|
20 |
#include <iostream>
|
williamr@2
|
21 |
#include <algorithm> // std::equal
|
williamr@2
|
22 |
#include <stdexcept>
|
williamr@2
|
23 |
#include <cmath> // std::pow
|
williamr@2
|
24 |
#include <boost/config.hpp>
|
williamr@2
|
25 |
#include <boost/limits.hpp>
|
williamr@2
|
26 |
#include <boost/cstdint.hpp>
|
williamr@2
|
27 |
#include <boost/static_assert.hpp>
|
williamr@2
|
28 |
#include <boost/detail/workaround.hpp>
|
williamr@2
|
29 |
#include <boost/random/linear_congruential.hpp>
|
williamr@2
|
30 |
|
williamr@2
|
31 |
|
williamr@2
|
32 |
namespace boost {
|
williamr@2
|
33 |
namespace random {
|
williamr@2
|
34 |
|
williamr@2
|
35 |
#if BOOST_WORKAROUND(_MSC_FULL_VER, BOOST_TESTED_AT(13102292)) && BOOST_MSVC > 1300
|
williamr@2
|
36 |
# define BOOST_RANDOM_EXTRACT_SWC_01
|
williamr@2
|
37 |
#endif
|
williamr@2
|
38 |
|
williamr@2
|
39 |
#if defined(__APPLE_CC__) && defined(__GNUC__) && (__GNUC__ == 3) && (__GNUC_MINOR__ <= 3)
|
williamr@2
|
40 |
# define BOOST_RANDOM_EXTRACT_SWC_01
|
williamr@2
|
41 |
#endif
|
williamr@2
|
42 |
|
williamr@2
|
43 |
# ifdef BOOST_RANDOM_EXTRACT_SWC_01
|
williamr@2
|
44 |
namespace detail
|
williamr@2
|
45 |
{
|
williamr@2
|
46 |
template <class IStream, class SubtractWithCarry, class RealType>
|
williamr@2
|
47 |
void extract_subtract_with_carry_01(
|
williamr@2
|
48 |
IStream& is
|
williamr@2
|
49 |
, SubtractWithCarry& f
|
williamr@2
|
50 |
, RealType& carry
|
williamr@2
|
51 |
, RealType* x
|
williamr@2
|
52 |
, RealType modulus)
|
williamr@2
|
53 |
{
|
williamr@2
|
54 |
RealType value;
|
williamr@2
|
55 |
for(unsigned int j = 0; j < f.long_lag; ++j) {
|
williamr@2
|
56 |
is >> value >> std::ws;
|
williamr@2
|
57 |
x[j] = value / modulus;
|
williamr@2
|
58 |
}
|
williamr@2
|
59 |
is >> value >> std::ws;
|
williamr@2
|
60 |
carry = value / modulus;
|
williamr@2
|
61 |
}
|
williamr@2
|
62 |
}
|
williamr@2
|
63 |
# endif
|
williamr@2
|
64 |
// subtract-with-carry generator
|
williamr@2
|
65 |
// Marsaglia and Zaman
|
williamr@2
|
66 |
|
williamr@2
|
67 |
template<class IntType, IntType m, unsigned int s, unsigned int r,
|
williamr@2
|
68 |
IntType val>
|
williamr@2
|
69 |
class subtract_with_carry
|
williamr@2
|
70 |
{
|
williamr@2
|
71 |
public:
|
williamr@2
|
72 |
typedef IntType result_type;
|
williamr@2
|
73 |
BOOST_STATIC_CONSTANT(bool, has_fixed_range = true);
|
williamr@2
|
74 |
BOOST_STATIC_CONSTANT(result_type, min_value = 0);
|
williamr@2
|
75 |
BOOST_STATIC_CONSTANT(result_type, max_value = m-1);
|
williamr@2
|
76 |
BOOST_STATIC_CONSTANT(result_type, modulus = m);
|
williamr@2
|
77 |
BOOST_STATIC_CONSTANT(unsigned int, long_lag = r);
|
williamr@2
|
78 |
BOOST_STATIC_CONSTANT(unsigned int, short_lag = s);
|
williamr@2
|
79 |
|
williamr@2
|
80 |
subtract_with_carry() {
|
williamr@2
|
81 |
// MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
|
williamr@2
|
82 |
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
williamr@2
|
83 |
BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_signed);
|
williamr@2
|
84 |
BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
|
williamr@2
|
85 |
#endif
|
williamr@2
|
86 |
seed();
|
williamr@2
|
87 |
}
|
williamr@2
|
88 |
explicit subtract_with_carry(uint32_t value) { seed(value); }
|
williamr@2
|
89 |
template<class Generator>
|
williamr@2
|
90 |
explicit subtract_with_carry(Generator & gen) { seed(gen); }
|
williamr@2
|
91 |
template<class It> subtract_with_carry(It& first, It last) { seed(first,last); }
|
williamr@2
|
92 |
|
williamr@2
|
93 |
// compiler-generated copy ctor and assignment operator are fine
|
williamr@2
|
94 |
|
williamr@2
|
95 |
void seed(uint32_t value = 19780503u)
|
williamr@2
|
96 |
{
|
williamr@2
|
97 |
random::linear_congruential<int32_t, 40014, 0, 2147483563, 0> intgen(value);
|
williamr@2
|
98 |
seed(intgen);
|
williamr@2
|
99 |
}
|
williamr@2
|
100 |
|
williamr@2
|
101 |
// For GCC, moving this function out-of-line prevents inlining, which may
|
williamr@2
|
102 |
// reduce overall object code size. However, MSVC does not grok
|
williamr@2
|
103 |
// out-of-line template member functions.
|
williamr@2
|
104 |
template<class Generator>
|
williamr@2
|
105 |
void seed(Generator & gen)
|
williamr@2
|
106 |
{
|
williamr@2
|
107 |
// I could have used std::generate_n, but it takes "gen" by value
|
williamr@2
|
108 |
for(unsigned int j = 0; j < long_lag; ++j)
|
williamr@2
|
109 |
x[j] = gen() % modulus;
|
williamr@2
|
110 |
carry = (x[long_lag-1] == 0);
|
williamr@2
|
111 |
k = 0;
|
williamr@2
|
112 |
}
|
williamr@2
|
113 |
|
williamr@2
|
114 |
template<class It>
|
williamr@2
|
115 |
void seed(It& first, It last)
|
williamr@2
|
116 |
{
|
williamr@2
|
117 |
unsigned int j;
|
williamr@2
|
118 |
for(j = 0; j < long_lag && first != last; ++j, ++first)
|
williamr@2
|
119 |
x[j] = *first % modulus;
|
williamr@2
|
120 |
if(first == last && j < long_lag)
|
williamr@2
|
121 |
throw std::invalid_argument("subtract_with_carry::seed");
|
williamr@2
|
122 |
carry = (x[long_lag-1] == 0);
|
williamr@2
|
123 |
k = 0;
|
williamr@2
|
124 |
}
|
williamr@2
|
125 |
|
williamr@2
|
126 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return min_value; }
|
williamr@2
|
127 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return max_value; }
|
williamr@2
|
128 |
|
williamr@2
|
129 |
result_type operator()()
|
williamr@2
|
130 |
{
|
williamr@2
|
131 |
int short_index = k - short_lag;
|
williamr@2
|
132 |
if(short_index < 0)
|
williamr@2
|
133 |
short_index += long_lag;
|
williamr@2
|
134 |
IntType delta;
|
williamr@2
|
135 |
if (x[short_index] >= x[k] + carry) {
|
williamr@2
|
136 |
// x(n) >= 0
|
williamr@2
|
137 |
delta = x[short_index] - (x[k] + carry);
|
williamr@2
|
138 |
carry = 0;
|
williamr@2
|
139 |
} else {
|
williamr@2
|
140 |
// x(n) < 0
|
williamr@2
|
141 |
delta = modulus - x[k] - carry + x[short_index];
|
williamr@2
|
142 |
carry = 1;
|
williamr@2
|
143 |
}
|
williamr@2
|
144 |
x[k] = delta;
|
williamr@2
|
145 |
++k;
|
williamr@2
|
146 |
if(k >= long_lag)
|
williamr@2
|
147 |
k = 0;
|
williamr@2
|
148 |
return delta;
|
williamr@2
|
149 |
}
|
williamr@2
|
150 |
|
williamr@2
|
151 |
public:
|
williamr@2
|
152 |
static bool validation(result_type x) { return x == val; }
|
williamr@2
|
153 |
|
williamr@2
|
154 |
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
|
williamr@2
|
155 |
|
williamr@2
|
156 |
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
williamr@2
|
157 |
template<class CharT, class Traits>
|
williamr@2
|
158 |
friend std::basic_ostream<CharT,Traits>&
|
williamr@2
|
159 |
operator<<(std::basic_ostream<CharT,Traits>& os,
|
williamr@2
|
160 |
const subtract_with_carry& f)
|
williamr@2
|
161 |
{
|
williamr@2
|
162 |
for(unsigned int j = 0; j < f.long_lag; ++j)
|
williamr@2
|
163 |
os << f.compute(j) << " ";
|
williamr@2
|
164 |
os << f.carry << " ";
|
williamr@2
|
165 |
return os;
|
williamr@2
|
166 |
}
|
williamr@2
|
167 |
|
williamr@2
|
168 |
template<class CharT, class Traits>
|
williamr@2
|
169 |
friend std::basic_istream<CharT,Traits>&
|
williamr@2
|
170 |
operator>>(std::basic_istream<CharT,Traits>& is, subtract_with_carry& f)
|
williamr@2
|
171 |
{
|
williamr@2
|
172 |
for(unsigned int j = 0; j < f.long_lag; ++j)
|
williamr@2
|
173 |
is >> f.x[j] >> std::ws;
|
williamr@2
|
174 |
is >> f.carry >> std::ws;
|
williamr@2
|
175 |
f.k = 0;
|
williamr@2
|
176 |
return is;
|
williamr@2
|
177 |
}
|
williamr@2
|
178 |
#endif
|
williamr@2
|
179 |
|
williamr@2
|
180 |
friend bool operator==(const subtract_with_carry& x, const subtract_with_carry& y)
|
williamr@2
|
181 |
{
|
williamr@2
|
182 |
for(unsigned int j = 0; j < r; ++j)
|
williamr@2
|
183 |
if(x.compute(j) != y.compute(j))
|
williamr@2
|
184 |
return false;
|
williamr@2
|
185 |
return true;
|
williamr@2
|
186 |
}
|
williamr@2
|
187 |
|
williamr@2
|
188 |
friend bool operator!=(const subtract_with_carry& x, const subtract_with_carry& y)
|
williamr@2
|
189 |
{ return !(x == y); }
|
williamr@2
|
190 |
#else
|
williamr@2
|
191 |
// Use a member function; Streamable concept not supported.
|
williamr@2
|
192 |
bool operator==(const subtract_with_carry& rhs) const
|
williamr@2
|
193 |
{
|
williamr@2
|
194 |
for(unsigned int j = 0; j < r; ++j)
|
williamr@2
|
195 |
if(compute(j) != rhs.compute(j))
|
williamr@2
|
196 |
return false;
|
williamr@2
|
197 |
return true;
|
williamr@2
|
198 |
}
|
williamr@2
|
199 |
|
williamr@2
|
200 |
bool operator!=(const subtract_with_carry& rhs) const
|
williamr@2
|
201 |
{ return !(*this == rhs); }
|
williamr@2
|
202 |
#endif
|
williamr@2
|
203 |
|
williamr@2
|
204 |
private:
|
williamr@2
|
205 |
// returns x(i-r+index), where index is in 0..r-1
|
williamr@2
|
206 |
IntType compute(unsigned int index) const
|
williamr@2
|
207 |
{
|
williamr@2
|
208 |
return x[(k+index) % long_lag];
|
williamr@2
|
209 |
}
|
williamr@2
|
210 |
|
williamr@2
|
211 |
// state representation; next output (state) is x(i)
|
williamr@2
|
212 |
// x[0] ... x[k] x[k+1] ... x[long_lag-1] represents
|
williamr@2
|
213 |
// x(i-k) ... x(i) x(i+1) ... x(i-k+long_lag-1)
|
williamr@2
|
214 |
// speed: base: 20-25 nsec
|
williamr@2
|
215 |
// ranlux_4: 230 nsec, ranlux_7: 430 nsec, ranlux_14: 810 nsec
|
williamr@2
|
216 |
// This state representation makes operator== and save/restore more
|
williamr@2
|
217 |
// difficult, because we've already computed "too much" and thus
|
williamr@2
|
218 |
// have to undo some steps to get at x(i-r) etc.
|
williamr@2
|
219 |
|
williamr@2
|
220 |
// state representation: next output (state) is x(i)
|
williamr@2
|
221 |
// x[0] ... x[k] x[k+1] ... x[long_lag-1] represents
|
williamr@2
|
222 |
// x(i-k) ... x(i) x(i-long_lag+1) ... x(i-k-1)
|
williamr@2
|
223 |
// speed: base 28 nsec
|
williamr@2
|
224 |
// ranlux_4: 370 nsec, ranlux_7: 688 nsec, ranlux_14: 1343 nsec
|
williamr@2
|
225 |
IntType x[long_lag];
|
williamr@2
|
226 |
unsigned int k;
|
williamr@2
|
227 |
int carry;
|
williamr@2
|
228 |
};
|
williamr@2
|
229 |
|
williamr@2
|
230 |
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
williamr@2
|
231 |
// A definition is required even for integral static constants
|
williamr@2
|
232 |
template<class IntType, IntType m, unsigned int s, unsigned int r, IntType val>
|
williamr@2
|
233 |
const bool subtract_with_carry<IntType, m, s, r, val>::has_fixed_range;
|
williamr@2
|
234 |
template<class IntType, IntType m, unsigned int s, unsigned int r, IntType val>
|
williamr@2
|
235 |
const IntType subtract_with_carry<IntType, m, s, r, val>::min_value;
|
williamr@2
|
236 |
template<class IntType, IntType m, unsigned int s, unsigned int r, IntType val>
|
williamr@2
|
237 |
const IntType subtract_with_carry<IntType, m, s, r, val>::max_value;
|
williamr@2
|
238 |
template<class IntType, IntType m, unsigned int s, unsigned int r, IntType val>
|
williamr@2
|
239 |
const IntType subtract_with_carry<IntType, m, s, r, val>::modulus;
|
williamr@2
|
240 |
template<class IntType, IntType m, unsigned int s, unsigned int r, IntType val>
|
williamr@2
|
241 |
const unsigned int subtract_with_carry<IntType, m, s, r, val>::long_lag;
|
williamr@2
|
242 |
template<class IntType, IntType m, unsigned int s, unsigned int r, IntType val>
|
williamr@2
|
243 |
const unsigned int subtract_with_carry<IntType, m, s, r, val>::short_lag;
|
williamr@2
|
244 |
#endif
|
williamr@2
|
245 |
|
williamr@2
|
246 |
|
williamr@2
|
247 |
// use a floating-point representation to produce values in [0..1)
|
williamr@2
|
248 |
template<class RealType, int w, unsigned int s, unsigned int r, int val=0>
|
williamr@2
|
249 |
class subtract_with_carry_01
|
williamr@2
|
250 |
{
|
williamr@2
|
251 |
public:
|
williamr@2
|
252 |
typedef RealType result_type;
|
williamr@2
|
253 |
BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
|
williamr@2
|
254 |
BOOST_STATIC_CONSTANT(int, word_size = w);
|
williamr@2
|
255 |
BOOST_STATIC_CONSTANT(unsigned int, long_lag = r);
|
williamr@2
|
256 |
BOOST_STATIC_CONSTANT(unsigned int, short_lag = s);
|
williamr@2
|
257 |
|
williamr@2
|
258 |
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
|
williamr@2
|
259 |
BOOST_STATIC_ASSERT(!std::numeric_limits<result_type>::is_integer);
|
williamr@2
|
260 |
#endif
|
williamr@2
|
261 |
|
williamr@2
|
262 |
subtract_with_carry_01() { init_modulus(); seed(); }
|
williamr@2
|
263 |
explicit subtract_with_carry_01(uint32_t value)
|
williamr@2
|
264 |
{ init_modulus(); seed(value); }
|
williamr@2
|
265 |
template<class It> subtract_with_carry_01(It& first, It last)
|
williamr@2
|
266 |
{ init_modulus(); seed(first,last); }
|
williamr@2
|
267 |
|
williamr@2
|
268 |
private:
|
williamr@2
|
269 |
void init_modulus()
|
williamr@2
|
270 |
{
|
williamr@2
|
271 |
#ifndef BOOST_NO_STDC_NAMESPACE
|
williamr@2
|
272 |
// allow for Koenig lookup
|
williamr@2
|
273 |
using std::pow;
|
williamr@2
|
274 |
#endif
|
williamr@2
|
275 |
_modulus = pow(RealType(2), word_size);
|
williamr@2
|
276 |
}
|
williamr@2
|
277 |
|
williamr@2
|
278 |
public:
|
williamr@2
|
279 |
// compiler-generated copy ctor and assignment operator are fine
|
williamr@2
|
280 |
|
williamr@2
|
281 |
void seed(uint32_t value = 19780503u)
|
williamr@2
|
282 |
{
|
williamr@2
|
283 |
#ifndef BOOST_NO_STDC_NAMESPACE
|
williamr@2
|
284 |
// allow for Koenig lookup
|
williamr@2
|
285 |
using std::fmod;
|
williamr@2
|
286 |
#endif
|
williamr@2
|
287 |
random::linear_congruential<int32_t, 40014, 0, 2147483563, 0> gen(value);
|
williamr@2
|
288 |
unsigned long array[(w+31)/32 * long_lag];
|
williamr@2
|
289 |
for(unsigned int j = 0; j < sizeof(array)/sizeof(unsigned long); ++j)
|
williamr@2
|
290 |
array[j] = gen();
|
williamr@2
|
291 |
unsigned long * start = array;
|
williamr@2
|
292 |
seed(start, array + sizeof(array)/sizeof(unsigned long));
|
williamr@2
|
293 |
}
|
williamr@2
|
294 |
|
williamr@2
|
295 |
template<class It>
|
williamr@2
|
296 |
void seed(It& first, It last)
|
williamr@2
|
297 |
{
|
williamr@2
|
298 |
#ifndef BOOST_NO_STDC_NAMESPACE
|
williamr@2
|
299 |
// allow for Koenig lookup
|
williamr@2
|
300 |
using std::fmod;
|
williamr@2
|
301 |
using std::pow;
|
williamr@2
|
302 |
#endif
|
williamr@2
|
303 |
unsigned long mask = ~((~0u) << (w%32)); // now lowest (w%32) bits set
|
williamr@2
|
304 |
RealType two32 = pow(RealType(2), 32);
|
williamr@2
|
305 |
unsigned int j;
|
williamr@2
|
306 |
for(j = 0; j < long_lag && first != last; ++j) {
|
williamr@2
|
307 |
x[j] = RealType(0);
|
williamr@2
|
308 |
for(int i = 0; i < w/32 && first != last; ++i, ++first)
|
williamr@2
|
309 |
x[j] += *first / pow(two32,i+1);
|
williamr@2
|
310 |
if(first != last && mask != 0) {
|
williamr@2
|
311 |
x[j] += fmod((*first & mask) / _modulus, RealType(1));
|
williamr@2
|
312 |
++first;
|
williamr@2
|
313 |
}
|
williamr@2
|
314 |
}
|
williamr@2
|
315 |
if(first == last && j < long_lag)
|
williamr@2
|
316 |
throw std::invalid_argument("subtract_with_carry_01::seed");
|
williamr@2
|
317 |
carry = (x[long_lag-1] ? 0 : 1 / _modulus);
|
williamr@2
|
318 |
k = 0;
|
williamr@2
|
319 |
}
|
williamr@2
|
320 |
|
williamr@2
|
321 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(0); }
|
williamr@2
|
322 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return result_type(1); }
|
williamr@2
|
323 |
|
williamr@2
|
324 |
result_type operator()()
|
williamr@2
|
325 |
{
|
williamr@2
|
326 |
int short_index = k - short_lag;
|
williamr@2
|
327 |
if(short_index < 0)
|
williamr@2
|
328 |
short_index += long_lag;
|
williamr@2
|
329 |
RealType delta = x[short_index] - x[k] - carry;
|
williamr@2
|
330 |
if(delta < 0) {
|
williamr@2
|
331 |
delta += RealType(1);
|
williamr@2
|
332 |
carry = RealType(1)/_modulus;
|
williamr@2
|
333 |
} else {
|
williamr@2
|
334 |
carry = 0;
|
williamr@2
|
335 |
}
|
williamr@2
|
336 |
x[k] = delta;
|
williamr@2
|
337 |
++k;
|
williamr@2
|
338 |
if(k >= long_lag)
|
williamr@2
|
339 |
k = 0;
|
williamr@2
|
340 |
return delta;
|
williamr@2
|
341 |
}
|
williamr@2
|
342 |
|
williamr@2
|
343 |
static bool validation(result_type x)
|
williamr@2
|
344 |
{ return x == val/pow(RealType(2), word_size); }
|
williamr@2
|
345 |
|
williamr@2
|
346 |
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
|
williamr@2
|
347 |
|
williamr@2
|
348 |
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
williamr@2
|
349 |
template<class CharT, class Traits>
|
williamr@2
|
350 |
friend std::basic_ostream<CharT,Traits>&
|
williamr@2
|
351 |
operator<<(std::basic_ostream<CharT,Traits>& os,
|
williamr@2
|
352 |
const subtract_with_carry_01& f)
|
williamr@2
|
353 |
{
|
williamr@2
|
354 |
#ifndef BOOST_NO_STDC_NAMESPACE
|
williamr@2
|
355 |
// allow for Koenig lookup
|
williamr@2
|
356 |
using std::pow;
|
williamr@2
|
357 |
#endif
|
williamr@2
|
358 |
std::ios_base::fmtflags oldflags = os.flags(os.dec | os.fixed | os.left);
|
williamr@2
|
359 |
for(unsigned int j = 0; j < f.long_lag; ++j)
|
williamr@2
|
360 |
os << (f.compute(j) * f._modulus) << " ";
|
williamr@2
|
361 |
os << (f.carry * f._modulus);
|
williamr@2
|
362 |
os.flags(oldflags);
|
williamr@2
|
363 |
return os;
|
williamr@2
|
364 |
}
|
williamr@2
|
365 |
|
williamr@2
|
366 |
template<class CharT, class Traits>
|
williamr@2
|
367 |
friend std::basic_istream<CharT,Traits>&
|
williamr@2
|
368 |
operator>>(std::basic_istream<CharT,Traits>& is, subtract_with_carry_01& f)
|
williamr@2
|
369 |
{
|
williamr@2
|
370 |
# ifdef BOOST_RANDOM_EXTRACT_SWC_01
|
williamr@2
|
371 |
detail::extract_subtract_with_carry_01(is, f, f.carry, f.x, f._modulus);
|
williamr@2
|
372 |
# else
|
williamr@2
|
373 |
// MSVC (up to 7.1) and Borland (up to 5.64) don't handle the template type
|
williamr@2
|
374 |
// parameter "RealType" available from the class template scope, so use
|
williamr@2
|
375 |
// the member typedef
|
williamr@2
|
376 |
typename subtract_with_carry_01::result_type value;
|
williamr@2
|
377 |
for(unsigned int j = 0; j < long_lag; ++j) {
|
williamr@2
|
378 |
is >> value >> std::ws;
|
williamr@2
|
379 |
f.x[j] = value / f._modulus;
|
williamr@2
|
380 |
}
|
williamr@2
|
381 |
is >> value >> std::ws;
|
williamr@2
|
382 |
f.carry = value / f._modulus;
|
williamr@2
|
383 |
# endif
|
williamr@2
|
384 |
f.k = 0;
|
williamr@2
|
385 |
return is;
|
williamr@2
|
386 |
}
|
williamr@2
|
387 |
#endif
|
williamr@2
|
388 |
|
williamr@2
|
389 |
friend bool operator==(const subtract_with_carry_01& x,
|
williamr@2
|
390 |
const subtract_with_carry_01& y)
|
williamr@2
|
391 |
{
|
williamr@2
|
392 |
for(unsigned int j = 0; j < r; ++j)
|
williamr@2
|
393 |
if(x.compute(j) != y.compute(j))
|
williamr@2
|
394 |
return false;
|
williamr@2
|
395 |
return true;
|
williamr@2
|
396 |
}
|
williamr@2
|
397 |
|
williamr@2
|
398 |
friend bool operator!=(const subtract_with_carry_01& x,
|
williamr@2
|
399 |
const subtract_with_carry_01& y)
|
williamr@2
|
400 |
{ return !(x == y); }
|
williamr@2
|
401 |
#else
|
williamr@2
|
402 |
// Use a member function; Streamable concept not supported.
|
williamr@2
|
403 |
bool operator==(const subtract_with_carry_01& rhs) const
|
williamr@2
|
404 |
{
|
williamr@2
|
405 |
for(unsigned int j = 0; j < r; ++j)
|
williamr@2
|
406 |
if(compute(j) != rhs.compute(j))
|
williamr@2
|
407 |
return false;
|
williamr@2
|
408 |
return true;
|
williamr@2
|
409 |
}
|
williamr@2
|
410 |
|
williamr@2
|
411 |
bool operator!=(const subtract_with_carry_01& rhs) const
|
williamr@2
|
412 |
{ return !(*this == rhs); }
|
williamr@2
|
413 |
#endif
|
williamr@2
|
414 |
|
williamr@2
|
415 |
private:
|
williamr@2
|
416 |
RealType compute(unsigned int index) const;
|
williamr@2
|
417 |
unsigned int k;
|
williamr@2
|
418 |
RealType carry;
|
williamr@2
|
419 |
RealType x[long_lag];
|
williamr@2
|
420 |
RealType _modulus;
|
williamr@2
|
421 |
};
|
williamr@2
|
422 |
|
williamr@2
|
423 |
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
williamr@2
|
424 |
// A definition is required even for integral static constants
|
williamr@2
|
425 |
template<class RealType, int w, unsigned int s, unsigned int r, int val>
|
williamr@2
|
426 |
const bool subtract_with_carry_01<RealType, w, s, r, val>::has_fixed_range;
|
williamr@2
|
427 |
template<class RealType, int w, unsigned int s, unsigned int r, int val>
|
williamr@2
|
428 |
const int subtract_with_carry_01<RealType, w, s, r, val>::word_size;
|
williamr@2
|
429 |
template<class RealType, int w, unsigned int s, unsigned int r, int val>
|
williamr@2
|
430 |
const unsigned int subtract_with_carry_01<RealType, w, s, r, val>::long_lag;
|
williamr@2
|
431 |
template<class RealType, int w, unsigned int s, unsigned int r, int val>
|
williamr@2
|
432 |
const unsigned int subtract_with_carry_01<RealType, w, s, r, val>::short_lag;
|
williamr@2
|
433 |
#endif
|
williamr@2
|
434 |
|
williamr@2
|
435 |
template<class RealType, int w, unsigned int s, unsigned int r, int val>
|
williamr@2
|
436 |
RealType subtract_with_carry_01<RealType, w, s, r, val>::compute(unsigned int index) const
|
williamr@2
|
437 |
{
|
williamr@2
|
438 |
return x[(k+index) % long_lag];
|
williamr@2
|
439 |
}
|
williamr@2
|
440 |
|
williamr@2
|
441 |
|
williamr@2
|
442 |
} // namespace random
|
williamr@2
|
443 |
} // namespace boost
|
williamr@2
|
444 |
|
williamr@2
|
445 |
#endif // BOOST_RANDOM_SUBTRACT_WITH_CARRY_HPP
|