2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2011-2013 Bolero MURAKAMI
|
|
|
|
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_INT_DISTRIBUTION_HPP
|
|
|
|
#define SPROUT_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
|
|
|
|
|
|
|
|
#include <ios>
|
|
|
|
#include <istream>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2011-10-12 20:28:33 +00:00
|
|
|
#include <sprout/random/detail/signed_unsigned_tools.hpp>
|
|
|
|
#include <sprout/random/detail/uniform_int_float.hpp>
|
|
|
|
#include <sprout/random/random_result.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 {
|
|
|
|
namespace detail {
|
|
|
|
template<typename T, typename Engine>
|
|
|
|
struct generate_uniform_int_result {
|
|
|
|
public:
|
|
|
|
T result;
|
|
|
|
Engine engine;
|
|
|
|
};
|
|
|
|
|
2013-11-05 14:46:23 +00:00
|
|
|
template<typename Engine, typename T>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR T
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine& eng, T min_value, T max_value, std::true_type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef T result_type;
|
|
|
|
typedef typename std::make_unsigned<T>::type range_type;
|
|
|
|
typedef typename Engine::result_type base_result;
|
|
|
|
typedef typename std::make_unsigned<base_result>::type base_unsigned;
|
|
|
|
range_type const range = sprout::random::detail::subtract<result_type>()(max_value, min_value);
|
|
|
|
base_result const bmin = eng.min();
|
|
|
|
base_unsigned const brange = sprout::random::detail::subtract<base_result>()(eng.max(), eng.min());
|
|
|
|
if (range == 0) {
|
|
|
|
return min_value;
|
|
|
|
} else if (brange == range) {
|
|
|
|
base_unsigned v = sprout::random::detail::subtract<base_result>()(static_cast<base_result>(eng()), bmin);
|
|
|
|
return sprout::random::detail::add<base_unsigned, result_type>()(
|
|
|
|
static_cast<base_unsigned>(sprout::random::detail::subtract<base_result>()(static_cast<base_result>(eng()), bmin)),
|
|
|
|
min_value
|
|
|
|
);
|
|
|
|
} else if (brange < range) {
|
|
|
|
for(; ; ) {
|
|
|
|
range_type limit = range_type();
|
2013-11-05 15:11:51 +00:00
|
|
|
if (range == sprout::numeric_limits<range_type>::max()) {
|
2013-11-05 14:46:23 +00:00
|
|
|
limit = range / (range_type(brange) + 1);
|
|
|
|
if (range % (range_type(brange) + 1) == range_type(brange)) {
|
|
|
|
++limit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
limit = (range + 1) / (range_type(brange) + 1);
|
|
|
|
}
|
|
|
|
range_type result = range_type(0);
|
|
|
|
range_type mult = range_type(1);
|
|
|
|
while (mult <= limit) {
|
|
|
|
result += static_cast<range_type>(sprout::random::detail::subtract<base_result>()(static_cast<base_result>(eng()), bmin) * mult);
|
|
|
|
if (mult * range_type(brange) == range - mult + 1) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
mult *= range_type(brange) + range_type(1);
|
|
|
|
}
|
|
|
|
range_type result_increment = sprout::random::detail::generate_uniform_int(
|
|
|
|
eng,
|
|
|
|
static_cast<range_type>(0),
|
|
|
|
static_cast<range_type>(range / mult),
|
|
|
|
std::true_type()
|
|
|
|
);
|
2013-11-05 15:11:51 +00:00
|
|
|
if (sprout::numeric_limits<range_type>::max() / mult < result_increment) {
|
2013-11-05 14:46:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result_increment *= mult;
|
|
|
|
result += result_increment;
|
|
|
|
if (result < result_increment) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (result > range) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return sprout::random::detail::add<range_type, result_type>()(result, min_value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
base_unsigned bucket_size = base_unsigned();
|
2013-11-05 15:11:51 +00:00
|
|
|
if (brange == sprout::numeric_limits<base_unsigned>::max()) {
|
2013-11-05 14:46:23 +00:00
|
|
|
bucket_size = brange / (static_cast<base_unsigned>(range) + 1);
|
|
|
|
if (brange % (static_cast<base_unsigned>(range) + 1) == static_cast<base_unsigned>(range)) {
|
|
|
|
++bucket_size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bucket_size = (brange + 1) / (static_cast<base_unsigned>(range) + 1);
|
|
|
|
}
|
|
|
|
for(; ; ) {
|
|
|
|
base_unsigned result = sprout::random::detail::subtract<base_result>()(static_cast<base_result>(eng()), bmin) / bucket_size;
|
|
|
|
if (result <= static_cast<base_unsigned>(range)) {
|
|
|
|
return sprout::random::detail::add<base_unsigned, result_type>()(result, min_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR T
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine& eng, T min_value, T max_value, std::false_type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
sprout::random::detail::uniform_int_float<Engine> wrapper(eng);
|
|
|
|
return sprout::random::detail::generate_uniform_int(wrapper, min_value, max_value, std::true_type());
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR T
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine& eng, T min_value, T max_value
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int(
|
|
|
|
eng, min_value, max_value,
|
|
|
|
std::is_integral<typename Engine::result_type>()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-07-25 14:32:42 +00:00
|
|
|
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
|
|
|
template<int D = 16, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(Engine const& eng, T min_value, T max_value, std::true_type);
|
|
|
|
template<int D = 16, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(Engine const& eng, T min_value, T max_value, std::true_type);
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_int_true_3_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd, T min_value, RangeType range,
|
2013-07-25 14:32:42 +00:00
|
|
|
BaseResult bmin, BaseUnsigned brange, BaseUnsigned bucket_size
|
|
|
|
);
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_int_true_3_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd, T min_value, RangeType range,
|
2013-07-25 14:32:42 +00:00
|
|
|
BaseResult bmin, BaseUnsigned brange, BaseUnsigned bucket_size
|
|
|
|
);
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_3_1_1(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, BaseUnsigned bucket_size, BaseUnsigned result
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef T result_type;
|
|
|
|
return result <= static_cast<BaseUnsigned>(range)
|
|
|
|
? sprout::random::detail::generate_uniform_int_result<T, Engine>{
|
|
|
|
sprout::random::detail::add<BaseUnsigned, result_type>()(result, min_value),
|
|
|
|
eng
|
|
|
|
}
|
|
|
|
: sprout::random::detail::generate_uniform_int_true_3_1<D + 1>(
|
|
|
|
eng(), min_value, range,
|
|
|
|
bmin, brange, bucket_size
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_3_1_1(
|
|
|
|
Engine const&, T, RangeType,
|
|
|
|
BaseResult, BaseUnsigned, BaseUnsigned, BaseUnsigned
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE_DECL(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_int_true_3_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd, T min_value, RangeType range,
|
2013-07-25 14:32:42 +00:00
|
|
|
BaseResult bmin, BaseUnsigned brange, BaseUnsigned bucket_size
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_3_1_1<D + 1>(
|
|
|
|
rnd.engine(), min_value, range,
|
|
|
|
bmin, brange, bucket_size,
|
|
|
|
sprout::random::detail::subtract<BaseResult>()(rnd.result(), bmin) / bucket_size
|
|
|
|
);
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK_DECL(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_int_true_3_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const&, T, RangeType,
|
2013-07-25 14:32:42 +00:00
|
|
|
BaseResult, BaseUnsigned, BaseUnsigned
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_3(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_3_1<D + 1>(
|
|
|
|
eng(), min_value, range,
|
|
|
|
bmin, brange,
|
2013-08-06 15:15:09 +00:00
|
|
|
brange == sprout::numeric_limits<BaseUnsigned>::max()
|
2013-07-25 14:32:42 +00:00
|
|
|
? brange / (static_cast<BaseUnsigned>(range) + 1) + (
|
|
|
|
brange % (static_cast<BaseUnsigned>(range) + 1) == static_cast<BaseUnsigned>(range) ? 1 : 0
|
|
|
|
)
|
|
|
|
: (brange + 1) / (static_cast<BaseUnsigned>(range) + 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_3(
|
|
|
|
Engine const&, T, RangeType,
|
|
|
|
BaseResult, BaseUnsigned
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange
|
|
|
|
);
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange
|
|
|
|
);
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_4(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType result, RangeType result_increment
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef T result_type;
|
|
|
|
return result < result_increment || result > range
|
|
|
|
? sprout::random::detail::generate_uniform_int_true_2<D + 1>(eng, min_value, range, bmin, brange)
|
|
|
|
: sprout::random::detail::generate_uniform_int_result<T, Engine>{
|
|
|
|
random::detail::add<RangeType, result_type>()(result, min_value),
|
|
|
|
eng
|
|
|
|
}
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_4(
|
|
|
|
Engine const&, T, RangeType,
|
|
|
|
BaseResult, BaseUnsigned, RangeType, RangeType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_3(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType result, RangeType mult, RangeType result_increment
|
|
|
|
)
|
|
|
|
{
|
2013-08-06 15:15:09 +00:00
|
|
|
return sprout::numeric_limits<RangeType>::max() / mult < result_increment
|
2013-07-25 14:32:42 +00:00
|
|
|
? sprout::random::detail::generate_uniform_int_true_2<D + 1>(eng, min_value, range, bmin, brange)
|
|
|
|
: sprout::random::detail::generate_uniform_int_true_2_4<D + 1>(
|
|
|
|
eng, min_value, range,
|
|
|
|
bmin, brange, result + (result_increment * mult), result_increment * mult
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_3(
|
|
|
|
Engine const&, T, RangeType,
|
|
|
|
BaseResult, BaseUnsigned, RangeType, RangeType, RangeType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, typename Result, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_2(
|
|
|
|
Engine const&, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType result, RangeType mult, Result const& result_increment_base
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_2_3<D + 1>(
|
|
|
|
result_increment_base.engine, min_value, range,
|
|
|
|
bmin, brange, result, mult, result_increment_base.result
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, typename Result, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_2(
|
|
|
|
Engine const&, T, RangeType,
|
|
|
|
BaseResult, BaseUnsigned, RangeType, RangeType, Result const&
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_1(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType limit,
|
|
|
|
RangeType result = RangeType(0), RangeType mult = RangeType(1)
|
|
|
|
);
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_1(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType limit,
|
|
|
|
RangeType result = RangeType(0), RangeType mult = RangeType(1)
|
|
|
|
);
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_int_true_2_1_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd, T min_value, RangeType range,
|
2013-07-25 14:32:42 +00:00
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType limit, RangeType result, RangeType mult
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return mult * RangeType(brange) == range - mult + 1
|
2013-11-04 09:50:48 +00:00
|
|
|
? sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>{
|
2013-07-25 14:32:42 +00:00
|
|
|
static_cast<T>(
|
|
|
|
result + static_cast<RangeType>(sprout::random::detail::subtract<BaseResult>()(rnd.result(), bmin) * mult)
|
|
|
|
),
|
|
|
|
rnd.engine()
|
|
|
|
}
|
|
|
|
: sprout::random::detail::generate_uniform_int_true_2_1<D + 1>(
|
|
|
|
rnd.engine(), min_value, range,
|
|
|
|
bmin, brange, limit,
|
|
|
|
result + static_cast<RangeType>(sprout::random::detail::subtract<BaseResult>()(rnd.result(), bmin) * mult),
|
|
|
|
mult * (RangeType(brange) + RangeType(1))
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_int_true_2_1_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const&, T, RangeType,
|
2013-07-25 14:32:42 +00:00
|
|
|
BaseResult, BaseUnsigned, RangeType, RangeType, RangeType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE_DECL(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_1(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType limit,
|
|
|
|
RangeType result, RangeType mult
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return mult <= limit
|
|
|
|
? sprout::random::detail::generate_uniform_int_true_2_1_1<D + 1>(
|
|
|
|
eng(), min_value, range,
|
|
|
|
bmin, brange, limit,
|
|
|
|
result, mult
|
|
|
|
)
|
|
|
|
: sprout::random::detail::generate_uniform_int_true_2_2<D + 1>(
|
|
|
|
eng, min_value, range,
|
|
|
|
bmin, brange,
|
|
|
|
result, mult,
|
|
|
|
sprout::random::detail::generate_uniform_int<D + 1>(
|
|
|
|
eng,
|
|
|
|
static_cast<RangeType>(0),
|
|
|
|
static_cast<RangeType>(range / mult),
|
|
|
|
std::true_type()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK_DECL(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_1(
|
|
|
|
Engine const&, T, RangeType,
|
|
|
|
BaseResult, BaseUnsigned, RangeType,
|
|
|
|
RangeType, RangeType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE_DECL(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_2_1<D + 1>(
|
|
|
|
eng, min_value, range,
|
|
|
|
bmin, brange,
|
2013-08-06 15:15:09 +00:00
|
|
|
range == sprout::numeric_limits<RangeType>::max()
|
2013-07-25 14:32:42 +00:00
|
|
|
? range / (RangeType(brange) + 1) + (
|
|
|
|
range % (RangeType(brange) + 1) == RangeType(brange) ? 1 : 0
|
|
|
|
)
|
|
|
|
: (range + 1) / (RangeType(brange) + 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK_DECL(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2(
|
|
|
|
Engine const&, T, RangeType,
|
|
|
|
BaseResult, BaseUnsigned
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, typename BaseResult, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_int_true_1_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd, T min_value, BaseResult bmin
|
2013-07-25 14:32:42 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef T result_type;
|
|
|
|
typedef typename std::make_unsigned<BaseResult>::type base_unsigned;
|
2013-11-04 09:50:48 +00:00
|
|
|
return sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>{
|
2013-07-25 14:32:42 +00:00
|
|
|
sprout::random::detail::add<base_unsigned, result_type>()(
|
|
|
|
base_unsigned(sprout::random::detail::subtract<BaseResult>()(rnd.result(), bmin)),
|
|
|
|
min_value
|
|
|
|
),
|
|
|
|
rnd.engine()
|
|
|
|
};
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, typename BaseResult, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_int_true_1_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const&, T, BaseResult
|
2013-07-25 14:32:42 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_1(
|
|
|
|
Engine const& eng, T min_value, T, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return range == 0 ? sprout::random::detail::generate_uniform_int_result<T, Engine>{min_value, eng}
|
|
|
|
: brange == range ? sprout::random::detail::generate_uniform_int_true_1_1<D + 1>(eng(), min_value, bmin)
|
|
|
|
: brange < range ? sprout::random::detail::generate_uniform_int_true_2<D + 1>(eng, min_value, range, bmin, brange)
|
|
|
|
: sprout::random::detail::generate_uniform_int_true_3<D + 1>(eng, min_value, range, bmin, brange)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_1(
|
|
|
|
Engine const&, T, T, RangeType,
|
|
|
|
BaseResult, BaseUnsigned
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE_DECL(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine const& eng, T min_value, T max_value, std::true_type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef T result_type;
|
|
|
|
typedef typename std::make_unsigned<T>::type range_type;
|
|
|
|
typedef typename Engine::result_type base_result;
|
|
|
|
typedef typename std::make_unsigned<base_result>::type base_unsigned;
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_1<D + 1>(
|
|
|
|
eng, min_value, max_value,
|
|
|
|
range_type(sprout::random::detail::subtract<result_type>()(max_value, min_value)),
|
|
|
|
base_result(eng.min()),
|
|
|
|
base_unsigned(sprout::random::detail::subtract<base_result>()(eng.max(), eng.min()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK_DECL(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine const&, T, T, std::true_type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename Result, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_false_1(Result const& rnd) {
|
|
|
|
return sprout::random::detail::generate_uniform_int_result<T, Engine>{
|
|
|
|
rnd.result, rnd.engine.base()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, typename Result, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_false_1(Result const& rnd) {
|
|
|
|
return sprout::random::detail::generate_uniform_int_result<T, Engine>{
|
|
|
|
rnd.result, rnd.engine.base()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine const& eng, T min_value, T max_value, std::false_type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int_false_1<D + 1, Engine, T>(
|
|
|
|
sprout::random::detail::generate_uniform_int<D + 1>(
|
|
|
|
sprout::random::detail::uniform_int_float<Engine>(eng),
|
|
|
|
min_value, max_value,
|
|
|
|
std::true_type()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<int D = 16, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine const&, T, T, std::false_type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
#else
|
2011-10-12 20:28:33 +00:00
|
|
|
template<typename Engine, typename T>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(Engine const& eng, T min_value, T max_value, std::true_type);
|
2013-11-04 09:50:48 +00:00
|
|
|
template<typename EngineResult, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2012-10-05 15:58:56 +00:00
|
|
|
generate_uniform_int_true_3_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd, T min_value, RangeType range,
|
2012-10-05 15:58:56 +00:00
|
|
|
BaseResult bmin, BaseUnsigned brange, BaseUnsigned bucket_size
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_3_1_1(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, BaseUnsigned bucket_size, BaseUnsigned result
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef T result_type;
|
|
|
|
return result <= static_cast<BaseUnsigned>(range)
|
|
|
|
? sprout::random::detail::generate_uniform_int_result<T, Engine>{
|
|
|
|
sprout::random::detail::add<BaseUnsigned, result_type>()(result, min_value),
|
|
|
|
eng
|
|
|
|
}
|
2013-07-25 14:32:42 +00:00
|
|
|
: sprout::random::detail::generate_uniform_int_true_3_1(
|
2012-10-05 15:58:56 +00:00
|
|
|
eng(), min_value, range,
|
|
|
|
bmin, brange, bucket_size
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<typename EngineResult, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2012-10-05 15:58:56 +00:00
|
|
|
generate_uniform_int_true_3_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd, T min_value, RangeType range,
|
2012-10-05 15:58:56 +00:00
|
|
|
BaseResult bmin, BaseUnsigned brange, BaseUnsigned bucket_size
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_3_1_1(
|
2012-10-05 15:58:56 +00:00
|
|
|
rnd.engine(), min_value, range,
|
|
|
|
bmin, brange, bucket_size,
|
2011-10-12 20:28:33 +00:00
|
|
|
sprout::random::detail::subtract<BaseResult>()(rnd.result(), bmin) / bucket_size
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_3(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_3_1(
|
2012-10-05 15:58:56 +00:00
|
|
|
eng(), min_value, range,
|
|
|
|
bmin, brange,
|
2013-08-06 15:15:09 +00:00
|
|
|
brange == sprout::numeric_limits<BaseUnsigned>::max()
|
2011-10-12 20:28:33 +00:00
|
|
|
? brange / (static_cast<BaseUnsigned>(range) + 1) + (
|
|
|
|
brange % (static_cast<BaseUnsigned>(range) + 1) == static_cast<BaseUnsigned>(range) ? 1 : 0
|
|
|
|
)
|
|
|
|
: (brange + 1) / (static_cast<BaseUnsigned>(range) + 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_4(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType result, RangeType result_increment
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef T result_type;
|
|
|
|
return result < result_increment || result > range
|
|
|
|
? sprout::random::detail::generate_uniform_int_true_2(eng, min_value, range, bmin, brange)
|
|
|
|
: sprout::random::detail::generate_uniform_int_result<T, Engine>{
|
|
|
|
random::detail::add<RangeType, result_type>()(result, min_value),
|
|
|
|
eng
|
|
|
|
}
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_3(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType result, RangeType mult, RangeType result_increment
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
2013-08-06 15:15:09 +00:00
|
|
|
return sprout::numeric_limits<RangeType>::max() / mult < result_increment
|
2011-10-12 20:28:33 +00:00
|
|
|
? sprout::random::detail::generate_uniform_int_true_2(eng, min_value, range, bmin, brange)
|
2013-07-25 14:32:42 +00:00
|
|
|
: sprout::random::detail::generate_uniform_int_true_2_4(
|
2012-10-05 15:58:56 +00:00
|
|
|
eng, min_value, range,
|
|
|
|
bmin, brange, result + (result_increment * mult), result_increment * mult
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned, typename Result>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_2(
|
2013-07-22 13:00:09 +00:00
|
|
|
Engine const&, T min_value, RangeType range,
|
2012-10-05 15:58:56 +00:00
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType result, RangeType mult, Result const& result_increment_base
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_2_3(
|
2012-10-05 15:58:56 +00:00
|
|
|
result_increment_base.engine, min_value, range,
|
|
|
|
bmin, brange, result, mult, result_increment_base.result
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_1(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType limit,
|
|
|
|
RangeType result = RangeType(0), RangeType mult = RangeType(1)
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
2013-11-04 09:50:48 +00:00
|
|
|
template<typename EngineResult, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2012-10-05 15:58:56 +00:00
|
|
|
generate_uniform_int_true_2_1_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd, T min_value, RangeType range,
|
2012-10-05 15:58:56 +00:00
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType limit, RangeType result, RangeType mult
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return mult * RangeType(brange) == range - mult + 1
|
2013-11-04 09:50:48 +00:00
|
|
|
? sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>{
|
2011-10-12 20:28:33 +00:00
|
|
|
static_cast<T>(
|
|
|
|
result + static_cast<RangeType>(sprout::random::detail::subtract<BaseResult>()(rnd.result(), bmin) * mult)
|
|
|
|
),
|
|
|
|
rnd.engine()
|
|
|
|
}
|
|
|
|
: sprout::random::detail::generate_uniform_int_true_2_1(
|
2012-10-05 15:58:56 +00:00
|
|
|
rnd.engine(), min_value, range,
|
|
|
|
bmin, brange, limit,
|
2011-10-12 20:28:33 +00:00
|
|
|
result + static_cast<RangeType>(sprout::random::detail::subtract<BaseResult>()(rnd.result(), bmin) * mult),
|
|
|
|
mult * (RangeType(brange) + RangeType(1))
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2_1(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange, RangeType limit,
|
|
|
|
RangeType result, RangeType mult
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return mult <= limit
|
2013-07-25 14:32:42 +00:00
|
|
|
? sprout::random::detail::generate_uniform_int_true_2_1_1(
|
2012-10-05 15:58:56 +00:00
|
|
|
eng(), min_value, range,
|
|
|
|
bmin, brange, limit,
|
|
|
|
result, mult
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
: sprout::random::detail::generate_uniform_int_true_2_2(
|
2012-10-05 15:58:56 +00:00
|
|
|
eng, min_value, range,
|
|
|
|
bmin, brange,
|
|
|
|
result, mult,
|
2011-10-12 20:28:33 +00:00
|
|
|
sprout::random::detail::generate_uniform_int(
|
|
|
|
eng,
|
|
|
|
static_cast<RangeType>(0),
|
|
|
|
static_cast<RangeType>(range / mult),
|
|
|
|
std::true_type()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_2(
|
|
|
|
Engine const& eng, T min_value, RangeType range,
|
|
|
|
BaseResult bmin, BaseUnsigned brange
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_2_1(
|
2012-10-05 15:58:56 +00:00
|
|
|
eng, min_value, range,
|
|
|
|
bmin, brange,
|
2013-08-06 15:15:09 +00:00
|
|
|
range == sprout::numeric_limits<RangeType>::max()
|
2011-10-12 20:28:33 +00:00
|
|
|
? range / (RangeType(brange) + 1) + (
|
|
|
|
range % (RangeType(brange) + 1) == RangeType(brange) ? 1 : 0
|
|
|
|
)
|
|
|
|
: (range + 1) / (RangeType(brange) + 1)
|
|
|
|
);
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<typename EngineResult, typename T, typename BaseResult>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>
|
2012-10-05 15:58:56 +00:00
|
|
|
generate_uniform_int_true_1_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd, T min_value, BaseResult bmin
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef T result_type;
|
|
|
|
typedef typename std::make_unsigned<BaseResult>::type base_unsigned;
|
2013-11-04 09:50:48 +00:00
|
|
|
return sprout::random::detail::generate_uniform_int_result<T, typename EngineResult::engine_type>{
|
2011-10-12 20:28:33 +00:00
|
|
|
sprout::random::detail::add<base_unsigned, result_type>()(
|
|
|
|
base_unsigned(sprout::random::detail::subtract<BaseResult>()(rnd.result(), bmin)),
|
|
|
|
min_value
|
|
|
|
),
|
|
|
|
rnd.engine()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T, typename RangeType, typename BaseResult, typename BaseUnsigned>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_true_1(
|
2013-07-22 13:00:09 +00:00
|
|
|
Engine const& eng, T min_value, T, RangeType range,
|
2012-10-05 15:58:56 +00:00
|
|
|
BaseResult bmin, BaseUnsigned brange
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return range == 0 ? sprout::random::detail::generate_uniform_int_result<T, Engine>{min_value, eng}
|
|
|
|
: brange == range ? sprout::random::detail::generate_uniform_int_true_1_1(eng(), min_value, bmin)
|
|
|
|
: brange < range ? sprout::random::detail::generate_uniform_int_true_2(eng, min_value, range, bmin, brange)
|
|
|
|
: sprout::random::detail::generate_uniform_int_true_3(eng, min_value, range, bmin, brange)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine const& eng, T min_value, T max_value, std::true_type
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef T result_type;
|
|
|
|
typedef typename std::make_unsigned<T>::type range_type;
|
|
|
|
typedef typename Engine::result_type base_result;
|
|
|
|
typedef typename std::make_unsigned<base_result>::type base_unsigned;
|
|
|
|
return sprout::random::detail::generate_uniform_int_true_1(
|
2012-10-05 15:58:56 +00:00
|
|
|
eng, min_value, max_value,
|
2011-10-12 20:28:33 +00:00
|
|
|
range_type(sprout::random::detail::subtract<result_type>()(max_value, min_value)),
|
|
|
|
base_result(eng.min()),
|
|
|
|
base_unsigned(sprout::random::detail::subtract<base_result>()(eng.max(), eng.min()))
|
|
|
|
);
|
2012-04-16 18:00:51 +00:00
|
|
|
}
|
2011-10-12 20:28:33 +00:00
|
|
|
template<typename Engine, typename T, typename Result>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int_false_1(Result const& rnd) {
|
2011-10-12 20:28:33 +00:00
|
|
|
return sprout::random::detail::generate_uniform_int_result<T, Engine>{
|
2012-10-05 15:58:56 +00:00
|
|
|
rnd.result, rnd.engine.base()
|
2011-10-12 20:28:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine const& eng, T min_value, T max_value, std::false_type
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
2013-07-25 14:32:42 +00:00
|
|
|
return sprout::random::detail::generate_uniform_int_false_1<Engine, T>(
|
2011-10-12 20:28:33 +00:00
|
|
|
sprout::random::detail::generate_uniform_int(
|
|
|
|
sprout::random::detail::uniform_int_float<Engine>(eng),
|
2012-10-05 15:58:56 +00:00
|
|
|
min_value, max_value,
|
2011-10-12 20:28:33 +00:00
|
|
|
std::true_type()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2013-07-25 14:32:42 +00:00
|
|
|
#endif
|
2011-10-12 20:28:33 +00:00
|
|
|
template<typename Engine, typename T>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_int_result<T, Engine>
|
|
|
|
generate_uniform_int(
|
|
|
|
Engine const& eng, T min_value, T max_value
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_int(
|
2012-10-05 15:58:56 +00:00
|
|
|
eng, min_value, max_value,
|
2011-10-12 20:28:33 +00:00
|
|
|
std::is_integral<typename Engine::result_type>()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// uniform_int_distribution
|
|
|
|
//
|
|
|
|
template<typename IntType = int>
|
|
|
|
class uniform_int_distribution {
|
|
|
|
public:
|
|
|
|
typedef IntType input_type;
|
|
|
|
typedef IntType result_type;
|
|
|
|
public:
|
|
|
|
//
|
|
|
|
// param_type
|
|
|
|
//
|
|
|
|
class param_type {
|
|
|
|
public:
|
|
|
|
typedef uniform_int_distribution distribution_type;
|
|
|
|
private:
|
|
|
|
IntType min_;
|
|
|
|
IntType max_;
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR param_type()
|
|
|
|
: min_(0)
|
2013-11-09 09:13:26 +00:00
|
|
|
, max_(sprout::numeric_limits<IntType>::max())
|
2011-10-12 20:28:33 +00:00
|
|
|
{}
|
2013-11-09 09:13:26 +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
|
|
|
)
|
|
|
|
{
|
2011-10-14 12:23:13 +00:00
|
|
|
IntType min;
|
|
|
|
IntType max;
|
|
|
|
if (lhs >> min >> std::ws >> max) {
|
2013-03-18 10:12:21 +00:00
|
|
|
if (min <= max) {
|
2011-10-14 12:23:13 +00:00
|
|
|
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:
|
2011-10-19 13:47:59 +00:00
|
|
|
IntType min_;
|
|
|
|
IntType max_;
|
2011-10-12 20:28:33 +00:00
|
|
|
private:
|
|
|
|
template<typename Engine, typename Result>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_int_distribution> generate(Result const& rnd) const {
|
|
|
|
return sprout::random::random_result<Engine, uniform_int_distribution>(
|
|
|
|
rnd.result,
|
|
|
|
rnd.engine,
|
|
|
|
*this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
public:
|
2013-11-05 14:46:23 +00:00
|
|
|
SPROUT_CONSTEXPR uniform_int_distribution() SPROUT_NOEXCEPT
|
2011-10-12 20:28:33 +00:00
|
|
|
: min_(0)
|
2013-11-09 09:13:26 +00:00
|
|
|
, max_(sprout::numeric_limits<IntType>::max())
|
2011-10-12 20:28:33 +00:00
|
|
|
{}
|
2013-11-09 09:13:26 +00:00
|
|
|
explicit SPROUT_CONSTEXPR uniform_int_distribution(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)
|
|
|
|
{}
|
2013-11-05 14:46:23 +00:00
|
|
|
explicit SPROUT_CONSTEXPR uniform_int_distribution(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_;
|
|
|
|
}
|
2013-11-09 09:13:26 +00:00
|
|
|
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>
|
2013-11-05 14:46:23 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng) const {
|
|
|
|
return sprout::random::detail::generate_uniform_int(eng, min_, max_);
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
2013-11-04 09:50:48 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_int_distribution> const operator()(Engine const& eng) const {
|
2011-10-12 20:28:33 +00:00
|
|
|
return generate<Engine>(sprout::random::detail::generate_uniform_int(eng, min_, max_));
|
|
|
|
}
|
2013-11-05 14:46:23 +00:00
|
|
|
template<typename Engine>
|
|
|
|
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng, param_type const& parm) const {
|
|
|
|
return sprout::random::detail::generate_uniform_int(eng, parm.a(), parm.b());
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_int_distribution> const operator()(Engine const& eng, param_type const& parm) const {
|
|
|
|
return generate<Engine>(sprout::random::detail::generate_uniform_int(eng, parm.a(), parm.b()));
|
|
|
|
}
|
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_int_distribution& rhs
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
param_type parm;
|
2011-10-19 13:47:59 +00:00
|
|
|
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_int_distribution 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_int_distribution const& lhs, uniform_int_distribution 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_int_distribution const& lhs, uniform_int_distribution const& rhs) SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace random
|
|
|
|
|
|
|
|
using sprout::random::uniform_int_distribution;
|
|
|
|
} // namespace sprout
|
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
#endif // #ifndef SPROUT_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
|