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 "False positive: also defines std::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_STDLIB_INCLUDE_STDLIB_DETAIL_ACCUMULATE_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_ACCUMULATE_HPP_
9
10// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
11// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
12// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
13
14// IWYU pragma: private, include <numeric>
15// IWYU pragma: friend "stdlib_detail/.*"
16//
17#include "arene/base/algorithm/detail/generalized_reduce.hpp"
18#include "arene/base/algorithm/detail/reduce_policy_copy_assign.hpp"
19#include "arene/base/algorithm/detail/traits.hpp"
21#include "arene/base/stdlib_choice/enable_if.hpp"
22#include "arene/base/stdlib_choice/is_assignable.hpp"
23#include "arene/base/stdlib_choice/is_move_constructible.hpp"
24#include "arene/base/stdlib_choice/move.hpp"
25#include "arene/base/stdlib_choice/plus.hpp"
29
30namespace std {
31// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
32/// @brief computes a left fold of a range
33/// @tparam InputIt type of the iterator for the range
34/// @tparam Value type of the accumulated value
35/// @tparam Plus type of the addition operation
36/// @param first iterator to the beginning of the range
37/// @param last iterator to one past the end of the range
38/// @param init initial value to start with
39/// @param plus binary function object used to reduce transformed elements
40/// to a single value
41///
42/// @return Initializes the accumulator @c acc (of type @c Value) with the
43/// initial value @c init and then modifies it with the expression
44/// <c> acc = plus(acc, *it) </c> for every iterator
45/// @c it in the range <c> [first, last) </c> in order.
46///
47/// @pre @c plus does not invalidate subranges, nor modify elements in the
48/// range <c> [first, last) </c>
49///
50/// @note Constraints <br>
51/// * @c InputIt must satisfy the input iterator requirements
52/// * @c Value is copy constructible
53/// * @c Value is copy assignable
54/// * <c> plus(init, *first) </c> is assignable to @c Value
55///
56/// @note Complexity <br>
57/// O(N) applications of @c plus, where
58/// <c> N = distance(first, last) </c>.
59///
60/// @note @c accumulate always performs the operations in the order given.
61///
62template <
63 class InputIt,
64 class Value,
65 class Plus,
70 std::is_assignable<
71 Value&,
73 Plus&,
74 Value&,
75 arene::base::algorithm_detail::iter_reference_t<InputIt>
76 >
77 >::value
78 >
79 > = nullptr>
81 InputIt first,
82 InputIt last,
83 Value init,
84 Plus plus
85) noexcept( //
88 arene::base::algorithm_detail::generalized_reduce,
89 arene::base::algorithm_detail::reduce_policy_copy_assign<Value>,
90 InputIt&,
91 InputIt&,
92 Value,
93 Plus&
94 >
95 )
96 -> Value
97{
98 using arene::base::algorithm_detail::generalized_reduce;
99 using policy = arene::base::algorithm_detail::reduce_policy_copy_assign<Value>;
100
101 return policy::access( //
102 generalized_reduce{}( //
103 policy{},
104 first,
105 last,
106 std::move(init),
107 plus
108 )
109 );
110}
111
112/// @brief computes a left fold of a range
113/// @tparam InputIt type of the iterator for the range
114/// @tparam Value type of the accumulated value
115/// @param first iterator to the beginning of the range
116/// @param last iterator to one past the end of the range
117/// @param init initial value to start with
118///
119/// @return Initializes the accumulator @c acc (of type @c Value) with the
120/// initial value @c init and then modifies it with the expression
121/// <c> acc = acc + *it </c> for every iterator
122/// @c it in the range <c> [first, last) </c> in order.
123///
124/// @pre binary @c operator+ does not invalidate subranges, nor modify elements
125/// in the range <c> [first, last) </c>
126///
127/// @note Constraints <br>
128/// * @c InputIt must satisfy the input iterator requirements
129/// * @c Value is copy constructible
130/// * @c Value is copy assignable
131/// * <c> init + *first </c> is assignable to @c Value
132///
133/// @note Complexity <br>
134/// O(N) applications of @c operator+, where
135/// <c> N = distance(first, last) </c>.
136///
137/// @note @c accumulate always performs the operations in the order given.
138///
139template <
140 class InputIt,
141 class Value,
146 Value&,
148 plus<>&,
149 Value&,
150 arene::base::algorithm_detail::iter_reference_t<InputIt>>>::value>> = nullptr>
152 InputIt first,
153 InputIt last,
154 Value init
155) noexcept( //
156 noexcept(std::accumulate(
161 ))
162) -> Value {
163 return std::accumulate(std::move(first), std::move(last), std::move(init), std::plus<>{});
164}
165
166} // namespace std
167
168#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_ACCUMULATE_HPP_
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
Public export header for the constraints subpackage.
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 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
STL namespace.
constexpr Type integral_constant< Type, Value >::value
Out-of-line definition for the value constant.
Definition integral_constant.hpp:64
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
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
auto accumulate(InputIt first, InputIt last, Value init, Plus plus) noexcept(//std::is_nothrow_move_constructible< Value >::value &&arene::base::is_nothrow_invocable_v< arene::base::algorithm_detail::generalized_reduce, arene::base::algorithm_detail::reduce_policy_copy_assign< Value >, InputIt &, InputIt &, Value, Plus & >) -> Value
computes a left fold of a range
Definition accumulate.hpp:80