epoc32/include/stdapis/boost/random/linear_congruential.hpp
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
permissions -rw-r--r--
Final list of Symbian^2 public API header files
williamr@2
     1
/* boost random/linear_congruential.hpp header file
williamr@2
     2
 *
williamr@2
     3
 * Copyright Jens Maurer 2000-2001
williamr@2
     4
 * Distributed under the Boost Software License, Version 1.0. (See
williamr@2
     5
 * accompanying file LICENSE_1_0.txt or copy at
williamr@2
     6
 * http://www.boost.org/LICENSE_1_0.txt)
williamr@2
     7
 *
williamr@2
     8
 * See http://www.boost.org for most recent version including documentation.
williamr@2
     9
 *
williamr@2
    10
 * $Id: linear_congruential.hpp,v 1.22 2005/05/21 15:57:00 dgregor Exp $
williamr@2
    11
 *
williamr@2
    12
 * Revision history
williamr@2
    13
 *  2001-02-18  moved to individual header files
williamr@2
    14
 */
williamr@2
    15
williamr@2
    16
#ifndef BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
williamr@2
    17
#define BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP
williamr@2
    18
williamr@2
    19
#include <iostream>
williamr@2
    20
#include <cassert>
williamr@2
    21
#include <stdexcept>
williamr@2
    22
#include <boost/config.hpp>
williamr@2
    23
#include <boost/limits.hpp>
williamr@2
    24
#include <boost/static_assert.hpp>
williamr@2
    25
#include <boost/random/detail/const_mod.hpp>
williamr@2
    26
#include <boost/detail/workaround.hpp>
williamr@2
    27
williamr@2
    28
namespace boost {
williamr@2
    29
namespace random {
williamr@2
    30
williamr@2
    31
// compile-time configurable linear congruential generator
williamr@2
    32
template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2
    33
class linear_congruential
williamr@2
    34
{
williamr@2
    35
public:
williamr@2
    36
  typedef IntType result_type;
williamr@2
    37
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
williamr@2
    38
  static const bool has_fixed_range = true;
williamr@2
    39
  static const result_type min_value = ( c == 0 ? 1 : 0 );
williamr@2
    40
  static const result_type max_value = m-1;
williamr@2
    41
#else
williamr@2
    42
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
williamr@2
    43
#endif
williamr@2
    44
  BOOST_STATIC_CONSTANT(IntType, multiplier = a);
williamr@2
    45
  BOOST_STATIC_CONSTANT(IntType, increment = c);
williamr@2
    46
  BOOST_STATIC_CONSTANT(IntType, modulus = m);
williamr@2
    47
williamr@2
    48
  // MSVC 6 and possibly others crash when encountering complicated integral
williamr@2
    49
  // constant expressions.  Avoid the check for now.
williamr@2
    50
  // BOOST_STATIC_ASSERT(m == 0 || a < m);
williamr@2
    51
  // BOOST_STATIC_ASSERT(m == 0 || c < m);
williamr@2
    52
williamr@2
    53
  explicit linear_congruential(IntType x0 = 1)
williamr@2
    54
    : _modulus(modulus), _x(_modulus ? (x0 % _modulus) : x0)
williamr@2
    55
  { 
williamr@2
    56
    assert(c || x0); /* if c == 0 and x(0) == 0 then x(n) = 0 for all n */
williamr@2
    57
    // overflow check
williamr@2
    58
    // disabled because it gives spurious "divide by zero" gcc warnings
williamr@2
    59
    // assert(m == 0 || (a*(m-1)+c) % m == (c < a ? c-a+m : c-a)); 
williamr@2
    60
williamr@2
    61
    // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
williamr@2
    62
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
williamr@2
    63
    BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
williamr@2
    64
#endif
williamr@2
    65
  }
williamr@2
    66
williamr@2
    67
  template<class It>
williamr@2
    68
  linear_congruential(It& first, It last) { seed(first, last); }
williamr@2
    69
williamr@2
    70
  // compiler-generated copy constructor and assignment operator are fine
williamr@2
    71
  void seed(IntType x0 = 1)
williamr@2
    72
  {
williamr@2
    73
    assert(c || x0);
williamr@2
    74
    _x = (_modulus ? (x0 % _modulus) : x0);
williamr@2
    75
  }
williamr@2
    76
williamr@2
    77
  template<class It>
williamr@2
    78
  void seed(It& first, It last)
williamr@2
    79
  {
williamr@2
    80
    if(first == last)
williamr@2
    81
      throw std::invalid_argument("linear_congruential::seed");
williamr@2
    82
    IntType value = *first++;
williamr@2
    83
    _x = (_modulus ? (value % _modulus) : value);
williamr@2
    84
  }
williamr@2
    85
williamr@2
    86
  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return c == 0 ? 1 : 0; }
williamr@2
    87
  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return modulus-1; }
williamr@2
    88
williamr@2
    89
  IntType operator()()
williamr@2
    90
  {
williamr@2
    91
    _x = const_mod<IntType, m>::mult_add(a, _x, c);
williamr@2
    92
    return _x;
williamr@2
    93
  }
williamr@2
    94
williamr@2
    95
  static bool validation(IntType x) { return val == x; }
williamr@2
    96
williamr@2
    97
#ifdef BOOST_NO_OPERATORS_IN_NAMESPACE
williamr@2
    98
    
williamr@2
    99
  // Use a member function; Streamable concept not supported.
williamr@2
   100
  bool operator==(const linear_congruential& rhs) const
williamr@2
   101
  { return _x == rhs._x; }
williamr@2
   102
  bool operator!=(const linear_congruential& rhs) const
williamr@2
   103
  { return !(*this == rhs); }
williamr@2
   104
williamr@2
   105
#else 
williamr@2
   106
  friend bool operator==(const linear_congruential& x,
williamr@2
   107
                         const linear_congruential& y)
williamr@2
   108
  { return x._x == y._x; }
williamr@2
   109
  friend bool operator!=(const linear_congruential& x,
williamr@2
   110
                         const linear_congruential& y)
williamr@2
   111
  { return !(x == y); }
williamr@2
   112
    
williamr@2
   113
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) && !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
williamr@2
   114
  template<class CharT, class Traits>
