123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- #ifndef BOOST_RANDOM_ADDITIVE_COMBINE_HPP
- #define BOOST_RANDOM_ADDITIVE_COMBINE_HPP
- #include <istream>
- #include <iosfwd>
- #include <algorithm> // for std::min and std::max
- #include <boost/config.hpp>
- #include <boost/cstdint.hpp>
- #include <boost/random/detail/config.hpp>
- #include <boost/random/detail/operators.hpp>
- #include <boost/random/detail/seed.hpp>
- #include <boost/random/linear_congruential.hpp>
- namespace boost {
- namespace random {
- template<class MLCG1, class MLCG2>
- class additive_combine_engine
- {
- public:
- typedef MLCG1 first_base;
- typedef MLCG2 second_base;
- typedef typename MLCG1::result_type result_type;
-
- BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
-
- static BOOST_CONSTEXPR result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return 1; }
-
- static BOOST_CONSTEXPR result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
- { return MLCG1::modulus-1; }
-
- additive_combine_engine() : _mlcg1(), _mlcg2() { }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(additive_combine_engine,
- result_type, seed_arg)
- {
- _mlcg1.seed(seed_arg);
- _mlcg2.seed(seed_arg);
- }
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(additive_combine_engine,
- SeedSeq, seq)
- {
- _mlcg1.seed(seq);
- _mlcg2.seed(seq);
- }
-
- additive_combine_engine(typename MLCG1::result_type seed1,
- typename MLCG2::result_type seed2)
- : _mlcg1(seed1), _mlcg2(seed2) { }
-
- template<class It> additive_combine_engine(It& first, It last)
- : _mlcg1(first, last), _mlcg2(first, last) { }
-
- void seed()
- {
- _mlcg1.seed();
- _mlcg2.seed();
- }
-
- BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(additive_combine_engine,
- result_type, seed_arg)
- {
- _mlcg1.seed(seed_arg);
- _mlcg2.seed(seed_arg);
- }
-
- BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(additive_combine_engine,
- SeedSeq, seq)
- {
- _mlcg1.seed(seq);
- _mlcg2.seed(seq);
- }
-
- void seed(typename MLCG1::result_type seed1,
- typename MLCG2::result_type seed2)
- {
- _mlcg1.seed(seed1);
- _mlcg2.seed(seed2);
- }
-
- template<class It> void seed(It& first, It last)
- {
- _mlcg1.seed(first, last);
- _mlcg2.seed(first, last);
- }
-
- result_type operator()() {
- result_type val1 = _mlcg1();
- result_type val2 = _mlcg2();
- if(val2 < val1) return val1 - val2;
- else return val1 - val2 + MLCG1::modulus - 1;
- }
-
-
- template<class Iter>
- void generate(Iter first, Iter last)
- { detail::generate_from_int(*this, first, last); }
-
- void discard(boost::uintmax_t z)
- {
- _mlcg1.discard(z);
- _mlcg2.discard(z);
- }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, additive_combine_engine, r)
- { os << r._mlcg1 << ' ' << r._mlcg2; return os; }
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, additive_combine_engine, r)
- { is >> r._mlcg1 >> std::ws >> r._mlcg2; return is; }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(additive_combine_engine, x, y)
- { return x._mlcg1 == y._mlcg1 && x._mlcg2 == y._mlcg2; }
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(additive_combine_engine)
- private:
- MLCG1 _mlcg1;
- MLCG2 _mlcg2;
- };
- #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
- template<class MLCG1, class MLCG2>
- const bool additive_combine_engine<MLCG1, MLCG2>::has_fixed_range;
- #endif
- template<class MLCG1, class MLCG2, typename MLCG1::result_type val = 0>
- class additive_combine : public additive_combine_engine<MLCG1, MLCG2>
- {
- typedef additive_combine_engine<MLCG1, MLCG2> base_t;
- public:
- typedef typename base_t::result_type result_type;
- additive_combine() {}
- template<class T>
- additive_combine(T& arg) : base_t(arg) {}
- template<class T>
- additive_combine(const T& arg) : base_t(arg) {}
- template<class It>
- additive_combine(It& first, It last) : base_t(first, last) {}
- };
- typedef additive_combine_engine<
- linear_congruential_engine<uint32_t, 40014, 0, 2147483563>,
- linear_congruential_engine<uint32_t, 40692, 0, 2147483399>
- > ecuyer1988;
- }
- using random::ecuyer1988;
- }
- #endif
|