Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
mdspan.hpp
Go to the documentation of this file.
1// parasoft-suppress CERT_C-EXP37-a AUTOSAR-A10_2_1-a "False positive: No requirement to name parameters; redefining
2// from a privately inherited base is allowed"
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_MDSPAN_HPP_
9#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_MDSPAN_HPP_
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
15#include "arene/base/detail/dynamic_extent.hpp"
17#include "arene/base/mdspan/default_accessor.hpp" // IWYU pragma: export
18#include "arene/base/mdspan/detail/representable_cast.hpp"
19#include "arene/base/mdspan/detail/tuple_span.hpp"
25#include "arene/base/stdlib_choice/cstddef.hpp"
26#include "arene/base/stdlib_choice/declval.hpp"
27#include "arene/base/stdlib_choice/enable_if.hpp"
28#include "arene/base/stdlib_choice/integral_constant.hpp"
29#include "arene/base/stdlib_choice/is_abstract.hpp"
30#include "arene/base/stdlib_choice/is_array.hpp"
31#include "arene/base/stdlib_choice/is_constructible.hpp"
32#include "arene/base/stdlib_choice/is_convertible.hpp"
33#include "arene/base/stdlib_choice/is_default_constructible.hpp"
34#include "arene/base/stdlib_choice/is_object.hpp"
35#include "arene/base/stdlib_choice/is_same.hpp"
36#include "arene/base/stdlib_choice/move.hpp"
37#include "arene/base/stdlib_choice/remove_cv.hpp"
43// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
44
45// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
46
47namespace arene {
48namespace base {
49
50namespace mdspan_detail {
51
52/// @brief Helper variable to indicate if all the extents are dynamic
53/// @tparam Extents The extents
54template <typename Extents>
55extern constexpr bool all_extents_are_dynamic_v = Extents::rank() == Extents::rank_dynamic();
56
57/// @brief Helper variable to indicate if all the extents are static
58/// @tparam Extents The extents
59template <typename Extents>
60extern constexpr bool all_extents_are_static_v = Extents::rank_dynamic() == 0;
61
62/// @brief Validate that @c SourceExtents can be used to construct @c TargetExtents at runtime
63/// @tparam TargetExtents The extents type to construct
64/// @tparam SourceExtents The extents type to construct from
65///
66/// @pre For each rank index @c r of @c extents_type, <c> static_extent(r) == dynamic_extent || static_extent(r) ==
67/// other.extent(r) </c> is true, otherwise this is an @c ARENE_PRECONDITION violation.
68///
69/// Note: this overload is a no-op as the extent values can be verified by the converting constructor constraints. This
70/// prevents instantiating a branch that cannot actually be covered by tests.
71// parasoft-begin-suppress AUTOSAR-M0_1_8-b "This overload avoids checking when the extents are statically known to be
72// compatible"
73template <
74 class TargetExtents,
75 class SourceExtents,
77 TargetExtents::rank() == 0 || all_extents_are_dynamic_v<TargetExtents> ||
78 all_extents_are_static_v<SourceExtents>>> = nullptr>
79constexpr auto validate_extents_compatibility(SourceExtents const&) -> void {}
80// parasoft-end-suppress AUTOSAR-M0_1_8-b
81
82/// @brief Validate that @c SourceExtents can be used to construct @c TargetExtents at runtime
83/// @tparam TargetExtents The extents type to construct
84/// @tparam SourceExtents The extents type to construct from
85/// @param source The extents to construct from
86///
87/// @pre For each rank index @c r of @c extents_type, <c> static_extent(r) == dynamic_extent || static_extent(r) ==
88/// other.extent(r) </c> is true, otherwise this is an @c ARENE_PRECONDITION violation.
89///
90/// Note: this overload does not perform the dynamic extent check as all the @c TargetExtents are static. This prevents
91/// instantiating a branch that cannot actually be covered by tests.
92template <
93 class TargetExtents,
94 class SourceExtents,
96 std::enable_if_t<TargetExtents::rank() != 0>,
99constexpr auto validate_extents_compatibility(SourceExtents const& source) -> void {
100 // parasoft-begin-suppress AUTOSAR-M14_6_1-a "False positive: No unqualified size_t"
101 for (auto dim : arene::base::sequential_values<typename TargetExtents::rank_type, TargetExtents::rank()>) {
102 // parasoft-end-suppress AUTOSAR-M14_6_1-a
103 // Note that static_extent() always returns std::size_t, while extent() returns index_type.
104 ARENE_PRECONDITION(cmp_equal(TargetExtents::static_extent(dim), source.extent(dim)));
105 }
106}
107
108/// @brief Validate that @c SourceExtents can be used to construct @c TargetExtents at runtime
109/// @tparam TargetExtents The extents type to construct
110/// @tparam SourceExtents The extents type to construct from
111/// @param source The extents to construct from
112///
113/// @pre For each rank index @c r of @c extents_type, <c> static_extent(r) == dynamic_extent || static_extent(r) ==
114/// other.extent(r) </c> is true, otherwise this is an @c ARENE_PRECONDITION violation.
115template <
116 class TargetExtents,
117 class SourceExtents,
119 std::enable_if_t<TargetExtents::rank() != 0>,
123constexpr auto validate_extents_compatibility(SourceExtents const& source) -> void {
124 // parasoft-begin-suppress AUTOSAR-M14_6_1-a "False positive: No unqualified size_t"
125 for (auto dim : arene::base::sequential_values<typename TargetExtents::rank_type, TargetExtents::rank()>) {
126 // parasoft-end-suppress AUTOSAR-M14_6_1-a
127 // Note that static_extent() always returns std::size_t, while extent() returns index_type.
129 (TargetExtents::static_extent(dim) == arene::base::dynamic_extent) ||
130 cmp_equal(TargetExtents::static_extent(dim), source.extent(dim))
131 );
132 }
133}
134
135} // namespace mdspan_detail
136
137// parasoft-begin-suppress AUTOSAR-A12_1_6-a "False positive: Constructors for mdspan have different constraints"
138// parasoft-begin-suppress AUTOSAR-A10_1_1-a "Multiple inheritance is private and used to allow for
139// empty-base-optimization"
140// parasoft-begin-suppress AUTOSAR-A12_1_5-a "False positive: Delegating constructors are used where they can be"
141/// @brief A multidimensional view of elements over a contiguous sequence of elements
142/// @tparam ElementType the element type
143/// @tparam Extents an instantiation of @c extents representing the shape of the multidimensional index space
144/// @tparam LayoutPolicy a layout policy (defaults to @c layout_right)
145/// @tparam AccessorPolicy an accessor policy (defaults to @c default_accessor<ElementType>)
146///
147/// Backport of @c std::mdspan from C++23. The @c mdspan class template is a multidimensional array view that maps a
148/// multidimensional index to an element of the array. The mapping and element access policies are configurable, and the
149/// underlying array need not be contiguous or even exist in memory at all.
150template <
151 typename ElementType,
152 typename Extents,
153 typename LayoutPolicy = layout_right,
154 typename AccessorPolicy = default_accessor<ElementType>>
155// NOLINTNEXTLINE(hicpp-special-member-functions) Defines all constructors specified by the standard.
157 : private AccessorPolicy
158 , private LayoutPolicy::template mapping<Extents> {
159 static_assert(std::is_object<ElementType>::value, "ElementType must be a complete object type");
160 static_assert(!std::is_abstract<ElementType>::value, "ElementType must not be an abstract class");
161 static_assert(!std::is_array<ElementType>::value, "ElementType must not be an array");
162 static_assert(is_extents_v<Extents>, "Extents type parameter must be a specialization of arene::base::extents");
163 static_assert(is_accessor_policy_v<AccessorPolicy>, "AccessorPolicy must meet the accessor policy requirements");
164 static_assert(
166 "ElementType must match AccessorPolicy::element_type"
167 );
168 static_assert(
170 "LayoutPolicy must meet the layout mapping policy requirements for the given Extents"
171 );
172
173 public:
174 /// @brief The extents type
175 using extents_type = Extents;
176 /// @brief The layout policy type
177 using layout_type = LayoutPolicy;
178 /// @brief The accessor policy type
179 using accessor_type = AccessorPolicy;
180 /// @brief The mapping type
181 using mapping_type = typename layout_type::template mapping<extents_type>;
182 /// @brief The element type
183 using element_type = ElementType;
184 /// @brief The value type
185
186 // parasoft-begin-suppress AUTOSAR-A2_10_1 "This name is specified by the C++ standard - access to
187 // ebo_holder::value_type is not required here."
189 // parasoft-end-suppress AUTOSAR-A2_10_1
190
191 /// @brief The index type
192 using index_type = typename extents_type::index_type;
193 /// @brief The size type
194 using size_type = typename extents_type::size_type;
195 /// @brief The rank type
196 using rank_type = typename extents_type::rank_type;
197 /// @brief The data handle type
199 /// @brief The reference type
201
202 // parasoft-begin-suppress AUTOSAR-M11_0_1-a-2 "False positive: these are not 'member data', they are a public
203 // property"
204 /// @brief Get the number of dimensions
205 /// @return The rank of the mdspan
206 static constexpr std::integral_constant<rank_type, Extents::rank()> rank{};
207
208 /// @brief Get the number of dimensions with dynamic extent
209 /// @return The dynamic rank of the mdspan
210 static constexpr std::integral_constant<rank_type, Extents::rank_dynamic()> rank_dynamic{};
211 // parasoft-end-suppress AUTOSAR-M11_0_1-a-2
212
213 // parasoft-begin-suppress AUTOSAR-A10_2_1-a "False positive: Extents is inherited privately by the mapping_type, so
214 // it is not redefined here."
215 /// @brief Get the static extent for the given dimension
216 /// @param dimension The index of the dimension for which to get the static extent
217 /// @return The value from @c Extents for the specified dimension
218 static constexpr auto static_extent(rank_type dimension) noexcept -> std::size_t {
219 return extents_type::static_extent(dimension);
220 }
221
222 /// @brief Get the actual extent for the given dimension, returning the extent supplied to the constructor for dynamic
223 /// extents
224 /// @param dimension The index of the dimension for which to get the extent
225 /// @return The extent of the specified dimension
226 constexpr auto extent(rank_type dimension) const noexcept -> index_type { return this->extents().extent(dimension); }
227 // parasoft-end-suppress AUTOSAR-A10_2_1-a
228
229 /// @brief Default constructor
230 ///
231 /// @pre <c>[0, mapping().required_span_size())</c> is an accessible range of @c data_handle() and @c accessor() for
232 /// the values of @c mapping() and @c accessor() after the invocation of this constructor. This precondition is not
233 /// checked, but violating it is undefined behavior.
234 ///
235 /// @note This constructor has no visible effect and creates an mdspan with all dynamic
236 /// extents equal to zero. This implies that a default constructed span covers no elements.
237 template <
238 typename Ext = Extents,
239 typename Map = mapping_type,
240 typename Acc = AccessorPolicy,
242 std::enable_if_t<(Ext::rank_dynamic() > 0)>,
246 constexpr mdspan() noexcept
247 : accessor_type{},
248 mapping_type{},
249 ptr_{} {}
250
251 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: Delegates to constructor which does initialize the base
252 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: All parameters *are* named."
253
254 /// @brief Construct mdspan with a data handle and extents
255 /// @tparam OtherIndexTypes The types of the extents used to initialize the layout mapping
256 /// @param handle A user supplied data handle
257 /// @param extents_in User supplied extents
258 ///
259 /// Construct an @c mdspan initialized with a user supplied @c data_handle_type, a variadic list of extents which are
260 /// converted to @c extents_type and used to initialize the layout mapping, and a default constructed @c
261 /// accessor_type.
262 ///
263 /// @pre <c> [0, mapping().required_span_size()) </c> is an accessible range of @c handle and @c accessor() for the
264 /// values of @c mapping() and @c accessor() after the invocation of this constructor. This precondition is not
265 /// checked, but violating it is undefined behavior.
266 template <
267 class... OtherIndexTypes,
268 typename Ext = Extents,
269 typename Map = mapping_type,
270 typename Acc = AccessorPolicy,
272 std::enable_if_t<all_of_v<std::is_convertible<OtherIndexTypes, index_type>::value...>>,
273 std::enable_if_t<all_of_v<std::is_nothrow_constructible<index_type, OtherIndexTypes>::value...>>,
274 std::enable_if_t<sizeof...(OtherIndexTypes) == rank() || sizeof...(OtherIndexTypes) == rank_dynamic()>,
275 std::enable_if_t<std::is_constructible<Map, Ext>::value>,
276 std::enable_if_t<std::is_default_constructible<Acc>::value>> = nullptr>
277 constexpr explicit mdspan(data_handle_type handle, OtherIndexTypes... extents_in) noexcept
278 : mdspan{std::move(handle), extents_type{static_cast<index_type>(std::move(extents_in))...}} {}
279 // parasoft-end-suppress CERT_C-EXP37-a
280
281 // classes"
282 /// @brief Construct mdspan with a data handle and extents
283 /// @tparam OtherIndexType The type of the extent values used to initialize the layout mapping
284 /// @param handle A user supplied data handle
285 /// @param extents_in User supplied extents
286 ///
287 /// Construct an @c mdspan initialized with a user supplied @c data_handle_type, a span of extents which are
288 /// converted to @c extents_type and used to initialize the layout mapping, and a default constructed @c
289 /// accessor_type.
290 ///
291 /// @pre <c> [0, mapping().required_span_size()) </c> is an accessible range of @c handle and @c accessor() for the
292 /// values of @c mapping() and @c accessor() after the invocation of this constructor. This precondition is not
293 /// checked, but violating it is undefined behavior.
294 ///
295 /// @note This constructor is @c explicit only if <c> N != rank_dynamic() </c>.
296 template <
297 class OtherIndexType,
298 std::size_t Size,
299 typename Ext = Extents,
300 typename Map = mapping_type,
301 typename Acc = AccessorPolicy,
305 std::enable_if_t<Size == rank()>,
309 constexpr explicit mdspan(data_handle_type handle, span<OtherIndexType, Size> extents_in) noexcept
310 : mdspan{std::move(handle), extents_type{extents_in}} {}
311
312 /// @brief Construct mdspan with a data handle and extents
313 /// @tparam OtherIndexType The type of the extent values used to initialize the layout mapping
314 /// @param handle A user supplied data handle
315 /// @param extents_in User supplied extents
316 ///
317 /// Construct an @c mdspan initialized with a user supplied @c data_handle_type, a span of extents which are
318 /// converted to @c extents_type and used to initialize the layout mapping, and a default constructed @c
319 /// accessor_type.
320 ///
321 /// @pre <c> [0, mapping().required_span_size()) </c> is an accessible range of @c handle and @c accessor() for the
322 /// values of @c mapping() and @c accessor after the invocation of this constructor. This precondition is not checked,
323 /// but violating it is undefined behavior.
324 ///
325 /// @note This constructor is @c explicit only if <c> N != rank_dynamic() </c>.
326 template <
327 class OtherIndexType,
328 typename Ext = Extents,
329 typename Map = mapping_type,
330 typename Acc = AccessorPolicy,
336 constexpr mdspan(data_handle_type handle, span<OtherIndexType, rank_dynamic()> extents_in) noexcept
337 : mdspan{std::move(handle), extents_type{extents_in}} {}
338
339 /// @brief Construct mdspan with a data handle and extents
340 /// @tparam OtherIndexType The type of the extent values used to initialize the layout mapping
341 /// @param handle A user supplied data handle
342 /// @param extents_in User supplied extents
343 ///
344 /// Construct an @c mdspan initialized with a user supplied @c data_handle_type, an array of extents which are
345 /// converted to @c extents_type and used to initialize the layout mapping, and a default constructed @c
346 /// accessor_type.
347 ///
348 /// @pre <c> [0, mapping().required_span_size()) </c> is an accessible range of @c handle and @c accessor() for the
349 /// values of @c mapping() and @c accessor after the invocation of this constructor. This precondition is not checked,
350 /// but violating it is undefined behavior.
351 ///
352 /// @note This constructor is @c explicit only if <c> N != rank_dynamic() </c>.
353 template <
354 class OtherIndexType,
355 std::size_t Size,
356 typename Ext = Extents,
357 typename Map = mapping_type,
358 typename Acc = AccessorPolicy,
362 std::enable_if_t<Size == rank()>,
366 constexpr explicit mdspan(data_handle_type handle, array<OtherIndexType, Size> const& extents_in) noexcept
368
369 /// @brief Construct mdspan with a data handle and extents
370 /// @tparam OtherIndexType The type of the extent values used to initialize the layout mapping
371 /// @param handle A user supplied data handle
372 /// @param extents_in User supplied extents
373 ///
374 /// Construct an @c mdspan initialized with a user supplied @c data_handle_type, an array of extents which are
375 /// converted to @c extents_type and used to initialize the layout mapping, and a default constructed @c
376 /// accessor_type.
377 ///
378 /// @pre <c> [0, mapping().required_span_size()) </c> is an accessible range of @c handle and @c accessor() for the
379 /// values of @c mapping() and @c accessor after the invocation of this constructor. This precondition is not checked,
380 /// but violating it is undefined behavior.
381 ///
382 /// @note This constructor is @c explicit only if <c> N != rank_dynamic() </c>.
383 template <
384 class OtherIndexType,
385 typename Ext = Extents,
386 typename Map = mapping_type,
387 typename Acc = AccessorPolicy,
393 constexpr mdspan(data_handle_type handle, array<OtherIndexType, rank_dynamic()> const& extents_in) noexcept
394 : mdspan{std::move(handle), arene::base::span<OtherIndexType const, rank_dynamic()>{extents_in}} {}
395
396 // parasoft-end-suppress AUTOSAR-A12_1_1-a
397
398 /// @brief Construct mdspan with a data handle and extents
399 /// @param handle A user supplied data handle
400 /// @param extents_in User supplied extents
401 ///
402 /// @pre <c>[0, mapping().required_span_size())</c> is an accessible range of @c handle and @c accessor() for
403 /// the values of @c mapping() and @c accessor() after the invocation of this constructor. This precondition is not
404 /// checked, but violating it is undefined behavior.
405 ///
406 /// Construct an @c mdspan initialized with a user supplied @c data_handle_type, a user supplied @c extents_type used
407 /// to initialize the layout mapping, and a default constructed @c accessor_type.
408 template <
409 typename Ext = Extents,
410 typename Map = mapping_type,
411 typename Acc = AccessorPolicy,
415 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: Parameter 'extents_in' is used"
416 constexpr mdspan(data_handle_type handle, extents_type const& extents_in) noexcept
417 : accessor_type{},
418 mapping_type{extents_in},
419 ptr_{std::move(handle)} {}
420 // parasoft-end-suppress AUTOSAR-A0_1_4-a
421
422 /// @brief Construct mdspan with a data handle and mapping
423 /// @param handle A user supplied data handle
424 /// @param mapping_in User supplied mapping
425 ///
426 /// @pre <c>[0, mapping_in.required_span_size())</c> is an accessible range of @c handle and @c accessor() for
427 /// the value of @c accessor() after the invocation of this constructor. This precondition is not checked, but
428 /// violating it is undefined behavior.
429 ///
430 /// Construct an @c mdspan initialized with a user supplied @c data_handle_type, a user supplied @c mapping, and a
431 /// default constructed @c accessor_type.
432 template <
433 typename Acc = AccessorPolicy,
434 constraints<std::enable_if_t<std::is_default_constructible<Acc>::value>> = nullptr>
435 constexpr mdspan(data_handle_type handle, mapping_type const& mapping_in) noexcept
436 : accessor_type{},
437 mapping_type{mapping_in},
438 ptr_{std::move(handle)} {}
439
440 /// @brief Construct mdspan with a data handle, mapping, and accessor
441 /// @param handle A user supplied data handle
442 /// @param mapping_in User supplied mapping
443 /// @param accessor_in User supplied accessor
444 ///
445 /// @pre <c>[0, mapping_in.required_span_size())</c> is an accessible range of @c handle and @c accessor_in. This
446 /// precondition is not checked, but violating it is undefined behavior.
447 ///
448 /// Construct an @c mdspan initialized with a user supplied @c data_handle_type, a user supplied @c mapping, and a
449 /// user supplied @c accessor_type.
450 constexpr mdspan(data_handle_type handle, mapping_type const& mapping_in, accessor_type const& accessor_in) noexcept
451 : accessor_type{accessor_in},
452 mapping_type{mapping_in},
453 ptr_{std::move(handle)} {}
454
455 private:
456 /// @brief Check if the converting constructor should be explicit
457 /// @tparam OtherExtents The extents type of the other @c mdspan
458 /// @tparam OtherLayoutPolicy The layout policy of the other @c mdspan
459 /// @tparam OtherAccessor The accessor policy of the other @c mdspan
460 template <class OtherExtents, class OtherLayoutPolicy, class OtherAccessor>
461 static constexpr bool converting_constructor_is_explicit_v{
462 !std::is_convertible<typename OtherLayoutPolicy::template mapping<OtherExtents> const&, mapping_type>::value ||
464 };
465
466 public:
467 /// @brief Converting constructor from another @c mdspan
468 /// @tparam OtherElementType The element type of the other @c mdspan
469 /// @tparam OtherExtents The extents type of the other @c mdspan
470 /// @tparam OtherLayoutPolicy The layout policy of the other @c mdspan
471 /// @tparam OtherAccessor The accessor policy of the other @c mdspan
472 /// @param other The other mdspan to construct from
473 ///
474 /// @pre For each rank index @c r of @c extents_type, <c> static_extent(r) == dynamic_extent || static_extent(r) ==
475 /// other.extent(r) </c> is true, otherwise this is an @c ARENE_PRECONDITION violation.
476 /// @pre <c>[0, mapping().required_span_size())</c> is an accessible range of @c handle() and @c accessor() for values
477 /// of handle(), mapping(), and accessor() after the invocation of this constructor. This precondition is not checked,
478 /// but violating it is undefined behavior.
479 ///
480 /// @note This constructor is @c explicit only if <c> !std::is_convertible<typename OtherLayoutPolicy::template
481 /// mapping<OtherExtents> const&, mapping_type>::value || !std::is_convertible<OtherAccessor const&,
482 /// accessor_type>::value; </c>
483 template <
484 class OtherElementType,
485 class OtherExtents,
486 class OtherLayoutPolicy,
487 class OtherAccessor,
490 std::enable_if_t<std::is_constructible<
492 typename OtherLayoutPolicy::template mapping<OtherExtents> const&>::value>,
493 std::enable_if_t<std::is_constructible<accessor_type, OtherAccessor const&>::value>,
496 std::enable_if_t<std::is_constructible<extents_type, OtherExtents const&>::value>> = nullptr>
498 : accessor_type{other.accessor()},
499 mapping_type{other.mapping()},
500 ptr_{other.data_handle()} {
501 mdspan_detail::validate_extents_compatibility<extents_type>(other.extents());
502 }
503
504 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: Delegates to constructor which does construct base
505 // classes"
506 /// @brief Converting constructor from another @c mdspan
507 /// @tparam OtherElementType The element type of the other @c mdspan
508 /// @tparam OtherExtents The extents type of the other @c mdspan
509 /// @tparam OtherLayoutPolicy The layout policy of the other @c mdspan
510 /// @tparam OtherAccessor The accessor policy of the other @c mdspan
511 /// @param other The other mdspan to construct from
512 ///
513 /// @pre For each rank index @c r of @c extents_type, <c> static_extent(r) == dynamic_extent || static_extent(r) ==
514 /// other.extent(r) </c> is true, otherwise this is an @c ARENE_PRECONDITION violation.
515 /// @pre <c>[0, mapping().required_span_size())</c> is an accessible range of @c handle() and @c accessor() for
516 /// values of handle(), mapping(), and accessor() after the invocation of this constructor. This precondition is not
517 /// checked, but violating it is undefined behavior.
518 ///
519 /// @note This constructor is @c explicit only if <c> !std::is_convertible<typename OtherLayoutPolicy::template
520 /// mapping<OtherExtents> const&, mapping_type>::value || !std::is_convertible<OtherAccessor const&,
521 /// accessor_type>::value; </c>
522 template <
523 class OtherElementType,
524 class OtherExtents,
525 class OtherLayoutPolicy,
526 class OtherAccessor,
531 typename OtherLayoutPolicy::template mapping<OtherExtents> const&>::value>,
536 // NOLINTNEXTLINE(hicpp-explicit-conversions)
538 : accessor_type{other.accessor()},
539 mapping_type{other.mapping()},
540 ptr_{other.data_handle()} {
541 mdspan_detail::validate_extents_compatibility<extents_type>(other.extents());
542 }
543
544 // parasoft-end-suppress AUTOSAR-A12_1_1-a
545
546 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: No identifiers are hidden by this function"
547 /// @brief Get the size of the multidimensional index space
548 /// @return The size of the multidimensional index space
549 constexpr auto size() const noexcept -> size_type {
550 return static_cast<size_type>(
551 layout_detail::extent_product(this->extents(), {static_cast<rank_type>(0), rank()}).value
552 );
553 }
554 // parasoft-end-suppress AUTOSAR-A2_10_1-d
555
556 /// @brief Check if the size is zero
557 /// @return @c true if the size is zero, @c false otherwise
558 constexpr auto empty() const noexcept -> bool { return this->size() == static_cast<size_type>(0); }
559
560 /// @brief Get the extents object
561 /// @return A reference to the extents object
562 using mapping_type::extents;
563
564 // parasoft-begin-suppress AUTOSAR-M9_3_1-a "False positive: Returns a const reference to member data."
565 // parasoft-begin-suppress AUTOSAR-A9_3_1-a "False positive: Returns a const reference to member data."
566 /// @brief Get the data handle
567 /// @return The data handle
568 constexpr auto data_handle() const noexcept -> data_handle_type const& { return ptr_; }
569 // parasoft-end-suppress AUTOSAR-M9_3_1-a
570 // parasoft-end-suppress AUTOSAR-A9_3_1-a
571
572 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: No identifiers are hidden by this function"
573 /// @brief Get the mapping
574 /// @return A reference to the mapping object
575 constexpr auto mapping() const noexcept -> mapping_type const& { return *this; }
576 // parasoft-end-suppress AUTOSAR-A2_10_1-d
577
578 /// @brief Get the accessor
579 /// @return A reference to the accessor object
580 constexpr auto accessor() const noexcept -> accessor_type const& { return *this; }
581
582 /// @brief Access an element at the specified multi-dimensional index
583 /// @tparam OtherIndexTypes Non-type template parameter pack of indices
584 /// @param indices The multi-dimensional index as a variadic
585 /// @return reference to the indices-th element of the mdspan
586 template <
587 class... OtherIndexTypes,
591 std::enable_if_t<sizeof...(OtherIndexTypes) == extents_type::rank>> = nullptr>
592 constexpr auto operator()(OtherIndexTypes... indices) const //
597 )
598 )) //
599 -> reference //
600 {
601 return this->accessor().access(
602 data_handle(),
603 mdspan_detail::representable_cast<std::size_t>(this->mapping()(std::move(indices)...))
604 );
605 }
606
607 /// @brief Access an element with an @c array of indices
608 /// @tparam OtherIndexType Index type contained by the array
609 /// @param indices The multi-dimensional index as an @c array
610 /// @return reference to the indices-th element of the mdspan
611 template <
612 class OtherIndexType,
616 constexpr auto operator()(array<OtherIndexType, extents_type::rank> const& indices) const //
618 decltype(apply),
619 mdspan const&,
620 array<OtherIndexType, extents_type::rank()> const&>) //
621 -> reference {
622 return apply(*this, indices);
623 }
624
625 /// @brief Access an mdspan element with a @c span of indices
626 /// @tparam OtherIndexType Index type contained by the span
627 /// @param indices The multi-dimensional index as an @c span
628 /// @return reference to the indices-th element of the mdspan
629 template <
630 class OtherIndexType,
634 constexpr auto operator()(span<OtherIndexType, extents_type::rank> const indices) const //
636 decltype(apply),
637 mdspan const&,
638 mdspan_detail::tuple_span<OtherIndexType, extents_type::rank> const&>) //
639 -> reference {
640 return apply(*this, mdspan_detail::tuple_span<OtherIndexType, extents_type::rank>(indices));
641 }
642
643 /// @brief Return whether or not this mdspan's mapping is always unique, i.e. distinct indices always map to distinct
644 /// elements
645 /// @return @c true because this is enforced by the constructors as a precondition
646 /// @note In mathematical terms this asks if the mapping is always injective/one-to-one.
647 using mapping_type::is_always_unique;
648
649 /// @brief Return whether or not this mdspan's mapping is always exhaustive, i.e. every element is reachable using
650 /// some indices
651 /// @return @c false because it's possible to construct non-exhaustive mappings using this class
652 /// @note In mathematical terms this asks if the mapping is always surjective/onto.
653 using mapping_type::is_always_exhaustive;
654
655 /// @brief Return whether or not this mdspan's mapping is always strided, i.e. every dimension has a constant stride
656 /// @return @c true because this class directly uses striding to compute its mapped indices
657 /// @note In mathematical terms this asks if the mapping is always affine (isomorphic to a dot product plus offset).
658 using mapping_type::is_always_strided;
659
660 /// @brief Return whether or not this instance is unique, i.e. distinct indices always map to distinct elements
661 /// @return Always @c true because this is enforced by the constructors as a precondition
662 /// @note In mathematical terms this asks if the mapping is injective/one-to-one.
663 using mapping_type::is_unique;
664
665 /// @brief Return whether or not this instance is exhaustive, i.e. every element is reachable using some indices
666 /// @return @c true if every provided stride is the product of the extents corresponding to some subset of the other
667 /// strides, otherwise @c false
668 /// @note In mathematical terms this asks if the mapping is surjective/onto.
669 using mapping_type::is_exhaustive;
670
671 /// @brief Return whether or not this instance is strided, i.e. every dimension has a constant stride
672 /// @return Always @c true because this class directly uses striding to compute its mapped indices
673 /// @note In mathematical terms this asks if the mapping is affine (isomorphic to a dot product plus an offset).
674 using mapping_type::is_strided;
675
676 /// @brief Get the stride of a particular rank in this @c mapping
677 /// @param dimension The index of the dimension to return the stride of
678 /// @return The stride of the selected rank
679 /// @pre <c>dimension < extents_type::rank()</c>
680 using mapping_type::stride;
681
682 // parasoft-begin-suppress AUTOSAR-A7_1_1-a "Declaring 'lhs' or 'rhs' as reference to const changes semantics"
683 // parasoft-begin-suppress AUTOSAR-M7_1_2-c "Declaring 'lhs' or 'rhs' as reference to const changes semantics"
684 // parasoft-begin-suppress AUTOSAR-A8_4_9-a "Declaring 'lhs' or 'rhs' as reference to const changes semantics"
685 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Friend swap operators permitted by A11-3-1 Permit #2 v1.0.0"
686 // parasoft-begin-suppress AUTOSAR-A0_1_3-a "False positive: This is a public function, and doesn't need to be used
687 // in the translation unit"
688
689 /// @brief swaps the contents
690 /// @param lhs left @c mdspan value to swap
691 /// @param rhs right @c mdspan value to swap
692 friend constexpr auto swap(mdspan& lhs, mdspan& rhs) noexcept -> void {
693 arene::base::swap(lhs.ptr_, rhs.ptr_);
694 arene::base::swap(static_cast<mapping_type&>(lhs), static_cast<mapping_type&>(rhs));
695 arene::base::swap(static_cast<accessor_type&>(lhs), static_cast<accessor_type&>(rhs));
696 }
697
698 // parasoft-end-suppress AUTOSAR-A7_1_1-a
699 // parasoft-end-suppress AUTOSAR-M7_1_2-c
700 // parasoft-end-suppress AUTOSAR-A8_4_9-a
701 // parasoft-end-suppress AUTOSAR-A11_3_1-a
702 // parasoft-end-suppress AUTOSAR-A0_1_3-a
703
704 private:
705 // parasoft-begin-suppress AUTOSAR-M14_6_1-a "False positive: The identifier data_handle_type is brought into this
706 // class scope"
707 /// @brief The data handle
708 data_handle_type ptr_;
709 // parasoft-end-suppress AUTOSAR-M14_6_1-a
710};
711
712// parasoft-end-suppress AUTOSAR-A12_1_5-a
713// parasoft-end-suppress AUTOSAR-A10_1_1-a
714// parasoft-end-suppress AUTOSAR-A12_1_6-a
715
716/// @brief Get the number of dimensions in the extents
717/// @tparam ElementType the element type
718/// @tparam Extents an instantiation of @c extents representing the shape of the multidimensional index space
719/// @tparam LayoutPolicy a layout policy (defaults to @c layout_right)
720/// @tparam AccessorPolicy an accessor policy (defaults to @c default_accessor<ElementType>)
721template <typename ElementType, typename Extents, typename LayoutPolicy, typename AccessorPolicy>
722constexpr std::
723 integral_constant<typename mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>::rank_type, Extents::rank()>
725
726/// @brief Get the number of dimensions with dynamic extent
727/// @tparam ElementType the element type
728/// @tparam Extents an instantiation of @c extents representing the shape of the multidimensional index space
729/// @tparam LayoutPolicy a layout policy (defaults to @c layout_right)
730/// @tparam AccessorPolicy an accessor policy (defaults to @c default_accessor<ElementType>)
731template <typename ElementType, typename Extents, typename LayoutPolicy, typename AccessorPolicy>
732constexpr std::integral_constant<
734 Extents::rank_dynamic()>
736
737namespace is_mdspan_detail {
738/// @brief Type trait to determine if the given type is an mdspan of some kind
739/// @tparam T The type to check
740template <typename T>
741extern constexpr bool is_mdspan_v{false};
742
743/// @brief Type trait to determine if the given type is an mdspan of some kind
744/// @tparam ElementType The element type of an @c mdspan being checked
745/// @tparam Extents The extents type of an @c mdspan being checked
746/// @tparam LayoutPolicy The layout policy type (note: not the layout mapping) of an @c mdspan being checked
747/// @tparam AccessorPolicy The accessor policy type of an @c mdspan being checked
748template <typename ElementType, typename Extents, typename LayoutPolicy, typename AccessorPolicy>
750} // namespace is_mdspan_detail
751
752/// @brief Type trait to determine if the given type is an mdspan of some kind
753/// @tparam T The type to check
754template <typename T>
755extern constexpr bool is_mdspan_v{is_mdspan_detail::is_mdspan_v<std::remove_cv_t<T>>};
756
757} // namespace base
758} // namespace arene
759
760#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MDSPAN_MDSPAN_HPP_
A fixed-size array of T objects.
Definition array.hpp:77
accesses a single element from a pointer and an index
Definition default_accessor.hpp:32
ElementType * data_handle_type
data handle type Refers to a group of elements. In this case, a pointer.
Definition default_accessor.hpp:48
constexpr auto access(data_handle_type ptr, std::size_t index) const noexcept -> reference
obtain the element at the specified index
Definition default_accessor.hpp:73
ElementType & reference
reference type Returned on element to access. In this case, an lvalue reference.
Definition default_accessor.hpp:45
represents a multidimensional index space
Definition extents.hpp:72
constexpr auto extent(rank_type dimension) const noexcept -> index_type
Get the actual extent for the given dimension, returning the extent supplied to the constructor for d...
Definition extents.hpp:294
constexpr mdspan(data_handle_type handle, array< OtherIndexType, rank_dynamic()> const &extents_in) noexcept
Construct mdspan with a data handle and extents.
Definition mdspan.hpp:393
friend constexpr auto swap(mdspan &lhs, mdspan &rhs) noexcept -> void
swaps the contents
Definition mdspan.hpp:692
constexpr mdspan(data_handle_type handle, mapping_type const &mapping_in, accessor_type const &accessor_in) noexcept
Construct mdspan with a data handle, mapping, and accessor.
Definition mdspan.hpp:450
constexpr auto operator()(array< OtherIndexType, extents_type::rank > const &indices) const noexcept(arene::base::is_nothrow_invocable_v<, mdspan const &, array< OtherIndexType, extents_type::rank()> const & >) -> reference
Access an element with an array of indices.
Definition mdspan.hpp:616
static constexpr std::integral_constant< rank_type, Extents::rank_dynamic()> rank_dynamic
Definition mdspan.hpp:210
constexpr auto empty() const noexcept -> bool
Check if the size is zero.
Definition mdspan.hpp:558
constexpr auto accessor() const noexcept -> accessor_type const &
Definition mdspan.hpp:580
constexpr mdspan(data_handle_type handle, extents_type const &extents_in) noexcept
Construct mdspan with a data handle and extents.
Definition mdspan.hpp:416
constexpr mdspan(data_handle_type handle, span< OtherIndexType, Size > extents_in) noexcept
Construct mdspan with a data handle and extents.
Definition mdspan.hpp:309
constexpr auto size() const noexcept -> size_type
Get the size of the multidimensional index space.
Definition mdspan.hpp:549
constexpr mdspan(data_handle_type handle, span< OtherIndexType, rank_dynamic()> extents_in) noexcept
Construct mdspan with a data handle and extents.
Definition mdspan.hpp:336
constexpr auto data_handle() const noexcept -> data_handle_type const &
Definition mdspan.hpp:568
constexpr auto operator()(span< OtherIndexType, extents_type::rank > const indices) const noexcept(arene::base::is_nothrow_invocable_v<, mdspan const &, mdspan_detail::tuple_span< OtherIndexType, extents_type::rank > const & >) -> reference
Access an mdspan element with a span of indices.
Definition mdspan.hpp:634
static constexpr std::integral_constant< rank_type, Extents::rank()> rank
Definition mdspan.hpp:206
constexpr mdspan() noexcept
Default constructor.
Definition mdspan.hpp:246
static constexpr auto static_extent(rank_type dimension) noexcept -> std::size_t
Get the static extent for the given dimension.
Definition mdspan.hpp:218
constexpr mdspan(data_handle_type handle, mapping_type const &mapping_in) noexcept
Construct mdspan with a data handle and mapping.
Definition mdspan.hpp:435
constexpr mdspan(data_handle_type handle, array< OtherIndexType, Size > const &extents_in) noexcept
Construct mdspan with a data handle and extents.
Definition mdspan.hpp:366
typename layout_type::template mapping< extents_type > mapping_type
Definition mdspan.hpp:181
constexpr mdspan(data_handle_type handle, OtherIndexTypes... extents_in) noexcept
Construct mdspan with a data handle and extents.
Definition mdspan.hpp:277
constexpr auto mapping() const noexcept -> mapping_type const &
Get the mapping.
Definition mdspan.hpp:575
layout_constant_diagonal< extents< std::size_t, 0U, 0U > > layout_type
Definition mdspan.hpp:177
constexpr mdspan(mdspan< OtherElementType, OtherExtents, OtherLayoutPolicy, OtherAccessor > const &other)
Converting constructor from another mdspan.
Definition mdspan.hpp:497
constexpr auto extent(rank_type dimension) const noexcept -> index_type
Get the actual extent for the given dimension, returning the extent supplied to the constructor for d...
Definition mdspan.hpp:226
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
A helper class to represent a compile-time constant with the specified type and value....
Definition integral_constant.hpp:23
static constexpr value_type value
Definition integral_constant.hpp:34
Type trait to detect if a type is constructible from arguments of the specified types.
Definition is_constructible.hpp:29
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr bool is_mdspan_v
Type trait to determine if the given type is an mdspan of some kind.
constexpr bool is_accessor_policy_v
Check if a type is a valid accessor policy.
constexpr auto sequential_values
An array holding a sequence of integer values starting at 0, incrementing by 1 each time.
constexpr bool is_layout_mapping_policy_for_extents_v
Helper trait to check if T is a valid layout mapping policy for the extents E.
constexpr bool is_extents_v
Helper variable to check if a provided type is an instantiation of extents.
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
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
typename remove_cv< T >::type remove_cv_t
An alias for T, but with top-level const and/or volatile qualifiers removed.
Definition remove_cv.hpp:64
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
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
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
Provides backports of the safe integer comparison functions from C++20.
Provides the all_of_v<bool...> trait.