Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
transform.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"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_TRANSFORM_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_TRANSFORM_HPP_
8
9// IWYU pragma: private, include "arene/base/algorithm.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
15#include "arene/base/stdlib_choice/cstddef.hpp"
16#include "arene/base/stdlib_choice/decay.hpp"
17#include "arene/base/stdlib_choice/declval.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/forward.hpp"
20#include "arene/base/stdlib_choice/integer_sequence.hpp"
21#include "arene/base/stdlib_choice/make_tuple.hpp"
22#include "arene/base/stdlib_choice/move.hpp"
23#include "arene/base/stdlib_choice/pair.hpp"
24#include "arene/base/stdlib_choice/remove_cv.hpp"
25#include "arene/base/stdlib_choice/remove_reference.hpp"
26#include "arene/base/stdlib_choice/tuple.hpp"
27#include "arene/base/stdlib_choice/tuple_size.hpp"
28#include "arene/base/tuple/detail/get.hpp"
34// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
35
36namespace arene {
37namespace base {
38
39namespace transform_detail {
40
41/// @brief Transform each element of a tuple-like object with the specified
42/// function. The tuple-like object should support std::get and
43/// std::tuple_size.
44/// @tparam F The transformation function type.
45/// @tparam TupleType The tuple type.
46/// @tparam Is The index sequence.
47/// @param func The transformation function.
48/// @param tuple The tuple.
49/// @return A tuple with elements defined by @c f(std::get<I>(tuple)) for each I.
50template <class F, typename TupleType, std::size_t... Is>
51constexpr auto transform_tuple_impl(F&& func, TupleType&& tuple, std::index_sequence<Is...>) -> decltype(auto) {
53}
54
55/// @brief Transform each element of an array with the specified function.
56/// @tparam F The transformation function type.
57/// @tparam ArrayType The array type.
58/// @tparam N The array size.
59/// @tparam Is The index sequence.
60/// @param func The transformation function.
61/// @param array The array.
62/// @return An array with elements defined by @c f(tuple_detail::get<I>(array)) for each I.
63template <template <typename U, std::size_t N> class ToType, typename F, typename FromArrayType, std::size_t... Is>
64constexpr auto transform_array_impl(F&& func, FromArrayType&& array, std::index_sequence<Is...>)
65 -> ToType<std::decay_t<decltype(func(*array.begin()))>, sizeof...(Is)> {
66 return ToType<std::decay_t<decltype(func(*array.begin()))>, sizeof...(Is)>{
67 func(tuple_detail::get<Is>(std::forward<FromArrayType>(array)))...
68 };
69}
70
71///
72/// @brief Helper for implementing transform as a Niebloid
73///
74class transform_fn {
75 public:
76 ///
77 /// @brief Backport of C++20's unary op constexpr @c std::transform .
78 ///
79 /// @tparam StartItr type of the iterator for the sequence to transform. Must model @c InputIterator .
80 /// @tparam EndItr type of the iterator to compare to for the sequence to transform. Must satisfy
81 /// @c is_equality_comparable<StartItr,EndItr> .
82 /// @tparam OutputItr type of iterator for the output sequence. Must model @c OutputIterator .
83 /// @tparam UnaryOperation type of the function to invoke to transform elements.
84 /// @param start Iterator to the start of the sequence to transform.
85 /// @param end Iterator to the end of the sequence to transform.
86 /// @param out Iterator to the start of the sequence to output the transformed values to.
87 /// @param transform_op Callable that will be invoked on each element in the range @c [start,end) exactly once.
88 /// @return OutputItr iterator in the output sequence pointing to the position after the last element transformed.
89 ///
90 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: identifier 'begin' does not hide anything"
91 template <
92 typename StartItr,
93 typename EndItr,
94 typename OutputItr,
95 typename UnaryOperation,
100 constexpr auto operator()(StartItr start, EndItr end, OutputItr out, UnaryOperation transform_op) const noexcept( //
102 noexcept(*std::declval<OutputItr&>()++ = transform_op(*std::declval<StartItr&>()++))
103 ) -> OutputItr {
104 while (start != end) {
105 // parasoft-begin-suppress AUTOSAR-M5_2_10-a "idiomatic iterator operations permitted by M5-2-10 Permit #1"
106 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "These are iterator types, so incrementing is ok"
107 *out++ = transform_op(*start++);
108 // parasoft-end-suppress AUTOSAR-M5_0_15-a
109 // parasoft-end-suppress AUTOSAR-M5_2_10-a
110 }
111 return out;
112 }
113 // parasoft-end-suppress AUTOSAR-A2_10_1-a
114
115 ///
116 /// @brief Backport of C++20's binary op constexpr @c std::transform.
117 ///
118 /// @tparam LhsStartItr type of the iterator for the left hand side sequence to transform. Must model
119 /// @c InputIterator.
120 /// @tparam LhsEndItr type of the iterator to compare to for the left hand side sequence to transform. Must satisfy
121 /// @c is_equality_comparable<StartItr,EndItr>.
122 /// @tparam RhsIter type of the iterator to the right hand side sequence to transform. Must model @c InputIterator.
123 /// @tparam OutputItr type of iterator for the output sequence. Must model @c OutputIterator.
124 /// @tparam BinaryOperation type of the function to invoke to transform elements.
125 /// @param lhs_start Iterator to the start of the left hand side sequence to transform.
126 /// @param lhs_end Iterator to the end of the left hand side sequence to transform.
127 /// @param rhs_start Iterator to the start of the right hand side sequence.
128 /// @param out Iterator to the start of the sequence to output the transformed values to.
129 /// @param transform_op Callable that will be invoked exactly once on each element in the range @c [lhs_start,lhs_end)
130 /// and the range starting at @c rhs_start of length @c distance(lhs_start,lhs_end).
131 /// @return OutputItr iterator in the output sequence pointing to the position after the last element transformed.
132 ///
133 template <
134 typename LhsStartItr,
135 typename LhsEndItr,
136 typename RhsItr,
137 typename OutputItr,
138 typename BinaryOperation,
144 constexpr auto operator()(
145 LhsStartItr lhs_start,
146 LhsEndItr lhs_end,
147 RhsItr rhs_start,
148 OutputItr out,
149 BinaryOperation transform_op
150 ) const noexcept( //
152 noexcept(
154 )
155 ) -> OutputItr {
156 while (lhs_start != lhs_end) {
157 // parasoft-begin-suppress AUTOSAR-M5_2_10-a "idiomatic iterator operations permitted by M5-2-10 Permit #1"
158 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "These are iterator types, so incrementing is ok"
159 *out++ = transform_op(*lhs_start++, *rhs_start++);
160 // parasoft-end-suppress AUTOSAR-M5_0_15-a
161 // parasoft-end-suppress AUTOSAR-M5_2_10-a
162 }
163 return out;
164 }
165
166 /// @brief Transform each element of a std::pair with the specified function.
167 /// @tparam F The transformation function type.
168 /// @param func The transformation function.
169 /// @param pair The pair.
170 /// @return A pair with elements defined by f(pair.first, pair.second).
171 template <
172 typename F,
173 typename PairType,
175 constexpr auto operator()(F&& func, PairType&& pair) const {
176 return std::make_pair(func(forward_like<PairType>(pair.first)), func(forward_like<PairType>(pair.second)));
177 }
178
179 /// @brief Transform each element of a @c std::tuple
180 /// @tparam F The transformation function type.
181 /// @tparam TupleType The tuple-like object type.
182 /// @param func The transformation function.
183 /// @param tuple The tuple-like object.
184 /// @return A @c std::tuple with elements defined by f(std::get<I>(tuple)) for each I.
185 template <
186 class F,
187 class TupleType,
189 constexpr auto operator()(F&& func, TupleType&& tuple) const { // CODEQLFP(DCL51-CPP)
190 return transform_tuple_impl(
191 std::forward<F>(func),
193 std::make_index_sequence<std::tuple_size<remove_cvref_t<TupleType>>::value>{}
194 );
195 }
196
197 /// @brief Transform each element of a std::array with the specified function.
198 /// @tparam F The transformation function type.
199 /// @tparam T The array element type.
200 /// @tparam N The array size.
201 /// @param func The transformation function.
202 /// @param array The array.
203 /// @return An array with elements defined by f(std::get<I>(array)) for each I.
204 template <
205 class F,
206 template <typename T, std::size_t N>
207 class ArrayLikeType,
208 typename T,
209 std::size_t N,
211 constexpr auto operator()(F&& func, ArrayLikeType<T, N> const& array) const {
212 return transform_array_impl<ArrayLikeType>(std::forward<F>(func), array, std::make_index_sequence<N>{});
213 }
214 /// @brief Transform each element of a std::array with the specified function.
215 /// @tparam F The transformation function type.
216 /// @tparam T The array element type.
217 /// @tparam N The array size.
218 /// @param func The transformation function.
219 /// @param array The array.
220 /// @return An array with elements defined by f(std::get<I>(array)) for each I.
221 template <
222 class F,
223 template <typename T, std::size_t N>
224 class ArrayLikeType,
225 typename T,
226 std::size_t N,
228 constexpr auto operator()(F&& func, ArrayLikeType<T, N>&& array) const {
229 return transform_array_impl<ArrayLikeType>(std::forward<F>(func), std::move(array), std::make_index_sequence<N>{});
230 }
231};
232
233} // namespace transform_detail
234
235/// @brief Apply an operation to elements of one or more input ranges and write the results to an output range.
236// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
237// object used in multiple TUs."
238// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
239// object used in multiple TUs."
240ARENE_CPP14_INLINE_VARIABLE(transform_detail::transform_fn, transform);
241// parasoft-end-suppress AUTOSAR-M7_3_3-a
242// parasoft-end-suppress CERT_CPP-DCL59-a
243
244} // namespace base
245} // namespace arene
246
247// parasoft-end-suppress AUTOSAR-A2_8_1-a-2
248#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_TRANSFORM_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Implementation of a backport of std::forward_like.
Definition accumulate.hpp:37
constexpr auto forward_like
A backport of C++23's std::forward_like, which allows forwarding a type with the same reference and c...
constexpr bool is_nothrow_inequality_comparable_v
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
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Backport of std::remove_cvref_t.
Definition remove_cvref.hpp:35
Definition accumulate.hpp:36
constexpr auto make_tuple(Types &&... args) noexcept(noexcept(tuple< arene::base::unwrap_ref_decay_t< Types >... >(std::forward< Types >(args)...))) -> tuple< arene::base::unwrap_ref_decay_t< Types >... >
construct a tuple with types deduced from the provided arguments
Definition make_tuple.hpp:30
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
constexpr auto make_pair(T1 &&first, T2 &&second) noexcept(is_nothrow_constructible_v< pair_detail::deduced_pair_type_t< T1, T2 >, T1, T2 >) -> pair_detail::deduced_pair_type_t< T1, T2 >
Creates a std::pair object, deducing the target type from the types of arguments.
Definition pair.hpp:611
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
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,...
typename decay< Type >::type decay_t
A conversion equivalent to those performed when passing function arguments by value.
Definition decay.hpp:48
constexpr auto get(pair< T1, T2 > &values) noexcept -> tuple_element_t< I, pair< T1, T2 > > &
access an element of a pair
Definition pair.hpp:665
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24