source: NonGTP/Boost/boost/random/xor_combine.hpp @ 857

Revision 857, 4.2 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]1/* boost random/xor_combine.hpp header file
2 *
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)
7 *
8 * See http://www.boost.org for most recent version including documentation.
9 *
10 * $Id: xor_combine.hpp,v 1.13 2005/05/21 15:57:00 dgregor Exp $
11 *
12 */
13
14#ifndef BOOST_RANDOM_XOR_COMBINE_HPP
15#define BOOST_RANDOM_XOR_COMBINE_HPP
16
17#include <iostream>
18#include <cassert>
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
24
25
26namespace boost {
27namespace random {
28
29template<class URNG1, int s1, class URNG2, int s2,
30#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
31  typename URNG1::result_type
32#else
33  uint32_t
34#endif
35  val = 0>
36class xor_combine
37{
38public:
39  typedef URNG1 base1_type;
40  typedef URNG2 base2_type;
41  typedef typename base1_type::result_type result_type;
42
43  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
44  BOOST_STATIC_CONSTANT(int, shift1 = s1);
45  BOOST_STATIC_CONSTANT(int, shfit2 = s2);
46
47  xor_combine() : _rng1(), _rng2()
48  { }
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)
55  {
56    _rng1.seed(first, last);
57    _rng2.seed(first, last);
58  }
59
60  const base1_type& base1() { return _rng1; }
61  const base2_type& base2() { return _rng2; }
62
63  result_type operator()()
64  {
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);
70#endif
71    return (_rng1() << s1) ^ (_rng2() << s2);
72  }
73
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; }
77
78#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
79
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)
84  {
85    os << s._rng1 << " " << s._rng2 << " ";
86    return os;
87  }
88
89  template<class CharT, class Traits>
90  friend std::basic_istream<CharT,Traits>&
91  operator>>(std::basic_istream<CharT,Traits>& is, xor_combine& s)
92  {
93    is >> s._rng1 >> std::ws >> s._rng2 >> std::ws;
94    return is;
95  }
96#endif
97
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); }
102#else
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); }
108#endif
109
110private:
111  base1_type _rng1;
112  base2_type _rng2;
113};
114
115#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
116//  A definition is required even for integral static constants
117template<class URNG1, int s1, class URNG2, int s2,
118#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
119  typename URNG1::result_type
120#else
121  uint32_t
122#endif
123  val>
124const bool xor_combine<URNG1, s1, URNG2, s2, val>::has_fixed_range;
125#endif
126
127} // namespace random
128} // namespace boost
129
130#endif // BOOST_RANDOM_XOR_COMBINE_HPP
Note: See TracBrowser for help on using the repository browser.