williamr@2: /* boost random/xor_combine.hpp header file
williamr@2:  *
williamr@2:  * Copyright Jens Maurer 2002
williamr@2:  * Distributed under the Boost Software License, Version 1.0. (See
williamr@2:  * accompanying file LICENSE_1_0.txt or copy at
williamr@2:  * http://www.boost.org/LICENSE_1_0.txt)
williamr@2:  *
williamr@2:  * See http://www.boost.org for most recent version including documentation.
williamr@2:  *
williamr@2:  * $Id: xor_combine.hpp,v 1.13 2005/05/21 15:57:00 dgregor Exp $
williamr@2:  *
williamr@2:  */
williamr@2: 
williamr@2: #ifndef BOOST_RANDOM_XOR_COMBINE_HPP
williamr@2: #define BOOST_RANDOM_XOR_COMBINE_HPP
williamr@2: 
williamr@2: #include <iostream>
williamr@2: #include <cassert>
williamr@2: #include <algorithm> // for std::min and std::max
williamr@2: #include <boost/config.hpp>
williamr@2: #include <boost/limits.hpp>
williamr@2: #include <boost/static_assert.hpp>
williamr@2: #include <boost/cstdint.hpp>     // uint32_t
williamr@2: 
williamr@2: 
williamr@2: namespace boost {
williamr@2: namespace random {
williamr@2: 
williamr@2: template<class URNG1, int s1, class URNG2, int s2,
williamr@2: #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
williamr@2:   typename URNG1::result_type 
williamr@2: #else
williamr@2:   uint32_t
williamr@2: #endif
williamr@2:   val = 0>
williamr@2: class xor_combine
williamr@2: {
williamr@2: public:
williamr@2:   typedef URNG1 base1_type;
williamr@2:   typedef URNG2 base2_type;
williamr@2:   typedef typename base1_type::result_type result_type;
williamr@2: 
williamr@2:   BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
williamr@2:   BOOST_STATIC_CONSTANT(int, shift1 = s1);
williamr@2:   BOOST_STATIC_CONSTANT(int, shfit2 = s2);
williamr@2: 
williamr@2:   xor_combine() : _rng1(), _rng2()
williamr@2:   { }
williamr@2:   xor_combine(const base1_type & rng1, const base2_type & rng2)
williamr@2:     : _rng1(rng1), _rng2(rng2) { }
williamr@2:   template<class It> xor_combine(It& first, It last)
williamr@2:     : _rng1(first, last), _rng2( /* advanced by other call */ first, last) { }
williamr@2:   void seed() { _rng1.seed(); _rng2.seed(); }
williamr@2:   template<class It> void seed(It& first, It last)
williamr@2:   {
williamr@2:     _rng1.seed(first, last);
williamr@2:     _rng2.seed(first, last);
williamr@2:   }
williamr@2: 
williamr@2:   const base1_type& base1() { return _rng1; }
williamr@2:   const base2_type& base2() { return _rng2; }
williamr@2: 
williamr@2:   result_type operator()()
williamr@2:   {
williamr@2:     // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
williamr@2: #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
williamr@2:     BOOST_STATIC_ASSERT(std::numeric_limits<typename base1_type::result_type>::is_integer);
williamr@2:     BOOST_STATIC_ASSERT(std::numeric_limits<typename base2_type::result_type>::is_integer);
williamr@2:     BOOST_STATIC_ASSERT(std::numeric_limits<typename base1_type::result_type>::digits >= std::numeric_limits<typename base2_type::result_type>::digits);
williamr@2: #endif
williamr@2:     return (_rng1() << s1) ^ (_rng2() << s2);
williamr@2:   }
williamr@2: 
williamr@2:   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::min BOOST_PREVENT_MACRO_SUBSTITUTION((_rng1.min)(), (_rng2.min)()); }
williamr@2:   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::max BOOST_PREVENT_MACRO_SUBSTITUTION((_rng1.min)(), (_rng2.max)()); }
williamr@2:   static bool validation(result_type x) { return val == x; }
williamr@2: 
williamr@2: #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
williamr@2: 
williamr@2: #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
williamr@2:   template<class CharT, class Traits>
williamr@2:   friend std::basic_ostream<CharT,Traits>&
williamr@2:   operator<<(std::basic_ostream<CharT,Traits>& os, const xor_combine& s)
williamr@2:   {
williamr@2:     os << s._rng1 << " " << s._rng2 << " ";
williamr@2:     return os;
williamr@2:   }
williamr@2: 
williamr@2:   template<class CharT, class Traits>
williamr@2:   friend std::basic_istream<CharT,Traits>&
williamr@2:   operator>>(std::basic_istream<CharT,Traits>& is, xor_combine& s)
williamr@2:   {
williamr@2:     is >> s._rng1 >> std::ws >> s._rng2 >> std::ws;
williamr@2:     return is;
williamr@2:   }
williamr@2: #endif
williamr@2: 
williamr@2:   friend bool operator==(const xor_combine& x, const xor_combine& y)
williamr@2:   { return x._rng1 == y._rng1 && x._rng2 == y._rng2; }
williamr@2:   friend bool operator!=(const xor_combine& x, const xor_combine& y)
williamr@2:   { return !(x == y); }
williamr@2: #else
williamr@2:   // Use a member function; Streamable concept not supported.
williamr@2:   bool operator==(const xor_combine& rhs) const
williamr@2:   { return _rng1 == rhs._rng1 && _rng2 == rhs._rng2; }
williamr@2:   bool operator!=(const xor_combine& rhs) const
williamr@2:   { return !(*this == rhs); }
williamr@2: #endif
williamr@2: 
williamr@2: private:
williamr@2:   base1_type _rng1;
williamr@2:   base2_type _rng2;
williamr@2: };
williamr@2: 
williamr@2: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
williamr@2: //  A definition is required even for integral static constants
williamr@2: template<class URNG1, int s1, class URNG2, int s2,
williamr@2: #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
williamr@2:   typename URNG1::result_type 
williamr@2: #else
williamr@2:   uint32_t
williamr@2: #endif
williamr@2:   val>
williamr@2: const bool xor_combine<URNG1, s1, URNG2, s2, val>::has_fixed_range;
williamr@2: #endif
williamr@2: 
williamr@2: } // namespace random
williamr@2: } // namespace boost
williamr@2: 
williamr@2: #endif // BOOST_RANDOM_XOR_COMBINE_HPP