Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
layout_right.hpp
Go to the documentation of this file.
1// parasoft-suppress CERT_C-EXP37-a AUTOSAR-A2_8_1-a "False positive: there is no function named '()' here. Header
2// defines layout_right types"
3
4// Copyright 2026, Toyota Motor Corporation
5//
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7
8#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_LAYOUT_RIGHT_HPP_
9#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_LAYOUT_RIGHT_HPP_
10
11// IWYU pragma: no_include "arene/base/mdspan/layout.hpp"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/mdspan/detail/all_valid_submdspan_slice_types_for.hpp"
17#include "arene/base/mdspan/detail/is_submdspan_layout_preserving.hpp"
18#include "arene/base/mdspan/detail/layout_common.hpp" // IWYU pragma: keep
19#include "arene/base/mdspan/detail/strided_mapping_base.hpp" // IWYU pragma: keep
20#include "arene/base/mdspan/detail/submdspan_mapping_impl.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/is_constructible.hpp"
24#include "arene/base/stdlib_choice/is_convertible.hpp"
25#include "arene/base/stdlib_choice/move.hpp"
26// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
27
28// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
29// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
30
31namespace arene {
32namespace base {
33
34// parasoft-begin-suppress AUTOSAR-A2_10_1 "Base class is inherited privately so its identifiers are not visible"
35// parasoft-begin-suppress AUTOSAR-A7_3_1-a "False positive: there is no rule A7-3-1"
36// parasoft-begin-suppress AUTOSAR-A10_2_1-b "False positive: this is not a multiple inheritance hierarchy"
37// parasoft-begin-suppress AUTOSAR-A10_2_1-a "False positive: this is not a multiple inheritance hierarchy"
38/// @brief A mapping from a logical pack of indices into a single flat physical output index
39/// @tparam The extents of the space used for this mapping; must be a specialization of @c extents
40template <class Extents>
41class layout_right::mapping : private layout_detail::strided_mapping_base<Extents> {
42 private:
43 /// @brief Convenience typedef referring to the base strided mapping
44 using base_type = layout_detail::strided_mapping_base<Extents>;
45
46 public:
47 using typename base_type::extents_type;
48 using typename base_type::index_type;
49 using typename base_type::rank_type;
50 using typename base_type::size_type;
51 /// @brief The tag type of the layout used by this @c mapping
52 using layout_type = layout_right;
53
54 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: this is a delegating constructor"
55 /// @brief Construct a right-strided mapping for a default-constructed set of extents
56 constexpr mapping() noexcept
57 : mapping(extents_type{}) {}
58 // parasoft-end-suppress AUTOSAR-A12_1_1-a
59
60 /// @brief Construct a right-strided mapping for the given extents
61 /// @param exts The extents to use
62 /// @pre The size of the index space represented by @c exts fits within <c>index_type</c>, else @c ARENE_PRECONDITION
63 /// violation
64 // NOLINTNEXTLINE(hicpp-explicit-conversions) C++23 defines this as implicit
65 constexpr mapping(extents_type const& exts) noexcept
66 : base_type(exts, layout_detail::right_strides(exts)) {}
67
68 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: this is a delegating constructor"
69 /// @brief Converting constructor from a mapping with a different (but compatible) @c extents_type
70 /// @note This is implicit if and only if @c OtherExtents is implicitly convertible to @c extents_type
71 /// @tparam OtherExtents Type of <c>other</c>'s @c extents
72 /// @param other The other @c layout_right::mapping
73 /// @pre @c other.required_span_size() can be represented as a value of *this* type's <c>index_type</c>, else
74 /// @c ARENE_PRECONDITION violation
75 template <
76 typename OtherExtents,
78 // NOLINTNEXTLINE(hicpp-explicit-conversions) C++23 defines this as implicit
79 constexpr mapping(mapping<OtherExtents> const& other) noexcept
80 : mapping(layout_detail::extents_cast<extents_type>(other.extents())) {}
81 // parasoft-end-suppress AUTOSAR-A12_1_1-a
82
83 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: this is a delegating constructor"
84 /// @brief Converting constructor from a mapping with a different (but compatible) @c extents_type
85 /// @note This is implicit if and only if @c OtherExtents is implicitly convertible to @c extents_type
86 /// @tparam OtherExtents Type of <c>other</c>'s @c extents
87 /// @param other The other @c layout_right::mapping
88 /// @pre @c other.required_span_size() can be represented as a value of *this* type's <c>index_type</c>, else
89 /// @c ARENE_PRECONDITION violation
90 template <
91 typename OtherExtents,
95 constexpr explicit mapping(mapping<OtherExtents> const& other) noexcept
96 : mapping(layout_detail::extents_cast<extents_type>(other.extents())) {}
97 // parasoft-end-suppress AUTOSAR-A12_1_1-a
98
99 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: this is a delegating constructor"
100 /// @brief Converting constructor from a @c layout_left::mapping when both have 0- or 1-dimensional extents
101 /// @note This is implicit if and only if @c OtherExtents is implicitly convertible to @c extents_type
102 /// @tparam OtherExtents Type of <c>other</c>'s @c extents
103 /// @param other The @c layout_left::mapping
104 /// @pre @c other.required_span_size() can be represented as a value of *this* type's <c>index_type</c>, else
105 /// @c ARENE_PRECONDITION violation
106 template <
107 typename OtherExtents,
110 std::enable_if_t<OtherExtents::rank() <= rank_type{1U}>> = nullptr>
111 // NOLINTNEXTLINE(hicpp-explicit-conversions) C++23 defines this as implicit
112 constexpr mapping(layout_left::mapping<OtherExtents> const& other) noexcept
113 : mapping(layout_detail::extents_cast<extents_type>(other.extents())) {}
114 // parasoft-end-suppress AUTOSAR-A12_1_1-a
115
116 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: this is a delegating constructor"
117 /// @brief Converting constructor from a @c layout_left::mapping when both have 0- or 1-dimensional extents
118 /// @note This is implicit if and only if @c OtherExtents is implicitly convertible to @c extents_type
119 /// @tparam OtherExtents Type of <c>other</c>'s @c extents
120 /// @param other The @c layout_left::mapping
121 /// @pre @c other.required_span_size() can be represented as a value of *this* type's <c>index_type</c>, else
122 /// @c ARENE_PRECONDITION violation
123 template <
124 typename OtherExtents,
128 std::enable_if_t<OtherExtents::rank() <= rank_type{1U}>> = nullptr>
129 constexpr explicit mapping(layout_left::mapping<OtherExtents> const& other) noexcept
130 : mapping(layout_detail::extents_cast<extents_type>(other.extents())) {}
131 // parasoft-end-suppress AUTOSAR-A12_1_1-a
132
133 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: this is a delegating constructor"
134 /// @brief Converting constructor from a @c layout_stride::mapping with a compatible @c extents_type
135 /// @note This is implicit if and only if <c>OtherExtents::rank() == 0</c>
136 /// @tparam OtherExtents Type of <c>other</c>'s @c extents
137 /// @param other The @c layout_stride::mapping
138 /// @pre @c other.required_span_size() can be represented as a value of *this* type's <c>index_type</c>, else
139 /// @c ARENE_PRECONDITION violation
140 /// @pre <c>other</c>'s strides are exactly those of a @c layout_right::mapping with its <c>extents</c>, else
141 /// @c ARENE_PRECONDITION violation
142 template <
143 typename OtherExtents,
146 std::enable_if_t<OtherExtents::rank() == rank_type{0U}>> = nullptr>
147 // NOLINTNEXTLINE(hicpp-explicit-conversions) C++23 defines this as implicit
148 constexpr mapping(layout_stride::mapping<OtherExtents> const& other) noexcept
149 : mapping(layout_detail::extents_cast<extents_type>(other.extents())) {
150 // The second precondition need not be checked here, because rank-0 strides are empty and so always right-strided.
151 }
152 // parasoft-end-suppress AUTOSAR-A12_1_1-a
153
154 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: this is a delegating constructor"
155 /// @brief Converting constructor from a @c layout_stride::mapping with a compatible @c extents_type
156 /// @note This is implicit if and only if <c>OtherExtents::rank() == 0</c>
157 /// @tparam OtherExtents Type of <c>other</c>'s @c extents
158 /// @param other The @c layout_stride::mapping
159 /// @pre @c other.required_span_size() can be represented as a value of *this* type's <c>index_type</c>, else
160 /// @c ARENE_PRECONDITION violation
161 /// @pre <c>other</c>'s strides are exactly those of a @c layout_right::mapping with its <c>extents</c>, else
162 /// @c ARENE_PRECONDITION violation
163 template <
164 typename OtherExtents,
167 std::enable_if_t<(OtherExtents::rank() > rank_type{0U})>> = nullptr>
168 constexpr explicit mapping(layout_stride::mapping<OtherExtents> const& other) noexcept
169 : mapping(layout_detail::extents_cast<extents_type>(other.extents())) {
170 // For coverage purposes, this enforces the 2nd precondition in a separate function that doesn't depend on Extents.
171 layout_detail::precondition_strides_equal<index_type, typename OtherExtents::index_type>(
172 this->strides(),
173 other.strides()
174 );
175 }
176 // parasoft-end-suppress AUTOSAR-A12_1_1-a
177
178 using base_type::extents;
179 using base_type::required_span_size;
180 using base_type::stride;
181 using base_type::operator();
182
183 using base_type::is_always_strided;
184 using base_type::is_always_unique;
185 using base_type::is_strided;
186 using base_type::is_unique;
187
188 /// @brief Return whether or not this mapping is always exhaustive, i.e. every element is reachable using some indices
189 /// @return @c true because the mapping is always computed by a simple invertible right-strided algorithm
190 /// @note In mathematical terms this asks if the mapping is always surjective/onto.
191 static constexpr auto is_always_exhaustive() noexcept -> bool { return true; }
192
193 /// @brief Return whether or not this instance is exhaustive, i.e. every element is reachable using some indices
194 /// @return @c true if every provided stride is the product of the extents corresponding to some subset of the other
195 /// strides, otherwise @c false
196 /// @note In mathematical terms this asks if the mapping is surjective/onto.
197 static constexpr auto is_exhaustive() noexcept -> bool { return true; }
198
199 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Hidden friends permitted by A11-3-1 Permit #2 v1.0.0"
200 // parasoft-begin-suppress AUTOSAR-A13_5_5-b-2 "Mixed comparisons permitted by A13-5-5 Permit #1"
201 /// @brief Compare two right-strided mappings for equality
202 /// @tparam OtherExtents The type of the extents used by the right-hand side of the comparison
203 /// @param left An instance of @c layout_right::mapping
204 /// @param right An instance of @c layout_right::mapping<OtherExtents>
205 /// @return @c true if @c left and @c right have equal extents
206 template <
207 typename OtherExtents,
208 constraints<std::enable_if_t<extents_type::rank() == OtherExtents::rank()>> = nullptr>
209 friend constexpr auto operator==(mapping const& left, mapping<OtherExtents> const& right) noexcept -> bool {
210 return left.extents() == right.extents();
211 }
212
213 /// @brief Compare two right-strided mappings for inequality
214 /// @tparam OtherExtents The type of the extents used by the right-hand side of the comparison
215 /// @param left An instance of @c layout_right::mapping
216 /// @param right An instance of @c layout_right::mapping<OtherExtents>
217 /// @return @c true if @c left and @c right have unequal extents
218 template <
219 typename OtherExtents,
220 constraints<std::enable_if_t<extents_type::rank() == OtherExtents::rank()>> = nullptr>
221 friend constexpr auto operator!=(mapping const& left, mapping<OtherExtents> const& right) noexcept -> bool {
222 return !(left == right);
223 }
224 // parasoft-end-suppress AUTOSAR-A13_5_5-b-2
225 // parasoft-end-suppress AUTOSAR-A11_3_1-a
226
227 private:
228 /// @brief obtain a check_bypasser passkey to skip precondition checks on
229 /// mapping construction
230 /// @return check_bypasser value
231 static constexpr auto passkey() noexcept -> layout_detail::check_bypasser { return layout_detail::check_bypasser{}; }
232
233 /// @brief Access the strides of a @c mapping via the derived class rather than its private base
234 /// @param src the @c mapping to read strides from
235 /// @return @c src 's strides
236 ///
237 /// @note This exists as a workaround for a GCC bug (present on gcc8) where access checking for a
238 /// hidden friend's trailing return type incorrectly treats the mapping's private base class as
239 /// inaccessible when the friend is found via ADL through a separate SFINAE context
240 /// (e.g. @c is_invocable_v inside @c arene::base::invoke ). Routing the call through this
241 /// static member performs the access check on the derived class, which GCC handles correctly,
242 /// just like the @c passkey() workaround above.
243 static constexpr auto strides_of(mapping const& src) noexcept
244 -> ::arene::base::array<index_type, extents_type::rank()> {
245 return src.strides();
246 }
247
248 public:
249 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Hidden friends permitted by A11-3-1 Permit #2 v1.0.0"
250
251 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: parameters 'src' and 'slices' are used"
252
253 // parasoft-begin-suppress AUTOSAR-A8_4_2-a "False positive: function does have a return"
254 // parasoft-begin-suppress CERT_C-MSC37-a "False positive: function does have a return"
255 // parasoft-begin-suppress CERT_CPP-MSC52-a "False positive: function does have a return"
256 /// @brief Compute the sub-mapping for a @c layout_right::mapping
257 /// @tparam CanonicalSliceSpecifiers types of the slice specifiers, one per dimension
258 /// @param src the source mapping to slice
259 /// @param slices the slice specifiers
260 ///
261 /// Uses @c slices to compute the sub-extents, sub-strides, and offset of the submdspan. The @c slices must be in
262 /// canonical form.
263 ///
264 /// @return A @c submdspan_mapping_result containing the sub-mapping and offset such that indexing the sub-mapping and
265 /// adding the offset recovers the corresponding index in the original mapping.
266 ///
267 /// @note Constraints <br>
268 /// * <c> sizeof...(CanonicalSliceSpecifiers) == extents_type::rank() </c>
269 /// * for each rank index @c k of @c src.extents(), @c CanonicalSliceSpecifiers...[k] is a valid submdspan slice
270 /// type for the @c k-th extent of @c extents_type
271 ///
272 /// @note Preconditions <br>
273 /// * for each rank index @c k of @c src.extents(), @c slices...[k] denotes a valid submdspan slice for the @c k-th
274 /// extent of @c src.extents()
275 ///
276 /// @note Remarks <br>
277 /// * the @c layout_right specialization of @c submdspan_mapping produces a @c layout_right::mapping if:
278 /// * for each k in the range <c> [SubExtents::rank() - 1, extents_type::rank()) </c>,
279 /// @c CanonicalSliceSpecifiers...[k] denotes @c full_extent_t; and
280 /// * for k equal to <c> extents_type::rank() - SubExtents::rank() </c>, @c CanonicalSliceSpecifiers...[k] is a
281 /// unit-stride slice type;
282 /// * Otherwise, it produces a @c layout_stride::mapping.
283 template <
284 class... CanonicalSliceSpecifiers,
286 std::enable_if_t<sizeof...(CanonicalSliceSpecifiers) == extents_type::rank()>,
288 mdspan_detail::all_valid_submdspan_slice_types_for_v<extents_type, CanonicalSliceSpecifiers...>>> =
289 nullptr>
290// parasoft-begin-suppress AUTOSAR-A16_0_1-a "Doxygen needs a declaration for this parser-hostile friend definition"
291#ifdef ARENE_DOC_GENERATION_RUNNING
292 friend constexpr auto submdspan_mapping(mapping const& src, CanonicalSliceSpecifiers... slices) noexcept;
293#else
294 friend constexpr auto submdspan_mapping(mapping const& src, CanonicalSliceSpecifiers... slices) noexcept
295 -> decltype(mdspan_detail::submdspan_mapping_impl<
296 submdspan_subextents_type_t<extents_type, CanonicalSliceSpecifiers...>>(
297 mdspan_detail::is_submdspan_layout_preserving<
298 layout_right,
300 CanonicalSliceSpecifiers...>{},
301 src,
302 mapping::passkey(),
303 mapping::strides_of(src),
304 std::move(slices)...
305 )) {
306 return mdspan_detail::submdspan_mapping_impl<
307 submdspan_subextents_type_t<extents_type, CanonicalSliceSpecifiers...>>(
308 mdspan_detail::is_submdspan_layout_preserving<
309 layout_right,
311 CanonicalSliceSpecifiers...>{},
312 src,
313 mapping::passkey(),
314 src.strides(),
315 std::move(slices)...
316 );
317 }
318#endif
319 // parasoft-end-suppress AUTOSAR-A16_0_1-a
320 // parasoft-end-suppress CERT_CPP-MSC52-a
321 // parasoft-end-suppress CERT_C-MSC37-a
322 // parasoft-end-suppress AUTOSAR-A8_4_2-a
323 // parasoft-end-suppress AUTOSAR-A0_1_4-a
324
325 // parasoft-end-suppress AUTOSAR-A11_3_1-a
326};
327// parasoft-end-suppress AUTOSAR-A2_10_1
328// parasoft-end-suppress AUTOSAR-A7_3_1-a
329// parasoft-end-suppress AUTOSAR-A10_2_1-b
330// parasoft-end-suppress AUTOSAR-A10_2_1-a
331
332} // namespace base
333} // namespace arene
334
335// parasoft-end-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
336// parasoft-end-suppress AUTOSAR-A7_1_5-a-2
337
338#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_LAYOUT_RIGHT_HPP_
A fixed-size array of T objects.
Definition array.hpp:77
A mapping from a logical pack of indices into a single flat physical output index.
Definition layout_left.hpp:86
A mapping from a logical pack of indices into a single flat physical output index.
Definition layout_right.hpp:41
static constexpr auto is_always_exhaustive() noexcept -> bool
Return whether or not this mapping is always exhaustive, i.e. every element is reachable using some i...
Definition layout_right.hpp:191
friend constexpr auto operator!=(mapping const &left, mapping< OtherExtents > const &right) noexcept -> bool
Compare two right-strided mappings for inequality.
Definition layout_right.hpp:221
constexpr mapping(layout_stride::mapping< OtherExtents > const &other) noexcept
Converting constructor from a layout_stride::mapping with a compatible extents_type.
Definition layout_right.hpp:148
friend constexpr auto operator==(mapping const &left, mapping< OtherExtents > const &right) noexcept -> bool
Compare two right-strided mappings for equality.
Definition layout_right.hpp:209
static constexpr auto is_exhaustive() noexcept -> bool
Return whether or not this instance is exhaustive, i.e. every element is reachable using some indices...
Definition layout_right.hpp:197
constexpr mapping(layout_left::mapping< OtherExtents > const &other) noexcept
Converting constructor from a layout_left::mapping when both have 0- or 1-dimensional extents.
Definition layout_right.hpp:112
constexpr mapping(extents_type const &exts) noexcept
Construct a right-strided mapping for the given extents.
Definition layout_right.hpp:65
constexpr mapping() noexcept
Construct a right-strided mapping for a default-constructed set of extents.
Definition layout_right.hpp:56
constexpr mapping(mapping< OtherExtents > const &other) noexcept
Converting constructor from a mapping with a different (but compatible) extents_type.
Definition layout_right.hpp:79
layout_right layout_type
The tag type of the layout used by this mapping.
Definition layout_right.hpp:52
friend constexpr auto submdspan_mapping(mapping const &src, CanonicalSliceSpecifiers... slices) noexcept
Compute the sub-mapping for a layout_right::mapping.
A mapping from a logical pack of indices into a single flat physical output index,...
Definition layout_stride.hpp:192
Definition accumulate.hpp:37
typename submdspan_subextents_type< Extents, SliceSpecifiers... >::type submdspan_subextents_type_t
determines the subextents type from an extents type and slice specifiers
Definition submdspan_subextents_type.hpp:124
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