]> git.donarmstrong.com Git - rsem.git/blob - boost/random/discard_block.hpp
RSEM Source Codes
[rsem.git] / boost / random / discard_block.hpp
1 /* boost random/discard_block.hpp header file
2  *
3  * Copyright Jens Maurer 2002
4  * Distributed under the Boost Software License, Version 1.0. (See
5  * accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * See http://www.boost.org for most recent version including documentation.
9  *
10  * $Id: discard_block.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
11  *
12  * Revision history
13  *  2001-03-02  created
14  */
15
16 #ifndef BOOST_RANDOM_DISCARD_BLOCK_HPP
17 #define BOOST_RANDOM_DISCARD_BLOCK_HPP
18
19 #include <iostream>
20 #include <boost/config.hpp>
21 #include <boost/limits.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/random/detail/config.hpp>
24
25
26 namespace boost {
27 namespace random {
28
29 /**
30  * The class template \discard_block is a model of
31  * \pseudo_random_number_generator.  It modifies
32  * another generator by discarding parts of its output.
33  * Out of every block of @c r results, the first @c p
34  * will be returned and the rest discarded.
35  *
36  * Requires: 0 < p <= r
37  */
38 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
39 class discard_block
40 {
41 public:
42   typedef UniformRandomNumberGenerator base_type;
43   typedef typename base_type::result_type result_type;
44
45   BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
46   BOOST_STATIC_CONSTANT(unsigned int, total_block = p);
47   BOOST_STATIC_CONSTANT(unsigned int, returned_block = r);
48
49 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
50   BOOST_STATIC_ASSERT(total_block >= returned_block);
51 #endif
52
53   discard_block() : _rng(), _n(0) { }
54   explicit discard_block(const base_type & rng) : _rng(rng), _n(0) { }
55   template<class T> explicit discard_block(T s) : _rng(s), _n(0) {}
56   template<class It> discard_block(It& first, It last)
57     : _rng(first, last), _n(0) { }
58   void seed() { _rng.seed(); _n = 0; }
59   template<class T> void seed(T s) { _rng.seed(s); _n = 0; }
60   template<class It> void seed(It& first, It last)
61   { _n = 0; _rng.seed(first, last); }
62
63   const base_type& base() const { return _rng; }
64
65   result_type operator()()
66   {
67     if(_n >= returned_block) {
68       // discard values of random number generator
69       for( ; _n < total_block; ++_n)
70         _rng();
71       _n = 0;
72     }
73     ++_n;
74     return _rng();
75   }
76
77   result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); }
78   result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); }
79   static bool validation(result_type x) { return true; }  // dummy
80
81 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
82
83 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
84   template<class CharT, class Traits>
85   friend std::basic_ostream<CharT,Traits>&
86   operator<<(std::basic_ostream<CharT,Traits>& os, const discard_block& s)
87   {
88     os << s._rng << " " << s._n << " ";
89     return os;
90   }
91
92   template<class CharT, class Traits>
93   friend std::basic_istream<CharT,Traits>&
94   operator>>(std::basic_istream<CharT,Traits>& is, discard_block& s)
95   {
96     is >> s._rng >> std::ws >> s._n >> std::ws;
97     return is;
98   }
99 #endif
100
101   friend bool operator==(const discard_block& x, const discard_block& y)
102   { return x._rng == y._rng && x._n == y._n; }
103   friend bool operator!=(const discard_block& x, const discard_block& y)
104   { return !(x == y); }
105 #else
106   // Use a member function; Streamable concept not supported.
107   bool operator==(const discard_block& rhs) const
108   { return _rng == rhs._rng && _n == rhs._n; }
109   bool operator!=(const discard_block& rhs) const
110   { return !(*this == rhs); }
111 #endif
112
113 private:
114   base_type _rng;
115   unsigned int _n;
116 };
117
118 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
119 //  A definition is required even for integral static constants
120 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
121 const bool discard_block<UniformRandomNumberGenerator, p, r>::has_fixed_range;
122 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
123 const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::total_block;
124 template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
125 const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::returned_block;
126 #endif
127
128 } // namespace random
129
130 } // namespace boost
131
132 #endif // BOOST_RANDOM_DISCARD_BLOCK_HPP