fix random_result<E, D>::next_generator(): returns variate_generator<E, D>

This commit is contained in:
bolero-MURAKAMI 2014-01-15 00:06:38 +09:00
parent 6e5004b6f4
commit 406a1ff789
8 changed files with 242 additions and 98 deletions

View file

@ -9,6 +9,7 @@
#define SPROUT_GENERATOR_HPP
#include <sprout/generator/functions.hpp>
#include <sprout/generator/results.hpp>
#include <sprout/generator/generator_access_traits.hpp>
#endif // #ifndef SPROUT_GENERATOR_HPP

View file

@ -0,0 +1,44 @@
/*=============================================================================
Copyright (c) 2011-2014 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)
=============================================================================*/
#ifndef SPROUT_GENERATOR_RESULTS_HPP
#define SPROUT_GENERATOR_RESULTS_HPP
#include <type_traits>
#include <utility>
#include <sprout/config.hpp>
#include <sprout/generator/generated_value.hpp>
#include <sprout/generator/next_generator.hpp>
namespace sprout {
namespace generators {
namespace results {
//
// generated_value
//
template<typename Generator>
struct generated_value
: public std::decay<decltype(sprout::generators::generated_value(std::declval<Generator>()()))>
{};
//
// next_generator
//
template<typename Generator>
struct next_generator
: public std::decay<decltype(sprout::generators::next_generator(std::declval<Generator>()()))>
{};
} // namespace results
} // namespace generators
namespace results {
using sprout::generators::generated_value;
using sprout::generators::next_generator;
} // namespace results
} // namespace sprout
#endif // #ifndef SPROUT_GENERATOR_RESULTS_HPP

View file

