epoc32/include/stdapis/boost/random/inversive_congruential.hpp
branchSymbian2
changeset 2 2fe1408b6811
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/stdapis/boost/random/inversive_congruential.hpp	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* boost random/inversive_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: inversive_congruential.hpp,v 1.11 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_INVERSIVE_CONGRUENTIAL_HPP
    1.20 +#define BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
    1.21 +
    1.22 +#include <iostream>
    1.23 +#include <cassert>
    1.24 +#include <boost/config.hpp>
    1.25 +#include <boost/static_assert.hpp>
    1.26 +#include <boost/random/detail/const_mod.hpp>
    1.27 +
    1.28 +namespace boost {
    1.29 +namespace random {
    1.30 +
    1.31 +// Eichenauer and Lehn 1986
    1.32 +template<class IntType, IntType a, IntType b, IntType p, IntType val>
    1.33 +class inversive_congruential
    1.34 +{
    1.35 +public:
    1.36 +  typedef IntType result_type;
    1.37 +#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
    1.38 +  static const bool has_fixed_range = true;
    1.39 +  static const result_type min_value = (b == 0 ? 1 : 0);
    1.40 +  static const result_type max_value = p-1;
    1.41 +#else
    1.42 +  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
    1.43 +#endif
    1.44 +  BOOST_STATIC_CONSTANT(result_type, multiplier = a);
    1.45 +  BOOST_STATIC_CONSTANT(result_type, increment = b);
    1.46 +  BOOST_STATIC_CONSTANT(result_type, modulus = p);
    1.47 +
    1.48 +  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return b == 0 ? 1 : 0; }
    1.49 +  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return p-1; }
    1.50 +
    1.51 +  explicit inversive_congruential(IntType y0 = 1) : value(y0)
    1.52 +  {
    1.53 +    BOOST_STATIC_ASSERT(b >= 0);
    1.54 +    BOOST_STATIC_ASSERT(p > 1);
    1.55 +    BOOST_STATIC_ASSERT(a >= 1);
    1.56 +    if(b == 0) 
    1.57 +      assert(y0 > 0); 
    1.58 +  }
    1.59 +  template<class It> inversive_congruential(It& first, It last)
    1.60 +  { seed(first, last); }
    1.61 +
    1.62 +  void seed(IntType y0 = 1) { value = y0; if(b == 0) assert(y0 > 0); }
    1.63 +  template<class It> void seed(It& first, It last)
    1.64 +  {
    1.65 +    if(first == last)
    1.66 +      throw std::invalid_argument("inversive_congruential::seed");
    1.67 +    value = *first++;
    1.68 +  }
    1.69 +  IntType operator()()
    1.70 +  {
    1.71 +    typedef const_mod<IntType, p> do_mod;
    1.72 +    value = do_mod::mult_add(a, do_mod::invert(value), b);
    1.73 +    return value;
    1.74 +  }
    1.75 +
    1.76 +  bool validation(result_type x) const { return val == x; }
    1.77 +
    1.78 +#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
    1.79 +
    1.80 +#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
    1.81 +  template<class CharT, class Traits>
    1.82 +  friend std::basic_ostream<CharT,Traits>&
    1.83 +  operator<<(std::basic_ostream<CharT,Traits>& os, inversive_congruential x)
    1.84 +  { os << x.value; return os; }
    1.85 +
    1.86 +  template<class CharT, class Traits>
    1.87 +  friend std::basic_istream<CharT,Traits>&
    1.88 +  operator>>(std::basic_istream<CharT,Traits>& is, inversive_congruential& x)
    1.89 +  { is >> x.value; return is; }
    1.90 +#endif
    1.91 +
    1.92 +  friend bool operator==(inversive_congruential x, inversive_congruential y)
    1.93 +  { return x.value == y.value; }
    1.94 +  friend bool operator!=(inversive_congruential x, inversive_congruential y)
    1.95 +  { return !(x == y); }
    1.96 +#else
    1.97 +  // Use a member function; Streamable concept not supported.
    1.98 +  bool operator==(inversive_congruential rhs) const
    1.99 +  { return value == rhs.value; }
   1.100 +  bool operator!=(inversive_congruential rhs) const
   1.101 +  { return !(*this == rhs); }
   1.102 +#endif
   1.103 +private:
   1.104 +  IntType value;
   1.105 +};
   1.106 +
   1.107 +#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
   1.108 +//  A definition is required even for integral static constants
   1.109 +template<class IntType, IntType a, IntType b, IntType p, IntType val>
   1.110 +const bool inversive_congruential<IntType, a, b, p, val>::has_fixed_range;
   1.111 +template<class IntType, IntType a, IntType b, IntType p, IntType val>
   1.112 +const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::min_value;
   1.113 +template<class IntType, IntType a, IntType b, IntType p, IntType val>
   1.114 +const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::max_value;
   1.115 +template<class IntType, IntType a, IntType b, IntType p, IntType val>
   1.116 +const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::multiplier;
   1.117 +template<class IntType, IntType a, IntType b, IntType p, IntType val>
   1.118 +const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::increment;
   1.119 +template<class IntType, IntType a, IntType b, IntType p, IntType val>
   1.120 +const typename inversive_congruential<IntType, a, b, p, val>::result_type inversive_congruential<IntType, a, b, p, val>::modulus;
   1.121 +#endif
   1.122 +
   1.123 +} // namespace random
   1.124 +
   1.125 +typedef random::inversive_congruential<int32_t, 9102, 2147483647-36884165,
   1.126 +  2147483647, 0> hellekalek1995;
   1.127 +
   1.128 +} // namespace boost
   1.129 +
   1.130 +#endif // BOOST_RANDOM_INVERSIVE_CONGRUENTIAL_HPP