williamr@2
|
1 |
/* boost random/xor_combine.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: xor_combine.hpp,v 1.13 2005/05/21 15:57:00 dgregor Exp $
|
williamr@2
|
11 |
*
|
williamr@2
|
12 |
*/
|
williamr@2
|
13 |
|
williamr@2
|
14 |
#ifndef BOOST_RANDOM_XOR_COMBINE_HPP
|
williamr@2
|
15 |
#define BOOST_RANDOM_XOR_COMBINE_HPP
|
williamr@2
|
16 |
|
williamr@2
|
17 |
#include <iostream>
|
williamr@2
|
18 |
#include <cassert>
|
williamr@2
|
19 |
#include <algorithm> // for std::min and std::max
|
williamr@2
|
20 |
#include <boost/config.hpp>
|
williamr@2
|
21 |
#include <boost/limits.hpp>
|
williamr@2
|
22 |
#include <boost/static_assert.hpp>
|
williamr@2
|
23 |
#include <boost/cstdint.hpp> // uint32_t
|
williamr@2
|
24 |
|
williamr@2
|
25 |
|
williamr@2
|
26 |
namespace boost {
|
williamr@2
|
27 |
namespace random {
|
williamr@2
|
28 |
|
williamr@2
|
29 |
template<class URNG1, int s1, class URNG2, int s2,
|
williamr@2
|
30 |
#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
|
williamr@2
|
31 |
typename URNG1::result_type
|
williamr@2
|
32 |
#else
|
williamr@2
|
33 |
uint32_t
|
williamr@2
|
34 |
#endif
|
williamr@2
|
35 |
val = 0>
|
williamr@2
|
36 |
class xor_combine
|
williamr@2
|
37 |
{
|
williamr@2
|
38 |
public:
|
williamr@2
|
39 |
typedef URNG1 base1_type;
|
williamr@2
|
40 |
typedef URNG2 base2_type;
|
williamr@2
|
41 |
typedef typename base1_type::result_type result_type;
|
williamr@2
|
42 |
|
williamr@2
|
43 |
BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
|
williamr@2
|
44 |
BOOST_STATIC_CONSTANT(int, shift1 = s1);
|
williamr@2
|
45 |
BOOST_STATIC_CONSTANT(int, shfit2 = s2);
|
williamr@2
|
46 |
|
williamr@2
|
47 |
xor_combine() : _rng1(), _rng2()
|
williamr@2
|
48 |
{ }
|
williamr@2
|
49 |
xor_combine(const base1_type & rng1, const base2_type & rng2)
|
williamr@2
|
50 |
: _rng1(rng1), _rng2(rng2) { }
|
williamr@2
|
51 |
template<class It> xor_combine(It& first, It last)
|
williamr@2
|
52 |
: _rng1(first, last), _rng2( /* advanced by other call */ first, last) { }
|
williamr@2
|
53 |
void seed() { _rng1.seed(); _rng2.seed(); }
|
williamr@2
|
54 |
template<class It> void seed(It& first, It last)
|
williamr@2
|
55 |
{
|
williamr@2
|
56 |
_rng1.seed(first, last);
|
williamr@2
|
57 |
_rng2.seed(first, last);
|
williamr@2
|
58 |
}
|
williamr@2
|
59 |
|
williamr@2
|
60 |
const base1_type& base1() { return _rng1; }
|
williamr@2
|
61 |
const base2_type& base2() { return _rng2; }
|
williamr@2
|
62 |
|
williamr@2
|
63 |
result_type operator()()
|
williamr@2
|
64 |
{
|
williamr@2
|
65 |
// MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
|
williamr@2
|
66 |
#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
|
williamr@2
|
67 |
BOOST_STATIC_ASSERT(std::numeric_limits<typename base1_type::result_type>::is_integer);
|
williamr@2
|
68 |
BOOST_STATIC_ASSERT(std::numeric_limits<typename base2_type::result_type>::is_integer);
|
williamr@2
|
69 |
BOOST_STATIC_ASSERT(std::numeric_limits<typename base1_type::result_type>::digits >= std::numeric_limits<typename base2_type::result_type>::digits);
|
williamr@2
|
70 |
#endif
|
williamr@2
|
71 |
return (_rng1() << s1) ^ (_rng2() << s2);
|
williamr@2
|
72 |
}
|
williamr@2
|
73 |
|
williamr@2
|
74 |
result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::min BOOST_PREVENT_MACRO_SUBSTITUTION((_rng1.min)(), (_rng2.min)()); }
|
williamr@2
|
75 |
result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::max BOOST_PREVENT_MACRO_SUBSTITUTION((_rng1.min)(), (_rng2.max)()); }
|
williamr@2
|
76 |
static bool validation(result_type x) { return val == x; }
|
williamr@2
|
77 |
|
williamr@2
|
78 |
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
|
williamr@2
|
79 |
|
williamr@2
|
80 |
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
williamr@2
|
81 |
template<class CharT, class Traits>
|
williamr@2
|
82 |
friend std::basic_ostream<CharT,Traits>&
|
williamr@2
|
83 |
operator<<(std::basic_ostream<CharT,Traits>& os, const xor_combine& s)
|
williamr@2
|
84 |
{
|
williamr@2
|
85 |
os << s._rng1 << " " << s._rng2 << " ";
|
williamr@2
|
86 |
return os;
|
williamr@2
|
87 |
}
|
williamr@2
|
88 |
|
williamr@2
|
89 |
template<class CharT, class Traits>
|
williamr@2
|
90 |
friend std::basic_istream<CharT,Traits>&
|
williamr@2
|
91 |
operator>>(std::basic_istream<CharT,Traits>& is, xor_combine& s)
|
williamr@2
|
92 |
{
|
williamr@2
|
93 |
is >> s._rng1 >> std::ws >> s._rng2 >> std::ws;
|
williamr@2
|
94 |
return is;
|
williamr@2
|
95 |
}
|
williamr@2
|
96 |
#endif
|
williamr@2
|
97 |
|
williamr@2
|
98 |
friend bool operator==(const xor_combine& x, const xor_combine& y)
|
williamr@2
|
99 |
{ return x._rng1 == y._rng1 && x._rng2 == y._rng2; }
|
williamr@2
|
100 |
friend bool operator!=(const xor_combine& x, const xor_combine& y)
|
williamr@2
|
101 |
{ return !(x == y); }
|
williamr@2
|
102 |
#else
|
williamr@2
|
103 |
// Use a member function; Streamable concept not supported.
|
williamr@2
|
104 |
bool operator==(const xor_combine& rhs) const
|
williamr@2
|
105 |
{ return _rng1 == rhs._rng1 && _rng2 == rhs._rng2; }
|
williamr@2
|
106 |
bool operator!=(const xor_combine& rhs) const
|
williamr@2
|
107 |
{ return !(*this == rhs); }
|
williamr@2
|
108 |
#endif
|
williamr@2
|
109 |
|
williamr@2
|
110 |
private:
|
williamr@2
|
111 |
base1_type _rng1;
|
williamr@2
|
112 |
base2_type _rng2;
|
williamr@2
|
113 |
};
|
williamr@2
|
114 |
|
williamr@2
|
115 |
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
williamr@2
|
116 |
// A definition is required even for integral static constants
|
williamr@2
|
117 |
template<class URNG1, int s1, class URNG2, int s2,
|
williamr@2
|
118 |
#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
|
williamr@2
|
119 |
typename URNG1::result_type
|
williamr@2
|
120 |
#else
|
williamr@2
|
121 |
uint32_t
|
williamr@2
|
122 |
#endif
|
williamr@2
|
123 |
val>
|
williamr@2
|
124 |
const bool xor_combine<URNG1, s1, URNG2, s2, val>::has_fixed_range;
|
williamr@2
|
125 |
#endif
|
williamr@2
|
126 |
|
williamr@2
|
127 |
} // namespace random
|
williamr@2
|
128 |
} // namespace boost
|
williamr@2
|
129 |
|
williamr@2
|
130 |
#endif // BOOST_RANDOM_XOR_COMBINE_HPP
|