Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
transposed.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_TRANSPOSED_HPP_
5#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_TRANSPOSED_HPP_
6
7// parasoft-begin-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
11#include "arene/base/mdspan/detail/reverse_extents.hpp"
14// parasoft-end-suppress AUTOSAR-A16_2_2-a
15
16// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
17// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
18
19namespace arene {
20namespace base {
21namespace linalg {
22
23namespace transposed_detail {
24
25/// @brief The transposed extents type for an input extents type @c InputExtents
26/// @tparam InputExtents The input mdspan's extents type
27///
28/// Reverses the extents and, as a single point of enforcement for the @c transposed mandate, statically rejects any
29/// input whose rank is not 2. Every @c transposed_fn overload derives its result extents through this alias, so the
30/// mandate is checked exactly once regardless of which layout overload is selected.
31/// @{
32template <class InputExtents>
33class transpose_extents {
34 // parasoft-begin-suppress CERT_C-PRE31-c-3 "False positive: static_assert is a compile-time assert and can have no
35 // side-effects"
36 static_assert(InputExtents::rank() == 2U, "transposed requires a rank-2 mdspan.");
37 // parasoft-end-suppress CERT_C-PRE31-c-3
38
39 public:
40 /// @brief The reversed extents type
41 using type = mdspan_detail::reverse_extents_t<InputExtents>;
42};
43
44/// @brief The transposed extents type for an input extents type @c InputExtents
45/// @tparam InputExtents The input mdspan's extents type
46template <class InputExtents>
47using transpose_extents_t = typename transpose_extents<InputExtents>::type;
48/// @}
49
50/// @brief Class to implement @c transposed
51class transposed_fn {
52 public:
53 /// @brief Return a transposed view of a @c layout_left mdspan
54 /// @tparam ElementType The input mdspan's element type
55 /// @tparam InputExtents The input mdspan's extents type. @c InputExtents::rank()==2 must be @c true else malformed.
56 /// @tparam AccessorPolicy The input mdspan's accessor policy
57 /// @param matrix The rank-2 mdspan to transpose
58 /// @return An mdspan over the same data with swapped extents and a @c layout_right mapping
59 /// @note The input matrix's data are neither copied nor modified.
60 template <class ElementType, class InputExtents, class AccessorPolicy>
61 constexpr auto operator()(mdspan<ElementType, InputExtents, layout_left, AccessorPolicy> matrix) const noexcept
62 -> mdspan<ElementType, transpose_extents_t<InputExtents>, layout_right, AccessorPolicy> {
63 using transposed_extents_type = transpose_extents_t<InputExtents>;
64 using return_type = mdspan<ElementType, transposed_extents_type, layout_right, AccessorPolicy>;
65 return return_type{
66 matrix.data_handle(),
67 typename return_type::mapping_type{transposed_extents_type{matrix.extent(1U), matrix.extent(0U)}},
68 matrix.accessor()
69 };
70 }
71
72 /// @brief Return a transposed view of a @c layout_right mdspan
73 /// @tparam ElementType The input mdspan's element type
74 /// @tparam InputExtents The input mdspan's extents type. @c InputExtents::rank()==2 must be @c true else malformed.
75 /// @tparam AccessorPolicy The input mdspan's accessor policy
76 /// @param matrix The rank-2 mdspan to transpose
77 /// @return An mdspan over the same data with swapped extents and a @c layout_left mapping
78 /// @note The input matrix's data are neither copied nor modified.
79 template <class ElementType, class InputExtents, class AccessorPolicy>
80 constexpr auto operator()(mdspan<ElementType, InputExtents, layout_right, AccessorPolicy> matrix) const noexcept
81 -> mdspan<ElementType, transpose_extents_t<InputExtents>, layout_left, AccessorPolicy> {
82 using transposed_extents_type = transpose_extents_t<InputExtents>;
83 using return_type = mdspan<ElementType, transposed_extents_type, layout_left, AccessorPolicy>;
84 return return_type{
85 matrix.data_handle(),
86 typename return_type::mapping_type{transposed_extents_type{matrix.extent(1U), matrix.extent(0U)}},
87 matrix.accessor()
88 };
89 }
90
91 /// @brief Return a transposed view of a @c layout_stride mdspan, swapping its two strides
92 /// @tparam ElementType The input mdspan's element type
93 /// @tparam InputExtents The input mdspan's extents type. @c InputExtents::rank()==2 must be @c true else malformed.
94 /// @tparam AccessorPolicy The input mdspan's accessor policy
95 /// @param matrix The rank-2 mdspan to transpose
96 /// @return An mdspan over the same data with swapped extents and swapped strides
97 /// @note The input matrix's data are neither copied nor modified.
98 template <class ElementType, class InputExtents, class AccessorPolicy>
99 constexpr auto operator()(mdspan<ElementType, InputExtents, layout_stride, AccessorPolicy> matrix) const noexcept
100 -> mdspan<ElementType, transpose_extents_t<InputExtents>, layout_stride, AccessorPolicy> {
101 using transposed_extents_type = transpose_extents_t<InputExtents>;
102 using return_type = mdspan<ElementType, transposed_extents_type, layout_stride, AccessorPolicy>;
103 return return_type{
104 matrix.data_handle(),
105 typename return_type::mapping_type{
106 transposed_extents_type{matrix.extent(1U), matrix.extent(0U)},
107 array<typename InputExtents::index_type, 2U>{matrix.mapping().stride(1U), matrix.mapping().stride(0U)}
108 },
109 matrix.accessor()
110 };
111 }
112
113 /// @brief Return a transposed view of a @c layout_transpose mdspan by unwrapping it to its nested layout
114 /// @tparam ElementType The input mdspan's element type
115 /// @tparam InputExtents The input mdspan's extents type. @c InputExtents::rank()==2 must be @c true else malformed.
116 /// @tparam NestedLayout The nested (un-transposed) layout policy
117 /// @tparam AccessorPolicy The input mdspan's accessor policy
118 /// @param matrix The rank-2 mdspan to transpose
119 /// @return An mdspan over the same data using the nested (un-transposed) mapping
120 /// @note The input matrix's data are neither copied nor modified.
121 template <class ElementType, class InputExtents, class NestedLayout, class AccessorPolicy>
122 constexpr auto operator()(mdspan<ElementType, InputExtents, layout_transpose<NestedLayout>, AccessorPolicy> matrix
123 ) const noexcept -> mdspan<ElementType, transpose_extents_t<InputExtents>, NestedLayout, AccessorPolicy> {
124 using transposed_extents_type = transpose_extents_t<InputExtents>;
125 using return_type = mdspan<ElementType, transposed_extents_type, NestedLayout, AccessorPolicy>;
126 return return_type{matrix.data_handle(), matrix.mapping().nested_mapping(), matrix.accessor()};
127 }
128
129 /// @brief Return a transposed view of any other layout by wrapping it in a @c layout_transpose mapping
130 /// @tparam ElementType The input mdspan's element type
131 /// @tparam InputExtents The input mdspan's extents type. @c InputExtents::rank()==2 must be @c true else malformed.
132 /// @tparam InputLayout The input mdspan's layout policy
133 /// @tparam AccessorPolicy The input mdspan's accessor policy
134 /// @param matrix The rank-2 mdspan to transpose
135 /// @return An mdspan over the same data with a @c layout_transpose mapping around the input mapping
136 /// @note The input matrix's data are neither copied nor modified.
137 template <class ElementType, class InputExtents, class InputLayout, class AccessorPolicy>
138 constexpr auto operator()(mdspan<ElementType, InputExtents, InputLayout, AccessorPolicy> matrix) const noexcept
139 -> mdspan<ElementType, transpose_extents_t<InputExtents>, layout_transpose<InputLayout>, AccessorPolicy> {
140 using transposed_extents_type = transpose_extents_t<InputExtents>;
141 using return_type = mdspan<ElementType, transposed_extents_type, layout_transpose<InputLayout>, AccessorPolicy>;
142 return return_type{matrix.data_handle(), typename return_type::mapping_type{matrix.mapping()}, matrix.accessor()};
143 }
144};
145
146} // namespace transposed_detail
147
148// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object"
149// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object"
150/// @brief Return a transposed view of a rank-2 mdspan that aliases the input data in place
151///
152/// Selects an overload of @c transposed_detail::transposed_fn::operator() by the input layout: a @c layout_left input
153/// yields a @c layout_right view (and vice versa), a @c layout_stride input yields a @c layout_stride view with its two
154/// strides swapped, a @c layout_transpose input is unwrapped to its nested layout, and any other layout is wrapped in
155/// @c layout_transpose.
156/// @note Mandates: the input mdspan has rank 2; the input's data are neither copied nor modified.
157ARENE_CPP14_INLINE_VARIABLE(transposed_detail::transposed_fn, transposed);
158// parasoft-end-suppress CERT_CPP-DCL59-a
159// parasoft-end-suppress AUTOSAR-M7_3_3-a
160
161} // namespace linalg
162} // namespace base
163} // namespace arene
164
165// parasoft-end-suppress AUTOSAR-A7_1_5-a
166// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
167
168#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_TRANSPOSED_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition add.hpp:20
Definition accumulate.hpp:37
Definition accumulate.hpp:36