1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/random/uniform_smallint.hpp

316 lines
11 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2011-10-12 20:28:33 +00:00
#ifndef SPROUT_RANDOM_UNIFORM_SMALLINT_HPP
#define SPROUT_RANDOM_UNIFORM_SMALLINT_HPP
#include <iosfwd>
#include <istream>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/limits.hpp>
2011-10-12 20:28:33 +00:00
#include <sprout/random/detail/signed_unsigned_tools.hpp>
#include <sprout/random/random_result.hpp>
#include <sprout/random/uniform_01.hpp>
#include <sprout/random/results.hpp>
2013-03-18 10:12:21 +00:00
#include <sprout/assert.hpp>
2011-10-12 20:28:33 +00:00
namespace sprout {
namespace random {
//
// uniform_smallint
//
template<typename IntType = int>
class uniform_smallint {
public:
typedef IntType input_type;
typedef IntType result_type;
public:
//
// param_type
//
class param_type {
public:
typedef uniform_smallint distribution_type;
private:
IntType min_;
IntType max_;
public:
SPROUT_CONSTEXPR param_type()
: min_(0)
, max_(sprout::numeric_limits<IntType>::max())
2011-10-12 20:28:33 +00:00
{}
explicit SPROUT_CONSTEXPR param_type(IntType min_arg, IntType max_arg = sprout::numeric_limits<IntType>::max())
2013-03-18 10:12:21 +00:00
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
2011-10-12 20:28:33 +00:00
, max_(max_arg)
{}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR IntType a() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return min_;
}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR IntType b() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return max_;
}
template<typename Elem, typename Traits>
2013-11-02 09:28:18 +00:00
friend SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>& operator>>(
2011-10-12 20:28:33 +00:00
std::basic_istream<Elem, Traits>& lhs,
2011-10-17 04:44:19 +00:00
param_type& rhs
2011-10-12 20:28:33 +00:00
)
{
IntType min;
IntType max;
if (lhs >> min >> std::ws >> max) {
2013-03-18 10:12:21 +00:00
if (min <= max) {
rhs.min_ = min;
rhs.max_ = max;
} else {
lhs.setstate(std::ios_base::failbit);
}
}
return lhs;
2011-10-12 20:28:33 +00:00
}
template<typename Elem, typename Traits>
2013-11-02 09:28:18 +00:00
friend SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>& operator<<(
2011-10-12 20:28:33 +00:00
std::basic_ostream<Elem, Traits>& lhs,
param_type const& rhs
)
{
return lhs << rhs.min_ << " " << rhs.max_;
}
2012-11-16 04:40:19 +00:00
friend SPROUT_CONSTEXPR bool operator==(param_type const& lhs, param_type const& rhs) SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return lhs.min_ == rhs.min_ && lhs.max_ == rhs.max_;
}
2012-11-16 04:40:19 +00:00
friend SPROUT_CONSTEXPR bool operator!=(param_type const& lhs, param_type const& rhs) SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return !(lhs == rhs);
}
};
private:
IntType min_;
IntType max_;
2011-10-12 20:28:33 +00:00
private:
template<typename Engine>
SPROUT_CXX14_CONSTEXPR result_type generate(
Engine& eng,
std::true_type
) const
{
typedef typename Engine::result_type base_result;
typedef typename std::make_unsigned<base_result>::type base_unsigned;
typedef typename std::make_unsigned<result_type>::type range_type;
range_type range = sprout::random::detail::subtract<result_type>()(max_, min_);
base_unsigned base_range = sprout::random::detail::subtract<result_type>()(eng.max(), eng.min());
base_unsigned val = sprout::random::detail::subtract<base_result>()(static_cast<base_result>(eng()), eng.min());
return range >= base_range
? sprout::random::detail::add<range_type, result_type>()(static_cast<range_type>(val), min_)
: sprout::random::detail::add<range_type, result_type>()(static_cast<range_type>(val % (static_cast<base_result>(range) + 1)), min_)
;
}
template<class Engine>
SPROUT_CXX14_CONSTEXPR result_type generate(
Engine& eng,
std::false_type
) const
{
typedef typename Engine::result_type base_result;
typedef typename std::make_unsigned<result_type>::type range_type;
range_type range = sprout::random::detail::subtract<result_type>()(max_, min_);
base_result val = sprout::random::uniform_01<base_result>()(eng);
range_type offset = static_cast<range_type>(val * (static_cast<base_result>(range) + 1));
return offset > range
? max_
: sprout::random::detail::add<range_type, result_type>()(offset, min_)
;
}
template<typename Engine, typename EngineResult, typename RangeType, typename BaseUnsigned>
2011-10-12 20:28:33 +00:00
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_smallint> generate_true_2(
2013-07-22 13:00:09 +00:00
Engine const&,
EngineResult const& rnd,
2011-10-12 20:28:33 +00:00
RangeType range,
BaseUnsigned base_range,
BaseUnsigned val
) const
{
return range >= base_range
? sprout::random::random_result<Engine, uniform_smallint>(
sprout::random::detail::add<RangeType, result_type>()(static_cast<RangeType>(val), min_),
2014-01-16 09:59:25 +00:00
sprout::random::next(rnd),
2011-10-12 20:28:33 +00:00
*this
)
: sprout::random::random_result<Engine, uniform_smallint>(
sprout::random::detail::add<RangeType, result_type>()(static_cast<RangeType>(val % (static_cast<BaseUnsigned>(range) + 1)), min_),
2014-01-16 09:59:25 +00:00
sprout::random::next(rnd),
2011-10-12 20:28:33 +00:00
*this
)
;
}
template<typename Engine, typename EngineResult, typename RangeType, typename BaseUnsigned>
2011-10-12 20:28:33 +00:00
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_smallint> generate_true_1(
Engine const& eng,
EngineResult const& rnd,
2011-10-12 20:28:33 +00:00
RangeType range,
BaseUnsigned base_range
) const
{
typedef typename Engine::result_type base_result;
return generate_true_2(
eng,
rnd,
range,
base_range,
2014-01-16 09:59:25 +00:00
BaseUnsigned(sprout::random::detail::subtract<base_result>()(sprout::random::result(rnd), eng.min()))
2011-10-12 20:28:33 +00:00
);
}
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_smallint> generate(
Engine const& eng,
std::true_type
) const
{
typedef typename Engine::result_type base_result;
typedef typename std::make_unsigned<base_result>::type base_unsigned;
typedef typename std::make_unsigned<result_type>::type range_type;
return generate_true_1(
eng,
eng(),
range_type(sprout::random::detail::subtract<result_type>()(max_, min_)),
base_unsigned(sprout::random::detail::subtract<result_type>()(eng.max(), eng.min()))
);
}
2014-01-15 16:04:31 +00:00
template<typename Engine, typename Random, typename RangeType>
2011-10-12 20:28:33 +00:00
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_smallint> generate_false_2(
2013-07-22 13:00:09 +00:00
Engine const&,
2014-01-15 16:04:31 +00:00
Random const& rnd,
2011-10-12 20:28:33 +00:00
RangeType range,
RangeType offset
) const
{
return offset > range
? sprout::random::random_result<Engine, uniform_smallint>(
max_,
2014-01-16 09:59:25 +00:00
sprout::random::next(rnd).engine(),
2011-10-12 20:28:33 +00:00
*this
)
: sprout::random::random_result<Engine, uniform_smallint>(
2013-02-26 01:43:27 +00:00
sprout::random::detail::add<RangeType, result_type>()(offset, min_),
2014-01-16 09:59:25 +00:00
sprout::random::next(rnd).engine(),
2011-10-12 20:28:33 +00:00
*this
)
;
}
2014-01-15 16:04:31 +00:00
template<typename Engine, typename Random, typename RangeType>
2011-10-12 20:28:33 +00:00
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_smallint> generate_false_1(
Engine const& eng,
2014-01-15 16:04:31 +00:00
Random const& rnd,
2011-10-12 20:28:33 +00:00
RangeType range
) const
{
typedef typename Engine::result_type base_result;
return generate_false_2(
eng,
rnd,
static_cast<RangeType>(sprout::random::detail::subtract<result_type>()(max_, min_)),
2014-01-16 09:59:25 +00:00
static_cast<RangeType>(sprout::random::result(rnd) * (static_cast<base_result>(range) + 1))
2011-10-12 20:28:33 +00:00
);
}
template<class Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_smallint> generate(
Engine const& eng,
std::false_type
) const
{
typedef typename Engine::result_type base_result;
typedef typename std::make_unsigned<result_type>::type range_type;
return generate_false_1(
eng,
sprout::random::uniform_01<base_result>()(eng),
static_cast<range_type>(sprout::random::detail::subtract<result_type>()(max_, min_))
2011-10-12 20:28:33 +00:00
);
}
public:
SPROUT_CONSTEXPR uniform_smallint() SPROUT_NOEXCEPT
2011-10-12 20:28:33 +00:00
: min_(0)
, max_(sprout::numeric_limits<IntType>::max())
2011-10-12 20:28:33 +00:00
{}
explicit SPROUT_CONSTEXPR uniform_smallint(IntType min_arg, IntType max_arg = sprout::numeric_limits<IntType>::max())
2013-03-18 10:12:21 +00:00
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
2011-10-12 20:28:33 +00:00
, max_(max_arg)
{}
explicit SPROUT_CONSTEXPR uniform_smallint(param_type const& parm) SPROUT_NOEXCEPT
2011-10-12 20:28:33 +00:00
: min_(parm.a())
, max_(parm.b())
{}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR result_type a() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return min_;
}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR result_type b() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return max_;
}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return min_;
}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return max_;
}
SPROUT_CXX14_CONSTEXPR void reset() SPROUT_NOEXCEPT {}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR param_type param() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return param_type(min_, max_);
}
2013-11-02 09:28:18 +00:00
SPROUT_CXX14_CONSTEXPR void param(param_type const& parm) {
2011-10-12 20:28:33 +00:00
min_ = parm.a();
max_ = parm.b();
}
template<typename Engine>
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng) const {
typedef typename Engine::result_type base_result;
return generate(eng, typename std::is_integral<base_result>::type());
}
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_smallint> const operator()(Engine const& eng) const {
2011-10-12 20:28:33 +00:00
typedef typename Engine::result_type base_result;
return generate(eng, typename std::is_integral<base_result>::type());
}
template<typename Engine>
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng, param_type const& parm) const {
return uniform_smallint(parm)(eng);
}
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_smallint> const operator()(Engine const& eng, param_type const& parm) const {
return uniform_smallint(parm)(eng);
}
2011-10-12 20:28:33 +00:00
template<typename Elem, typename Traits>
2013-11-02 09:28:18 +00:00
friend SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>& operator>>(
2011-10-12 20:28:33 +00:00
std::basic_istream<Elem, Traits>& lhs,
2011-10-17 04:44:19 +00:00
uniform_smallint& rhs
2011-10-12 20:28:33 +00:00
)
{
param_type parm;
if (lhs >> parm) {
rhs.param(parm);
}
2011-10-12 20:28:33 +00:00
return lhs;
}
template<typename Elem, typename Traits>
2013-11-02 09:28:18 +00:00
friend SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>& operator<<(
2011-10-12 20:28:33 +00:00
std::basic_ostream<Elem, Traits>& lhs,
uniform_smallint const& rhs
)
{
2011-10-17 04:44:19 +00:00
return lhs << rhs.param();
2011-10-12 20:28:33 +00:00
}
2012-11-16 04:40:19 +00:00
friend SPROUT_CONSTEXPR bool operator==(uniform_smallint const& lhs, uniform_smallint const& rhs) SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return lhs.param() == rhs.param();
}
2012-11-16 04:40:19 +00:00
friend SPROUT_CONSTEXPR bool operator!=(uniform_smallint const& lhs, uniform_smallint const& rhs) SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return !(lhs == rhs);
}
};
} // namespace random
using sprout::random::uniform_smallint;
} // namespace sprout
2013-03-22 05:24:19 +00:00
#endif // #ifndef SPROUT_RANDOM_UNIFORM_SMALLINT_HPP