1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/random/shuffle_output.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,175 @@
1.4 +/* boost random/shuffle_output.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: shuffle_output.hpp,v 1.10 2005/08/25 16:27:22 johnmaddock 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_SHUFFLE_OUTPUT_HPP
1.20 +#define BOOST_RANDOM_SHUFFLE_OUTPUT_HPP
1.21 +
1.22 +#include <iostream>
1.23 +#include <algorithm> // std::copy
1.24 +#include <cassert>
1.25 +#include <boost/config.hpp>
1.26 +#include <boost/limits.hpp>
1.27 +#include <boost/static_assert.hpp>
1.28 +#include <boost/cstdint.hpp>
1.29 +#include <boost/random/linear_congruential.hpp>
1.30 +
1.31 +namespace boost {
1.32 +namespace random {
1.33 +
1.34 +// Carter Bays and S.D. Durham 1979
1.35 +template<class UniformRandomNumberGenerator, int k,
1.36 +#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
1.37 + typename UniformRandomNumberGenerator::result_type
1.38 +#else
1.39 + uint32_t
1.40 +#endif
1.41 + val = 0>
1.42 +class shuffle_output
1.43 +{
1.44 +public:
1.45 + typedef UniformRandomNumberGenerator base_type;
1.46 + typedef typename base_type::result_type result_type;
1.47 +
1.48 + BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
1.49 + BOOST_STATIC_CONSTANT(int, buffer_size = k);
1.50 +
1.51 + shuffle_output() : _rng() { init(); }
1.52 +#if defined(BOOST_MSVC) && _MSC_VER < 1300
1.53 + // MSVC does not implicitly generate the copy constructor here
1.54 + shuffle_output(const shuffle_output & x)
1.55 + : _rng(x._rng), y(x.y) { std::copy(x.v, x.v+k, v); }
1.56 +#endif
1.57 + template<class T>
1.58 + explicit shuffle_output(T seed) : _rng(seed) { init(); }
1.59 + explicit shuffle_output(const base_type & rng) : _rng(rng) { init(); }
1.60 + template<class It> shuffle_output(It& first, It last)
1.61 + : _rng(first, last) { init(); }
1.62 + void seed() { _rng.seed(); init(); }
1.63 + template<class T>
1.64 + void seed(T s) { _rng.seed(s); init(); }
1.65 + template<class It> void seed(It& first, It last)
1.66 + {
1.67 + _rng.seed(first, last);
1.68 + init();
1.69 + }
1.70 +
1.71 + const base_type& base() const { return _rng; }
1.72 +
1.73 + result_type operator()() {
1.74 + // calculating the range every time may seem wasteful. However, this
1.75 + // makes the information locally available for the optimizer.
1.76 + result_type range = (max)()-(min)()+1;
1.77 + int j = k*(y-(min)())/range;
1.78 + // assert(0 <= j && j < k);
1.79 + y = v[j];
1.80 + v[j] = _rng();
1.81 + return y;
1.82 + }
1.83 +
1.84 + result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); }
1.85 + result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); }
1.86 + static bool validation(result_type x) { return val == x; }
1.87 +
1.88 +#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
1.89 +
1.90 +#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
1.91 + template<class CharT, class Traits>
1.92 + friend std::basic_ostream<CharT,Traits>&
1.93 + operator<<(std::basic_ostream<CharT,Traits>& os, const shuffle_output& s)
1.94 + {
1.95 + os << s._rng << " " << s.y << " ";
1.96 + for(int i = 0; i < s.buffer_size; ++i)
1.97 + os << s.v[i] << " ";
1.98 + return os;
1.99 + }
1.100 +
1.101 + template<class CharT, class Traits>
1.102 + friend std::basic_istream<CharT,Traits>&
1.103 + operator>>(std::basic_istream<CharT,Traits>& is, shuffle_output& s)
1.104 + {
1.105 + is >> s._rng >> std::ws >> s.y >> std::ws;
1.106 + for(int i = 0; i < s.buffer_size; ++i)
1.107 + is >> s.v[i] >> std::ws;
1.108 + return is;
1.109 + }
1.110 +#endif
1.111 +
1.112 + friend bool operator==(const shuffle_output& x, const shuffle_output& y)
1.113 + { return x._rng == y._rng && x.y == y.y && std::equal(x.v, x.v+k, y.v); }
1.114 + friend bool operator!=(const shuffle_output& x, const shuffle_output& y)
1.115 + { return !(x == y); }
1.116 +#else
1.117 + // Use a member function; Streamable concept not supported.
1.118 + bool operator==(const shuffle_output& rhs) const
1.119 + { return _rng == rhs._rng && y == rhs.y && std::equal(v, v+k, rhs.v); }
1.120 + bool operator!=(const shuffle_output& rhs) const
1.121 + { return !(*this == rhs); }
1.122 +#endif
1.123 +private:
1.124 + void init()
1.125 + {
1.126 +#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
1.127 + BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
1.128 +#endif
1.129 + result_type range = (max)()-(min)();
1.130 + assert(range > 0); // otherwise there would be little choice
1.131 + if(static_cast<unsigned long>(k * range) <
1.132 + static_cast<unsigned long>(range)) // not a sufficient condition
1.133 + // likely overflow with bucket number computation
1.134 + assert(!"overflow will occur");
1.135 +
1.136 + // we cannot use std::generate, because it uses pass-by-value for _rng
1.137 + for(result_type * p = v; p != v+k; ++p)
1.138 + *p = _rng();
1.139 + y = _rng();
1.140 + }
1.141 +
1.142 + base_type _rng;
1.143 + result_type v[k];
1.144 + result_type y;
1.145 +};
1.146 +
1.147 +#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
1.148 +// A definition is required even for integral static constants
1.149 +template<class UniformRandomNumberGenerator, int k,
1.150 +#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
1.151 + typename UniformRandomNumberGenerator::result_type
1.152 +#else
1.153 + uint32_t
1.154 +#endif
1.155 + val>
1.156 +const bool shuffle_output<UniformRandomNumberGenerator, k, val>::has_fixed_range;
1.157 +
1.158 +template<class UniformRandomNumberGenerator, int k,
1.159 +#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
1.160 + typename UniformRandomNumberGenerator::result_type
1.161 +#else
1.162 + uint32_t
1.163 +#endif
1.164 + val>
1.165 +const int shuffle_output<UniformRandomNumberGenerator, k, val>::buffer_size;
1.166 +#endif
1.167 +
1.168 +} // namespace random
1.169 +
1.170 +// validation by experiment from Harry Erwin's generator.h (private e-mail)
1.171 +typedef random::shuffle_output<
1.172 + random::linear_congruential<uint32_t, 1366, 150889, 714025, 0>,
1.173 + 97, 139726> kreutzer1986;
1.174 +
1.175 +
1.176 +} // namespace boost
1.177 +
1.178 +#endif // BOOST_RANDOM_SHUFFLE_OUTPUT_HPP