2 * Copyright 2008-2013 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 /*! \file discard_block_engine.h
19 * \brief A random number engine which adapts a base engine and produces
20 * numbers by discarding all but a contiguous blocks of its values.
25 #include <thrust/detail/config.h>
27 #include <thrust/detail/config.h>
29 #include <thrust/detail/cstdint.h>
30 #include <thrust/random/detail/random_core_access.h>
38 /*! \addtogroup random_number_engine_adaptors Random Number Engine Adaptor Class Templates
43 /*! \class discard_block_engine
44 * \brief A \p discard_block_engine adapts an existing base random number engine and produces
45 * random values by discarding some of the values returned by its base engine.
46 * Each cycle of the compound engine begins by returning \c r values successively produced
47 * by the base engine and ends by discarding <tt>p-r</tt> such values. The engine's state
48 * is the state of its base engine followed by the number of calls to <tt>operator()</tt>
49 * that have occurred since the beginning of the current cycle.
51 * \tparam Engine The type of the base random number engine to adapt.
52 * \tparam p The discard cycle length.
53 * \tparam r The number of values to return of the base engine. Because <tt>p-r</tt> will be
54 * discarded, <tt>r <= p</tt>.
56 * The following code snippet shows an example of using a \p discard_block_engine instance:
59 * #include <thrust/random/linear_congruential_engine.h>
60 * #include <thrust/random/discard_block_engine.h>
65 * // create a discard_block_engine from minstd_rand, with a cycle length of 13
66 * // keep every first 10 values, and discard the next 3
67 * thrust::discard_block_engine<thrust::minstd_rand, 13, 10> rng;
69 * // print a random number to standard output
70 * std::cout << rng() << std::endl;
76 template<typename Engine, size_t p, size_t r>
77 class discard_block_engine
82 /*! \typedef base_type
83 * \brief The type of the adapted base random number engine.
85 typedef Engine base_type;
87 /*! \typedef result_type
88 * \brief The type of the unsigned integer produced by this \p linear_congruential_engine.
90 typedef typename base_type::result_type result_type;
92 // engine characteristics
94 /*! The length of the production cycle.
96 static const size_t block_size = p;
98 /*! The number of used numbers per production cycle.
100 static const size_t used_block = r;
102 /*! The smallest value this \p discard_block_engine may potentially produce.
104 static const result_type min = base_type::min;
106 /*! The largest value this \p discard_block_engine may potentially produce.
108 static const result_type max = base_type::max;
110 // constructors and seeding functions
112 /*! This constructor constructs a new \p discard_block_engine and constructs
113 * its \p base_type engine using its null constructor.
116 discard_block_engine();
118 /*! This constructor constructs a new \p discard_block_engine using
119 * a given \p base_type engine to initialize its adapted base engine.
121 * \param urng A \p base_type to use to initialize this \p discard_block_engine's
122 * adapted base engine.
125 explicit discard_block_engine(const base_type &urng);
127 /*! This constructor initializes a new \p discard_block_engine with a given seed.
129 * \param s The seed used to intialize this \p discard_block_engine's adapted base engine.
132 explicit discard_block_engine(result_type s);
134 /*! This method initializes the state of this \p discard_block_engine's adapted base engine
135 * by using its \p default_seed value.
140 /*! This method initializes the state of this \p discard_block_engine's adapted base engine
141 * by using the given seed.
143 * \param s The seed with which to intialize this \p discard_block_engine's adapted base engine.
146 void seed(result_type s);
148 // generating functions
150 /*! This member function produces a new random value and updates this \p discard_block_engine's state.
151 * \return A new random number.
154 result_type operator()(void);
156 /*! This member function advances this \p discard_block_engine's state a given number of times
157 * and discards the results.
159 * \param z The number of random values to discard.
160 * \note This function is provided because an implementation may be able to accelerate it.
163 void discard(unsigned long long z);
165 // property functions
167 /*! This member function returns a const reference to this \p discard_block_engine's
168 * adapted base engine.
170 * \return A const reference to the base engine this \p discard_block_engine adapts.
173 const base_type &base(void) const;
181 friend struct thrust::random::detail::random_core_access;
184 bool equal(const discard_block_engine &rhs) const;
186 template<typename CharT, typename Traits>
187 std::basic_ostream<CharT,Traits>& stream_out(std::basic_ostream<CharT,Traits> &os) const;
189 template<typename CharT, typename Traits>
190 std::basic_istream<CharT,Traits>& stream_in(std::basic_istream<CharT,Traits> &is);
193 }; // end discard_block_engine
196 /*! This function checks two \p discard_block_engines for equality.
197 * \param lhs The first \p discard_block_engine to test.
198 * \param rhs The second \p discard_block_engine to test.
199 * \return \c true if \p lhs is equal to \p rhs; \c false, otherwise.
201 template<typename Engine, size_t p, size_t r>
203 bool operator==(const discard_block_engine<Engine,p,r> &lhs,
204 const discard_block_engine<Engine,p,r> &rhs);
207 /*! This function checks two \p discard_block_engines for inequality.
208 * \param lhs The first \p discard_block_engine to test.
209 * \param rhs The second \p discard_block_engine to test.
210 * \return \c true if \p lhs is not equal to \p rhs; \c false, otherwise.
212 template<typename Engine, size_t p, size_t r>
214 bool operator!=(const discard_block_engine<Engine,p,r> &lhs,
215 const discard_block_engine<Engine,p,r> &rhs);
218 /*! This function streams a discard_block_engine to a \p std::basic_ostream.
219 * \param os The \p basic_ostream to stream out to.
220 * \param e The \p discard_block_engine to stream out.
223 template<typename Engine, size_t p, size_t r,
224 typename CharT, typename Traits>
225 std::basic_ostream<CharT,Traits>&
226 operator<<(std::basic_ostream<CharT,Traits> &os,
227 const discard_block_engine<Engine,p,r> &e);
230 /*! This function streams a discard_block_engine in from a std::basic_istream.
231 * \param is The \p basic_istream to stream from.
232 * \param e The \p discard_block_engine to stream in.
235 template<typename Engine, size_t p, size_t r,
236 typename CharT, typename Traits>
237 std::basic_istream<CharT,Traits>&
238 operator>>(std::basic_istream<CharT,Traits> &is,
239 discard_block_engine<Engine,p,r> &e);
241 /*! \} // end random_number_engine_adaptors
246 // import names into thrust::
247 using random::discard_block_engine;
251 #include <thrust/random/detail/discard_block_engine.inl>