Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
reduce.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::reduce"
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_REDUCE_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_REDUCE_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_movable_box.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_constructible.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"
29// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
30
31// IWYU pragma: no_include "arene/base/stdlib_choice/iterator_traits.hpp"
32
33// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
34
35namespace arene {
36namespace base {
37namespace algorithm_detail {
38namespace reduce_detail {
39
40/// @brief function object implementing 'reduce'
41class reduce_fn {
42 public:
43 /// @brief performs a fold with an unspecified order
44 /// @tparam InputIt type of the iterator for the range
45 /// @tparam Value type of the reduced value
46 /// @tparam Reduce type of the reduction operation
47 /// @param first iterator to the beginning of the range
48 /// @param last iterator to one past the end of the range
49 /// @param init initial value to start with
50 /// @param reduce binary function object used to reduce transformed elements
51 /// to a single value
52 ///
53 /// @return The generalized sum of @c init and @c values over @c reduce.
54 ///
55 /// @pre @c reduce does not invalidate subranges, nor modify
56 /// elements in the range <c> [first, last) </c>
57 ///
58 /// @note Constraints <br>
59 /// * @c InputIt must satisfy the input iterator requirements
60 /// * @c Value is move constructible
61 /// * <c> reduce(init, init) </c> is convertible to @c Value
62 /// * <c> reduce(init, *first) </c> is convertible to @c Value
63 /// * <c> reduce(*first, init) </c> is convertible to @c Value
64 /// * <c> reduce(*first, *first) </c> is convertible to @c Value
65 ///
66 /// @note Complexity <br>
67 /// O(N) applications of @c reduce , where
68 /// <c> N = distance(first1, last1) </c>.
69 ///
70 /// @note Unlike @c accumulate, the order in which elements are reduced
71 /// is unspecified.
72 ///
73 /// @note The result is non-deterministic if @c reduce is not associative or
74 /// not commutative (e.g. floating-point addition).
75 ///
76 template <
77 class InputIt,
78 class Value,
79 class Reduce,
82 std::enable_if_t<std::is_move_constructible<Value>::value>,
84 Value,
85 Reduce&,
86 Value&,
89 Value,
90 Reduce&,
92 Value& >>,
94 Value,
95 Reduce&,
96 Value&,
97 Value& >>,
99 Value,
100 Reduce&,
102 iter_reference_t<InputIt> >>> = nullptr>
103 constexpr auto operator()(
104 InputIt first,
105 InputIt last,
106 Value init,
107 Reduce reduce
108 ) const noexcept( //
109 std::is_nothrow_constructible<
110 typename reduce_policy_movable_box<Value>::value_type,
111 Value&&
112 >::value &&
115 generalized_reduce,
116 reduce_policy_movable_box<Value>,
117 InputIt&,
118 InputIt&,
119 typename reduce_policy_movable_box<Value>::value_type,
120 Reduce& >
121 )
122 -> Value
123 {
124 using value_policy = reduce_policy_movable_box<Value>;
125
126 return value_policy::access( //
127 generalized_reduce{}( //
128 value_policy{},
129 first,
130 last,
131 typename value_policy::value_type{std::move(init)},
132 reduce
133 )
134 );
135 }
136
137 /// @brief performs a fold with an unspecified order
138 /// @tparam InputIt type of the iterator for the range
139 /// @tparam Value type of the reduced value
140 /// @param first iterator to the beginning of the range
141 /// @param last iterator to one past the end of the range
142 /// @param init initial value to start with
143 ///
144 /// @return The generalized sum of @c init and @c values over @c operator+.
145 ///
146 /// @pre binary @c operator+ does not invalidate subranges, nor modify
147 /// elements in the range <c> [first, last) </c>
148 ///
149 /// @note Constraints <br>
150 /// * @c InputIt must satisfy the input iterator requirements
151 /// * @c Value is move constructible
152 /// * <c> init + init </c> is convertible to @c Value
153 /// * <c> init + *first </c> is convertible to @c Value
154 /// * <c> *first + init </c> is convertible to @c Value
155 /// * <c> *first + *first </c> is convertible to @c Value
156 ///
157 /// @note Complexity <br>
158 /// O(N) applications of @c operator+ , where
159 /// <c> N = distance(first, last) </c>.
160 ///
161 /// @note Unlike @c accumulate, the order in which elements are reduced
162 /// is unspecified.
163 ///
164 /// @note The result is non-deterministic if @c operator+ is not associative or
165 /// not commutative (e.g. floating-point addition).
166 ///
167 template <
168 class InputIt,
169 class Value,
172 std::enable_if_t<std::is_move_constructible<Value>::value>,
174 Value,
175 std::plus<>&,
176 Value&,
179 Value,
180 std::plus<>&,
182 Value& >>,
184 Value,
185 std::plus<>&,
186 Value&,
187 Value& >>,
189 Value,
190 std::plus<>&,
192 iter_reference_t<InputIt> >>> = nullptr>
193 constexpr auto operator()(
194 InputIt first,
195 InputIt last,
196 Value init
197 ) const noexcept( //
199 reduce_fn,
200 InputIt&&,
201 InputIt&&,
202 Value&&,
203 std::plus<>&&>
204 )
205 -> Value
206 {
207 return (*this)( //
208 std::move(first),
209 std::move(last),
210 std::move(init),
211 std::plus<>{}
212 );
213 }
214};
215} // namespace reduce_detail
216} // namespace algorithm_detail
217
218/// @brief performs a fold with an unspecified order
219// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
220// object used in multiple TUs."
221// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
222// object used in multiple TUs."
223// parasoft-begin-suppress AUTOSAR-M3_4_1-b "This is a per-TU reference to a global used in multiple TUs"
224// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
226 algorithm_detail::synthesize_range1_overload_t<algorithm_detail::reduce_detail::reduce_fn>,
227 reduce
228);
229// parasoft-end-suppress CERT_CPP-DCL56-a
230// parasoft-end-suppress AUTOSAR-M3_4_1-b
231// parasoft-end-suppress AUTOSAR-M7_3_3-a
232// parasoft-end-suppress CERT_CPP-DCL59-a
233
234} // namespace base
235} // namespace arene
236
237#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_REDUCE_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
Definition accumulate.hpp:37
constexpr bool is_invocable_r_v
Determine if a functor type is invocable with the specified return and argument types.
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