1 /* boost random/shuffle_output.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: shuffle_output.hpp 52492 2009-04-19 14:55:57Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_SHUFFLE_OUTPUT_HPP
\r
17 #define BOOST_RANDOM_SHUFFLE_OUTPUT_HPP
\r
20 #include <algorithm> // std::copy
\r
22 #include <boost/config.hpp>
\r
23 #include <boost/limits.hpp>
\r
24 #include <boost/static_assert.hpp>
\r
25 #include <boost/cstdint.hpp>
\r
26 #include <boost/random/linear_congruential.hpp>
\r
31 // Carter Bays and S.D. Durham 1979
\r
32 template<class UniformRandomNumberGenerator, int k,
\r
33 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
\r
34 typename UniformRandomNumberGenerator::result_type
\r
39 class shuffle_output
\r
42 typedef UniformRandomNumberGenerator base_type;
\r
43 typedef typename base_type::result_type result_type;
\r
45 BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
\r
46 BOOST_STATIC_CONSTANT(int, buffer_size = k);
\r
48 shuffle_output() : _rng() { init(); }
\r
49 #if defined(BOOST_MSVC) && _MSC_VER < 1300
\r
50 // MSVC does not implicitly generate the copy constructor here
\r
51 shuffle_output(const shuffle_output & x)
\r
52 : _rng(x._rng), y(x.y) { std::copy(x.v, x.v+k, v); }
\r
55 explicit shuffle_output(T s) : _rng(s) { init(); }
\r
56 explicit shuffle_output(const base_type & rng) : _rng(rng) { init(); }
\r
57 template<class It> shuffle_output(It& first, It last)
\r
58 : _rng(first, last) { init(); }
\r
59 void seed() { _rng.seed(); init(); }
\r
61 void seed(T s) { _rng.seed(s); init(); }
\r
62 template<class It> void seed(It& first, It last)
\r
64 _rng.seed(first, last);
\r
68 const base_type& base() const { return _rng; }
\r
70 result_type operator()() {
\r
71 // calculating the range every time may seem wasteful. However, this
\r
72 // makes the information locally available for the optimizer.
\r
73 result_type range = (max)()-(min)()+1;
\r
74 int j = k*(y-(min)())/range;
\r
75 // assert(0 <= j && j < k);
\r
81 result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); }
\r
82 result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); }
\r
83 static bool validation(result_type x) { return val == x; }
\r
85 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
\r
87 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
\r
88 template<class CharT, class Traits>
\r
89 friend std::basic_ostream<CharT,Traits>&
\r
90 operator<<(std::basic_ostream<CharT,Traits>& os, const shuffle_output& s)
\r
92 os << s._rng << " " << s.y << " ";
\r
93 for(int i = 0; i < s.buffer_size; ++i)
\r
94 os << s.v[i] << " ";
\r
98 template<class CharT, class Traits>
\r
99 friend std::basic_istream<CharT,Traits>&
\r
100 operator>>(std::basic_istream<CharT,Traits>& is, shuffle_output& s)
\r
102 is >> s._rng >> std::ws >> s.y >> std::ws;
\r
103 for(int i = 0; i < s.buffer_size; ++i)
\r
104 is >> s.v[i] >> std::ws;
\r
109 friend bool operator==(const shuffle_output& x, const shuffle_output& y)
\r
110 { return x._rng == y._rng && x.y == y.y && std::equal(x.v, x.v+k, y.v); }
\r
111 friend bool operator!=(const shuffle_output& x, const shuffle_output& y)
\r
112 { return !(x == y); }
\r
114 // Use a member function; Streamable concept not supported.
\r
115 bool operator==(const shuffle_output& rhs) const
\r
116 { return _rng == rhs._rng && y == rhs.y && std::equal(v, v+k, rhs.v); }
\r
117 bool operator!=(const shuffle_output& rhs) const
\r
118 { return !(*this == rhs); }
\r
123 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
\r
124 BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
\r
126 result_type range = (max)()-(min)();
\r
127 assert(range > 0); // otherwise there would be little choice
\r
128 if(static_cast<unsigned long>(k * range) <
\r
129 static_cast<unsigned long>(range)) // not a sufficient condition
\r
130 // likely overflow with bucket number computation
\r
131 assert(!"overflow will occur");
\r
133 // we cannot use std::generate, because it uses pass-by-value for _rng
\r
134 for(result_type * p = v; p != v+k; ++p)
\r
144 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
\r
145 // A definition is required even for integral static constants
\r
146 template<class UniformRandomNumberGenerator, int k,
\r
147 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
\r
148 typename UniformRandomNumberGenerator::result_type
\r
153 const bool shuffle_output<UniformRandomNumberGenerator, k, val>::has_fixed_range;
\r
155 template<class UniformRandomNumberGenerator, int k,
\r
156 #ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
\r
157 typename UniformRandomNumberGenerator::result_type
\r
162 const int shuffle_output<UniformRandomNumberGenerator, k, val>::buffer_size;
\r
165 } // namespace random
\r
167 // validation by experiment from Harry Erwin's generator.h (private e-mail)
\r
168 typedef random::shuffle_output<
\r
169 random::linear_congruential<uint32_t, 1366, 150889, 714025, 0>,
\r
170 97, 139726> kreutzer1986;
\r
173 } // namespace boost
\r
175 #endif // BOOST_RANDOM_SHUFFLE_OUTPUT_HPP
\r