@ -12,16 +12,18 @@
#include <sprout/config.hpp>
#include <sprout/array/array.hpp>
#include <sprout/utility/pair/pair.hpp>
#include <sprout/generator/functions.hpp>
#include <sprout/generator/results.hpp>
namespace sprout {
namespace random {
//
// generate_array
//
template<std::size_t N, typename URNG>
inline SPROUT_CXX14_CONSTEXPR sprout::array<typename URNG::result_type, N>
generate_array(URNG& rng) {
sprout::array<typename URNG::result_type, N> result{{}};
template<std::size_t N, typename RandomNumberGenerator>
inline SPROUT_CXX14_CONSTEXPR sprout::array<typename RandomNumberGenerator::result_type, N>
generate_array(RandomNumberGenerator& rng) {
sprout::array<typename RandomNumberGenerator::result_type, N> result{{}};
for (std::size_t i = 0; i != N; ++i) {
result[i] = rng();
}
@ -29,32 +31,47 @@ namespace sprout {
}
namespace detail {
template<std::size_t N, typename URNG, typename Random, typename... Args>
template<std::size_t N, typename RandomNumberGenerator, typename Random, typename... Args>
static SPROUT_CONSTEXPR typename std::enable_if<
(sizeof...(Args) + 1 == N),
sprout::pair<sprout::array<typename URNG::result_type, N>, URNG> const
sprout::pair<
sprout::array<typename sprout::generators::results::generated_value<RandomNumberGenerator const>::type, N>,
typename sprout::generators::results::next_generator<RandomNumberGenerator const>::type
> const
>::type generate_array_impl(Random const& rnd, Args const&... args) {
typedef sprout::pair<sprout::array<typename URNG::result_type, N>, URNG> const pair_type;
typedef sprout::pair<
sprout::array<typename sprout::generators::results::generated_value<RandomNumberGenerator const>::type, N>,
typename sprout::generators::results::next_generator<RandomNumberGenerator const>::type
> const pair_type;
return pair_type{
sprout::array<typename URNG::result_type, N>{{args..., rnd.result()}},
rnd.engine()
sprout::array<typename RandomNumberGenerator::result_type, N>{{args..., sprout::generators::generated_value(rnd)}},
sprout::generators::next_generator(rnd)
};
}
template<std::size_t N, typename URNG, typename Random, typename... Args>
template<std::size_t N, typename RandomNumberGenerator, typename Random, typename... Args>
static SPROUT_CONSTEXPR typename std::enable_if<
(sizeof...(Args) + 1 < N),
sprout::pair<sprout::array<typename URNG::result_type, N>, URNG> const
sprout::pair<
sprout::array<typename sprout::generators::results::generated_value<RandomNumberGenerator const>::type, N>,
typename sprout::generators::results::next_generator<RandomNumberGenerator const>::type
> const
>::type generate_array_impl(Random const& rnd, Args const&... args) {
return sprout::random::detail::generate_array_impl<N, URNG>(rnd(), args..., rnd.result());
return sprout::random::detail::generate_array_impl<N, RandomNumberGenerator>(
sprout::generators::next_generator(rnd)(),
args..., sprout::generators::generated_value(rnd)
);
}
} // namespace detail
//
// generate_array
//
template<std::size_t N, typename URNG>
inline SPROUT_CONSTEXPR sprout::pair<sprout::array<typename URNG::result_type, N>, URNG> const
generate_array(URNG const& rng) {
return sprout::random::detail::generate_array_impl<N, URNG>(rng());
template<std::size_t N, typename RandomNumberGenerator>
inline SPROUT_CONSTEXPR sprout::pair<
sprout::array<typename sprout::generators::results::generated_value<RandomNumberGenerator const>::type, N>,
typename sprout::generators::results::next_generator<RandomNumberGenerator const>::type
> const
generate_array(RandomNumberGenerator const& rng) {
return sprout::random::detail::generate_array_impl<N, RandomNumberGenerator>(rng());
}
} // namespace random
} // namespace sprout

View file

@ -18,6 +18,7 @@
#include <sprout/random/detail/const_mod.hpp>
#include <sprout/random/detail/signed_unsigned_tools.hpp>
#include <sprout/random/detail/generator_bits.hpp>
#include <sprout/generator/functions.hpp>
#include <sprout/utility/pair/pair.hpp>
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
@ -89,13 +90,23 @@ namespace sprout {
typedef sprout::pair<RealType, URNG> const pair_type;
return mult * r < limit ? sprout::random::detail::generate_canonical_impl_1_1<RealType, bits, URNG>(
r, limit,
s + RealType(sprout::random::detail::subtract<base_result>()(rnd.result(), rnd.engine().min())) * mult,
s + RealType(
sprout::random::detail::subtract<base_result>()(
sprout::generators::generated_value(rnd),
sprout::generators::next_generator(rnd).min()
)
) * mult,
mult * r,
rnd()
)
: pair_type(
(s + RealType(sprout::random::detail::subtract<base_result>()(rnd.result(), rnd.engine().min())) * mult) / (mult * r),
rnd.engine()
(s + RealType(
sprout::random::detail::subtract<base_result>()(
sprout::generators::generated_value(rnd),
sprout::generators::next_generator(rnd).min()
)
) * mult) / (mult * r),
sprout::generators::next_generator(rnd)
)
;
}
@ -106,13 +117,23 @@ namespace sprout {
typedef sprout::pair<RealType, URNG> const pair_type;
return r < limit ? sprout::random::detail::generate_canonical_impl_1_1<RealType, bits, URNG>(
r, limit,
RealType(sprout::random::detail::subtract<base_result>()(rnd.result(), rnd.engine().min())),
RealType(
sprout::random::detail::subtract<base_result>()(
sprout::generators::generated_value(rnd),
sprout::generators::next_generator(rnd).min()
)
),
r,
rnd()
)
: pair_type(
RealType(sprout::random::detail::subtract<base_result>()(rnd.result(), rnd.engine().min())) / r,
rnd.engine()
RealType(
sprout::random::detail::subtract<base_result>()(
sprout::generators::generated_value(rnd),
sprout::generators::next_generator(rnd).min()
)
) / r,
sprout::generators::next_generator(rnd)
)
;
}
@ -135,13 +156,17 @@ namespace sprout {
typedef sprout::pair<RealType, URNG> const pair_type;
return mult * r < limit ? sprout::random::detail::generate_canonical_impl_0_1<RealType, bits, URNG>(
r, limit,
s + sprout::math::floor((RealType(rnd.result()) - RealType(rnd.engine().min())) * r) * mult,
s + sprout::math::floor(
(RealType(sprout::generators::generated_value(rnd)) - RealType(sprout::generators::next_generator(rnd).min())) * r
) * mult,
mult * r,
rnd()
)
: pair_type(
(s + sprout::math::floor((RealType(rnd.result()) - RealType(rnd.engine().min())) * r) * mult) / (mult * r),
rnd.engine()
(s + sprout::math::floor(
(RealType(sprout::generators::generated_value(rnd)) - RealType(sprout::generators::next_generator(rnd).min())) * r
) * mult) / (mult * r),
sprout::generators::next_generator(rnd)
)
;
}
@ -151,13 +176,13 @@ namespace sprout {
typedef sprout::pair<RealType, URNG> const pair_type;
return r < limit ? sprout::random::detail::generate_canonical_impl_0_1<RealType, bits, URNG>(
r, limit,
RealType(rnd.result() - rnd.engine().min()),
RealType(sprout::generators::generated_value(rnd) - sprout::generators::next_generator(rnd).min()),
r,
rnd()
)
: pair_type(
RealType(rnd.result() - rnd.engine().min()) / r,
rnd.engine()
RealType(sprout::generators::generated_value(rnd) - sprout::generators::next_generator(rnd).min()) / r,
sprout::generators::next_generator(rnd)
)
;
}

View file

@ -15,7 +15,9 @@
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/tuple/tuple.hpp>
#include <sprout/generator.hpp>
#include <sprout/generator/functions.hpp>
#include <sprout/random/random_result_fwd.hpp>
#include <sprout/random/variate_generator.hpp>
#include <sprout/utility/swap.hpp>
#include <sprout/utility/move.hpp>
#include <sprout/type_traits/integral_constant.hpp>
@ -25,9 +27,6 @@ namespace sprout {
//
// random_result
//
template<typename Engine, typename Distribution = void, typename Enable = void>
class random_result;
template<typename Engine, typename Distribution>
class random_result<
Engine,
@ -46,6 +45,7 @@ namespace sprout {
typedef Engine engine_type;
typedef Distribution distribution_type;
typedef typename distribution_type::result_type result_type;
typedef sprout::random::variate_generator<engine_type, distribution_type> generator_type;
private:
typedef std::iterator<
std::input_iterator_tag,
@ -62,31 +62,23 @@ namespace sprout {
typedef typename base_type::reference reference;
private:
result_type result_;
engine_type engine_;
distribution_type distribution_;
generator_type generator_;
public:
SPROUT_CONSTEXPR random_result()
: result_()
, engine_()
, distribution_()
, generator_()
{}
random_result(random_result const&) = default;
SPROUT_CONSTEXPR random_result(
result_type result,
engine_type const& engine,
distribution_type const& distribution
)
: result_(result)
, engine_(engine)
, distribution_(distribution)
, generator_(engine, distribution)
{}
SPROUT_CONSTEXPR random_result operator()() const {
return distribution_(engine_);
}
SPROUT_CXX14_CONSTEXPR result_type& result() SPROUT_NOEXCEPT {
return result_;
}
SPROUT_CONSTEXPR result_type const& result() const SPROUT_NOEXCEPT {
return result_;
return generator_();
}
SPROUT_CXX14_CONSTEXPR result_type& generated_value() SPROUT_NOEXCEPT {
return result_;
@ -94,51 +86,54 @@ namespace sprout {
SPROUT_CONSTEXPR result_type const& generated_value() const SPROUT_NOEXCEPT {
return result_;
}
SPROUT_CXX14_CONSTEXPR generator_type& next_generator() SPROUT_NOEXCEPT {
return generator_;
}
SPROUT_CONSTEXPR generator_type const& next_generator() const SPROUT_NOEXCEPT {
return generator_;
}
SPROUT_CXX14_CONSTEXPR result_type& result() SPROUT_NOEXCEPT {
return result_;
}
SPROUT_CONSTEXPR result_type const& result() const SPROUT_NOEXCEPT {
return result_;
}
SPROUT_CXX14_CONSTEXPR engine_type& engine() SPROUT_NOEXCEPT {
return engine_;
return generator_.engine();
}
SPROUT_CONSTEXPR engine_type const& engine() const SPROUT_NOEXCEPT {
return engine_;
}
SPROUT_CXX14_CONSTEXPR random_result& next_generator() SPROUT_NOEXCEPT {
return *this;
}
SPROUT_CONSTEXPR random_result const& next_generator() const SPROUT_NOEXCEPT {
return *this;
return generator_.engine();
}
SPROUT_CXX14_CONSTEXPR distribution_type& distribution() SPROUT_NOEXCEPT {
return distribution_;
return generator_.distribution();
}
SPROUT_CONSTEXPR distribution_type const& distribution() const SPROUT_NOEXCEPT {
return distribution_;
return generator_.distribution();
}
SPROUT_CXX14_CONSTEXPR operator result_type&() SPROUT_NOEXCEPT {
return generated_value();
return result_;
}
SPROUT_CONSTEXPR operator result_type const&() const SPROUT_NOEXCEPT {
return generated_value();
return result_;
}
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
return distribution_.min();
return distribution().min();
}
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
return distribution_.max();
return distribution().max();
}
SPROUT_CXX14_CONSTEXPR void swap(random_result& other)
SPROUT_NOEXCEPT_EXPR(
SPROUT_NOEXCEPT_EXPR(sprout::swap(result_, other.result_))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(engine_, other.engine_))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(distribution_, other.distribution_))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(generator_, other.generator_))
)
{
sprout::swap(result_, other.result_);
sprout::swap(engine_, other.engine_);
sprout::swap(distribution_, other.distribution_);
sprout::swap(generator_, other.generator_);
}
friend SPROUT_CONSTEXPR bool operator==(random_result const& lhs, random_result const& rhs) SPROUT_NOEXCEPT {
return lhs.result_ == rhs.result_
&& lhs.engine_ == rhs.engine_
&& lhs.distribution_ == rhs.distribution_
&& lhs.generator_ == rhs.generator_
;
}
friend SPROUT_CONSTEXPR bool operator!=(random_result const& lhs, random_result const& rhs) SPROUT_NOEXCEPT {
@ -178,6 +173,7 @@ namespace sprout {
public:
typedef Engine engine_type;
typedef typename engine_type::result_type result_type;
typedef engine_type generator_type;
private:
typedef std::iterator<
std::input_iterator_tag,
@ -194,27 +190,22 @@ namespace sprout {
typedef typename base_type::reference reference;
private:
result_type result_;
engine_type engine_;
generator_type generator_;
public:
SPROUT_CONSTEXPR random_result()
: result_()
, engine_()
, generator_()
{}
random_result(random_result const&) = default;
SPROUT_CONSTEXPR random_result(
result_type result,
engine_type const& engine
)
: result_(result)
, engine_(engine)
, generator_(engine)
{}
SPROUT_CONSTEXPR random_result operator()() const {
return engine_();
}
SPROUT_CXX14_CONSTEXPR result_type& result() SPROUT_NOEXCEPT {
return result_;
}
SPROUT_CONSTEXPR result_type const& result() const SPROUT_NOEXCEPT {
return result_;
return generator_();
}
SPROUT_CXX14_CONSTEXPR result_type& generated_value() SPROUT_NOEXCEPT {
return result_;
@ -222,33 +213,39 @@ namespace sprout {
SPROUT_CONSTEXPR result_type const& generated_value() const SPROUT_NOEXCEPT {
return result_;
}
SPROUT_CXX14_CONSTEXPR generator_type& next_generator() SPROUT_NOEXCEPT {
return generator_;
}
SPROUT_CONSTEXPR generator_type const& next_generator() const SPROUT_NOEXCEPT {
return generator_;
}
SPROUT_CXX14_CONSTEXPR result_type& result() SPROUT_NOEXCEPT {
return result_;
}
SPROUT_CONSTEXPR result_type const& result() const SPROUT_NOEXCEPT {
return result_;
}
SPROUT_CXX14_CONSTEXPR engine_type& engine() SPROUT_NOEXCEPT {
return engine_;
return generator_;
}
SPROUT_CONSTEXPR engine_type const& engine() const SPROUT_NOEXCEPT {
return engine_;
}
SPROUT_CXX14_CONSTEXPR random_result& next_generator() SPROUT_NOEXCEPT {
return *this;
}
SPROUT_CONSTEXPR random_result const& next_generator() const SPROUT_NOEXCEPT {
return *this;
return generator_;
}
SPROUT_CXX14_CONSTEXPR operator result_type&() SPROUT_NOEXCEPT {
return generated_value();
return result_;
}
SPROUT_CONSTEXPR operator result_type const&() const SPROUT_NOEXCEPT {
return generated_value();
return result_;
}
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
return engine_.min();
return engine().min();
}
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
return engine_.max();
return engine().max();
}
friend SPROUT_CONSTEXPR bool operator==(random_result const& lhs, random_result const& rhs) SPROUT_NOEXCEPT {
return lhs.result_ == rhs.result_
&& lhs.engine_ == rhs.engine_
&& lhs.generator_ == rhs.generator_
;
}
friend SPROUT_CONSTEXPR bool operator!=(random_result const& lhs, random_result const& rhs) SPROUT_NOEXCEPT {
@ -257,11 +254,11 @@ namespace sprout {
SPROUT_CXX14_CONSTEXPR void swap(random_result& other)
SPROUT_NOEXCEPT_EXPR(
SPROUT_NOEXCEPT_EXPR(sprout::swap(result_, other.result_))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(engine_, other.engine_))
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(generator_, other.generator_))
)
{
sprout::swap(result_, other.result_);
sprout::swap(engine_, other.engine_);
sprout::swap(generator_, other.generator_);
}
SPROUT_CONSTEXPR reference operator*() const SPROUT_NOEXCEPT {
return result_;
@ -303,8 +300,6 @@ namespace sprout {
}
} // namespace random
using sprout::random::random_result;
namespace tuples {
namespace detail {
template<std::size_t I, typename T>
@ -317,7 +312,7 @@ namespace sprout {
template<typename Engine, typename Distribution>
struct tuple_element_impl<1, sprout::random::random_result<Engine, Distribution> > {
public:
typedef sprout::random::random_result<Engine, Distribution> type;
typedef typename sprout::random::random_result<Engine, Distribution>::generator_type type;
};
template<std::size_t I, typename T>
@ -337,11 +332,11 @@ namespace sprout {
template<typename Engine, typename Distribution>
struct get_impl<1, sprout::random::random_result<Engine, Distribution> > {
public:
SPROUT_CONSTEXPR sprout::random::random_result<Engine, Distribution>&
SPROUT_CONSTEXPR typename sprout::random::random_result<Engine, Distribution>::generator_type&
operator()(sprout::random::random_result<Engine, Distribution>& t) const {
return t.next_generator();
}
SPROUT_CONSTEXPR sprout::random::random_result<Engine, Distribution> const&
SPROUT_CONSTEXPR typename sprout::random::random_result<Engine, Distribution>::generator_type const&
operator()(sprout::random::random_result<Engine, Distribution> const& t) const {
return t.next_generator();
}

View file

@ -0,0 +1,25 @@
/*=============================================================================
Copyright (c) 2011-2014 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)
=============================================================================*/
#ifndef SPROUT_RANDOM_RANDOM_RESULT_FWD_HPP
#define SPROUT_RANDOM_RANDOM_RESULT_FWD_HPP
#include <sprout/config.hpp>
namespace sprout {
namespace random {
//
// random_result
//
template<typename Engine, typename Distribution = void, typename Enable = void>
class random_result;
} // namespace random
using sprout::random::random_result;
} // namespace sprout
#endif // #ifndef SPROUT_RANDOM_RANDOM_RESULT_FWD_HPP

View file

@ -9,10 +9,11 @@
#define SPROUT_RANDOM_UTILITY_HPP
#include <sprout/config.hpp>
#include <sprout/random/random_result_fwd.hpp>
#include <sprout/random/random_result.hpp>
#include <sprout/random/variate_generator.hpp>
#include <sprout/random/generate_canonical.hpp>
#include <sprout/random/generate_array.hpp>
#include <sprout/random/variate_generator.hpp>
#include <sprout/random/iterator.hpp>
#include <sprout/random/range.hpp>

View file

@ -11,7 +11,8 @@
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/random/detail/ptr_helper.hpp>
#include <sprout/random/random_result.hpp>
#include <sprout/random/random_result_fwd.hpp>
#include <sprout/utility/swap.hpp>
namespace sprout {
namespace random {
@ -41,6 +42,11 @@ namespace sprout {
engine_type engine_;
distribution_type distribution_;
public:
SPROUT_CONSTEXPR variate_generator()
: engine_()
, distribution_()
{}
variate_generator(variate_generator const&) = default;
SPROUT_CONSTEXPR variate_generator(
engine_param_type engine,
distribution_param_type distribution
@ -48,7 +54,7 @@ namespace sprout {
: engine_(engine)
, distribution_(distribution)
{}
SPROUT_CONSTEXPR random_result_type operator()() const {
SPROUT_CONSTEXPR random_result_type const operator()() const {
return distribution_(engine_);
}
engine_reference_type engine() SPROUT_NOEXCEPT {
@ -69,8 +75,36 @@ namespace sprout {
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
return distribution_.max();
}
SPROUT_CXX14_CONSTEXPR void swap(variate_generator& other)
SPROUT_NOEXCEPT_EXPR(
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);
}
};
//
// 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)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
//
// combine
//
@ -93,4 +127,6 @@ namespace sprout {
using sprout::random::variate_generator;
} // namespace sprout
#include <sprout/random/random_result.hpp>
#endif // #ifndef SPROUT_RANDOM_VARIATE_GENERATOR_HPP