Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
elementwise_unary_transform.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::elementwise_unary_transform"
2// Copyright 2026, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_ELEMENTWISE_UNARY_TRANSFORM_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_ELEMENTWISE_UNARY_TRANSFORM_HPP_
7
11#include "arene/base/detail/index_permutation_range.hpp"
12#include "arene/base/linalg/detail/mandates.hpp"
13#include "arene/base/mdspan/detail/to_array.hpp"
14#include "arene/base/stdlib_choice/enable_if.hpp"
15#include "arene/base/stdlib_choice/is_assignable.hpp"
17
18namespace arene {
19namespace base {
20namespace linalg {
21
22namespace elementwise_unary_transform_detail {
23/// @brief Implementation class for elementwise unary transform
24class elementwise_unary_transform_fn {
25 public:
26 /// @brief Apply a unary transform to the corresponding elements of the input span, and assign the result to the
27 /// corresponding element of the output span.
28 /// @tparam InSpan The type of the input span
29 /// @tparam OutSpan The type of the output span
30 /// @tparam Transform The type of the transform
31 /// @param in_span The input span
32 /// @param out_span The output span
33 /// @param transform_fn The transform operation to apply
34 /// @pre All of @c InSpan and @c OutSpan must be instantiations of @c mdspan with the same @c rank
35 /// @pre if @c in_span and @c out_span overlap, they must alias
36 /// @pre The specified @c Transform must be invocable with elements of the input span, and the result must be
37 /// assignable to the elements of the output span
38 /// @pre @c in_span and @c out_span must have the same extents
39 template <
40 typename InSpan,
41 typename OutSpan,
42 typename Transform,
44 linalg_detail::possibly_comparable_constraints<InSpan, OutSpan>,
46 std::enable_if_t<std::is_assignable<
47 typename OutSpan::reference,
49 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: All parameters are used"
50 constexpr void operator()(InSpan const in_span, OutSpan const out_span, Transform transform_fn) const noexcept( //
52 std::is_nothrow_assignable<typename OutSpan::reference, invoke_result_t<Transform&, typename InSpan::reference>>::
53 value
54 ) {
55 // parasoft-end-suppress AUTOSAR-A0_1_4-a
56 ARENE_PRECONDITION(in_span.extents() == out_span.extents());
57 if (in_span.empty()) {
58 return;
59 }
60 auto const extents_array = mdspan_detail::to_array(in_span.extents());
61 // parasoft-begin-suppress AUTOSAR-M0_1_3 "False positive: All local variables are used"
62 for (auto const& indices : arene::base::detail::make_index_permutation_range(extents_array)) {
63 // parasoft-end-suppress AUTOSAR-M0_1_3
64 out_span(indices) = transform_fn(in_span(indices));
65 }
66 }
67};
68} // namespace elementwise_unary_transform_detail
69
70/// @brief Apply a unary operation to each element of a multidimensional view.
71// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
72// object used in multiple TUs."
73// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
74// object used in multiple TUs."
76 elementwise_unary_transform_detail::elementwise_unary_transform_fn,
77 elementwise_unary_transform
78);
79// parasoft-end-suppress AUTOSAR-M7_3_3-a
80// parasoft-end-suppress CERT_CPP-DCL59-a
81} // namespace linalg
82} // namespace base
83} // namespace arene
84
85#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_ELEMENTWISE_UNARY_TRANSFORM_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition add.hpp:20
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 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
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