Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
elementwise_binary_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_binary_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_BINARY_TRANSFORM_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_ELEMENTWISE_BINARY_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_binary_transform_detail {
23/// @brief Implementation class for elementwise binary transform
24class elementwise_binary_transform_fn {
25 public:
26 /// @brief Apply a binary transform to the corresponding elements of the two input spans, and assign the result to the
27 /// corresponding element of the output span.
28 /// @tparam LhsInSpan The type of the first input span
29 /// @tparam RhsInSpan The type of the second input span
30 /// @tparam OutSpan The type of the output span
31 /// @tparam Transform The type of the transform
32 /// @param lhs_span The first input span
33 /// @param rhs_span The second input span
34 /// @param out_span The output span
35 /// @param transform The transform operation to apply
36 /// @pre All of @c LhsInSpan , @c RhsInSpan , and @c OutSpan must be instantiations of @c mdspan with the same @c rank
37 /// @pre if @c lhs_in_span and @c out_span overlap, they must alias
38 /// @pre if @c rhs_in_span and @c out_span overlap, they must alias
39 /// @pre The specified @c Transform must be invocable with elements of the input spans, and the result must be
40 /// assignable to the elements of the output span
41 /// @pre All of @c lhs_span , @c rhs_span , and @c out_span must have the same extents
42 template <
43 typename LhsInSpan,
44 typename RhsInSpan,
45 typename OutSpan,
46 typename Transform,
48 linalg_detail::possibly_addable_constraints<LhsInSpan, RhsInSpan, OutSpan>,
50 std::enable_if_t<std::is_assignable<
51 typename OutSpan::reference,
53 nullptr>
54 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: All parameters are used"
55 constexpr void
56 operator()(LhsInSpan const lhs_span, RhsInSpan const rhs_span, OutSpan const out_span, Transform transform) const
57 noexcept( //
59 std::is_nothrow_assignable<
60 typename OutSpan::reference,
62 ) {
63 // parasoft-end-suppress AUTOSAR-A0_1_4-a
64 ARENE_PRECONDITION(lhs_span.extents() == rhs_span.extents());
65 ARENE_PRECONDITION(lhs_span.extents() == out_span.extents());
66 if (lhs_span.empty()) {
67 return;
68 }
69 auto const extents_array = mdspan_detail::to_array(lhs_span.extents());
70 // parasoft-begin-suppress AUTOSAR-M0_1_3 "False positive: All local variables are used"
71 for (auto const& indices : arene::base::detail::make_index_permutation_range(extents_array)) {
72 // parasoft-end-suppress AUTOSAR-M0_1_3
73 out_span(indices) = transform(lhs_span(indices), rhs_span(indices));
74 }
75 }
76};
77} // namespace elementwise_binary_transform_detail
78
79/// @brief Apply a binary operation to corresponding elements of two multidimensional views.
80// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
81// object used in multiple TUs."
82// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
83// object used in multiple TUs."
85 elementwise_binary_transform_detail::elementwise_binary_transform_fn,
86 elementwise_binary_transform
87);
88// parasoft-end-suppress AUTOSAR-M7_3_3-a
89// parasoft-end-suppress CERT_CPP-DCL59-a
90} // namespace linalg
91} // namespace base
92} // namespace arene
93
94#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_ELEMENTWISE_BINARY_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