1 /* boost random/xor_combine.hpp header file
\r
3 * Copyright Jens Maurer 2002
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: xor_combine.hpp 53871 2009-06-13 17:54:06Z steven_watanabe $
\r
14 #ifndef BOOST_RANDOM_XOR_COMBINE_HPP
\r
15 #define BOOST_RANDOM_XOR_COMBINE_HPP
\r
19 #include <algorithm> // for std::min and std::max
\r
20 #include <boost/config.hpp>
\r
21 #include <boost/limits.hpp>
\r
22 #include <boost/static_assert.hpp>
\r
23 #include <boost/cstdint.hpp> // uint32_t
\r
24 #include <boost/random/detail/config.hpp>
\r
30 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
\r
31 #define BOOST_RANDOM_VAL_TYPE typename URNG1::result_type
\r
33 #define BOOST_RANDOM_VAL_TYPE uint32_t
\r
36 template<class URNG1, int s1, class URNG2, int s2, BOOST_RANDOM_VAL_TYPE val = 0>
\r
40 typedef URNG1 base1_type;
\r
41 typedef URNG2 base2_type;
\r
42 typedef typename base1_type::result_type result_type;
\r
44 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
\r
45 BOOST_STATIC_CONSTANT(int, shift1 = s1);
\r
46 BOOST_STATIC_CONSTANT(int, shift2 = s2);
\r
48 xor_combine() : _rng1(), _rng2()
\r
50 xor_combine(const base1_type & rng1, const base2_type & rng2)
\r
51 : _rng1(rng1), _rng2(rng2) { }
\r
52 xor_combine(const result_type & v)
\r
53 : _rng1(v), _rng2(v) { }
\r
54 template<class It> xor_combine(It& first, It last)
\r
55 : _rng1(first, last), _rng2( /* advanced by other call */ first, last) { }
\r
56 void seed() { _rng1.seed(); _rng2.seed(); }
\r
57 void seed(const result_type & v) { _rng1.seed(v); _rng2.seed(v); }
\r
58 template<class It> void seed(It& first, It last)
\r
60 _rng1.seed(first, last);
\r
61 _rng2.seed(first, last);
\r
64 const base1_type& base1() { return _rng1; }
\r
65 const base2_type& base2() { return _rng2; }
\r
67 result_type operator()()
\r
69 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
\r
70 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
\r
71 BOOST_STATIC_ASSERT(std::numeric_limits<typename base1_type::result_type>::is_integer);
\r
72 BOOST_STATIC_ASSERT(std::numeric_limits<typename base2_type::result_type>::is_integer);
\r
73 BOOST_STATIC_ASSERT(std::numeric_limits<typename base1_type::result_type>::digits >= std::numeric_limits<typename base2_type::result_type>::digits);
\r
75 return (_rng1() << s1) ^ (_rng2() << s2);
\r
78 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::min BOOST_PREVENT_MACRO_SUBSTITUTION((_rng1.min)(), (_rng2.min)()); }
\r
79 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::max BOOST_PREVENT_MACRO_SUBSTITUTION((_rng1.min)(), (_rng2.max)()); }
\r
80 static bool validation(result_type x) { return val == x; }
\r
82 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
\r
84 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
85 template<class CharT, class Traits>
\r
86 friend std::basic_ostream<CharT,Traits>&
\r
87 operator<<(std::basic_ostream<CharT,Traits>& os, const xor_combine& s)
\r
89 os << s._rng1 << " " << s._rng2 << " ";
\r
93 template<class CharT, class Traits>
\r
94 friend std::basic_istream<CharT,Traits>&
\r
95 operator>>(std::basic_istream<CharT,Traits>& is, xor_combine& s)
\r
97 is >> s._rng1 >> std::ws >> s._rng2 >> std::ws;
\r
102 friend bool operator==(const xor_combine& x, const xor_combine& y)
\r
103 { return x._rng1 == y._rng1 && x._rng2 == y._rng2; }
\r
104 friend bool operator!=(const xor_combine& x, const xor_combine& y)
\r
105 { return !(x == y); }
\r
107 // Use a member function; Streamable concept not supported.
\r
108 bool operator==(const xor_combine& rhs) const
\r
109 { return _rng1 == rhs._rng1 && _rng2 == rhs._rng2; }
\r
110 bool operator!=(const xor_combine& rhs) const
\r
111 { return !(*this == rhs); }
\r
119 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
\r
120 // A definition is required even for integral static constants
\r
121 template<class URNG1, int s1, class URNG2, int s2, BOOST_RANDOM_VAL_TYPE val>
\r
122 const bool xor_combine<URNG1, s1, URNG2, s2, val>::has_fixed_range;
\r
123 template<class URNG1, int s1, class URNG2, int s2, BOOST_RANDOM_VAL_TYPE val>
\r
124 const int xor_combine<URNG1, s1, URNG2, s2, val>::shift1;
\r
125 template<class URNG1, int s1, class URNG2, int s2, BOOST_RANDOM_VAL_TYPE val>
\r
126 const int xor_combine<URNG1, s1, URNG2, s2, val>::shift2;
\r
129 #undef BOOST_RANDOM_VAL_TYPE
\r
131 } // namespace random
\r
132 } // namespace boost
\r
134 #endif // BOOST_RANDOM_XOR_COMBINE_HPP
\r