123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #ifndef BOOST_RANDOM_SHUFFLE_ORDER_HPP
- #define BOOST_RANDOM_SHUFFLE_ORDER_HPP
- #include <iostream>
- #include <algorithm> // std::copy
- #include <cassert>
- #include <boost/config.hpp>
- #include <boost/limits.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/cstdint.hpp>
- #include <boost/random/detail/operators.hpp>
- #include <boost/random/detail/seed.hpp>
- #include <boost/random/detail/signed_unsigned_tools.hpp>
- #include <boost/random/linear_congruential.hpp>
- #include <boost/random/detail/disable_warnings.hpp>
- namespace boost {
- namespace random {
- template<class UniformRandomNumberGenerator, std::size_t k>
- class shuffle_order_engine
- {
- public:
- typedef UniformRandomNumberGenerator base_type;
- typedef typename base_type::result_type result_type;
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
- BOOST_STATIC_CONSTANT(std::size_t, buffer_size = k);
- BOOST_STATIC_CONSTANT(std::size_t, table_size = k);
- BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
- BOOST_STATIC_ASSERT(k > 0);
-
- shuffle_order_engine() : _rng() { init(); }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(shuffle_order_engine,
- result_type, s)
- { _rng.seed(s); init(); }
- BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(shuffle_order_engine, SeedSeq, seq)
- { _rng.seed(seq); init(); }
-
- explicit shuffle_order_engine(const base_type & rng) : _rng(rng) { init(); }
- #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
- explicit shuffle_order_engine(base_type&& rng) : _rng(rng) { init(); }
- #endif
- template<class It> shuffle_order_engine(It& first, It last)
- : _rng(first, last) { init(); }
- void seed() { _rng.seed(); init(); }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(shuffle_order_engine,
- result_type, seed_arg)
- { _rng.seed(seed_arg); init(); }
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(shuffle_order_engine, SeedSeq, seq)
- { _rng.seed(seq); init(); }
- template<class It> void seed(It& first, It last)
- { _rng.seed(first, last); init(); }
- const base_type& base() const { return _rng; }
- result_type operator()() {
-
-
- typedef typename boost::random::traits::make_unsigned<result_type>::type base_unsigned;
- const base_unsigned brange =
- detail::subtract<result_type>()((max)(), (min)());
- const base_unsigned off =
- detail::subtract<result_type>()(y, (min)());
- base_unsigned j;
- if(k == 1) {
- j = 0;
- } else if(brange < (std::numeric_limits<base_unsigned>::max)() / k) {
-
-
- j = k * off / (brange + 1);
- } else if(brange < (std::numeric_limits<uintmax_t>::max)() / k) {
-
- j = static_cast<base_unsigned>(
- static_cast<uintmax_t>(off) * k /
- (static_cast<uintmax_t>(brange) + 1));
- } else {
- boost::uintmax_t divisor =
- static_cast<boost::uintmax_t>(brange) + 1;
- j = static_cast<base_unsigned>(detail::muldiv(off, k, divisor));
- }
-
- y = v[j];
- v[j] = _rng();
- return y;
- }
-
- void discard(boost::uintmax_t z)
- {
- for(boost::uintmax_t j = 0; j < z; ++j) {
- (*this)();
- }
- }
-
- template<class Iter>
- void generate(Iter first, Iter last)
- { detail::generate_from_int(*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)(); }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, shuffle_order_engine, s)
- {
- os << s._rng;
- for(std::size_t i = 0; i < k; ++i)
- os << ' ' << s.v[i];
- os << ' ' << s.y;
- return os;
- }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, shuffle_order_engine, s)
- {
- is >> s._rng;
- for(std::size_t i = 0; i < k; ++i)
- is >> std::ws >> s.v[i];
- is >> std::ws >> s.y;
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(shuffle_order_engine, x, y)
- { return x._rng == y._rng && x.y == y.y && std::equal(x.v, x.v+k, y.v); }
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(shuffle_order_engine)
- private:
-
- void init()
- {
-
- for(result_type * p = v; p != v+k; ++p)
- *p = _rng();
- y = _rng();
- }
-
- base_type _rng;
- result_type v[k];
- result_type y;
- };
- #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
- template<class URNG, std::size_t k>
- const bool shuffle_order_engine<URNG, k>::has_fixed_range;
- template<class URNG, std::size_t k>
- const std::size_t shuffle_order_engine<URNG, k>::table_size;
- template<class URNG, std::size_t k>
- const std::size_t shuffle_order_engine<URNG, k>::buffer_size;
- #endif
- typedef shuffle_order_engine<
- linear_congruential_engine<uint32_t, 1366, 150889, 714025>,
- 97> kreutzer1986;
- typedef shuffle_order_engine<minstd_rand0, 256> knuth_b;
- }
- using random::kreutzer1986;
- }
- #include <boost/random/detail/enable_warnings.hpp>
- #endif
|