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 
32 #include "detail/tbb/iterators.h"
33 #include "detail/Engine.hpp"
34 #include "detail/functors/EngineCaller.hpp"
35 
36 namespace random_iterator {
37 
38 
51 
52 #if RANDOM_ITERATOR_R123_USE_AES_NI
53 typedef detail::ars ars;
55 #endif
56 
57 
117 template<typename DistibutionType, typename EngineType>
118 class Stream
119 {
120  typedef detail::EngineCaller<DistibutionType,EngineType> caller_type;
121  typedef random_iterator_tbb::counting_iterator<typename EngineType::advance_type> counting_iterator;
122 
123 public:
124 
125  typedef EngineType engine_type;
126  typedef typename engine_type::state_type state_type;
127  typedef typename engine_type::seed_type seed_type;
128  typedef typename engine_type::advance_type advance_type;
129  typedef typename engine_type::init_type init_type;
130 
131  typedef DistibutionType distribution_type;
132  typedef typename distribution_type::result_type result_type;
133 
134  typedef random_iterator_tbb::transform_iterator< caller_type, counting_iterator> iterator;
135 
136  Stream()=delete;
137 
145  Stream(distribution_type const& distribution, seed_type seed, uint32_t stream=0):
146  distribution_(distribution),
147  engine_(seed, stream),
148  seed_(seed),
149  stream_(stream)
150  {}
151 
158  distribution_(other.getDistribution()),
159  engine_(other.getEngine()),
160  seed_(other.getSeed()),
161  stream_(other.getStream())
162  {}
163 
168  inline iterator begin() const{
169 
170  counting_iterator first( 0);
171  caller_type caller(distribution_, seed_, stream_ );
172 
173  return iterator(first, caller);
174  }
175 
180  inline iterator end() const{
181 
182  counting_iterator last(std::numeric_limits<advance_type>::max());
183  caller_type caller(distribution_, seed_, stream_ );
184 
185  return iterator(last, caller) ;
186  }
187 
194  inline result_type operator[](advance_type n) const{
195 
196  caller_type caller(distribution_, seed_, stream_ );
197 
198  return caller(n);
199  }
200 
206  inline result_type operator()(void){
207 
208  return distribution_(engine_);
209  }
210 
216  inline distribution_type getDistribution() const {
217  return distribution_;
218  }
219 
225  inline void setDistribution(distribution_type dist) {
226  distribution_ = dist;
227  }
228 
234  inline engine_type getEngine() const {
235  return engine_;
236  }
237 
243  inline void setEngine(engine_type engine) {
244  engine_ = engine;
245  }
246 
252  inline const seed_type& getSeed() const {
253  return seed_;
254  }
255 
261  inline void setSeed(const seed_type& seed) {
262  seed_ = seed;
263  }
264 
270  inline uint32_t getStream() const {
271  return stream_;
272  }
273 
279  inline void setStream(uint32_t stream) {
280  stream_ = stream;
281  }
282 
283  friend inline std::ostream& operator<<(std::ostream& os, const Stream<DistibutionType, EngineType>& be){
284  return os << "engine: " << be.getEngine()
285  << " stream: "<< be.getStream()
286  << " distribution: " << be.getDistribution() ;
287  }
288 private:
289 
290  distribution_type distribution_;
291  engine_type engine_;
292  seed_type seed_;
293  uint32_t stream_;
294 
295 };
296 
305 template<typename DistibutionType, typename EngineType>
307 make_stream( DistibutionType const& dist, EngineType eng, uint32_t stream ){
308 
309  return Stream<DistibutionType, EngineType>( dist, eng.getSeed(), stream);
310 }
311 
312 
313 } // namespace random_iterator
EngineType engine_type
Definition: Stream.hpp:125
detail::squares3_64 squares3_64
squares3_64 engine
Definition: Stream.hpp:48
Stream< DistibutionType, EngineType > make_stream(DistibutionType const &dist, EngineType eng, uint32_t stream)
Stream.
Definition: Stream.hpp:307
result_type operator[](advance_type n) const
Returns the element at specified location n.
Definition: Stream.hpp:194
const seed_type & getSeed() const
Get the seed.
Definition: Stream.hpp:252
detail::threefry threefry
threefry engine
Definition: Stream.hpp:42
result_type operator()(void)
At each call, this function will produce a pseudorandom number and advance the engine state...
Definition: Stream.hpp:206
iterator end() const
Returns an iterator to the past last element of the stream.
Definition: Stream.hpp:180
uint32_t getStream() const
Get the object&#39;s stream number.
Definition: Stream.hpp:270
distribution_type::result_type result_type
type of the STL compliant distribution
Definition: Stream.hpp:132
engine_type::seed_type seed_type
Type of RNG state.
Definition: Stream.hpp:127
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:46
representation for streams of pseudorandom numbers
Definition: Stream.hpp:118
engine_type::state_type state_type
Engine type.
Definition: Stream.hpp:126
engine_type::advance_type advance_type
Type of RNG seed.
Definition: Stream.hpp:128
distribution_type getDistribution() const
Get the distribution.
Definition: Stream.hpp:216
DistibutionType distribution_type
Type of RNG initializer.
Definition: Stream.hpp:131
void setDistribution(distribution_type dist)
Set the distribution.
Definition: Stream.hpp:225
void setStream(uint32_t stream)
Set the object&#39;s stream number.
Definition: Stream.hpp:279
void setEngine(engine_type engine)
Set the object&#39;s engine.
Definition: Stream.hpp:243
Stream(Stream< DistibutionType, EngineType > const &other)
Copy constructor.
Definition: Stream.hpp:157
detail::squares4_64 squares4_64
squares4_64 engine
Definition: Stream.hpp:50
Stream(distribution_type const &distribution, seed_type seed, uint32_t stream=0)
Constructor.
Definition: Stream.hpp:145
random_iterator_tbb::transform_iterator< caller_type, counting_iterator > iterator
type of the result
Definition: Stream.hpp:134
void setSeed(const seed_type &seed)
Set the seed.
Definition: Stream.hpp:261
engine_type getEngine() const
Get the associated engine.
Definition: Stream.hpp:234
engine_type::init_type init_type
Type of RNG displacement.
Definition: Stream.hpp:129
Stream()=delete
type of stream iterator
detail::philox philox
philox engine
Definition: Stream.hpp:40
iterator begin() const
Returns an iterator to the first element of the stream.
Definition: Stream.hpp:168
detail::squares3_128 squares3_128
squares3_128 engine
Definition: Stream.hpp:44