1 /* boost random/xor_combine.hpp header file
3 * Copyright Jens Maurer 2002
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: xor_combine.hpp,v 1.13 2005/05/21 15:57:00 dgregor Exp $
14 #ifndef BOOST_RANDOM_XOR_COMBINE_HPP
15 #define BOOST_RANDOM_XOR_COMBINE_HPP
19 #include <algorithm> // for std::min and std::max
20 #include <boost/config.hpp>
21 #include <boost/limits.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/cstdint.hpp> // uint32_t
29 template<class URNG1, int s1, class URNG2, int s2,
30 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
31 typename URNG1::result_type
39 typedef URNG1 base1_type;
40 typedef URNG2 base2_type;
41 typedef typename base1_type::result_type result_type;
43 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
44 BOOST_STATIC_CONSTANT(int, shift1 = s1);
45 BOOST_STATIC_CONSTANT(int, shfit2 = s2);
47 xor_combine() : _rng1(), _rng2()
49 xor_combine(const base1_type & rng1, const base2_type & rng2)
50 : _rng1(rng1), _rng2(rng2) { }
51 template<class It> xor_combine(It& first, It last)
52 : _rng1(first, last), _rng2( /* advanced by other call */ first, last) { }
53 void seed() { _rng1.seed(); _rng2.seed(); }
54 template<class It> void seed(It& first, It last)
56 _rng1.seed(first, last);
57 _rng2.seed(first, last);
60 const base1_type& base1() { return _rng1; }
61 const base2_type& base2() { return _rng2; }
63 result_type operator()()
65 // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
66 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
67 BOOST_STATIC_ASSERT(std::numeric_limits<typename base1_type::result_type>::is_integer);
68 BOOST_STATIC_ASSERT(std::numeric_limits<typename base2_type::result_type>::is_integer);
69 BOOST_STATIC_ASSERT(std::numeric_limits<typename base1_type::result_type>::digits >= std::numeric_limits<typename base2_type::result_type>::digits);
71 return (_rng1() << s1) ^ (_rng2() << s2);
74 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::min BOOST_PREVENT_MACRO_SUBSTITUTION((_rng1.min)(), (_rng2.min)()); }
75 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::max BOOST_PREVENT_MACRO_SUBSTITUTION((_rng1.min)(), (_rng2.max)()); }
76 static bool validation(result_type x) { return val == x; }
78 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
80 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
81 template<class CharT, class Traits>
82 friend std::basic_ostream<CharT,Traits>&
83 operator<<(std::basic_ostream<CharT,Traits>& os, const xor_combine& s)
85 os << s._rng1 << " " << s._rng2 << " ";
89 template<class CharT, class Traits>
90 friend std::basic_istream<CharT,Traits>&
91 operator>>(std::basic_istream<CharT,Traits>& is, xor_combine& s)
93 is >> s._rng1 >> std::ws >> s._rng2 >> std::ws;
98 friend bool operator==(const xor_combine& x, const xor_combine& y)
99 { return x._rng1 == y._rng1 && x._rng2 == y._rng2; }
100 friend bool operator!=(const xor_combine& x, const xor_combine& y)
101 { return !(x == y); }
103 // Use a member function; Streamable concept not supported.
104 bool operator==(const xor_combine& rhs) const
105 { return _rng1 == rhs._rng1 && _rng2 == rhs._rng2; }
106 bool operator!=(const xor_combine& rhs) const
107 { return !(*this == rhs); }
115 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
116 // A definition is required even for integral static constants
117 template<class URNG1, int s1, class URNG2, int s2,
118 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
119 typename URNG1::result_type
124 const bool xor_combine<URNG1, s1, URNG2, s2, val>::has_fixed_range;
127 } // namespace random
130 #endif // BOOST_RANDOM_XOR_COMBINE_HPP