Horizon
accumulate.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2013-present
5 //
6 // Use, modification and distribution is subject to the
7 // Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Project home: https://github.com/ericniebler/range-v3
12 //
13 #ifndef RANGES_V3_NUMERIC_ACCUMULATE_HPP
14 #define RANGES_V3_NUMERIC_ACCUMULATE_HPP
15 
16 #include <meta/meta.hpp>
17 
26 #include <range/v3/utility/static_const.hpp>
27 
28 #include <range/v3/detail/prologue.hpp>
29 
30 namespace ranges
31 {
35  {
36  template(typename I, typename S, typename T, typename Op = plus,
37  typename P = identity)(
38  requires sentinel_for<S, I> AND input_iterator<I> AND
39  indirectly_binary_invocable_<Op, T *, projected<I, P>> AND
40  assignable_from<T &, indirect_result_t<Op &, T *, projected<I, P>>>)
41  T operator()(I first, S last, T init, Op op = Op{},
42  P proj = P{}) const
43  {
44  for(; first != last; ++first)
45  init = invoke(op, init, invoke(proj, *first));
46  return init;
47  }
48 
49  template(typename Rng, typename T, typename Op = plus, typename P = identity)(
50  requires input_range<Rng> AND
51  indirectly_binary_invocable_<Op, T *, projected<iterator_t<Rng>, P>> AND
52  assignable_from<
53  T &, indirect_result_t<Op &, T *, projected<iterator_t<Rng>, P>>>)
54  T operator()(Rng && rng, T init, Op op = Op{}, P proj = P{}) const
55  {
56  return (*this)(
57  begin(rng), end(rng), std::move(init), std::move(op), std::move(proj));
58  }
59  };
60 
63 } // namespace ranges
64 
65 #include <range/v3/detail/epilogue.hpp>
66 
67 #endif
decltype(begin(declval(Rng &))) iterator_t
Definition: access.hpp:698
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
typename Fn::template invoke< Args... > invoke
Evaluate the invocable Fn with the arguments Args.
Definition: meta.hpp:541
front< Pair > first
Retrieve the first element of the pair Pair.
Definition: meta.hpp:2251
std::integral_constant< decltype(T::type::value+U::type::value), T::type::value+U::type::value > plus
An integral constant wrapper around the result of adding the two wrapped integers T::type::value and ...
Definition: meta.hpp:197
fold< L, State, Fn > accumulate
An alias for meta::fold.
Definition: meta.hpp:1597
Tiny meta-programming library.
Definition: accumulate.hpp:35
Definition: identity.hpp:25