Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
accumulate.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::accumulate"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ACCUMULATE_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ACCUMULATE_HPP_
9
10// IWYU pragma: private, include "arene/base/algorithm.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/algorithm/detail/generalized_reduce.hpp"
17#include "arene/base/algorithm/detail/reduce_policy_move_assign.hpp"
18#include "arene/base/algorithm/detail/synthesize_range1_overload.hpp" // IWYU pragma: export
19#include "arene/base/algorithm/detail/traits.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/is_assignable.hpp"
24#include "arene/base/stdlib_choice/is_move_constructible.hpp"
25#include "arene/base/stdlib_choice/move.hpp"
26#include "arene/base/stdlib_choice/plus.hpp"
30// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
31
32// IWYU pragma: no_include "arene/base/stdlib_choice/iterator_traits.hpp"
33
34// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
35
36namespace arene {
37namespace base {
38namespace algorithm_detail {
39namespace accumulate_detail {
40
41/// @brief function object implementing 'accumulate'
42class accumulate_fn {
43 public:
44 /// @brief computes a left fold of a range
45 /// @tparam InputIt type of the iterator for the range
46 /// @tparam Value type of the accumulated value
47 /// @tparam Plus type of the addition operation
48 /// @param first iterator to the beginning of the range
49 /// @param last iterator to one past the end of the range
50 /// @param init initial value to start with
51 /// @param plus binary function object used to "add" elements to the accumulated value.
52 ///
53 /// @return Initializes the accumulator @c acc (of type @c Value) with the
54 /// initial value @c init and then modifies it with the expression
55 /// <c> acc = plus(std::move(acc), *it) </c> for every iterator
56 /// @c it in the range <c> [first, last) </c> in order.
57 ///
58 /// @pre @c plus does not invalidate subranges, nor modify elements in the
59 /// range <c> [first, last) </c>, else behavior is undefined.
60 ///
61 /// @note Constraints <br>
62 /// * @c InputIt must satisfy the input iterator requirements
63 /// * @c Value is copy constructible
64 /// * @c Value is copy assignable
65 /// * <c> plus(init, *first) </c> is assignable to @c Value
66 ///
67 /// @note Complexity <br>
68 /// O(N) applications of @c plus, where
69 /// <c> N = distance(first, last) </c>.
70 ///
71 /// @note @c accumulate always performs the operations in the order given.
72 template <
73 class InputIt,
74 class Value,
75 class Plus,
81 Value&,
83 Plus&,
84 Value&&,
86 >
87 >::value
88 >
89 > = nullptr>
90 constexpr auto operator()(
91 InputIt first,
92 InputIt last,
93 Value init,
94 Plus plus
95 ) const noexcept( //
98 generalized_reduce,
99 reduce_policy_move_assign<Value>,
100 InputIt&,
101 InputIt&,
102 Value,
103 Plus& >
104 )
105 -> Value
106 {
107 using value_policy = reduce_policy_move_assign<Value>;
108
109 return value_policy::access( //
110 generalized_reduce{}( //
111 value_policy{},
112 first,
113 last,
114 std::move(init),
115 plus
116 )
117 );
118 }
119
120 /// @brief computes a left fold of a range
121 /// @tparam InputIt type of the iterator for the range
122 /// @tparam Value type of the accumulated value
123 /// @param first iterator to the beginning of the range
124 /// @param last iterator to one past the end of the range
125 /// @param init initial value to start with
126 ///
127 /// @return Initializes the accumulator @c acc (of type @c Value) with the
128 /// initial value @c init and then modifies it with the expression
129 /// <c> acc = acc + *it </c> for every iterator
130 /// @c it in the range <c> [first, last) </c> in order.
131 ///
132 /// @pre binary @c operator+ does not invalidate subranges, nor modify elements
133 /// in the range <c> [first, last) </c>
134 ///
135 /// @note Constraints <br>
136 /// * @c InputIt must satisfy the input iterator requirements
137 /// * @c Value is copy constructible
138 /// * @c Value is copy assignable
139 /// * <c> init + *first </c> is assignable to @c Value
140 ///
141 /// @note Complexity <br>
142 /// O(N) applications of @c operator+ , where
143 /// <c> N = distance(first, last) </c>.
144 ///
145 /// @note @c accumulate always performs the operations in the order given.
146 template <
147 class InputIt,
148 class Value,
153 Value&,
156 Value&&,
157 iter_reference_t<InputIt>>>::value>> = nullptr>
158 constexpr auto operator()(
159 InputIt first,
160 InputIt last,
161 Value init
162 ) const noexcept( //
164 accumulate_fn,
165 InputIt&&,
166 InputIt&&,
167 Value&&,
168 std::plus<>&&>
169 )
170 -> Value
171 {
172 return (*this)( //
173 std::move(first),
174 std::move(last),
175 std::move(init),
177 );
178 }
179};
180} // namespace accumulate_detail
181} // namespace algorithm_detail
182
183/// @brief performs a left fold of a range
184/// Starting with the initial value, combines each element in order using the supplied binary operation.
185/// Overloads accept either an iterator pair or a range.
186// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
187// object used in multiple TUs."
188// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
189// object used in multiple TUs."
190// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
192 algorithm_detail::synthesize_range1_overload_t<algorithm_detail::accumulate_detail::accumulate_fn>,
193 accumulate
194);
195// parasoft-end-suppress CERT_CPP-DCL56-a
196// parasoft-end-suppress AUTOSAR-M7_3_3-a
197// parasoft-end-suppress CERT_CPP-DCL59-a
198
199} // namespace base
200} // namespace arene
201
202#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_ACCUMULATE_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
static constexpr value_type value
Definition integral_constant.hpp:34
Type trait to detect if an instance of To is assignable to from arguments of From.
Definition is_assignable.hpp:47
Function object for performing addition.
Definition plus.hpp:27
Definition accumulate.hpp:37
typename invoke_result< Fn, Args... >::type invoke_result_t
The return type of a functor invoked with the specified arguments.
Definition is_invocable.hpp:271
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
typename iter_reference_detail::iter_reference_impl< Itr >::type iter_reference_t
Determins the reference type of the given iterator.
Definition iter_reference_t.hpp:47
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41