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

fix recursion depth: distance

add random::make_range
This commit is contained in:
bolero-MURAKAMI 2012-12-23 17:59:00 +09:00
parent 6a6fc03806
commit 5fcb039ab4
3 changed files with 86 additions and 6 deletions

View file

@ -5,6 +5,7 @@
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/iterator/next.hpp>
#include <sprout/utility/pair.hpp>
#include <sprout/adl/not_found.hpp>
namespace sprout_adl {
@ -22,18 +23,48 @@ namespace sprout {
return last - first;
}
// Copyright (C) 2011 RiSK (sscrisk)
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
iterator_distance_impl(InputIterator first, InputIterator last) {
return first == last ? 0
: 1 + sprout::iterator_detail::iterator_distance_impl(sprout::next(first), last)
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type>
iterator_distance_impl_1(
sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type> const& current,
InputIterator last, typename std::iterator_traits<InputIterator>::difference_type n
)
{
typedef sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type> type;
return current.first == last ? current
: n == 1 ? type(sprout::next(current.first), current.second + 1)
: sprout::iterator_detail::iterator_distance_impl_1(
sprout::iterator_detail::iterator_distance_impl_1(
current,
last, n / 2
),
last, n - n / 2
)
;
}
template<typename InputIterator>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type>
iterator_distance_impl(
sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type> const& current,
InputIterator last, typename std::iterator_traits<InputIterator>::difference_type n
)
{
typedef sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type> type;
return current.first == last ? current
: sprout::iterator_detail::iterator_distance_impl(
sprout::iterator_detail::iterator_distance_impl_1(
current,
last, n
),
last, n * 2
)
;
}
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
iterator_distance(InputIterator first, InputIterator last, void*) {
return sprout::iterator_detail::iterator_distance_impl(first, last);
typedef sprout::pair<InputIterator, typename std::iterator_traits<InputIterator>::difference_type> type;
return sprout::iterator_detail::iterator_distance_impl(type(first, 0), last, 1).second;
}
template<typename InputIterator>

View file

@ -7,6 +7,7 @@
#include <sprout/random/variate_generator.hpp>
#include <sprout/random/random_result.hpp>
#include <sprout/random/iterator.hpp>
#include <sprout/random/range.hpp>
#include <sprout/random/unique_seed.hpp>
#endif // #ifndef SPROUT_RANDOM_HPP

48
sprout/random/range.hpp Normal file
View file

@ -0,0 +1,48 @@
#ifndef SPROUT_RANDOM_RANGE_HPP
#define SPROUT_RANDOM_RANGE_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/iterator/generator_iterator.hpp>
#include <sprout/type_traits/enabler_if.hpp>
#include <sprout/range/range_container.hpp>
#include <sprout/random/iterator.hpp>
namespace sprout {
namespace random {
//
// make_range
//
template<
typename Engine, typename Distribution,
typename sprout::enabler_if<!std::is_integral<Distribution>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR auto
make_range(
Engine const& engine, Distribution const& distribution,
typename sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type>::difference_type count = -1
)
-> sprout::range::range_container<sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type> >
{
return sprout::range::range_container<sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type> >(
sprout::random::begin(engine, distribution, count),
sprout::random::end(engine, distribution)
);
}
template<typename Engine>
inline SPROUT_CONSTEXPR auto
make_range(
Engine const& engine,
typename sprout::generator_iterator<typename std::remove_reference<decltype(engine())>::type>::difference_type count = -1
)
-> sprout::range::range_container<sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type> >
{
return sprout::range::range_container<sprout::generator_iterator<typename std::remove_reference<decltype(distribution(engine))>::type> >(
sprout::random::begin(engine, count),
sprout::random::end(engine)
);
}
} // namespace random
} // namespace sprout
#endif // #ifndef SPROUT_RANDOM_RANGE_HPP