RandomIterator
Stream.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------
2  *
3  * Copyright (C) 2021 Antonio Augusto Alves Junior
4  *
5  * This file is part of RandomIterator.
6  *
7  * RandomIterator is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * RandomIterator is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with RandomIterator. If not, see <http://www.gnu.org/licenses/>.
19  *
20  *---------------------------------------------------------------------------*/
21 /*
22  * Stream.hpp
23  *
24  * Created on: 23/02/2021
25  * Author: Antonio Augusto Alves Junior
26  */
27 
28 #pragma once
29 
30 #include <stdint.h>
31 #include <random_iterator/detail/tbb/iterators.h>
32 #include <random_iterator/detail/Engine.hpp>
33 #include <random_iterator/detail/functors/EngineCaller.hpp>
34 
35 namespace random_iterator {
36 
49 
50 #if RANDOM_ITERATOR_R123_USE_AES_NI
51 typedef detail::ars ars;
53 #endif
54 
55 
115 template<typename DistibutionType, typename EngineType>
116 class Stream
117 {
118  typedef detail::EngineCaller<DistibutionType,EngineType> caller_type;
119  typedef random_iterator_tbb::counting_iterator<typename EngineType::advance_type> counting_iterator;
120 
121 public:
122 
123  typedef EngineType engine_type;
124  typedef typename engine_type::state_type state_type;
125  typedef typename engine_type::seed_type seed_type;
126  typedef typename engine_type::advance_type advance_type;
127  typedef typename engine_type::init_type init_type;
128 
129  typedef DistibutionType distribution_type;
130  typedef typename distribution_type::result_type result_type;
131 
132  typedef random_iterator_tbb::transform_iterator< caller_type, counting_iterator> iterator;
133 
134  Stream()=delete;
135 
143  Stream(distribution_type const& distribution, seed_type seed, uint32_t stream=0):
144  distribution_(distribution),
145  engine_(seed, stream),
146  seed_(seed),
147  stream_(stream)
148  {}
149 
156  distribution_(other.getDistribution()),
157  engine_(other.getEngine()),
158  seed_(other.getSeed()),
159  stream_(other.getStream())
160  {}
161 
166  inline iterator begin() const{
167 
168  counting_iterator first( 0);
169  caller_type caller(distribution_, seed_, stream_ );
170 
171  return iterator(first, caller);
172  }
173 
178  inline iterator end() const{
179 
180  counting_iterator last(std::numeric_limits<advance_type>::max());
181  caller_type caller(distribution_, seed_, stream_ );
182 
183  return iterator(last, caller) ;
184  }
185 
192  inline result_type operator[](advance_type n) const{
193 
194  caller_type caller(distribution_, seed_, stream_ );
195 
196  return caller(n);
197  }
198 
204  inline result_type operator()(void){
205 
206  return distribution_(engine_);
207  }
208 
214  inline distribution_type getDistribution() const {
215  return distribution_;
216  }
217 
223  inline void setDistribution(distribution_type dist) {
224  distribution_ = dist;
225  }
226 
232  inline engine_type getEngine() const {
233  return engine_;
234  }
235 
241  inline void setEngine(engine_type engine) {
242  engine_ = engine;
243  }
244 
250  inline const seed_type& getSeed() const {
251  return seed_;
252  }
253 
259  inline void setSeed(const seed_type& seed) {
260  seed_ = seed;
261  }
262 
268  inline uint32_t getStream() const {
269  return stream_;
270  }
271 
277  inline void setStream(uint32_t stream) {
278  stream_ = stream;
279  }
280 
281 private:
282 
283  distribution_type distribution_;
284  engine_type engine_;
285  seed_type seed_;
286  uint32_t stream_;
287 
288 };
289 
298 template<typename DistibutionType, typename EngineType>
300 make_stream( DistibutionType const& dist, EngineType eng, uint32_t stream ){
301 
302  return Stream<DistibutionType, EngineType>( dist, eng.getSeed(), stream);
303 }
304 
305 } // namespace random_iterator
EngineType engine_type
Definition: Stream.hpp:123
detail::squares3_64 squares3_64
squares3_64 engine
Definition: Stream.hpp:46
Stream< DistibutionType, EngineType > make_stream(DistibutionType const &dist, EngineType eng, uint32_t stream)
Stream.
Definition: Stream.hpp:300
result_type operator[](advance_type n) const
Returns the element at specified location n.
Definition: Stream.hpp:192
const seed_type & getSeed() const
Get the seed.
Definition: Stream.hpp:250
detail::threefry threefry
threefry engine
Definition: Stream.hpp:40
result_type operator()(void)
At each call, this function will produce a pseudorandom number and advance the engine state...
Definition: Stream.hpp:204
iterator end() const
Returns an iterator to the past last element of the stream.
Definition: Stream.hpp:178
uint32_t getStream() const
Get the object&#39;s stream number.
Definition: Stream.hpp:268
distribution_type::result_type result_type
type of the STL compliant distribution
Definition: Stream.hpp:130
engine_type::seed_type seed_type
Type of RNG state.
Definition: Stream.hpp:125
random_iterator is the top-level namespace which contains all RandomIterator functions and types...
detail::squares4_128 squares4_128
squares4_128 engine
Definition: Stream.hpp:44
representation for streams of pseudorandom numbers
Definition: Stream.hpp:116
engine_type::state_type state_type
Engine type.
Definition: Stream.hpp:124
engine_type::advance_type advance_type
Type of RNG seed.
Definition: Stream.hpp:126
distribution_type getDistribution() const
Get the distribution.
Definition: Stream.hpp:214
DistibutionType distribution_type
Type of RNG initializer.
Definition: Stream.hpp:129
void setDistribution(distribution_type dist)
Set the distribution.
Definition: Stream.hpp:223
void setStream(uint32_t stream)
Set the object&#39;s stream number.
Definition: Stream.hpp:277
void setEngine(engine_type engine)
Set the object&#39;s engine.
Definition: Stream.hpp:241
Stream(Stream< DistibutionType, EngineType > const &other)
Copy constructor.
Definition: Stream.hpp:155
detail::squares4_64 squares4_64
squares4_64 engine
Definition: Stream.hpp:48
Stream(distribution_type const &distribution, seed_type seed, uint32_t stream=0)
Constructor.
Definition: Stream.hpp:143
random_iterator_tbb::transform_iterator< caller_type, counting_iterator > iterator
type of the result
Definition: Stream.hpp:132
void setSeed(const seed_type &seed)
Set the seed.
Definition: Stream.hpp:259
engine_type getEngine() const
Get the associated engine.
Definition: Stream.hpp:232
engine_type::init_type init_type
Type of RNG displacement.
Definition: Stream.hpp:127
Stream()=delete
type of stream iterator
detail::philox philox
philox engine
Definition: Stream.hpp:38
iterator begin() const
Returns an iterator to the first element of the stream.
Definition: Stream.hpp:166
detail::squares3_128 squares3_128
squares3_128 engine
Definition: Stream.hpp:42