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

177 lines
5.9 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2017 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_BERNOULLI_DISTRIBUTION_HPP
#define SPROUT_RANDOM_BERNOULLI_DISTRIBUTION_HPP
#include <iosfwd>
#include <sprout/config.hpp>
#include <sprout/random/random_result.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 {
//
// bernoulli_distribution
//
template<typename RealType = double>
class bernoulli_distribution {
public:
typedef int input_type;
typedef bool result_type;
public:
//
// param_type
//
class param_type {
public:
typedef bernoulli_distribution distribution_type;
private:
RealType p_;
public:
SPROUT_CONSTEXPR param_type()
: p_(RealType(0.5))
{}
2014-08-01 06:24:00 +00:00
param_type(param_type const&) = default;
2012-04-11 14:28:29 +00:00
explicit SPROUT_CONSTEXPR param_type(RealType p_arg)
2013-03-18 10:12:21 +00:00
: p_((SPROUT_ASSERT(p_arg >= RealType(0)), SPROUT_ASSERT(p_arg <= RealType(1)), p_arg))
2011-10-12 20:28:33 +00:00
{}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR RealType p() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return p_;
}
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
)
{
RealType p;
if (lhs >> p) {
2013-03-18 10:12:21 +00:00
if (p >= RealType(0) && p <= RealType(1)) {
rhs.p_ = p;
} 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.p_;
}
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.p_ == rhs.p_;
}
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:
RealType p_;
private:
template<typename Engine, typename EngineResult>
2011-10-12 20:28:33 +00:00
SPROUT_CONSTEXPR sprout::random::random_result<Engine, bernoulli_distribution> generate(
EngineResult const& rnd
2011-10-12 20:28:33 +00:00
) const
{
return sprout::random::random_result<Engine, bernoulli_distribution>(
2014-01-16 09:59:25 +00:00
RealType(sprout::random::result(rnd) - sprout::random::next(rnd).min())
<= p_ * RealType(sprout::random::next(rnd).max() - sprout::random::next(rnd).min())
2014-01-15 16:04:31 +00:00
,
2014-01-16 09:59:25 +00:00
sprout::random::next(rnd),
2011-10-12 20:28:33 +00:00
*this
);
}
public:
SPROUT_CONSTEXPR bernoulli_distribution() SPROUT_NOEXCEPT
2011-10-12 20:28:33 +00:00
: p_(RealType(0.5))
{}
2014-08-01 06:24:00 +00:00
bernoulli_distribution(bernoulli_distribution const&) = default;
2012-04-11 14:28:29 +00:00
explicit SPROUT_CONSTEXPR bernoulli_distribution(RealType p_arg)
2013-03-18 10:12:21 +00:00
: p_((SPROUT_ASSERT(p_arg >= RealType(0)), SPROUT_ASSERT(p_arg <= RealType(1)), p_arg))
2011-10-12 20:28:33 +00:00
{}
explicit SPROUT_CONSTEXPR bernoulli_distribution(param_type const& parm) SPROUT_NOEXCEPT
2011-10-16 14:38:40 +00:00
: p_(parm.p())
{}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR RealType p() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return p_;
}
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 false;
}
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 true;
}
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(p_);
}
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
p_ = parm.p();
}
template<typename Engine>
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng) const {
typedef typename Engine::result_type base_result;
return p_ == RealType(0)
? false
: RealType(static_cast<base_result>(eng()) - eng.min()) <= p_ * RealType(eng.max() - eng.min())
;
}
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, bernoulli_distribution> const operator()(Engine const& eng) const {
2011-10-12 20:28:33 +00:00
return p_ == RealType(0)
? sprout::random::random_result<Engine, bernoulli_distribution>(false, eng, *this)
: generate<Engine>(eng())
2011-10-12 20:28:33 +00:00
;
}
template<typename Engine>
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng, param_type const& parm) const {
return bernoulli_distribution(parm)(eng);
}
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, bernoulli_distribution> const operator()(Engine const& eng, param_type const& parm) const {
return bernoulli_distribution(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
bernoulli_distribution& 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,
bernoulli_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==(bernoulli_distribution const& lhs, bernoulli_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!=(bernoulli_distribution const& lhs, bernoulli_distribution const& rhs) SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return !(lhs == rhs);
}
};
} // namespace random
using sprout::random::bernoulli_distribution;
} // namespace sprout
2013-03-22 05:24:19 +00:00
#endif // #ifndef SPROUT_RANDOM_BERNOULLI_DISTRIBUTION_HPP