1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/random/linear_congruential.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,258 @@
1.4 +/* boost random/linear_congruential.hpp header file
1.5 + *
1.6 + * Copyright Jens Maurer 2000-2001
1.7 + * Distributed under the Boost Software License, Version 1.0. (See
1.8 + * accompanying file LICENSE_1_0.txt or copy at
1.9 + * http://www.boost.org/LICENSE_1_0.txt)
1.10 + *
1.11 + * See http://www.boost.org for most recent version including documentation.
1.12 + *
1.13 + * $Id: linear_congruential.hpp,v 1.22 2005/05/21 15:57:00 dgregor Exp $
1.14 + *
1.15 + * Revision history
1.16 + * 2001-02-18 moved to individual header files
1.17 + */
1.18 +
1.19 +#ifndef BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
1.20 +#define BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
1.21 +
1.22 +#include <iostream>
1.23 +#include <cassert>
1.24 +#include <stdexcept>
1.25 +#include <boost/config.hpp>
1.26 +#include <boost/limits.hpp>
1.27 +#include <boost/static_assert.hpp>
1.28 +#include <boost/random/detail/const_mod.hpp>
1.29 +#include <boost/detail/workaround.hpp>
1.30 +
1.31 +namespace boost {
1.32 +namespace random {
1.33 +
1.34 +// compile-time configurable linear congruential generator
1.35 +template<class IntType, IntType a, IntType c, IntType m, IntType val>
1.36 +class linear_congruential
1.37 +{
1.38 +public:
1.39 + typedef IntType result_type;
1.40 +#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
1.41 + static const bool has_fixed_range = true;
1.42 + static const result_type min_value = ( c == 0 ? 1 : 0 );
1.43 + static const result_type max_value = m-1;
1.44 +#else
1.45 + BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
1.46 +#endif
1.47 + BOOST_STATIC_CONSTANT(IntType, multiplier = a);
1.48 + BOOST_STATIC_CONSTANT(IntType, increment = c);
1.49 + BOOST_STATIC_CONSTANT(IntType, modulus = m);
1.50 +
1.51 + // MSVC 6 and possibly others crash when encountering complicated integral
1.52 + // constant expressions. Avoid the check for now.
1.53 + // BOOST_STATIC_ASSERT(m == 0 || a < m);
1.54 + // BOOST_STATIC_ASSERT(m == 0 || c < m);
1.55 +
1.56 + explicit linear_congruential(IntType x0 = 1)
1.57 + : _modulus(modulus), _x(_modulus ? (x0 % _modulus) : x0)
1.58 + {
1.59 + assert(c || x0); /* if c == 0 and x(0) == 0 then x(n) = 0 for all n */
1.60 + // overflow check
1.61 + // disabled because it gives spurious "divide by zero" gcc warnings
1.62 + // assert(m == 0 || (a*(m-1)+c) % m == (c < a ? c-a+m : c-a));
1.63 +
1.64 + // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
1.65 +#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
1.66 + BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
1.67 +#endif
1.68 + }
1.69 +
1.70 + template<class It>
1.71 + linear_congruential(It& first, It last) { seed(first, last); }
1.72 +
1.73 + // compiler-generated copy constructor and assignment operator are fine
1.74 + void seed(IntType x0 = 1)
1.75 + {
1.76 + assert(c || x0);
1.77 + _x = (_modulus ? (x0 % _modulus) : x0);
1.78 + }
1.79 +
1.80 + template<class It>
1.81 + void seed(It& first, It last)
1.82 + {
1.83 + if(first == last)
1.84 + throw std::invalid_argument("linear_congruential::seed");
1.85 + IntType value = *first++;
1.86 + _x = (_modulus ? (value % _modulus) : value);
1.87 + }
1.88 +
1.89 + result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return c == 0 ? 1 : 0; }
1.90 + result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return modulus-1; }
1.91 +
1.92 + IntType operator()()
1.93 + {
1.94 + _x = const_mod<IntType, m>::mult_add(a, _x, c);
1.95 + return _x;
1.96 + }
1.97 +
1.98 + static bool validation(IntType x) { return val == x; }
1.99 +
1.100 +#ifdef BOOST_NO_OPERATORS_IN_NAMESPACE
1.101 +
1.102 + // Use a member function; Streamable concept not supported.
1.103 + bool operator==(const linear_congruential& rhs) const
1.104 + { return _x == rhs._x; }
1.105 + bool operator!=(const linear_congruential& rhs) const
1.106 + { return !(*this == rhs); }
1.107 +
1.108 +#else
1.109 + friend bool operator==(const linear_congruential& x,
1.110 + const linear_congruential& y)
1.111 + { return x._x == y._x; }
1.112 + friend bool operator!=(const linear_congruential& x,
1.113 + const linear_congruential& y)
1.114 + { return !(x == y); }
1.115 +
1.116 +#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
1.117 + template<class CharT, class Traits>
1.118 + friend std::basic_ostream<CharT,Traits>&
1.119 + operator<<(std::basic_ostream<CharT,Traits>& os,
1.120 + const linear_congruential& lcg)
1.121 + {
1.122 + return os << lcg._x;
1.123 + }
1.124 +
1.125 + template<class CharT, class Traits>
1.126 + friend std::basic_istream<CharT,Traits>&
1.127 + operator>>(std::basic_istream<CharT,Traits>& is,
1.128 + linear_congruential& lcg)
1.129 + {
1.130 + return is >> lcg._x;
1.131 + }
1.132 +
1.133 +private:
1.134 +#endif
1.135 +#endif
1.136 +
1.137 + IntType _modulus; // work-around for gcc "divide by zero" warning in ctor
1.138 + IntType _x;
1.139 +};
1.140 +
1.141 +// probably needs the "no native streams" caveat for STLPort
1.142 +#if !defined(__SGI_STL_PORT) && BOOST_WORKAROUND(__GNUC__, == 2)
1.143 +template<class IntType, IntType a, IntType c, IntType m, IntType val>
1.144 +std::ostream&
1.145 +operator<<(std::ostream& os,
1.146 + const linear_congruential<IntType,a,c,m,val>& lcg)
1.147 +{
1.148 + return os << lcg._x;
1.149 +}
1.150 +
1.151 +template<class IntType, IntType a, IntType c, IntType m, IntType val>
1.152 +std::istream&
1.153 +operator>>(std::istream& is,
1.154 + linear_congruential<IntType,a,c,m,val>& lcg)
1.155 +{
1.156 + return is >> lcg._x;
1.157 +}
1.158 +#elif defined(BOOST_NO_OPERATORS_IN_NAMESPACE) || defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
1.159 +template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
1.160 +std::basic_ostream<CharT,Traits>&
1.161 +operator<<(std::basic_ostream<CharT,Traits>& os,
1.162 + const linear_congruential<IntType,a,c,m,val>& lcg)
1.163 +{
1.164 + return os << lcg._x;
1.165 +}
1.166 +
1.167 +template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
1.168 +std::basic_istream<CharT,Traits>&
1.169 +operator>>(std::basic_istream<CharT,Traits>& is,
1.170 + linear_congruential<IntType,a,c,m,val>& lcg)
1.171 +{
1.172 + return is >> lcg._x;
1.173 +}
1.174 +#endif
1.175 +
1.176 +#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
1.177 +// A definition is required even for integral static constants
1.178 +template<class IntType, IntType a, IntType c, IntType m, IntType val>
1.179 +const bool linear_congruential<IntType, a, c, m, val>::has_fixed_range;
1.180 +template<class IntType, IntType a, IntType c, IntType m, IntType val>
1.181 +const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::min_value;
1.182 +template<class IntType, IntType a, IntType c, IntType m, IntType val>
1.183 +const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::max_value;
1.184 +template<class IntType, IntType a, IntType c, IntType m, IntType val>
1.185 +const IntType linear_congruential<IntType,a,c,m,val>::modulus;
1.186 +#endif
1.187 +
1.188 +} // namespace random
1.189 +
1.190 +// validation values from the publications
1.191 +typedef random::linear_congruential<int32_t, 16807, 0, 2147483647,
1.192 + 1043618065> minstd_rand0;
1.193 +typedef random::linear_congruential<int32_t, 48271, 0, 2147483647,
1.194 + 399268537> minstd_rand;
1.195 +
1.196 +
1.197 +#if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
1.198 +// emulate the lrand48() C library function; requires support for uint64_t
1.199 +class rand48
1.200 +{
1.201 +public:
1.202 + typedef int32_t result_type;
1.203 +#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
1.204 + static const bool has_fixed_range = true;
1.205 + static const int32_t min_value = 0;
1.206 + static const int32_t max_value = integer_traits<int32_t>::const_max;
1.207 +#else
1.208 + enum { has_fixed_range = false };
1.209 +#endif
1.210 + int32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
1.211 + int32_t max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::numeric_limits<int32_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION (); }
1.212 +
1.213 + explicit rand48(int32_t x0 = 1) : lcf(cnv(x0)) { }
1.214 + explicit rand48(uint64_t x0) : lcf(x0) { }
1.215 + template<class It> rand48(It& first, It last) : lcf(first, last) { }
1.216 + // compiler-generated copy ctor and assignment operator are fine
1.217 + void seed(int32_t x0 = 1) { lcf.seed(cnv(x0)); }
1.218 + void seed(uint64_t x0) { lcf.seed(x0); }
1.219 + template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
1.220 +
1.221 + int32_t operator()() { return static_cast<int32_t>(lcf() >> 17); }
1.222 + // by experiment from lrand48()
1.223 + static bool validation(int32_t x) { return x == 1993516219; }
1.224 +
1.225 +#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
1.226 +
1.227 +#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
1.228 + template<class CharT,class Traits>
1.229 + friend std::basic_ostream<CharT,Traits>&
1.230 + operator<<(std::basic_ostream<CharT,Traits>& os, const rand48& r)
1.231 + { os << r.lcf; return os; }
1.232 +
1.233 + template<class CharT,class Traits>
1.234 + friend std::basic_istream<CharT,Traits>&
1.235 + operator>>(std::basic_istream<CharT,Traits>& is, rand48& r)
1.236 + { is >> r.lcf; return is; }
1.237 +#endif
1.238 +
1.239 + friend bool operator==(const rand48& x, const rand48& y)
1.240 + { return x.lcf == y.lcf; }
1.241 + friend bool operator!=(const rand48& x, const rand48& y)
1.242 + { return !(x == y); }
1.243 +#else
1.244 + // Use a member function; Streamable concept not supported.
1.245 + bool operator==(const rand48& rhs) const
1.246 + { return lcf == rhs.lcf; }
1.247 + bool operator!=(const rand48& rhs) const
1.248 + { return !(*this == rhs); }
1.249 +#endif
1.250 +private:
1.251 + random::linear_congruential<uint64_t,
1.252 + uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32), // xxxxULL is not portable
1.253 + 0xB, uint64_t(1)<<48, /* unknown */ 0> lcf;
1.254 + static uint64_t cnv(int32_t x)
1.255 + { return (static_cast<uint64_t>(x) << 16) | 0x330e; }
1.256 +};
1.257 +#endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */
1.258 +
1.259 +} // namespace boost
1.260 +
1.261 +#endif // BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP