1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-12-03 21:15:42 +00:00
Sprout/sprout/random/uniform_01.hpp

195 lines
7.1 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-04-09 09:11:40 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
https://github.com/bolero-MURAKAMI/Sprout
2013-08-08 09:54:33 +00:00
2014-04-09 09:11:40 +00:00
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)
2013-08-08 09:54:33 +00:00
=============================================================================*/
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 <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/random_result.hpp>
#include <sprout/random/results.hpp>
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
# 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>
2013-11-02 09:28:18 +00:00
friend SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>& operator>>(
2011-10-16 14:38:40 +00:00
std::basic_istream<Elem, Traits>& lhs,
2013-07-22 13:00:09 +00:00
param_type&
2011-10-16 14:38:40 +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-16 14:38:40 +00:00
std::basic_ostream<Elem, Traits>& lhs,
2013-07-22 13:00:09 +00:00
param_type const&
2011-10-16 14:38:40 +00:00
)
{
return lhs;
}
2013-07-22 13:00:09 +00:00
friend SPROUT_CONSTEXPR bool operator==(param_type const&, param_type const&) SPROUT_NOEXCEPT {
2011-10-16 14:38:40 +00:00
return true;
}
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-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, typename EngineResult, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate_1(Engine const&, EngineResult const& rnd, result_type result) const {
return result < result_type(1)
? sprout::random::random_result<Engine, uniform_01>(result, rnd.engine(), *this)
2014-01-16 09:59:25 +00:00
: generate<D + 1>(sprout::random::next(rnd), sprout::random::next(rnd)())
;
}
template<int D, typename Engine, typename EngineResult, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate_1(Engine const&, EngineResult const&, result_type) const {
2012-11-13 13:17:10 +00:00
return sprout::throw_recursive_function_template_instantiation_exeeded();
}
template<int D = 16, typename Engine, typename EngineResult, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate(Engine const& eng, EngineResult const& rnd) const {
typedef typename Engine::result_type base_result;
return generate_1<D + 1>(
eng,
rnd,
2014-01-16 09:59:25 +00:00
result_type(sprout::random::result(rnd) - eng.min()) * (
result_type(1) / (
result_type(eng.max() - eng.min()) + result_type(
2013-08-06 15:15:09 +00:00
sprout::numeric_limits<base_result>::is_integer ? 1 : 0
)
)
)
);
}
template<int D = 16, typename Engine, typename EngineResult, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate(Engine const&, EngineResult const&) const {
2012-11-13 13:17:10 +00:00
return sprout::throw_recursive_function_template_instantiation_exeeded();
}
#else
template<typename Engine, typename EngineResult>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate_1(Engine const&, EngineResult 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)
2014-01-16 09:59:25 +00:00
: generate(sprout::random::next(rnd), sprout::random::next(rnd)())
2011-10-12 20:28:33 +00:00
;
}
template<typename Engine, typename EngineResult>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01>
generate(Engine const& eng, EngineResult const& rnd) const {
2011-10-12 20:28:33 +00:00
typedef typename Engine::result_type base_result;
return generate_1(
eng,
rnd,
2014-01-16 09:59:25 +00:00
result_type(sprout::random::result(rnd) - eng.min()) * (
2011-10-12 20:28:33 +00:00
result_type(1) / (
result_type(eng.max() - eng.min()) + result_type(
2013-08-06 15:15:09 +00:00
sprout::numeric_limits<base_result>::is_integer ? 1 : 0
2011-10-12 20:28:33 +00:00
)
)
)
);
}
#endif
2011-10-12 20:28:33 +00:00
public:
2014-01-13 15:18:06 +00:00
SPROUT_CONSTEXPR uniform_01() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
explicit SPROUT_CONSTEXPR uniform_01(param_type const&) SPROUT_NOEXCEPT {}
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 result_type(0);
}
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 result_type(1);
}
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-16 14:38:40 +00:00
return param_type();
}
SPROUT_CXX14_CONSTEXPR void param(param_type const&) SPROUT_NOEXCEPT {}
template<typename Engine>
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng) const {
for (; ; ) {
typedef typename Engine::result_type base_result;
result_type result = result_type(static_cast<base_result>(eng()) - eng.min()) * (
result_type(1) / (
result_type(eng.max() - eng.min()) + result_type(
sprout::numeric_limits<base_result>::is_integer ? 1 : 0
)
)
);
if (result < result_type(1)) {
return result;
}
}
}
2011-10-12 20:28:33 +00:00
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01> const operator()(Engine const& eng) const {
2011-10-12 20:28:33 +00:00
return generate(eng, eng());
}
template<typename Engine>
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng, param_type const&) const {
return operator()(eng);
}
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01> const operator()(Engine const& eng, param_type const&) const {
return operator()(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,
2013-07-22 13:00:09 +00:00
uniform_01&
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,
2013-07-22 13:00:09 +00:00
uniform_01 const&
2011-10-12 20:28:33 +00:00
)
{
return lhs;
}
2013-07-22 13:00:09 +00:00
friend SPROUT_CONSTEXPR bool operator==(uniform_01 const&, uniform_01 const&) SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return true;
}
2012-11-16 04:40:19 +00:00
friend SPROUT_CONSTEXPR bool operator!=(uniform_01 const& lhs, uniform_01 const& rhs) SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return !(lhs == rhs);
}
};
} // namespace random
using sprout::random::uniform_01;
} // namespace sprout
2013-03-22 05:24:19 +00:00
#endif // #ifndef SPROUT_RANDOM_UNIFORM_01_HPP