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_01.hpp

170 lines
5.6 KiB
C++
Raw Normal View History

2011-10-12 20:28:33 +00:00
#ifndef SPROUT_RANDOM_UNIFORM_01_HPP
#define SPROUT_RANDOM_UNIFORM_01_HPP
#include <iosfwd>
#include <istream>
#include <limits>
#include <sprout/config.hpp>
#include <sprout/random/random_result.hpp>
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
# include <stdexcept>
# include <sprout/workaround/recursive_function_template.hpp>
#endif
2011-10-12 20:28:33 +00:00
namespace sprout {
namespace random {
//
// uniform_01
//
template<typename RealType = double>
class uniform_01 {
public:
typedef RealType input_type;
typedef RealType result_type;
2011-10-16 14:38:40 +00:00
public:
//
// param_type
//
class param_type {
public:
typedef uniform_01 distribution_type;
public:
template<typename Elem, typename Traits>
2011-10-17 04:44:19 +00:00
friend std::basic_istream<Elem, Traits>& operator>>(
2011-10-16 14:38:40 +00:00
std::basic_istream<Elem, Traits>& lhs,
2011-10-17 04:44:19 +00:00
param_type& rhs
2011-10-16 14:38:40 +00:00
)
{
return lhs;
}
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& lhs,
param_type const& rhs
)
{
return lhs;
}
2012-06-17 10:14:38 +00:00
friend SPROUT_CONSTEXPR bool operator==(param_type const& lhs, param_type const& rhs) {
2011-10-16 14:38:40 +00:00
return true;
}
2012-06-17 10:14:38 +00:00
friend SPROUT_CONSTEXPR bool operator!=(param_type const& lhs, param_type const& rhs) {
2011-10-16 14:38:40 +00:00
return !(lhs == rhs);
}
};
2011-10-12 20:28:33 +00:00
private:
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
template<int D, typename Engine, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate_1(Engine const& eng, sprout::random::random_result<Engine> const& rnd, result_type result) const {
return result < result_type(1)
? sprout::random::random_result<Engine, uniform_01>(result, rnd.engine(), *this)
: generate<D + 1>(rnd.engine(), rnd.engine()())
;
}
template<int D, typename Engine, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate_1(Engine const& eng, sprout::random::random_result<Engine> const& rnd, result_type result) const {
return throw std::runtime_error(SPROUT_RECURSIVE_FUNCTION_TEMPLATE_INSTANTIATION_EXCEEDED_MESSAGE),
sprout::random::random_result<Engine, uniform_01>()
;
}
template<int D = 16, typename Engine, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate(Engine const& eng, sprout::random::random_result<Engine> const& rnd) const {
typedef typename Engine::result_type base_result;
return generate_1<D + 1>(
eng,
rnd,
result_type(rnd.result() - eng.min()) * (
result_type(1) / (
result_type(eng.max() - eng.min()) + result_type(
std::numeric_limits<base_result>::is_integer ? 1 : 0
)
)
)
);
}
template<int D = 16, typename Engine, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate(Engine const& eng, sprout::random::random_result<Engine> const& rnd) const {
return throw std::runtime_error(SPROUT_RECURSIVE_FUNCTION_TEMPLATE_INSTANTIATION_EXCEEDED_MESSAGE),
sprout::random::random_result<Engine, uniform_01>()
;
}
#else
2011-10-12 20:28:33 +00:00
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate_1(Engine const& eng, sprout::random::random_result<Engine> const& rnd, result_type result) const {
2011-10-12 20:28:33 +00:00
return result < result_type(1)
? sprout::random::random_result<Engine, uniform_01>(result, rnd.engine(), *this)
: generate(rnd.engine(), rnd.engine()())
2011-10-12 20:28:33 +00:00
;
}
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate(Engine const& eng, sprout::random::random_result<Engine> const& rnd) const {
2011-10-12 20:28:33 +00:00
typedef typename Engine::result_type base_result;
return generate_1(
eng,
rnd,
result_type(rnd.result() - eng.min()) * (
result_type(1) / (
result_type(eng.max() - eng.min()) + result_type(
std::numeric_limits<base_result>::is_integer ? 1 : 0
)
)
)
);
}
#endif
2011-10-12 20:28:33 +00:00
public:
2012-04-11 14:28:29 +00:00
explicit SPROUT_CONSTEXPR uniform_01()
2011-10-17 04:44:19 +00:00
{}
2012-04-11 14:28:29 +00:00
explicit SPROUT_CONSTEXPR uniform_01(param_type const& parm)
2011-10-16 14:38:40 +00:00
{}
2011-10-12 20:28:33 +00:00
SPROUT_CONSTEXPR result_type min() const {
return result_type(0);
}
SPROUT_CONSTEXPR result_type max() const {
return result_type(1);
}
2011-10-16 14:38:40 +00:00
SPROUT_CONSTEXPR param_type param() const {
return param_type();
}
void param(param_type const& parm) {
}
2011-10-12 20:28:33 +00:00
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01> operator()(Engine const& eng) const {
return generate(eng, eng());
}
template<typename Elem, typename Traits>
2011-10-17 04:44:19 +00:00
friend 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_01& rhs
2011-10-12 20:28:33 +00:00
)
{
return lhs;
}
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& lhs,
uniform_01 const& rhs
)
{
return lhs;
}
2012-06-17 10:14:38 +00:00
friend SPROUT_CONSTEXPR bool operator==(uniform_01 const& lhs, uniform_01 const& rhs) {
2011-10-12 20:28:33 +00:00
return true;
}
2012-06-17 10:14:38 +00:00
friend SPROUT_CONSTEXPR bool operator!=(uniform_01 const& lhs, uniform_01 const& rhs) {
2011-10-12 20:28:33 +00:00
return !(lhs == rhs);
}
};
} // namespace random
using sprout::random::uniform_01;
} // namespace sprout
#endif // #ifndef SPROUT_RANDOM_UNIFORM_01_HPP