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