2012-04-01 13:15:09 +00:00
|
|
|
#ifndef SPROUT_NUMERIC_ACCUMLATE_HPP
|
|
|
|
#define SPROUT_NUMERIC_ACCUMLATE_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
2013-01-03 08:01:50 +00:00
|
|
|
#include <type_traits>
|
2012-04-01 13:15:09 +00:00
|
|
|
#include <sprout/config.hpp>
|
2013-01-03 08:01:50 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
|
|
|
#include <sprout/iterator/type_traits/is_iterator.hpp>
|
|
|
|
#include <sprout/functional/plus.hpp>
|
|
|
|
#include <sprout/utility/pair.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2013-01-03 08:01:50 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename RandomAccessIterator, typename T, typename BinaryOperation>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
accumulate_ra(
|
|
|
|
RandomAccessIterator first, RandomAccessIterator last, BinaryOperation binary_op,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator>::difference_type pivot, T const& init
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return pivot == 0 ? binary_op(init, *first)
|
|
|
|
: sprout::detail::accumulate_ra(
|
|
|
|
sprout::next(first, pivot), last, binary_op,
|
|
|
|
(sprout::distance(first, last) - pivot) / 2,
|
|
|
|
sprout::detail::accumulate_ra(
|
|
|
|
first, sprout::next(first, pivot), binary_op,
|
|
|
|
pivot / 2,
|
|
|
|
init
|
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator, typename T, typename BinaryOperation>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator>::value,
|
|
|
|
T
|
|
|
|
>::type
|
|
|
|
accumulate(
|
|
|
|
RandomAccessIterator first, RandomAccessIterator last, T init, BinaryOperation binary_op,
|
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return first == last ? init
|
|
|
|
: sprout::detail::accumulate_ra(
|
|
|
|
first, last, binary_op,
|
|
|
|
sprout::distance(first, last) / 2,
|
|
|
|
init
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2012-04-01 13:15:09 +00:00
|
|
|
|
2013-01-03 08:01:50 +00:00
|
|
|
template<typename InputIterator, typename T, typename BinaryOperation>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, T>
|
|
|
|
accumulate_impl_1(
|
|
|
|
sprout::pair<InputIterator, bool> const& current,
|
|
|
|
InputIterator last, BinaryOperation binary_op, typename std::iterator_traits<InputIterator>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<InputIterator, T> type;
|
|
|
|
return current.first == last ? current
|
|
|
|
: n == 1 ? type(sprout::next(current.first), binary_op(current.second, *current.first))
|
|
|
|
: sprout::detail::accumulate_impl_1(
|
|
|
|
sprout::detail::accumulate_impl_1(
|
|
|
|
current,
|
|
|
|
last, binary_op, n / 2
|
|
|
|
),
|
|
|
|
last, binary_op, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename InputIterator, typename T, typename BinaryOperation>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, T>
|
|
|
|
accumulate_impl(
|
|
|
|
sprout::pair<InputIterator, T> const& current,
|
|
|
|
InputIterator last, BinaryOperation binary_op, typename std::iterator_traits<InputIterator>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<InputIterator, T> type;
|
|
|
|
return current.first == last ? current
|
|
|
|
: sprout::detail::accumulate_impl(
|
|
|
|
sprout::detail::accumulate_impl_1(
|
|
|
|
current,
|
|
|
|
last, binary_op, n
|
|
|
|
),
|
|
|
|
last, binary_op, n * 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename InputIterator, typename T, typename BinaryOperation>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
accumulate(
|
|
|
|
InputIterator first, InputIterator last, T init, BinaryOperation binary_op,
|
|
|
|
void*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<InputIterator, T> type;
|
|
|
|
return sprout::detail::accumulate_impl(type(first, init), last, binary_op, 1).second;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// accumulate
|
|
|
|
//
|
|
|
|
// recursion depth:
|
|
|
|
// O(log N)
|
|
|
|
//
|
2012-04-04 08:48:02 +00:00
|
|
|
template<typename InputIterator, typename T, typename BinaryOperation>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op) {
|
2013-01-03 08:01:50 +00:00
|
|
|
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
|
|
|
|
return sprout::detail::accumulate(first, last, init, binary_op, category());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
|
2012-04-04 08:48:02 +00:00
|
|
|
template<typename InputIterator, typename T>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
accumulate(InputIterator first, InputIterator last, T init) {
|
|
|
|
return sprout::accumulate(
|
|
|
|
first, last, init,
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::plus<>()
|
2012-10-06 04:53:07 +00:00
|
|
|
);
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_NUMERIC_ACCUMLATE_HPP
|