fix generator_iterator

fix random::begin, end
add random::combine_copy
This commit is contained in:
bolero-MURAKAMI 2012-09-27 21:57:51 +09:00
parent f40ee0a2ff
commit 10e9a7679d
4 changed files with 39 additions and 31 deletions

View file

@ -17,16 +17,16 @@ namespace sprout {
class generator_iterator
: public std::iterator<
std::forward_iterator_tag,
typename std::remove_reference<decltype(sprout::generators::generated_value(std::declval<Generator>()))>::type,
typename std::remove_reference<decltype(sprout::generators::generated_value(std::declval<Generator const>()))>::type,
std::ptrdiff_t,
typename std::remove_reference<decltype(sprout::generators::generated_value(std::declval<Generator>()))>::type*,
decltype(sprout::generators::generated_value(std::declval<Generator>()))
typename std::remove_reference<decltype(sprout::generators::generated_value(std::declval<Generator const>()))>::type*,
decltype(sprout::generators::generated_value(std::declval<Generator const>()))
>
{
public:
typedef Generator generator_type;
typedef std::forward_iterator_tag iterator_category;
typedef decltype(sprout::generators::generated_value(std::declval<Generator>())) reference;
typedef decltype(sprout::generators::generated_value(std::declval<Generator const>())) reference;
typedef typename std::remove_reference<reference>::type value_type;
typedef std::ptrdiff_t difference_type;
typedef value_type* pointer;
@ -96,7 +96,10 @@ namespace sprout {
SPROUT_CONSTEXPR reference operator*() const {
return count_ != 0
? sprout::generators::generated_value(gen_)
: (throw std::out_of_range("generator_iterator<>: dereference at out of range"), gen_.result())
: (
throw std::out_of_range("generator_iterator<>: dereference at out of range"),
sprout::generators::generated_value(gen_)
)
;
}
SPROUT_CONSTEXPR pointer operator->() const {

View file

@ -1,7 +1,6 @@
#ifndef SPROUT_RANDOM_ITERATOR_HPP
#define SPROUT_RANDOM_ITERATOR_HPP
#include <utility>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/iterator/generator_iterator.hpp>
@ -14,26 +13,26 @@ namespace sprout {
//
template<
typename Engine, typename Distribution,
typename sprout::enabler_if<!std::is_integral<Distribution>::value>::tyep = sprout::enabler
typename sprout::enabler_if<!std::is_integral<Distribution>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR auto
begin(
Engine const& engine, Distribution const& distribution,
typename sprout::generator_iterator<typename std::decay<decltype(distribution(engine))>::type>::difference_type count = -1
typename sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type>::difference_type count = -1
)
-> sprout::generator_iterator<typename std::decay<decltype(distribution(engine))>::type>
-> sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type>
{
return sprout::generator_iterator<typename std::decay<decltype(distribution(engine))>::type>(distribution(engine), count);
return sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type>(distribution(engine), count);
}
template<typename Engine>
inline SPROUT_CONSTEXPR auto
begin(
Engine const& engine,
typename sprout::generator_iterator<typename std::decay<decltype(engine())>::type>::difference_type count = -1
typename sprout::generator_iterator<typename std::remove_reference<decltype(engine())>::type>::difference_type count = -1
)
-> sprout::generator_iterator<typename std::decay<decltype(engine())>::type>
-> sprout::generator_iterator<typename std::remove_reference<decltype(engine())>::type>
{
return sprout::generator_iterator<typename std::decay<decltype(engine())>::type>(engine(), count);
return sprout::generator_iterator<typename std::remove_reference<decltype(engine())>::type>(engine(), count);
}
//
@ -41,20 +40,20 @@ namespace sprout {
//
template<
typename Engine, typename Distribution,
typename sprout::enabler_if<!std::is_integral<Distribution>::value>::tyep = sprout::enabler
typename sprout::enabler_if<!std::is_integral<Distribution>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR auto
end(Engine const& engine, Distribution const& distribution)
-> sprout::generator_iterator<typename std::decay<decltype(distribution(engine))>::type>
-> sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type>
{
return sprout::generator_iterator<typename std::decay<decltype(distribution(engine))>::type>();
return sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type>();
}
template<typename Engine>
inline SPROUT_CONSTEXPR auto
end(Engine const& engine)
-> sprout::generator_iterator<typename std::decay<decltype(engine())>::type>
-> sprout::generator_iterator<typename std::remove_reference<decltype(engine())>::type>
{
return sprout::generator_iterator<typename std::decay<decltype(engine())>::type>();
return sprout::generator_iterator<typename std::remove_reference<decltype(engine())>::type>();
}
} // namespace random
} // namespace sprout

View file

@ -280,13 +280,13 @@ namespace sprout {
// generated_value
//
template<typename Engine, typename Distribution>
inline SPROUT_CONSTEXPR typename sprout::random::random_result<Engine, Distribution>::result_type const&
generated_value(sprout::random::random_result<Engine, Distribution> const& t) {
inline typename sprout::random::random_result<Engine, Distribution>::result_type&
generated_value(sprout::random::random_result<Engine, Distribution>& t) {
return t.generated_value();
}
template<typename Engine, typename Distribution>
inline typename sprout::random::random_result<Engine, Distribution>::result_type&
generated_value(sprout::random::random_result<Engine, Distribution>& t) {
inline SPROUT_CONSTEXPR typename sprout::random::random_result<Engine, Distribution>::result_type const&
generated_value(sprout::random::random_result<Engine, Distribution> const& t) {
return t.generated_value();
}
@ -294,13 +294,13 @@ namespace sprout {
// next_generator
//
template<typename Engine, typename Distribution>
inline SPROUT_CONSTEXPR typename sprout::random::random_result<Engine, Distribution> const&
next_generator(sprout::random::random_result<Engine, Distribution> const& t) {
inline typename sprout::random::random_result<Engine, Distribution>&
next_generator(sprout::random::random_result<Engine, Distribution>& t) {
return t.next_generator();
}
template<typename Engine, typename Distribution>
inline typename sprout::random::random_result<Engine, Distribution>&
next_generator(sprout::random::random_result<Engine, Distribution>& t) {
inline SPROUT_CONSTEXPR typename sprout::random::random_result<Engine, Distribution> const&
next_generator(sprout::random::random_result<Engine, Distribution> const& t) {
return t.next_generator();
}
} // namespace random

View file

@ -67,13 +67,19 @@ namespace sprout {
// combine
//
template<typename Engine, typename Distribution>
SPROUT_CONSTEXPR sprout::random::variate_generator<Engine const&, Distribution const&> combine(
Engine const& engine,
Distribution const& distribution
)
{
inline SPROUT_CONSTEXPR sprout::random::variate_generator<Engine const&, Distribution const&>
combine(Engine const& engine, Distribution const& distribution) {
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);
}
} // namespace random
using sprout::random::variate_generator;