Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-13 08:50:31

0001 /* boost random/seed_seq.hpp header file
0002  *
0003  * Copyright Steven Watanabe 2010
0004  * Distributed under the Boost Software License, Version 1.0. (See
0005  * accompanying file LICENSE_1_0.txt or copy at
0006  * http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  * See http://www.boost.org for most recent version including documentation.
0009  *
0010  * $Id$
0011  *
0012  */
0013 
0014 #ifndef BOOST_RANDOM_SEED_SEQ_HPP
0015 #define BOOST_RANDOM_SEED_SEQ_HPP
0016 
0017 #include <boost/config.hpp>
0018 #include <boost/cstdint.hpp>
0019 #include <cstddef>
0020 #include <vector>
0021 #include <algorithm>
0022 #include <iterator>
0023 
0024 #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
0025 #include <initializer_list>
0026 #endif
0027 
0028 namespace boost {
0029 namespace random {
0030 
0031 /**
0032  * The class @c seed_seq stores a sequence of 32-bit words
0033  * for seeding a \pseudo_random_number_generator.  These
0034  * words will be combined to fill the entire state of the
0035  * generator.
0036  */
0037 class seed_seq {
0038 public:
0039     typedef boost::uint_least32_t result_type;
0040 
0041     /** Initializes a seed_seq to hold an empty sequence. */
0042     seed_seq() {}
0043 #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
0044     /** Initializes the sequence from an initializer_list. */
0045     template<class T>
0046     seed_seq(const std::initializer_list<T>& il) : v(il.begin(), il.end()) {}
0047 #endif
0048     /** Initializes the sequence from an iterator range. */
0049     template<class Iter>
0050     seed_seq(Iter first, Iter last) : v(first, last) {}
0051     /** Initializes the sequence from Boost.Range range. */
0052     template<class Range>
0053     explicit seed_seq(const Range& range)
0054       : v(std::begin(range), std::end(range)) {}
0055 
0056     /**
0057      * Fills a range with 32-bit values based on the stored sequence.
0058      *
0059      * Requires: Iter must be a Random Access Iterator whose value type
0060      * is an unsigned integral type at least 32 bits wide.
0061      */
0062     template<class Iter>
0063     void generate(Iter first, Iter last) const
0064     {
0065         typedef typename std::iterator_traits<Iter>::value_type value_type;
0066         std::fill(first, last, static_cast<value_type>(0x8b8b8b8bu));
0067         std::size_t s = v.size();
0068         std::size_t n = last - first;
0069         std::size_t t =
0070             (n >= 623) ? 11 :
0071             (n >=  68) ?  7 :
0072             (n >=  39) ?  5 :
0073             (n >=   7) ?  3 :
0074             (n - 1)/2;
0075         std::size_t p = (n - t) / 2;
0076         std::size_t q = p + t;
0077         std::size_t m = (std::max)(s+1, n);
0078         value_type mask = 0xffffffffu;
0079         for(std::size_t k = 0; k < m; ++k) {
0080             value_type r1 = static_cast<value_type>
0081                 (*(first + k%n) ^ *(first + (k+p)%n) ^ *(first + (k+n-1)%n));
0082             r1 = r1 ^ (r1 >> 27);
0083             r1 = (r1 * 1664525u) & mask;
0084             value_type r2 = static_cast<value_type>(r1 +
0085                 ((k == 0) ? s :
0086                  (k <= s) ? k % n + v[k - 1] :
0087                  (k % n)));
0088             *(first + (k+p)%n) = (*(first + (k+p)%n) + r1) & mask;
0089             *(first + (k+q)%n) = (*(first + (k+q)%n) + r2) & mask;
0090             *(first + k%n) = r2;
0091         }
0092         for(std::size_t k = m; k < m + n; ++k) {
0093             value_type r3 = static_cast<value_type>
0094                 ((*(first + k%n) + *(first + (k+p)%n) + *(first + (k+n-1)%n))
0095                 & mask);
0096             r3 = r3 ^ (r3 >> 27);
0097             r3 = (r3 * 1566083941u) & mask;
0098             value_type r4 = static_cast<value_type>(r3 - k%n);
0099             *(first + (k+p)%n) ^= r3;
0100             *(first + (k+q)%n) ^= r4;
0101             *(first + k%n) = r4;
0102         }
0103     }
0104     /** Returns the size of the sequence. */
0105     std::size_t size() const { return v.size(); }
0106     /** Writes the stored sequence to iter. */
0107     template<class Iter>
0108     void param(Iter out) { std::copy(v.begin(), v.end(), out); }
0109 private:
0110     std::vector<result_type> v;
0111 };
0112 
0113 }
0114 }
0115 
0116 #endif