Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
transform_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::transform_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_TRANSFORM_REDUCE_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_TRANSFORM_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_transform_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/declval.hpp"
23#include "arene/base/stdlib_choice/enable_if.hpp"
24#include "arene/base/stdlib_choice/is_constructible.hpp"
25#include "arene/base/stdlib_choice/is_move_constructible.hpp"
26#include "arene/base/stdlib_choice/move.hpp"
27#include "arene/base/stdlib_choice/multiplies.hpp"
28#include "arene/base/stdlib_choice/plus.hpp"
31// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
32
33// IWYU pragma: no_include "arene/base/stdlib_choice/iterator_traits.hpp"
34
35// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
36
37namespace arene {
38namespace base {
39namespace algorithm_detail {
40namespace transform_reduce_detail {
41
42/// @brief function object implementing 'transform_reduce'
43class transform_reduce_fn {
44 /// @brief obtains the return type of invoking the first argument on a pack of dereferenced instances of the remaining
45 /// arguments
46 /// @tparam Invocable invocable type
47 /// @tparam Iterator iterator argument types
48 ///
49 template <class Invocable, class... Iterator>
51
52 public:
53 /// @brief applies an invocable, then reduces out of order
54 /// @tparam InputIt1 type of the iterator for the first range
55 /// @tparam InputIt2 type of the iterator for the second range
56 /// @tparam Value type of the reduced value
57 /// @tparam Reduce type of the reduction operation
58 /// @tparam Transform type of the transform operation
59 /// @param first1 iterator to the beginning of the first range
60 /// @param last1 iterator to one past the end of the first range
61 /// @param first2 iterator to the beginning of the second range
62 /// @param init initial value to start with
63 /// @param reduce binary function object used to reduce transformed elements
64 /// to a single value
65 /// @param transform binary function object applied to each pair of elements
66 /// of the input ranges
67 ///
68 /// @return The generalized sum of @c init and @c values over @c reduce, where
69 /// @c values are the values transformed by @c transform, each value is
70 /// transformed from a pair of elements from the two input ranges.
71 ///
72 /// @pre <c> [first2, first2 + distance(first1, last1)) </c> is a valid range
73 /// @pre @c reduce does not invalidate subranges, nor modify elements in the
74 /// ranges <c> [first1, last1) </c> and
75 /// <c> [first2, first2 + distance(first1, last1))) </c>
76 /// @pre @c transform does not invalidate subranges, nor modify elements in the
77 /// ranges <c> [first1, last1) </c> and
78 /// <c> [first2, first2 + distance(first1, last1)) </c>
79 ///
80 /// @note Constraints <br>
81 /// * @c InputIt1 must satisfy the input iterator requirements
82 /// * @c InputIt2 must satisfy the input iterator requirements
83 /// * @c Value is move constructible
84 /// * <c> reduce(init, init) </c> is convertible to @c Value
85 /// * <c> reduce(init, transform(*first1, *first2)) </c>
86 /// is convertible to @c Value
87 /// * <c> reduce(transform(*first1, *first2), init) </c>
88 /// is convertible to @c Value
89 /// * <c> reduce(transform(*first1, *first2), transform(*first1, *first2)) </c>
90 /// is convertible to @c Value
91 ///
92 /// @note Complexity <br>
93 /// O(N) applications of @c reduce and @c transform, where
94 /// <c> N = distance(first1, last1) </c>.
95 ///
96 /// @note Unlike @c inner_product, the order in which elements are transformed
97 /// and reduced is unspecified.
98 ///
99 /// @note The result is non-deterministic if @c reduce is not associative or
100 /// not commutative (e.g. floating-point addition).
101 ///
102 template <
103 class InputIt1,
104 class InputIt2,
105 class Value,
106 class Reduce,
107 class Transform,
111 std::enable_if_t<std::is_move_constructible<Value>::value>,
113 Value,
114 Reduce&,
115 Value&,
116 Value&>>,
118 Value,
119 Reduce&,
120 Value&,
121 iter_result_t<Transform&, InputIt1, InputIt2>>>,
123 Value,
124 Reduce&,
125 iter_result_t<Transform&, InputIt1, InputIt2>,
126 Value&>>,
128 Value,
129 Reduce&,
130 iter_result_t<Transform&, InputIt1, InputIt2>,
131 iter_result_t<Transform&, InputIt1, InputIt2>>>> = nullptr>
132 constexpr auto operator()(
133 InputIt1 first1,
134 InputIt1 last1,
135 InputIt2 first2,
136 Value init,
137 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "It does not matter if 'reduce' hides an identifier at global or namespace scope"
138 Reduce reduce,
139 // parasoft-end-suppress AUTOSAR-A2_10_1-a
140 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "It does not matter if 'transform' hides an identifier at global or namespace scope"
141 Transform transform
142 // parasoft-end-suppress AUTOSAR-A2_10_1-a
143 ) const noexcept( //
144 std::is_nothrow_constructible<
145 typename reduce_policy_movable_box<Value>::value_type,
146 Value&&
147 >::value &&
150 generalized_transform_reduce,
151 reduce_policy_movable_box<Value>,
152 InputIt1&,
153 InputIt1&,
154 InputIt2&,
155 typename reduce_policy_movable_box<Value>::value_type,
156 Reduce&,
157 Transform&>
158 )
159 -> Value
160 {
161 using value_policy = reduce_policy_movable_box<Value>;
162
163 return value_policy::access( //
164 generalized_transform_reduce{}( //
165 value_policy{},
166 first1,
167 last1,
168 first2,
169 typename value_policy::value_type{std::move(init)},
170 reduce,
171 transform
172 )
173 );
174 }
175
176 /// @brief applies an invocable, then reduces out of order
177 /// @tparam InputIt1 type of the iterator for the first range
178 /// @tparam InputIt2 type of the iterator for the second range
179 /// @tparam Value type of the reduced value
180 /// @param first1 iterator to the beginning of the first range
181 /// @param last1 iterator to one past the end of the first range
182 /// @param first2 iterator to the beginning of the second range
183 /// @param init initial value to start with
184 ///
185 /// @return The generalized sum of @c init and @c values over @c operator+, where
186 /// @c values are the values transformed by @c operator*, each value is
187 /// transformed from a pair of elements from the two input ranges.
188 ///
189 /// @pre <c> [first2, first2 + distance(first1, last1)) </c> is a valid range
190 /// @pre binary @c operator+ does not invalidate subranges, nor modify
191 /// elements in the ranges <c> [first1, last1) </c> and
192 /// <c> [first2, first2 + distance(first1, last1))) </c>
193 /// @pre binary @c operator* does not invalidate subranges, nor modify
194 /// elements in the ranges <c> [first1, last1) </c> and
195 /// <c> [first2, first2 + distance(first1, last1)) </c>
196 ///
197 /// @note Constraints <br>
198 /// * @c InputIt1 must satisfy the input iterator requirements
199 /// * @c InputIt2 must satisfy the input iterator requirements
200 /// * @c Value is move constructible
201 /// * <c> init + init </c> is convertible to @c Value
202 /// * <c> init + (*first1 * *first2) </c>
203 /// is convertible to @c Value
204 /// * <c> (*first1 * *first2) + init </c>
205 /// is convertible to @c Value
206 /// * <c> (*first1 * *first2) + (*first1 * *first2)) </c>
207 /// is convertible to @c Value
208 ///
209 /// @note Complexity <br>
210 /// O(N) applications of @c operator+ and @c operator*, where
211 /// <c> N = distance(first1, last1) </c>.
212 ///
213 /// @note Unlike @c inner_product, the order in which elements are transformed
214 /// and reduced is unspecified.
215 ///
216 /// @note The result is non-deterministic if @c operator+ is not associative or
217 /// not commutative (e.g. floating-point addition).
218 ///
219 template <
220 class InputIt1,
221 class InputIt2,
222 class Value,
226 std::enable_if_t<std::is_move_constructible<Value>::value>,
228 Value,
229 std::plus<>&,
230 Value&,
231 Value&>>,
233 Value,
234 std::plus<>&,
235 Value&,
236 iter_result_t<std::multiplies<>&, InputIt1, InputIt2>>>,
238 Value,
239 std::plus<>&,
240 iter_result_t<std::multiplies<>&, InputIt1, InputIt2>,
241 Value&>>,
243 Value,
244 std::plus<>&,
245 iter_result_t<std::multiplies<>&, InputIt1, InputIt2>,
246 iter_result_t<std::multiplies<>&, InputIt1, InputIt2>>>> = nullptr>
247 constexpr auto operator()(
248 InputIt1 first1,
249 InputIt1 last1,
250 InputIt2 first2,
251 Value init
252 ) const noexcept( //
254 transform_reduce_fn,
255 InputIt1&&,
256 InputIt1&&,
257 InputIt2&&,
258 Value&&,
259 std::plus<>&&,
260 std::multiplies<>&&>
261 )
262 -> Value
263 {
264 return (*this)(
265 std::move(first1),
266 std::move(last1),
267 std::move(first2),
268 std::move(init),
269 std::plus<>{},
270 std::multiplies<>{}
271 );
272 }
273
274 /// @brief applies an invocable, then reduces out of order
275 /// @tparam InputIt type of the iterator for the range
276 /// @tparam Value type of the reduced value
277 /// @tparam Reduce type of the reduction operation
278 /// @tparam Transform type of the transform operation
279 /// @param first iterator to the beginning of the range
280 /// @param last iterator to one past the end of the range
281 /// @param init initial value to start with
282 /// @param reduce binary function object used to reduce transformed elements
283 /// to a single value
284 /// @param transform unary function object applied to each element of the
285 /// input range
286 ///
287 /// @return The generalized sum of @c init and @c values over @c reduce, where
288 /// @c values are the values of the input range transformed by @c transform.
289 ///
290 /// @pre @c transform does not invalidate subranges, nor modify
291 /// elements in the ranges <c> [first, last) </c>
292 /// @pre binary @c reduce does not invalidate subranges, nor modify
293 /// elements in the ranges <c> [first, last) </c>
294 ///
295 /// @note Constraints <br>
296 /// * @c InputIt must satisfy the input iterator requirements
297 /// * @c Value is move constructible
298 /// * <c> reduce(init, init) </c> is convertible to @c Value
299 /// * <c> reduce(init, transform(*first)) </c>
300 /// is convertible to @c Value
301 /// * <c> reduce(transform(*first), init) </c>
302 /// is convertible to @c Value
303 /// * <c> reduce(transform(*first), transform(*first)) </c>
304 /// is convertible to @c Value
305 ///
306 /// @note Complexity <br>
307 /// O(N) applications of @c reduce and <c>transform</c>, where
308 /// <c> N = distance(first, last) </c>.
309 ///
310 /// @note The order in which elements are transformed and reduced is
311 /// unspecified.
312 ///
313 /// @note The result is non-deterministic if @c reduce is not associative or
314 /// not commutative (e.g. floating-point addition).
315 ///
316 template <
317 class InputIt,
318 class Value,
319 class Reduce,
320 class Transform,
323 std::enable_if_t<std::is_move_constructible<Value>::value>,
325 Value,
326 Reduce&,
327 Value&,
328 Value&>>,
330 Value,
331 Reduce&,
332 Value&,
333 iter_result_t<Transform&, InputIt>>>,
335 Value,
336 Reduce&,
337 iter_result_t<Transform&, InputIt>,
338 Value&>>,
340 Value,
341 Reduce&,
342 iter_result_t<Transform&, InputIt>,
343 iter_result_t<Transform&, InputIt>>>> = nullptr>
344 constexpr auto operator()(
345 InputIt first,
346 InputIt last,
347 Value init,
348 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: other
349 // 'reduce' identifiers are availble with fully qualified namespace"
350 Reduce reduce,
351 // parasoft-end-suppress AUTOSAR-A2_10_1-a
352 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: other
353 // 'transform' identifiers are availble with fully qualified namespace"
354 Transform transform
355 // parasoft-end-suppress AUTOSAR-A2_10_1-a
356 ) const noexcept( //
358 noexcept(std::declval<InputIt&>() != std::declval<InputIt>()) && //
359 noexcept(reduce_policy_movable_box<Value>::assign( //
360 std::declval<typename reduce_policy_movable_box<Value>::value_type&>(), //
362 reduce_policy_movable_box<Value>::access( //
363 std::declval<typename reduce_policy_movable_box<Value>::value_type&>() //
364 ), //
366 ) //
367 )) && //
368 noexcept(++std::declval<InputIt&>()) //
369 ) //
370 -> Value //
371 {
372 using reduce_policy = reduce_policy_movable_box<Value>;
373
374 auto acc = typename reduce_policy::value_type{std::move(init)};
375
376 for (; first != last; ++first) {
377 reduce_policy::assign( //
378 acc,
379 reduce(reduce_policy::access(acc), transform(*first))
380 );
381 }
382
383 return reduce_policy::access(acc);
384 }
385};
386} // namespace transform_reduce_detail
387} // namespace algorithm_detail
388
389/// @brief applies an invocable, then reduces out of order
390// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
391// object used in multiple TUs."
392// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
393// object used in multiple TUs."
394// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
396 algorithm_detail::synthesize_range1_overload_t<algorithm_detail::transform_reduce_detail::transform_reduce_fn>,
397 transform_reduce
398);
399// parasoft-end-suppress CERT_CPP-DCL56-a
400// parasoft-end-suppress AUTOSAR-M7_3_3-a
401// parasoft-end-suppress CERT_CPP-DCL59-a
402
403} // namespace base
404} // namespace arene
405
406#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_TRANSFORM_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.
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
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
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...