williamr@2
   115
  friend std::basic_ostream<CharT,Traits>&
williamr@2
   116
  operator<<(std::basic_ostream<CharT,Traits>& os,
williamr@2
   117
             const linear_congruential& lcg)
williamr@2
   118
  {
williamr@2
   119
    return os << lcg._x;
williamr@2
   120
  }
williamr@2
   121
williamr@2
   122
  template<class CharT, class Traits>
williamr@2
   123
  friend std::basic_istream<CharT,Traits>&
williamr@2
   124
  operator>>(std::basic_istream<CharT,Traits>& is,
williamr@2
   125
             linear_congruential& lcg)
williamr@2
   126
  {
williamr@2
   127
    return is >> lcg._x;
williamr@2
   128
  }
williamr@2
   129
 
williamr@2
   130
private:
williamr@2
   131
#endif
williamr@2
   132
#endif
williamr@2
   133
    
williamr@2
   134
  IntType _modulus;   // work-around for gcc "divide by zero" warning in ctor
williamr@2
   135
  IntType _x;
williamr@2
   136
};
williamr@2
   137
williamr@2
   138
// probably needs the "no native streams" caveat for STLPort
williamr@2
   139
#if !defined(__SGI_STL_PORT) && BOOST_WORKAROUND(__GNUC__, == 2)
williamr@2
   140
template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2
   141
std::ostream&
williamr@2
   142
operator<<(std::ostream& os,
williamr@2
   143
           const linear_congruential<IntType,a,c,m,val>& lcg)
williamr@2
   144
{
williamr@2
   145
    return os << lcg._x;
williamr@2
   146
}
williamr@2
   147
williamr@2
   148
template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2
   149
std::istream&
williamr@2
   150
operator>>(std::istream& is,
williamr@2
   151
           linear_congruential<IntType,a,c,m,val>& lcg)
williamr@2
   152
{
williamr@2
   153
    return is >> lcg._x;
williamr@2
   154
}
williamr@2
   155
#elif defined(BOOST_NO_OPERATORS_IN_NAMESPACE) || defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
williamr@2
   156
template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2
   157
std::basic_ostream<CharT,Traits>&
williamr@2
   158
operator<<(std::basic_ostream<CharT,Traits>& os,
williamr@2
   159
           const linear_congruential<IntType,a,c,m,val>& lcg)
williamr@2
   160
{
williamr@2
   161
    return os << lcg._x;
williamr@2
   162
}
williamr@2
   163
williamr@2
   164
template<class CharT, class Traits, class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2
   165
std::basic_istream<CharT,Traits>&
williamr@2
   166
operator>>(std::basic_istream<CharT,Traits>& is,
williamr@2
   167
           linear_congruential<IntType,a,c,m,val>& lcg)
williamr@2
   168
{
williamr@2
   169
    return is >> lcg._x;
williamr@2
   170
}
williamr@2
   171
#endif
williamr@2
   172
williamr@2
   173
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
williamr@2
   174
//  A definition is required even for integral static constants
williamr@2
   175
template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2
   176
const bool linear_congruential<IntType, a, c, m, val>::has_fixed_range;
williamr@2
   177
template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2
   178
const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::min_value;
williamr@2
   179
template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2
   180
const typename linear_congruential<IntType, a, c, m, val>::result_type linear_congruential<IntType, a, c, m, val>::max_value;
williamr@2
   181
template<class IntType, IntType a, IntType c, IntType m, IntType val>
williamr@2
   182
