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

131 lines
4.3 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2016-02-25 09:48:28 +00:00
Copyright (c) 2011-2016 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>
#include <type_traits>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
2013-02-26 07:14:04 +00:00
#include <sprout/iterator/type_traits/category.hpp>
#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 {
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
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,
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
)
{
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,
2013-02-23 06:21:27 +00:00
std::input_iterator_tag*
)
{
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) {
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,
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