2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 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)
|
|
|
|
=============================================================================*/
|
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>
|
2013-02-26 07:14:04 +00:00
|
|
|
#include <sprout/iterator/type_traits/category.hpp>
|
2013-01-03 08:01:50 +00:00
|
|
|
#include <sprout/functional/plus.hpp>
|
2013-02-04 14:10:24 +00:00
|
|
|
#include <sprout/utility/pair/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(
|
2016-04-01 14:37:48 +00:00
|
|
|
RandomAccessIterator const& first, RandomAccessIterator const& last, BinaryOperation binary_op,
|
2013-01-03 08:01:50 +00:00
|
|
|
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(
|
2016-04-01 14:37:48 +00:00
|
|
|
RandomAccessIterator const& first, RandomAccessIterator const& last, T init, BinaryOperation binary_op,
|
2013-01-03 08:01:50 +00:00
|
|
|
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(
|
2013-02-04 14:10:24 +00:00
|
|
|
sprout::pair<InputIterator, T> const& current,
|
2016-04-01 14:37:48 +00:00
|
|
|
InputIterator const& last, BinaryOperation binary_op, typename std::iterator_traits<InputIterator>::difference_type n
|
2013-01-03 08:01:50 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
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,
|
2016-04-01 14:37:48 +00:00
|
|
|
InputIterator const& last, BinaryOperation binary_op, typename std::iterator_traits<InputIterator>::difference_type n
|
2013-01-03 08:01:50 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
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(
|
2016-04-01 14:37:48 +00:00
|
|
|
InputIterator const& first, InputIterator const& last, T init, BinaryOperation binary_op,
|
2013-02-23 06:21:27 +00:00
|
|
|
std::input_iterator_tag*
|
2013-01-03 08:01:50 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
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
|