epoc32/include/stdapis/boost/random/linear_feedback_shift.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/linear_feedback_shift.hpp	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -0,0 +1,145 @@
     1.4 +/* boost random/tausworthe.hpp header file
     1.5 + *
     1.6 + * Copyright Jens Maurer 2002
     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_feedback_shift.hpp,v 1.12 2005/05/21 15:57:00 dgregor Exp $
    1.14 + *
    1.15 + */
    1.16 +
    1.17 +#ifndef BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
    1.18 +#define BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
    1.19 +
    1.20 +#include <iostream>
    1.21 +#include <cassert>
    1.22 +#include <stdexcept>
    1.23 +#include <boost/config.hpp>
    1.24 +#include <boost/static_assert.hpp>
    1.25 +#include <boost/limits.hpp>
    1.26 +
    1.27 +namespace boost {
    1.28 +namespace random {
    1.29 +
    1.30 +// Tausworte 1965
    1.31 +template<class UIntType, int w, int k, int q, int s, UIntType val>
    1.32 +class linear_feedback_shift
    1.33 +{
    1.34 +public:
    1.35 +  typedef UIntType result_type;
    1.36 +  // avoid the warning trouble when using (1<<w) on 32 bit machines
    1.37 +  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
    1.38 +  BOOST_STATIC_CONSTANT(int, word_size = w);
    1.39 +  BOOST_STATIC_CONSTANT(int, exponent1 = k);
    1.40 +  BOOST_STATIC_CONSTANT(int, exponent2 = q);
    1.41 +  BOOST_STATIC_CONSTANT(int, step_size = s);
    1.42 +
    1.43 +  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
    1.44 +  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return wordmask; }
    1.45 +
    1.46 +  // MSVC 6 and possibly others crash when encountering complicated integral
    1.47 +  // constant expressions.  Avoid the checks for now.
    1.48 +  // BOOST_STATIC_ASSERT(w > 0);
    1.49 +  // BOOST_STATIC_ASSERT(q > 0);
    1.50 +  // BOOST_STATIC_ASSERT(k < w);
    1.51 +  // BOOST_STATIC_ASSERT(0 < 2*q && 2*q < k);
    1.52 +  // BOOST_STATIC_ASSERT(0 < s && s <= k-q);
    1.53 +
    1.54 +  explicit linear_feedback_shift(UIntType s0 = 341) : wordmask(0)
    1.55 +  {
    1.56 +    // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
    1.57 +#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
    1.58 +    BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
    1.59 +    BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
    1.60 +#endif
    1.61 +
    1.62 +    // avoid "left shift count >= with of type" warning
    1.63 +    for(int i = 0; i < w; ++i)
    1.64 +      wordmask |= (1u << i);
    1.65 +    seed(s0);
    1.66 +  }
    1.67 +
    1.68 +  template<class It> linear_feedback_shift(It& first, It last) : wordmask(0)
    1.69 +  {
    1.70 +    // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
    1.71 +#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
    1.72 +    BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_integer);
    1.73 +    BOOST_STATIC_ASSERT(!std::numeric_limits<UIntType>::is_signed);
    1.74 +#endif
    1.75 +
    1.76 +    // avoid "left shift count >= with of type" warning
    1.77 +    for(int i = 0; i < w; ++i)
    1.78 +      wordmask |= (1u << i);
    1.79 +    seed(first, last);
    1.80 +  }
    1.81 +
    1.82 +  void seed(UIntType s0 = 341) { assert(s0 >= (1 << (w-k))); value = s0; }
    1.83 +  template<class It> void seed(It& first, It last)
    1.84 +  {
    1.85 +    if(first == last)
    1.86 +      throw std::invalid_argument("linear_feedback_shift::seed");
    1.87 +    value = *first++;
    1.88 +    assert(value >= (1 << (w-k)));
    1.89 +  }
    1.90 +
    1.91 +  result_type operator()()
    1.92 +  {
    1.93 +    const UIntType b = (((value << q) ^ value) & wordmask) >> (k-s);
    1.94 +    const UIntType mask = ( (~static_cast<UIntType>(0)) << (w-k) ) & wordmask;
    1.95 +    value = ((value & mask) << s) ^ b;
    1.96 +    return value;
    1.97 +  }
    1.98 +  bool validation(result_type x) const { return val == x; }
    1.99 +
   1.100 +#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
   1.101 +
   1.102 +#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
   1.103 +  template<class CharT, class Traits>
   1.104 +  friend std::basic_ostream<CharT,Traits>&
   1.105 +  operator<<(std::basic_ostream<CharT,Traits>& os, linear_feedback_shift x)
   1.106 +  { os << x.value; return os; }
   1.107 +
   1.108 +  template<class CharT, class Traits>
   1.109 +  friend std::basic_istream<CharT,Traits>&
   1.110 +  operator>>(std::basic_istream<CharT,Traits>& is, linear_feedback_shift& x)
   1.111 +  { is >> x.value; return is; }
   1.112 +#endif
   1.113 +
   1.114 +  friend bool operator==(linear_feedback_shift x, linear_feedback_shift y)
   1.115 +  { return x.value == y.value; }
   1.116 +  friend bool operator!=(linear_feedback_shift x, linear_feedback_shift y)
   1.117 +  { return !(x == y); }
   1.118 +#else
   1.119 +  // Use a member function; Streamable concept not supported.
   1.120 +  bool operator==(linear_feedback_shift rhs) const
   1.121 +  { return value == rhs.value; }
   1.122 +  bool operator!=(linear_feedback_shift rhs) const
   1.123 +  { return !(*this == rhs); }
   1.124 +#endif
   1.125 +
   1.126 +private:
   1.127 +  UIntType wordmask; // avoid "left shift count >= width of type" warnings
   1.128 +  UIntType value;
   1.129 +};
   1.130 +
   1.131 +#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
   1.132 +//  A definition is required even for integral static constants
   1.133 +template<class UIntType, int w, int k, int q, int s, UIntType val>
   1.134 +const bool linear_feedback_shift<UIntType, w, k, q, s, val>::has_fixed_range;
   1.135 +template<class UIntType, int w, int k, int q, int s, UIntType val>
   1.136 +const int linear_feedback_shift<UIntType, w, k, q, s, val>::word_size;
   1.137 +template<class UIntType, int w, int k, int q, int s, UIntType val>
   1.138 +const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent1;
   1.139 +template<class UIntType, int w, int k, int q, int s, UIntType val>
   1.140 +const int linear_feedback_shift<UIntType, w, k, q, s, val>::exponent2;
   1.141 +template<class UIntType, int w, int k, int q, int s, UIntType val>
   1.142 +const int linear_feedback_shift<UIntType, w, k, q, s, val>::step_size;
   1.143 +#endif
   1.144 +
   1.145 +} // namespace random
   1.146 +} // namespace boost
   1.147 +
   1.148 +#endif // BOOST_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP