123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- #ifndef BOOST_RANDOM_INDEPENDENT_BITS_HPP
- #define BOOST_RANDOM_INDEPENDENT_BITS_HPP
- #include <istream>
- #include <iosfwd>
- #include <boost/assert.hpp>
- #include <boost/limits.hpp>
- #include <boost/config.hpp>
- #include <boost/cstdint.hpp>
- #include <boost/integer/integer_mask.hpp>
- #include <boost/random/traits.hpp>
- #include <boost/random/detail/config.hpp>
- #include <boost/random/detail/integer_log2.hpp>
- #include <boost/random/detail/operators.hpp>
- #include <boost/random/detail/seed.hpp>
- #include <boost/random/detail/seed_impl.hpp>
- #include <boost/random/detail/signed_unsigned_tools.hpp>
- namespace boost {
- namespace random {
- template<class Engine, std::size_t w, class UIntType>
- class independent_bits_engine
- {
- public:
- typedef Engine base_type;
- typedef UIntType result_type;
- typedef typename Engine::result_type base_result_type;
-
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
-
- static BOOST_CONSTEXPR result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return 0; }
-
- static BOOST_CONSTEXPR result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return max_imp(boost::is_integral<UIntType>()); }
-
- independent_bits_engine() { }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(independent_bits_engine,
- base_result_type, seed_arg)
- {
- _base.seed(seed_arg);
- }
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(independent_bits_engine,
- SeedSeq, seq)
- { _base.seed(seq); }
-
- independent_bits_engine(const base_type& base_arg) : _base(base_arg) {}
-
- template<class It>
- independent_bits_engine(It& first, It last) : _base(first, last) { }
-
- void seed() { _base.seed(); }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(independent_bits_engine,
- base_result_type, seed_arg)
- { _base.seed(seed_arg); }
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(independent_bits_engine,
- SeedSeq, seq)
- { _base.seed(seq); }
-
- template<class It> void seed(It& first, It last)
- { _base.seed(first, last); }
-
- result_type operator()()
- {
-
-
-
- base_unsigned range =
- detail::subtract<base_result_type>()((_base.max)(), (_base.min)());
- std::size_t m =
- (range == (std::numeric_limits<base_unsigned>::max)()) ?
- std::numeric_limits<base_unsigned>::digits :
- detail::integer_log2(range + 1);
- std::size_t n = (w + m - 1) / m;
- std::size_t w0, n0;
- base_unsigned y0, y1;
- base_unsigned y0_mask, y1_mask;
- calc_params(n, range, w0, n0, y0, y1, y0_mask, y1_mask);
- if(base_unsigned(range - y0 + 1) > y0 / n) {
-
- ++n;
- calc_params(n, range, w0, n0, y0, y1, y0_mask, y1_mask);
- }
- BOOST_ASSERT(n0*w0 + (n - n0)*(w0 + 1) == w);
- BOOST_ASSERT((n == 1) == (w0 == w));
-
- if(n == 1) {
- BOOST_ASSERT(n0 == 1);
- base_unsigned u;
- do {
- u = detail::subtract<base_result_type>()(_base(), (_base.min)());
- } while(u > base_unsigned(y0 - 1));
- return u & y0_mask;
- }
- result_type S = 0;
- for(std::size_t k = 0; k < n0; ++k) {
- base_unsigned u;
- do {
- u = detail::subtract<base_result_type>()(_base(), (_base.min)());
- } while(u > base_unsigned(y0 - 1));
- S = (S << w0) + (u & y0_mask);
- }
- for(std::size_t k = 0; k < (n - n0); ++k) {
- base_unsigned u;
- do {
- u = detail::subtract<base_result_type>()(_base(), (_base.min)());
- } while(u > base_unsigned(y1 - 1));
- S = (S << (w0 + 1)) + (u & y1_mask);
- }
- return S;
- }
-
-
- template<class Iter>
- void generate(Iter first, Iter last)
- { detail::generate_from_int(*this, first, last); }
-
- void discard(boost::uintmax_t z)
- {
- for(boost::uintmax_t i = 0; i < z; ++i) {
- (*this)();
- }
- }
- const base_type& base() const { return _base; }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, independent_bits_engine, r)
- {
- os << r._base;
- return os;
- }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, independent_bits_engine, r)
- {
- is >> r._base;
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(independent_bits_engine, x, y)
- { return x._base == y._base; }
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(independent_bits_engine)
- private:
-
- typedef typename boost::random::traits::make_unsigned<base_result_type>::type base_unsigned;
- static BOOST_CONSTEXPR UIntType max_imp(const boost::true_type&)
- {
- return boost::low_bits_mask_t<w>::sig_bits;
- }
- static UIntType max_imp(const boost::false_type&)
- {
-
- BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_specialized);
- return w < std::numeric_limits<UIntType>::digits ? UIntType((UIntType(1) << w) - 1) : UIntType((((UIntType(1) << (w - 1)) - 1) << 1) | 1u);
- }
- void calc_params(
- std::size_t n, base_unsigned range,
- std::size_t& w0, std::size_t& n0,
- base_unsigned& y0, base_unsigned& y1,
- base_unsigned& y0_mask, base_unsigned& y1_mask)
- {
- BOOST_ASSERT(w >= n);
- w0 = w/n;
- n0 = n - w % n;
- y0_mask = (base_unsigned(2) << (w0 - 1)) - 1;
- y1_mask = (y0_mask << 1) | 1;
- y0 = (range + 1) & ~y0_mask;
- y1 = (range + 1) & ~y1_mask;
- BOOST_ASSERT(y0 != 0 || base_unsigned(range + 1) == 0);
- }
-
- Engine _base;
- };
- #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
- template<class Engine, std::size_t w, class UIntType>
- const bool independent_bits_engine<Engine, w, UIntType>::has_fixed_range;
- #endif
- }
- }
- #endif
|