123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #ifndef BOOST_RANDOM_DISCARD_BLOCK_HPP
- #define BOOST_RANDOM_DISCARD_BLOCK_HPP
- #include <iostream>
- #include <boost/config.hpp>
- #include <boost/cstdint.hpp>
- #include <boost/limits.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/random/detail/config.hpp>
- #include <boost/random/detail/seed.hpp>
- #include <boost/random/detail/seed_impl.hpp>
- namespace boost {
- namespace random {
- template<class UniformRandomNumberGenerator, std::size_t p, std::size_t r>
- class discard_block_engine
- {
- typedef typename detail::seed_type<
- typename UniformRandomNumberGenerator::result_type>::type seed_type;
- public:
- typedef UniformRandomNumberGenerator base_type;
- typedef typename base_type::result_type result_type;
- BOOST_STATIC_CONSTANT(std::size_t, block_size = p);
- BOOST_STATIC_CONSTANT(std::size_t, used_block = r);
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
- BOOST_STATIC_CONSTANT(std::size_t, total_block = p);
- BOOST_STATIC_CONSTANT(std::size_t, returned_block = r);
- BOOST_STATIC_ASSERT(total_block >= returned_block);
-
- discard_block_engine() : _rng(), _n(0) { }
-
- explicit discard_block_engine(const base_type & rng) : _rng(rng), _n(0) { }
- #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
-
- explicit discard_block_engine(base_type && rng) : _rng(rng), _n(0) { }
- #endif
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(discard_block_engine,
- seed_type, value)
- { _rng.seed(value); _n = 0; }
-
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(discard_block_engine, SeedSeq, seq)
- { _rng.seed(seq); _n = 0; }
-
-
- template<class It> discard_block_engine(It& first, It last)
- : _rng(first, last), _n(0) { }
-
-
- void seed() { _rng.seed(); _n = 0; }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(discard_block_engine, seed_type, s)
- { _rng.seed(s); _n = 0; }
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(discard_block_engine, SeedSeq, seq)
- { _rng.seed(seq); _n = 0; }
-
- template<class It> void seed(It& first, It last)
- { _rng.seed(first, last); _n = 0; }
-
- const base_type& base() const { return _rng; }
-
- result_type operator()()
- {
- if(_n >= returned_block) {
-
-
-
-
- for(std::size_t i = 0; i < total_block - _n; ++i) {
- _rng();
- }
- _n = 0;
- }
- ++_n;
- return _rng();
- }
- void discard(boost::uintmax_t z)
- {
- for(boost::uintmax_t j = 0; j < z; ++j) {
- (*this)();
- }
- }
- template<class It>
- void generate(It first, It last)
- { detail::generate(*this, first, last); }
-
- static BOOST_CONSTEXPR result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return (base_type::min)(); }
-
- static BOOST_CONSTEXPR result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return (base_type::max)(); }
- #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
-
- template<class CharT, class Traits>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& os,
- const discard_block_engine& s)
- {
- os << s._rng << ' ' << s._n;
- return os;
- }
-
- template<class CharT, class Traits>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& is, discard_block_engine& s)
- {
- is >> s._rng >> std::ws >> s._n;
- return is;
- }
- #endif
-
- friend bool operator==(const discard_block_engine& x,
- const discard_block_engine& y)
- { return x._rng == y._rng && x._n == y._n; }
-
- friend bool operator!=(const discard_block_engine& x,
- const discard_block_engine& y)
- { return !(x == y); }
- private:
- base_type _rng;
- std::size_t _n;
- };
- #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
- template<class URNG, std::size_t p, std::size_t r>
- const bool discard_block_engine<URNG, p, r>::has_fixed_range;
- template<class URNG, std::size_t p, std::size_t r>
- const std::size_t discard_block_engine<URNG, p, r>::total_block;
- template<class URNG, std::size_t p, std::size_t r>
- const std::size_t discard_block_engine<URNG, p, r>::returned_block;
- template<class URNG, std::size_t p, std::size_t r>
- const std::size_t discard_block_engine<URNG, p, r>::block_size;
- template<class URNG, std::size_t p, std::size_t r>
- const std::size_t discard_block_engine<URNG, p, r>::used_block;
- #endif
- template<class URNG, int p, int r>
- class discard_block : public discard_block_engine<URNG, p, r>
- {
- typedef discard_block_engine<URNG, p, r> base_t;
- public:
- typedef typename base_t::result_type result_type;
- discard_block() {}
- template<class T>
- discard_block(T& arg) : base_t(arg) {}
- template<class T>
- discard_block(const T& arg) : base_t(arg) {}
- template<class It>
- discard_block(It& first, It last) : base_t(first, last) {}
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return (this->base().min)(); }
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return (this->base().max)(); }
- };
- namespace detail {
- template<class Engine>
- struct generator_bits;
-
- template<class URNG, std::size_t p, std::size_t r>
- struct generator_bits<discard_block_engine<URNG, p, r> > {
- static std::size_t value() { return generator_bits<URNG>::value(); }
- };
- template<class URNG, int p, int r>
- struct generator_bits<discard_block<URNG, p, r> > {
- static std::size_t value() { return generator_bits<URNG>::value(); }
- };
- }
- }
- }
- #endif
|