123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- #ifndef BOOST_RANDOM_UNIFORM_SMALLINT_HPP
- #define BOOST_RANDOM_UNIFORM_SMALLINT_HPP
- #include <istream>
- #include <iosfwd>
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/limits.hpp>
- #include <boost/type_traits/is_integral.hpp>
- #include <boost/random/detail/config.hpp>
- #include <boost/random/detail/operators.hpp>
- #include <boost/random/detail/signed_unsigned_tools.hpp>
- #include <boost/random/uniform_01.hpp>
- #include <boost/detail/workaround.hpp>
- #ifdef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
- #include <boost/type_traits/conditional.hpp>
- #endif
- namespace boost {
- namespace random {
- template<class IntType = int>
- class uniform_smallint
- {
- public:
- typedef IntType input_type;
- typedef IntType result_type;
- class param_type
- {
- public:
- typedef uniform_smallint distribution_type;
-
- param_type(IntType min_arg = 0, IntType max_arg = 9)
- : _min(min_arg), _max(max_arg)
- {
- BOOST_ASSERT(_min <= _max);
- }
-
- IntType a() const { return _min; }
-
- IntType b() const { return _max; }
-
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
- {
- os << parm._min << " " << parm._max;
- return os;
- }
-
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
- {
- is >> parm._min >> std::ws >> parm._max;
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
- { return lhs._min == rhs._min && lhs._max == rhs._max; }
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
- private:
- IntType _min;
- IntType _max;
- };
-
- explicit uniform_smallint(IntType min_arg = 0, IntType max_arg = 9)
- : _min(min_arg), _max(max_arg) {}
-
- explicit uniform_smallint(const param_type& parm)
- : _min(parm.a()), _max(parm.b()) {}
-
- result_type a() const { return _min; }
-
- result_type b() const { return _max; }
-
- result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _min; }
-
- result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _max; }
-
- param_type param() const { return param_type(_min, _max); }
-
- void param(const param_type& parm)
- {
- _min = parm.a();
- _max = parm.b();
- }
-
- void reset() { }
-
- template<class Engine>
- result_type operator()(Engine& eng) const
- {
- typedef typename Engine::result_type base_result;
- return generate(eng, boost::random::traits::is_integral<base_result>());
- }
-
- template<class Engine>
- result_type operator()(Engine& eng, const param_type& parm) const
- { return uniform_smallint(parm)(eng); }
-
- BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, uniform_smallint, ud)
- {
- os << ud._min << " " << ud._max;
- return os;
- }
-
-
- BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, uniform_smallint, ud)
- {
- is >> ud._min >> std::ws >> ud._max;
- return is;
- }
-
- BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(uniform_smallint, lhs, rhs)
- { return lhs._min == rhs._min && lhs._max == rhs._max; }
-
-
- BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(uniform_smallint)
- private:
-
-
- template<class Engine>
- result_type generate(Engine& eng, boost::true_type) const
- {
-
-
- typedef typename Engine::result_type base_result;
- typedef typename boost::random::traits::make_unsigned<base_result>::type base_unsigned;
- typedef typename boost::random::traits::make_unsigned_or_unbounded<result_type>::type range_type;
- #ifdef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
- typedef typename conditional<
- std::numeric_limits<range_type>::is_specialized && std::numeric_limits<base_unsigned>::is_specialized
- && (std::numeric_limits<range_type>::digits >= std::numeric_limits<base_unsigned>::digits),
- range_type, base_unsigned>::type mixed_range_type;
- #else
- typedef base_unsigned mixed_range_type;
- #endif
- range_type range = random::detail::subtract<result_type>()(_max, _min);
- base_unsigned base_range =
- random::detail::subtract<base_result>()((eng.max)(), (eng.min)());
- base_unsigned val =
- random::detail::subtract<base_result>()(eng(), (eng.min)());
- if(range >= base_range) {
- return boost::random::detail::add<range_type, result_type>()(
- static_cast<range_type>(val), _min);
- } else {
-
-
-
-
-
-
- mixed_range_type modulus = static_cast<mixed_range_type>(range)+1;
- return boost::random::detail::add<range_type, result_type>()(
- static_cast<mixed_range_type>(val) % modulus, _min);
- }
- }
-
- template<class Engine>
- result_type generate(Engine& eng, boost::false_type) const
- {
- typedef typename Engine::result_type base_result;
- typedef typename boost::random::traits::make_unsigned<result_type>::type range_type;
- range_type range = random::detail::subtract<result_type>()(_max, _min);
- base_result val = boost::uniform_01<base_result>()(eng);
-
-
-
-
-
-
-
-
- range_type offset = static_cast<range_type>(val * (static_cast<base_result>(range) + 1));
- if(offset > range) return _max;
- return boost::random::detail::add<range_type, result_type>()(offset , _min);
- }
-
- result_type _min;
- result_type _max;
- };
- }
- using random::uniform_smallint;
- }
- #endif
|