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

170 lines
6.6 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_VARIATE_GENERATOR_HPP
#define SPROUT_RANDOM_VARIATE_GENERATOR_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/random/detail/ptr_helper.hpp>
#include <sprout/random/random_result_fwd.hpp>
2016-04-28 01:48:50 +00:00
#include <sprout/type_traits/lvalue_reference.hpp>
#include <sprout/utility/lvalue_forward.hpp>
#include <sprout/utility/as_const.hpp>
#include <sprout/utility/swap.hpp>
2011-10-12 20:28:33 +00:00
namespace sprout {
namespace random {
//
// variate_generator
//
template<typename Engine, typename Distribution>
class variate_generator {
private:
typedef sprout::random::detail::ptr_helper<Engine> engine_helper_type;
typedef sprout::random::detail::ptr_helper<Distribution> distribution_helper_type;
public:
typedef typename engine_helper_type::value_type engine_value_type;
typedef typename distribution_helper_type::value_type distribution_value_type;
typedef typename engine_helper_type::reference_type engine_reference_type;
typedef typename distribution_helper_type::reference_type distribution_reference_type;
typedef typename engine_helper_type::const_reference_type engine_const_reference_type;
typedef typename distribution_helper_type::const_reference_type distribution_const_reference_type;
typedef typename engine_helper_type::rvalue_type engine_param_type;
typedef typename distribution_helper_type::rvalue_type distribution_param_type;
public:
typedef Engine engine_type;
typedef Distribution distribution_type;
typedef typename distribution_value_type::result_type result_type;
2016-04-28 01:48:50 +00:00
typedef typename std::conditional<
std::is_reference<engine_type>::value && !std::is_const<typename std::remove_reference<engine_type>::type>::value,
sprout::random::random_result<engine_type, distribution_type>,
2016-04-28 01:48:50 +00:00
sprout::random::random_result<engine_value_type, distribution_value_type>
>::type random_result_type;
private:
static SPROUT_CONSTEXPR random_result_type call(variate_generator const& g, sprout::true_type) {
return random_result_type(g.distribution_(g.engine_), g.distribution_, g.engine_);
}
static SPROUT_CONSTEXPR random_result_type call(variate_generator const& g, sprout::false_type) {
return g.distribution_(sprout::as_const(g.engine_));
}
2011-10-12 20:28:33 +00:00
private:
engine_type engine_;
distribution_type distribution_;
public:
SPROUT_CONSTEXPR variate_generator()
: engine_()
, distribution_()
{}
variate_generator(variate_generator const&) = default;
2011-10-12 20:28:33 +00:00
SPROUT_CONSTEXPR variate_generator(
engine_param_type engine,
distribution_param_type distribution
)
: engine_(engine)
, distribution_(distribution)
{}
SPROUT_CONSTEXPR random_result_type operator()() const {
typedef sprout::bool_constant<std::is_reference<engine_type>::value && !std::is_const<typename std::remove_reference<engine_type>::type>::value> tag;
return call(*this, tag());
}
SPROUT_CONSTEXPR random_result_type next_value() const {
return (*this)();
2011-10-12 20:28:33 +00:00
}
SPROUT_CXX14_CONSTEXPR engine_reference_type engine() SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return engine_helper_type::ref(engine_);
}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR engine_const_reference_type engine() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return engine_helper_type::ref(engine_);
}
SPROUT_CXX14_CONSTEXPR distribution_reference_type distribution() SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return distribution_helper_type::ref(distribution_);
}
2012-11-16 04:40:19 +00:00
SPROUT_CONSTEXPR distribution_const_reference_type distribution() const SPROUT_NOEXCEPT {
2011-10-12 20:28:33 +00:00
return distribution_helper_type::ref(distribution_);
}
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 distribution_.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 distribution_.max();
}
SPROUT_CXX14_CONSTEXPR void swap(variate_generator& other)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF(
SPROUT_NOEXCEPT_EXPR(sprout::swap(engine_, other.engine_))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(distribution_, other.distribution_))
)
{
sprout::swap(engine_, other.engine_);
sprout::swap(distribution_, other.distribution_);
}
friend SPROUT_CONSTEXPR bool operator==(variate_generator const& lhs, variate_generator const& rhs) SPROUT_NOEXCEPT {
return lhs.engine_ == rhs.engine_
&& lhs.distribution_ == rhs.distribution_
;
}
friend SPROUT_CONSTEXPR bool operator!=(variate_generator const& lhs, variate_generator const& rhs) SPROUT_NOEXCEPT {
return !(lhs == rhs);
}
2011-10-12 20:28:33 +00:00
};
//
// swap
//
template<typename Engine, typename Distribution>
inline SPROUT_CXX14_CONSTEXPR void
swap(sprout::random::variate_generator<Engine, Distribution>& lhs, sprout::random::variate_generator<Engine, Distribution>& rhs)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs))
{
lhs.swap(rhs);
}
2011-10-12 20:28:33 +00:00
//
// combine
//
template<typename Engine, typename Distribution>
2016-04-28 01:48:50 +00:00
inline SPROUT_CONSTEXPR sprout::random::variate_generator<
typename sprout::lvalue_reference<Engine>::type,
typename sprout::lvalue_reference<Distribution>::type
>
combine(Engine&& engine, Distribution&& distribution) {
typedef sprout::random::variate_generator<
typename sprout::lvalue_reference<Engine>::type,
typename sprout::lvalue_reference<Distribution>::type
> type;
return type(
sprout::lvalue_forward<Engine>(engine),
sprout::lvalue_forward<Distribution>(distribution)
);
}
//
// ccombine
//
template<typename Engine, typename Distribution>
inline SPROUT_CONSTEXPR sprout::random::variate_generator<Engine const&, Distribution const&>
2016-04-28 01:48:50 +00:00
ccombine(Engine const& engine, Distribution const& distribution) {
2011-10-12 20:28:33 +00:00
return sprout::random::variate_generator<Engine const&, Distribution const&>(engine, distribution);
}
//
// combine_copy
//
template<typename Engine, typename Distribution>
inline SPROUT_CONSTEXPR sprout::random::variate_generator<Engine, Distribution>
combine_copy(Engine const& engine, Distribution const& distribution) {
return sprout::random::variate_generator<Engine, Distribution>(engine, distribution);
}
2011-10-12 20:28:33 +00:00
} // namespace random
using sprout::random::variate_generator;
} // namespace sprout
#include <sprout/random/random_result.hpp>
2013-03-22 05:24:19 +00:00
#endif // #ifndef SPROUT_RANDOM_VARIATE_GENERATOR_HPP