const IntType linear_congruential<IntType,a,c,m,val>::modulus;
williamr@2
   183
#endif
williamr@2
   184
williamr@2
   185
} // namespace random
williamr@2
   186
williamr@2
   187
// validation values from the publications
williamr@2
   188
typedef random::linear_congruential<int32_t, 16807, 0, 2147483647, 
williamr@2
   189
  1043618065> minstd_rand0;
williamr@2
   190
typedef random::linear_congruential<int32_t, 48271, 0, 2147483647,
williamr@2
   191
  399268537> minstd_rand;
williamr@2
   192
williamr@2
   193
williamr@2
   194
#if !defined(BOOST_NO_INT64_T) && !defined(BOOST_NO_INTEGRAL_INT64_T)
williamr@2
   195
// emulate the lrand48() C library function; requires support for uint64_t
williamr@2
   196
class rand48 
williamr@2
   197
{
williamr@2
   198
public:
williamr@2
   199
  typedef int32_t result_type;
williamr@2
   200
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
williamr@2
   201
  static const bool has_fixed_range = true;
williamr@2
   202
  static const int32_t min_value = 0;
williamr@2
   203
  static const int32_t max_value = integer_traits<int32_t>::const_max;
williamr@2
   204
#else
williamr@2
   205
  enum { has_fixed_range = false };
williamr@2
   206
#endif
williamr@2
   207
  int32_t min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
williamr@2
   208
  int32_t max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return std::numeric_limits<int32_t>::max BOOST_PREVENT_MACRO_SUBSTITUTION (); }
williamr@2
   209
  
williamr@2
   210
  explicit rand48(int32_t x0 = 1) : lcf(cnv(x0)) { }
williamr@2
   211
  explicit rand48(uint64_t x0) : lcf(x0) { }
williamr@2
   212
  template<class It> rand48(It& first, It last) : lcf(first, last) { }
williamr@2
   213
  // compiler-generated copy ctor and assignment operator are fine
williamr@2
   214
  void seed(int32_t x0 = 1) { lcf.seed(cnv(x0)); }
williamr@2
   215
  void seed(uint64_t x0) { lcf.seed(x0); }
williamr@2
   216
  template<class It> void seed(It& first, It last) { lcf.seed(first,last); }
williamr@2
   217
williamr@2
   218
  int32_t operator()() { return static_cast<int32_t>(lcf() >> 17); }
williamr@2
   219
  // by experiment from lrand48()
williamr@2
   220
  static bool validation(int32_t x) { return x == 1993516219; }
williamr@2
   221
williamr@2
   222
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
williamr@2
   223
williamr@2
   224
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
williamr@2
   225
  template<class CharT,class Traits>
williamr@2
   226
  friend std::basic_ostream<CharT,Traits>&
williamr@2
   227
  operator<<(std::basic_ostream<CharT,Traits>& os, const rand48& r)
williamr@2
   228
  { os << r.lcf; return os; }
williamr@2
   229
williamr@2
   230
  template<class CharT,class Traits>
williamr@2
   231
  friend std::basic_istream<CharT,Traits>&
williamr@2
   232
  operator>>(std::basic_istream<CharT,Traits>& is, rand48& r)
williamr@2
   233
  { is >> r.lcf; return is; }
williamr@2
   234
#endif
williamr@2
   235
williamr@2
   236
  friend bool operator==(const rand48& x, const rand48& y)
williamr@2
   237
  { return x.lcf == y.lcf; }
williamr@2
   238
  friend bool operator!=(const rand48& x, const rand48& y)
williamr@2
   239
  { return !(x == y); }
williamr@2
   240
#else
williamr@2
   241
  // Use a member function; Streamable concept not supported.
williamr@2
   242
  bool operator==(const rand48& rhs) const
williamr@2
   243
  { return lcf == rhs.lcf; }
williamr@2
   244
  bool operator!=(const rand48& rhs) const
williamr@2
   245
  { return !(*this == rhs); }
williamr@2
   246
#endif
williamr@2
   247
private:
williamr@2
   248
  random::linear_congruential<uint64_t,
williamr@2
   249
    uint64_t(0xDEECE66DUL) | (uint64_t(0x5) << 32), // xxxxULL is not portable
williamr@2
   250
    0xB, uint64_t(1)<<48, /* unknown */ 0> lcf;
williamr@2
   251
  static uint64_t cnv(int32_t x) 
williamr@2
   252
  { return (static_cast<uint64_t>(x) << 16) | 0x330e;  }
williamr@2
   253
};
williamr@2
   254
#endif /* !BOOST_NO_INT64_T && !BOOST_NO_INTEGRAL_INT64_T */
williamr@2
   255
williamr@2
   256
} // namespace boost
williamr@2
   257
williamr@2
   258
#endif // BOOST_RANDOM_LINEAR_CONGRUENTIAL_HPP