RandomIterator
Stream.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------
2 *
3 * Copyright (C) 2021 - 2024 Antonio Augusto Alves Junior
4 *
5 * This file is part of RandomIterator.
6 
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation and/or
15 * other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its contributors
18 * may be used to endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * ----------------------------------------------------------------------------*/
34 /*
35  * Stream.hpp
36  *
37  * Created on: 23/02/2021
38  * Author: Antonio Augusto Alves Junior
39  */
40 
41 #pragma once
42 
43 #include <stdint.h>
44 
45 #include "detail/tbb/iterators.h"
46 #include "detail/Engine.hpp"
47 #include "detail/functors/EngineCaller.hpp"
48 
49 namespace random_iterator {
50 
51 
64 
65 #if RANDOM_ITERATOR_R123_USE_AES_NI
67 typedef detail::ars ars;
68 #endif
69 
70 
130 template<typename DistibutionType, typename EngineType>
131 class Stream
132 {
133  typedef detail::EngineCaller<DistibutionType,EngineType> caller_type;
134  typedef random_iterator_tbb::counting_iterator<typename EngineType::advance_type> counting_iterator;
135 
136 public:
137 
138  typedef EngineType engine_type;
139  typedef typename engine_type::state_type state_type;
140  typedef typename engine_type::seed_type seed_type;
141  typedef typename engine_type::advance_type advance_type;
142  typedef typename engine_type::init_type init_type;
143 
144  typedef DistibutionType distribution_type;
145  typedef typename distribution_type::result_type result_type;
146 
147  typedef random_iterator_tbb::transform_iterator< caller_type, counting_iterator> iterator;
148 
149  Stream()=delete;
150 
158  Stream(distribution_type const& distribution, seed_type seed, uint32_t stream=0):
159  distribution_(distribution),
160  engine_(seed, stream),
161  seed_(seed),
162  stream_(stream)
163  {
164  distribution_.reset();
165  }
166 
173  distribution_(other.getDistribution()),
174  engine_(other.getEngine()),
175  seed_(other.getSeed()),
176  stream_(other.getStream())
177  { }
178 
183  inline iterator begin() const{
184 
185  counting_iterator first( 0);
186  caller_type caller(distribution_, seed_, stream_ );
187 
188  return iterator(first, caller);
189  }
190 
195  inline iterator end() const{
196 
197  counting_iterator last(std::numeric_limits<advance_type>::max());
198  caller_type caller(distribution_, seed_, stream_ );
199 
200  return iterator(last, caller) ;
201  }
202 
210 
211  counting_iterator first( 0);
212  caller_type caller(distribution_, seed_, stream_ );
213 
214  return caller(n);
215  }
216 
222  inline result_type operator()(void){
223 
224  return distribution_(engine_);
225  }
226 
233  return distribution_;
234  }
235 
241  inline void setDistribution(distribution_type dist) {
242  distribution_ = dist;
243  }
244 
250  inline engine_type const& getEngine() const {
251  return engine_;
252  }
253 
259  inline void setEngine(engine_type engine) {
260  engine_ = engine;
261  }
262 
268  inline const seed_type& getSeed() const {
269  return seed_;
270  }
271 
277  inline void setSeed(const seed_type& seed) {
278  seed_ = seed;
279  }
280 
286  inline uint32_t getStream() const {
287  return stream_;
288  }
289 
295  inline void setStream(uint32_t stream) {
296  stream_ = stream;
297  }
298 
299  friend inline std::ostream& operator<<(std::ostream& os, const Stream<DistibutionType, EngineType>& be){
300  return os << "engine: " << be.getEngine()
301  << " stream: "<< be.getStream()
302  << " distribution: " << be.getDistribution() ;
303  }
304 private:
305 
306  distribution_type distribution_;
307  engine_type engine_;
308  seed_type seed_;
309  uint32_t stream_;
310 
311 };
312 
321 template<typename DistibutionType, typename EngineType>
322 Stream<DistibutionType, EngineType>
323 make_stream( DistibutionType const& dist, EngineType const& eng, uint32_t stream ){
324 
325  return Stream<DistibutionType, EngineType>( dist, eng.getSeed(), stream);
326 }
327 
328 
329 } // namespace random_iterator
representation for streams of pseudorandom numbers
Definition: Stream.hpp:132
const seed_type & getSeed() const
Get the seed.
Definition: Stream.hpp:268
result_type operator[](advance_type n) const
Returns the element at specified location n.
Definition: Stream.hpp:209
void setStream(uint32_t stream)
Set the object's stream number.
Definition: Stream.hpp:295
void setEngine(engine_type engine)
Set the object's engine.
Definition: Stream.hpp:259
EngineType engine_type
Definition: Stream.hpp:138
void setSeed(const seed_type &seed)
Set the seed.
Definition: Stream.hpp:277
iterator end() const
Returns an iterator to the past last element of the stream.
Definition: Stream.hpp:195
distribution_type getDistribution() const
Get the distribution.
Definition: Stream.hpp:232
distribution_type::result_type result_type
type of the STL compliant distribution
Definition: Stream.hpp:145
Stream()=delete
type of stream iterator
engine_type::seed_type seed_type
Type of RNG state.
Definition: Stream.hpp:140
random_iterator_tbb::transform_iterator< caller_type, counting_iterator > iterator
type of the result
Definition: Stream.hpp:147
engine_type::init_type init_type
Type of RNG displacement.
Definition: Stream.hpp:142
result_type operator()(void)
At each call, this function will produce a pseudorandom number and advance the engine state.
Definition: Stream.hpp:222
void setDistribution(distribution_type dist)
Set the distribution.
Definition: Stream.hpp:241
engine_type const & getEngine() const
Get the associated engine.
Definition: Stream.hpp:250
Stream(distribution_type const &distribution, seed_type seed, uint32_t stream=0)
Constructor.
Definition: Stream.hpp:158
DistibutionType distribution_type
Type of RNG initializer.
Definition: Stream.hpp:144
friend std::ostream & operator<<(std::ostream &os, const Stream< DistibutionType, EngineType > &be)
Definition: Stream.hpp:299
iterator begin() const
Returns an iterator to the first element of the stream.
Definition: Stream.hpp:183
engine_type::advance_type advance_type
Type of RNG seed.
Definition: Stream.hpp:141
Stream(Stream< DistibutionType, EngineType > const &other)
Copy constructor.
Definition: Stream.hpp:172
engine_type::state_type state_type
Engine type.
Definition: Stream.hpp:139
uint32_t getStream() const
Get the object's stream number.
Definition: Stream.hpp:286
random_iterator is the top-level namespace which contains all RandomIterator functions and types.
detail::squares3_128 squares3_128
squares3_128 engine
Definition: Stream.hpp:57
detail::squares3_64 squares3_64
squares3_64 engine
Definition: Stream.hpp:61
detail::squares4_128 squares4_128
squares4_128 engine
Definition: Stream.hpp:59
detail::philox philox
philox engine
Definition: Stream.hpp:53
detail::squares4_64 squares4_64
squares4_64 engine
Definition: Stream.hpp:63
Stream< DistibutionType, EngineType > make_stream(DistibutionType const &dist, EngineType const &eng, uint32_t stream)
Stream.
Definition: Stream.hpp:323
detail::threefry threefry
threefry engine
Definition: Stream.hpp:55