os/ossrv/ossrv_pub/boost_apis/boost/random/inversive_congruential.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* boost random/inversive_congruential.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2000-2001
sl@0
     4
 * Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
 * accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
 * http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
 *
sl@0
     8
 * See http://www.boost.org for most recent version including documentation.
sl@0
     9
 *
sl@0
    10
 * $Id: inversive_congruential.hpp,v 1.11 2005/05/21 15:57:00 dgregor Exp $
sl@0
    11
 *
sl@0
    12
 * Revision history
sl@0
    13
 *  2001-02-18  moved to individual header files
sl@0
    14
 */
sl@0
    15
sl@0
    16
#ifndef BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
sl@0
    17
#define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
sl@0
    18
sl@0
    19
#include <iostream>
sl@0
    20
#include <cassert>
sl@0
    21
#include <boost/config.hpp>
sl@0
    22
#include <boost/static_assert.hpp>
sl@0
    23
#include <boost/random/detail/const_mod.hpp>
sl@0
    24
sl@0
    25
namespace boost {
sl@0
    26
namespace random {
sl@0
    27
sl@0
    28
// Eichenauer and Lehn 1986
sl@0
    29
template<class IntType, IntType a, IntType b, IntType p, IntType val>
sl@0
    30
class inversive_congruential
sl@0
    31
{
sl@0
    32
public:
sl@0
    33
  typedef IntType result_type;
sl@0
    34
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
sl@0
    35
  static const bool has_fixed_range = true;
sl@0
    36
  static const result_type min_value = (b == 0 ? 1 : 0);
sl@0
    37
  static const result_type max_value = p-1;
sl@0
    38
#else
sl@0
    39
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
sl@0
    40
#endif
sl@0
    41
  BOOST_STATIC_CONSTANT(result_type, multiplier = a);
sl@0
    42
  BOOST_STATIC_CONSTANT(result_type, increment = b);
sl@0
    43
  BOOST_STATIC_CONSTANT(result_type, modulus = p);
sl@0
    44
sl@0
    45
  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return b == 0 ? 1 : 0; }
sl@0
    46
  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return p-1; }
sl@0
    47
sl@0
    48
  explicit inversive_congruential(IntType y0 = 1) : value(y0)
sl@0
    49
  {
sl@0
    50
    BOOST_STATIC_ASSERT(b >= 0);
sl@0
    51
    BOOST_STATIC_ASSERT(p > 1);
sl@0
    52
    BOOST_STATIC_ASSERT(a >= 1);
sl@0
    53
    if(b == 0) 
sl@0
    54
      assert(y0 > 0); 
sl@0
    55
  }
sl@0
    56
  template<class It> inversive_congruential(It& first, It last)
sl@0
    57
  { seed(first, last); }
sl@0
    58
sl@0
    59
  void seed(IntType y0 = 1) { value = y0; if(b == 0) assert(y0 > 0); }
sl@0
    60
  template<class It> void seed(It& first, It last)
sl@0
    61
  {
sl@0
    62
    if(first == last)
sl@0
    63
      throw std::invalid_argument("inversive_congruential::seed");
sl@0
    64
    value = *first++;
sl@0
    65
  }
sl@0
    66
  IntType operator()()
sl@0
    67
  {
sl@0
    68
    typedef const_mod<IntType, p> do_mod;
sl@0
    69
    value = do_mod::mult_add(a, do_mod::invert(value), b);
sl@0
    70
    return value;
sl@0
    71
  }
sl@0
    72
sl@0
    73
  bool validation(result_type x) const { return val == x; }
sl@0
    74
sl@0
    75
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
sl@0
    76
sl@0
    77
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
sl@0
    78
  template<class CharT, class Traits>
sl@0
    79
  friend std::basic_ostream<CharT,Traits>&
sl@0
    80
  operator<<(std::basic_ostream<CharT,Traits>& os, inversive_congruential x)
sl@0
    81
  { os << x.value; return os; }
sl@0
    82
sl@0
    83
  template<class CharT, class Traits>
sl@0
    84
  friend std::basic_istream<CharT,Traits>&
sl@0
    85
  operator>>(std::basic_istream<CharT,Traits>& is, inversive_congruential& x)
sl@0
    86
  { is >> x.value; return is; }
sl@0
    87
#endif
sl@0
    88
sl@0
    89
  friend bool operator==(inversive_congruential x, inversive_congruential y)
sl@0
    90
  { return x.value == y.value; }
sl@0
    91
  friend bool operator!=(inversive_congruential x, inversive_congruential y)
sl@0
    92
  { return !(x == y); }
sl@0
    93
#else
sl@0
    94
  // Use a member function; Streamable concept not supported.
sl@0
    95
  bool operator==(inversive_congruential rhs) const
sl@0
    96
  { return value == rhs.value; }
sl@0
    97
  bool operator!=(inversive_congruential rhs) const
sl@0
    98
  { return !(*this == rhs); }
sl@0
    99
#endif
sl@0
   100
private:
sl@0
   101
  IntType value;
sl@0
   102
};
sl@0
   103
sl@0
   104
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
sl@0
   105
//  A definition is required even for integral static constants
sl@0
   106
template<class IntType, IntType a, IntType b, IntType p, IntType val>
sl@0
   107
const bool inversive_congruential<IntType, a, b, p, val>::has_fixed_range;
sl@0
   108
template<class IntType, IntType a, IntType b, IntType p, IntType val>
sl@0
   109
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::min_value;
sl@0
   110
template<class IntType, IntType a, IntType b, IntType p, IntType val>
sl@0
   111
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::max_value;
sl@0
   112
template<class IntType, IntType a, IntType b, IntType p, IntType val>
sl@0
   113
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::multiplier;
sl@0
   114
template<class IntType, IntType a, IntType b, IntType p, IntType val>
sl@0
   115
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::increment;
sl@0
   116
template<class IntType, IntType a, IntType b, IntType p, IntType val>
sl@0
   117
const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::modulus;
sl@0
   118
#endif
sl@0
   119
sl@0
   120
} // namespace random
sl@0
   121
sl@0
   122
typedef random::inversive_congruential<int32_t, 9102, 2147483647-36884165,
sl@0
   123
  2147483647, 0> hellekalek1995;
sl@0
   124
sl@0
   125
} // namespace boost
sl@0
   126
sl@0
   127
#endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP