3// Copyright The GNU Toolchain Authors.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
26 * This is a Standard C++ Library header.
29#ifndef _GLIBCXX_MDSPAN
30#define _GLIBCXX_MDSPAN 1
33#pragma GCC system_header
41#if __cplusplus > 202302L
42#include <bits/align.h>
45#define __glibcxx_want_mdspan
46#define __glibcxx_want_aligned_accessor
47#include <bits/version.h>
49#ifdef __glibcxx_mdspan
51namespace std _GLIBCXX_VISIBILITY(default)
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
57 __all_static(std::span<const size_t> __extents)
59 for(auto __ext : __extents)
60 if (__ext == dynamic_extent)
66 __all_dynamic(std::span<const size_t> __extents)
68 for(auto __ext : __extents)
69 if (__ext != dynamic_extent)
74 template<typename _IndexType, typename _OIndexType>
76 __index_type_cast(_OIndexType&& __other)
78 if constexpr (std::is_integral_v<_OIndexType>)
80 __glibcxx_assert(cmp_less_equal(__other,
81 __gnu_cxx::__int_traits<_IndexType>::__max));
82 if constexpr (std::is_signed_v<_OIndexType>)
83 __glibcxx_assert(__other >= 0);
84 return std::move(__other);
88 auto __ret = static_cast<_IndexType>(std::move(__other));
89 if constexpr (std::is_signed_v<_IndexType>)
90 __glibcxx_assert(__ret >= 0);
95 template<array _Extents>
99 static constexpr size_t _S_rank = _Extents.size();
101 // For __r in [0, _S_rank], _S_dynamic_index(__r) is the number
102 // of dynamic extents up to (and not including) __r.
104 // If __r is the index of a dynamic extent, then
105 // _S_dynamic_index[__r] is the index of that extent in
107 static constexpr size_t
108 _S_dynamic_index(size_t __r) noexcept
109 { return _S_dynamic_index_data[__r]; }
111 static constexpr auto _S_dynamic_index_data = [] consteval
113 array<size_t, _S_rank+1> __ret;
115 for (size_t __i = 0; __i < _S_rank; ++__i)
118 __dyn += (_Extents[__i] == dynamic_extent);
120 __ret[_S_rank] = __dyn;
124 static constexpr size_t _S_rank_dynamic = _S_dynamic_index(_S_rank);
126 // For __r in [0, _S_rank_dynamic), _S_dynamic_index_inv(__r) is the
127 // index of the __r-th dynamic extent in _Extents.
128 static constexpr size_t
129 _S_dynamic_index_inv(size_t __r) noexcept
130 { return _S_dynamic_index_inv_data[__r]; }
132 static constexpr auto _S_dynamic_index_inv_data = [] consteval
134 array<size_t, _S_rank_dynamic> __ret;
135 for (size_t __i = 0, __r = 0; __i < _S_rank; ++__i)
136 if (_Extents[__i] == dynamic_extent)
141 static constexpr size_t
142 _S_static_extent(size_t __r) noexcept
143 { return _Extents[__r]; }
146 template<array _Extents>
147 requires (__all_dynamic<_Extents>())
148 class _StaticExtents<_Extents>
151 static constexpr size_t _S_rank = _Extents.size();
153 static constexpr size_t
154 _S_dynamic_index(size_t __r) noexcept
157 static constexpr size_t _S_rank_dynamic = _S_rank;
159 static constexpr size_t
160 _S_dynamic_index_inv(size_t __k) noexcept
163 static constexpr size_t
164 _S_static_extent(size_t) noexcept
165 { return dynamic_extent; }
168 template<typename _IndexType, array _Extents>
169 class _ExtentsStorage : public _StaticExtents<_Extents>
172 using _Base = _StaticExtents<_Extents>;
175 using _Base::_S_rank;
176 using _Base::_S_rank_dynamic;
177 using _Base::_S_dynamic_index;
178 using _Base::_S_dynamic_index_inv;
179 using _Base::_S_static_extent;
181 static constexpr bool
182 _S_is_dynamic(size_t __r) noexcept
184 if constexpr (__all_static(_Extents))
186 else if constexpr (__all_dynamic(_Extents))
189 return _Extents[__r] == dynamic_extent;
192 template<typename _OIndexType>
193 static constexpr _IndexType
194 _S_int_cast(const _OIndexType& __other) noexcept
195 { return _IndexType(__other); }
198 _M_extent(size_t __r) const noexcept
200 if (_S_is_dynamic(__r))
201 return _M_dyn_exts[_S_dynamic_index(__r)];
203 return _S_static_extent(__r);
206 template<size_t _OtherRank, typename _GetOtherExtent>
207 static constexpr bool
208 _S_is_compatible_extents(_GetOtherExtent __get_extent) noexcept
210 if constexpr (_OtherRank == _S_rank)
211 for (size_t __i = 0; __i < _S_rank; ++__i)
212 if (!_S_is_dynamic(__i)
213 && !cmp_equal(_Extents[__i], _S_int_cast(__get_extent(__i))))
218 template<size_t _OtherRank, typename _GetOtherExtent>
220 _M_init_dynamic_extents(_GetOtherExtent __get_extent) noexcept
222 __glibcxx_assert(_S_is_compatible_extents<_OtherRank>(__get_extent));
223 for (size_t __i = 0; __i < _S_rank_dynamic; ++__i)
226 if constexpr (_OtherRank != _S_rank_dynamic)
227 __di = _S_dynamic_index_inv(__i);
228 _M_dyn_exts[__i] = _S_int_cast(__get_extent(__di));
233 _ExtentsStorage() noexcept = default;
235 template<typename _OIndexType, array _OExtents>
237 _ExtentsStorage(const _ExtentsStorage<_OIndexType, _OExtents>&
240 _M_init_dynamic_extents<_S_rank>([&__other](size_t __i)
241 { return __other._M_extent(__i); });
244 template<typename _OIndexType, size_t _Nm>
246 _ExtentsStorage(span<const _OIndexType, _Nm> __exts) noexcept
248 _M_init_dynamic_extents<_Nm>(
249 [&__exts](size_t __i) -> const _OIndexType&
250 { return __exts[__i]; });
253 static constexpr const array<size_t, _S_rank>&
254 _S_static_extents() noexcept
257 constexpr span<const _IndexType>
258 _M_dynamic_extents(size_t __begin, size_t __end) const noexcept
259 requires (_Extents.size() > 0)
261 return {_M_dyn_exts + _S_dynamic_index(__begin),
262 _M_dyn_exts + _S_dynamic_index(__end)};
266 using _Storage = __array_traits<_IndexType, _S_rank_dynamic>::_Type;
267 [[no_unique_address]] _Storage _M_dyn_exts{};
270 template<typename _OIndexType, typename _SIndexType>
271 concept __valid_index_type =
272 is_convertible_v<_OIndexType, _SIndexType> &&
273 is_nothrow_constructible_v<_SIndexType, _OIndexType>;
275 template<size_t _Extent, typename _IndexType>
277 __valid_static_extent = _Extent == dynamic_extent
278 || _Extent <= __gnu_cxx::__int_traits<_IndexType>::__max;
280 template<typename _Extents>
281 constexpr const array<size_t, _Extents::rank()>&
282 __static_extents() noexcept
283 { return _Extents::_Storage::_S_static_extents(); }
285 template<typename _Extents>
286 constexpr span<const size_t>
287 __static_extents(size_t __begin, size_t __end) noexcept
289 const auto& __sta_exts = __static_extents<_Extents>();
290 return span<const size_t>(__sta_exts.data() + __begin, __end - __begin);
293 // Pre-compute: \prod_{i = 0}^r _Extents[i], for r = 0,..., n (exclusive)
294 template<array _Extents>
295 constexpr auto __fwd_partial_prods = [] consteval
297 constexpr size_t __rank = _Extents.size();
298 std::array<size_t, __rank> __ret;
300 for (size_t __r = 0; __r < __rank; ++__r)
303 if (size_t __ext = _Extents[__r]; __ext != dynamic_extent)
309 // Pre-compute: \prod_{i = r+1}^{n-1} _Extents[i]
310 template<array _Extents>
311 constexpr auto __rev_partial_prods = [] consteval
313 constexpr size_t __rank = _Extents.size();
314 std::array<size_t, __rank> __ret;
316 for (size_t __r = __rank; __r > 0; --__r)
318 __ret[__r - 1] = __prod;
319 if (size_t __ext = _Extents[__r - 1]; __ext != dynamic_extent)
325 template<typename _Extents>
326 constexpr span<const typename _Extents::index_type>
327 __dynamic_extents(const _Extents& __exts, size_t __begin = 0,
328 size_t __end = _Extents::rank()) noexcept
329 { return __exts._M_exts._M_dynamic_extents(__begin, __end); }
332 template<typename _IndexType, size_t... _Extents>
335 static_assert(__is_standard_integer<_IndexType>::value,
336 "IndexType must be a signed or unsigned integer type");
338 (__mdspan::__valid_static_extent<_Extents, _IndexType> && ...),
339 "Extents must either be dynamic or representable as IndexType");
341 using index_type = _IndexType;
342 using size_type = make_unsigned_t<index_type>;
343 using rank_type = size_t;
345 static constexpr rank_type
346 rank() noexcept { return _Storage::_S_rank; }
348 static constexpr rank_type
349 rank_dynamic() noexcept { return _Storage::_S_rank_dynamic; }
351 static constexpr size_t
352 static_extent(rank_type __r) noexcept
354 __glibcxx_assert(__r < rank());
355 if constexpr (rank() == 0)
358 return _Storage::_S_static_extent(__r);
362 extent(rank_type __r) const noexcept
364 __glibcxx_assert(__r < rank());
365 if constexpr (rank() == 0)
368 return _M_exts._M_extent(__r);
372 extents() noexcept = default;
375 static consteval bool
376 _S_is_less_dynamic(size_t __ext, size_t __oext)
377 { return (__ext != dynamic_extent) && (__oext == dynamic_extent); }
379 template<typename _OIndexType, size_t... _OExtents>
380 static consteval bool
383 return (_S_is_less_dynamic(_Extents, _OExtents) || ...)
384 || (__gnu_cxx::__int_traits<index_type>::__max
385 < __gnu_cxx::__int_traits<_OIndexType>::__max);
388 template<size_t... _OExtents>
389 static consteval bool
390 _S_is_compatible_extents()
392 if constexpr (sizeof...(_OExtents) != rank())
395 return ((_OExtents == dynamic_extent || _Extents == dynamic_extent
396 || _OExtents == _Extents) && ...);
400 template<typename _OIndexType, size_t... _OExtents>
401 requires (_S_is_compatible_extents<_OExtents...>())
402 constexpr explicit(_S_ctor_explicit<_OIndexType, _OExtents...>())
403 extents(const extents<_OIndexType, _OExtents...>& __other) noexcept
404 : _M_exts(__other._M_exts)
407 template<__mdspan::__valid_index_type<index_type>... _OIndexTypes>
408 requires (sizeof...(_OIndexTypes) == rank()
409 || sizeof...(_OIndexTypes) == rank_dynamic())
410 constexpr explicit extents(_OIndexTypes... __exts) noexcept
411 : _M_exts(span<const _IndexType, sizeof...(_OIndexTypes)>(
412 initializer_list{static_cast<_IndexType>(std::move(__exts))...}))
415 template<typename _OIndexType, size_t _Nm>
416 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
417 && (_Nm == rank() || _Nm == rank_dynamic())
418 constexpr explicit(_Nm != rank_dynamic())
419 extents(span<_OIndexType, _Nm> __exts) noexcept
420 : _M_exts(span<const _OIndexType, _Nm>(__exts))
423 template<typename _OIndexType, size_t _Nm>
424 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
425 && (_Nm == rank() || _Nm == rank_dynamic())
426 constexpr explicit(_Nm != rank_dynamic())
427 extents(const array<_OIndexType, _Nm>& __exts) noexcept
428 : _M_exts(span<const _OIndexType, _Nm>(__exts))
431 template<typename _OIndexType, size_t... _OExtents>
432 friend constexpr bool
433 operator==(const extents& __self,
434 const extents<_OIndexType, _OExtents...>& __other) noexcept
436 if constexpr (!_S_is_compatible_extents<_OExtents...>())
440 auto __impl = [&__self, &__other]<size_t... _Counts>(
441 index_sequence<_Counts...>)
442 { return (cmp_equal(__self.extent(_Counts),
443 __other.extent(_Counts)) && ...); };
444 return __impl(make_index_sequence<__self.rank()>());
449 friend const array<size_t, rank()>&
450 __mdspan::__static_extents<extents>();
452 friend span<const index_type>
453 __mdspan::__dynamic_extents<extents>(const extents&, size_t, size_t);
455 using _Storage = __mdspan::_ExtentsStorage<
456 _IndexType, array<size_t, sizeof...(_Extents)>{_Extents...}>;
457 [[no_unique_address]] _Storage _M_exts;
459 template<typename _OIndexType, size_t... _OExtents>
460 friend class extents;
465 template<typename _Tp, size_t _Nm>
467 __contains_zero(span<_Tp, _Nm> __exts) noexcept
469 for (size_t __i = 0; __i < __exts.size(); ++__i)
470 if (__exts[__i] == 0)
475 template<typename _Tp, size_t _Nm>
477 __contains_zero(const array<_Tp, _Nm>& __exts) noexcept
478 { return __contains_zero(span<const _Tp>(__exts)); }
480 template<typename _Extents>
482 __empty(const _Extents& __exts) noexcept
484 if constexpr (__contains_zero(__static_extents<_Extents>()))
486 else if constexpr (_Extents::rank_dynamic() > 0)
487 return __contains_zero(__dynamic_extents(__exts));
492 template<typename _Extents>
493 constexpr typename _Extents::index_type
494 __extents_prod(const _Extents& __exts, size_t __sta_prod, size_t __begin,
495 size_t __end) noexcept
500 size_t __ret = __sta_prod;
501 if constexpr (_Extents::rank_dynamic() > 0)
502 for (auto __factor : __dynamic_extents(__exts, __begin, __end))
503 __ret *= size_t(__factor);
504 return static_cast<typename _Extents::index_type>(__ret);
507 // Preconditions: _r < _Extents::rank()
508 template<typename _Extents>
509 constexpr typename _Extents::index_type
510 __fwd_prod(const _Extents& __exts, size_t __begin, size_t __end) noexcept
512 size_t __sta_prod = [__begin, __end] {
513 span<const size_t> __sta_exts = __static_extents<_Extents>(__begin, __end);
515 for(auto __ext : __sta_exts)
516 if (__ext != dynamic_extent)
520 return __extents_prod(__exts, __sta_prod, __begin, __end);
523 template<typename _Extents>
524 constexpr typename _Extents::index_type
525 __fwd_prod(const _Extents& __exts, size_t __r) noexcept
527 constexpr size_t __rank = _Extents::rank();
528 constexpr auto& __sta_exts = __static_extents<_Extents>();
529 if constexpr (__rank == 1)
531 else if constexpr (__rank == 2)
532 return __r == 0 ? 1 : __exts.extent(0);
533 else if constexpr (__all_dynamic(std::span(__sta_exts).first(__rank-1)))
534 return __extents_prod(__exts, 1, 0, __r);
537 size_t __sta_prod = __fwd_partial_prods<__sta_exts>[__r];
538 return __extents_prod(__exts, __sta_prod, 0, __r);
542 // Preconditions: _r < _Extents::rank()
543 template<typename _Extents>
544 constexpr typename _Extents::index_type
545 __rev_prod(const _Extents& __exts, size_t __r) noexcept
547 constexpr size_t __rank = _Extents::rank();
548 constexpr auto& __sta_exts = __static_extents<_Extents>();
549 if constexpr (__rank == 1)
551 else if constexpr (__rank == 2)
552 return __r == 0 ? __exts.extent(1) : 1;
553 else if constexpr (__all_dynamic(std::span(__sta_exts).last(__rank-1)))
554 return __extents_prod(__exts, 1, __r + 1, __rank);
557 size_t __sta_prod = __rev_partial_prods<__sta_exts>[__r];
558 return __extents_prod(__exts, __sta_prod, __r + 1, __rank);
562 template<typename _Extents>
563 constexpr typename _Extents::index_type
564 __size(const _Extents& __exts) noexcept
566 constexpr size_t __sta_prod = [] {
567 span<const size_t> __sta_exts = __static_extents<_Extents>();
569 for(auto __ext : __sta_exts)
570 if (__ext != dynamic_extent)
574 return __extents_prod(__exts, __sta_prod, 0, _Extents::rank());
577 template<typename _IndexType, size_t... _Counts>
578 auto __build_dextents_type(integer_sequence<size_t, _Counts...>)
579 -> extents<_IndexType, ((void) _Counts, dynamic_extent)...>;
582 template<typename _IndexType, size_t _Rank>
583 using dextents = decltype(__mdspan::__build_dextents_type<_IndexType>(
584 make_index_sequence<_Rank>()));
586#if __glibcxx_mdspan >= 202406L
587 template<size_t _Rank, typename _IndexType = size_t>
588 using dims = dextents<_IndexType, _Rank>;
591 template<typename... _Integrals>
592 requires (is_convertible_v<_Integrals, size_t> && ...)
593 explicit extents(_Integrals...) ->
594 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>;
598 template<typename _Extents>
604 template<typename _Extents>
610 template<typename _Extents>
614#ifdef __glibcxx_padded_layouts
615 template<size_t _PaddingValue>
616 struct layout_left_padded
618 template<typename _Extents>
622 template<size_t _PaddingValue>
623 struct layout_right_padded
625 template<typename _Extents>
632 template<typename _Tp>
633 constexpr bool __is_extents = false;
635 template<typename _IndexType, size_t... _Extents>
636 constexpr bool __is_extents<extents<_IndexType, _Extents...>> = true;
638 template<typename _Extents, typename... _Indices>
639 constexpr typename _Extents::index_type
640 __linear_index_left(const _Extents& __exts, _Indices... __indices)
643 using _IndexType = typename _Extents::index_type;
644 _IndexType __res = 0;
645 if constexpr (sizeof...(__indices) > 0)
647 _IndexType __mult = 1;
648 auto __update = [&, __pos = 0u](_IndexType __idx) mutable
650 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx, __exts.extent(__pos)));
651 __res += __idx * __mult;
652 __mult *= __exts.extent(__pos);
655 (__update(__indices), ...);
660 template<typename _IndexType>
662 __static_quotient(std::span<const size_t> __sta_exts,
663 _IndexType __nom = __gnu_cxx::__int_traits<_IndexType>::__max)
665 for (auto __factor : __sta_exts)
667 if (__factor != dynamic_extent)
668 __nom /= _IndexType(__factor);
675 template<typename _Extents,
676 typename _IndexType = typename _Extents::index_type>
677 requires __is_extents<_Extents>
679 __static_quotient(_IndexType __nom
680 = __gnu_cxx::__int_traits<_IndexType>::__max)
682 std::span<const size_t> __sta_exts = __static_extents<_Extents>();
683 return __static_quotient<_IndexType>(__sta_exts, __nom);
686 template<typename _Extents>
688 __is_representable_extents(const _Extents& __exts) noexcept
690 using _IndexType = _Extents::index_type;
692 if constexpr (__contains_zero(__static_extents<_Extents>()))
696 constexpr auto __sta_quo = __static_quotient<_Extents>();
697 if constexpr (_Extents::rank_dynamic() == 0)
698 return __sta_quo != 0;
701 auto __dyn_exts = __dynamic_extents(__exts);
702 if (__contains_zero(__dyn_exts))
705 if constexpr (__sta_quo == 0)
709 auto __dyn_quo = _IndexType(__sta_quo);
710 for (auto __factor : __dyn_exts)
712 __dyn_quo /= __factor;
722 template<typename _Extents, typename _IndexType>
723 concept __representable_size = _Extents::rank_dynamic() != 0
724 || __contains_zero(__static_extents<_Extents>())
725 || (__static_quotient<_Extents, _IndexType>() != 0);
727 template<typename _Layout, typename _Mapping>
728 concept __mapping_of =
729 is_same_v<typename _Layout::template mapping<typename _Mapping::extents_type>,
732 template<template<size_t> typename _Layout, typename _Mapping>
733 concept __padded_mapping_of = __mapping_of<
734 _Layout<_Mapping::padding_value>, _Mapping>;
736#ifdef __glibcxx_padded_layouts
737 template<typename _Mapping>
738 constexpr bool __is_left_padded_mapping = __padded_mapping_of<
739 layout_left_padded, _Mapping>;
741 template<typename _Mapping>
742 constexpr bool __is_right_padded_mapping = __padded_mapping_of<
743 layout_right_padded, _Mapping>;
746 template<typename _PaddedMapping>
748 __get_static_stride()
749 { return _PaddedMapping::_PaddedStorage::_S_static_stride; }
751 template<typename _Mapping>
752 concept __standardized_mapping = __mapping_of<layout_left, _Mapping>
753 || __mapping_of<layout_right, _Mapping>
754 || __mapping_of<layout_stride, _Mapping>
755#ifdef __glibcxx_padded_layouts
756 || __is_left_padded_mapping<_Mapping>
757 || __is_right_padded_mapping<_Mapping>
761 // A tag type to create internal ctors.
762 class __internal_ctor
766 template<typename _Extents>
767 class layout_left::mapping
770 using extents_type = _Extents;
771 using index_type = typename extents_type::index_type;
772 using size_type = typename extents_type::size_type;
773 using rank_type = typename extents_type::rank_type;
774 using layout_type = layout_left;
776 static_assert(__mdspan::__representable_size<extents_type, index_type>,
777 "The size of extents_type must be representable as index_type");
780 mapping() noexcept = default;
783 mapping(const mapping&) noexcept = default;
786 mapping(const extents_type& __extents) noexcept
787 : _M_extents(__extents)
788 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
790 template<typename _OExtents>
791 requires is_constructible_v<extents_type, _OExtents>
792 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
793 mapping(const mapping<_OExtents>& __other) noexcept
794 : mapping(__other.extents(), __mdspan::__internal_ctor{})
797 template<typename _OExtents>
798 requires (extents_type::rank() <= 1)
799 && is_constructible_v<extents_type, _OExtents>
800 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
801 mapping(const layout_right::mapping<_OExtents>& __other) noexcept
802 : mapping(__other.extents(), __mdspan::__internal_ctor{})
805 // noexcept for consistency with other layouts.
806 template<typename _OExtents>
807 requires is_constructible_v<extents_type, _OExtents>
808 constexpr explicit(extents_type::rank() > 0)
809 mapping(const layout_stride::mapping<_OExtents>& __other) noexcept
810 : mapping(__other.extents(), __mdspan::__internal_ctor{})
811 { __glibcxx_assert(*this == __other); }
813#if __glibcxx_padded_layouts
814 template<typename _LeftpadMapping>
815 requires __mdspan::__is_left_padded_mapping<_LeftpadMapping>
816 && is_constructible_v<extents_type,
817 typename _LeftpadMapping::extents_type>
819 explicit(!is_convertible_v<typename _LeftpadMapping::extents_type,
821 mapping(const _LeftpadMapping& __other) noexcept
822 : mapping(__other.extents(), __mdspan::__internal_ctor{})
824 constexpr size_t __ostride_sta = __mdspan::__get_static_stride<
827 if constexpr (extents_type::rank() > 1)
829 if constexpr (extents_type::static_extent(0) != dynamic_extent
830 && __ostride_sta != dynamic_extent)
831 static_assert(extents_type::static_extent(0) == __ostride_sta);
833 __glibcxx_assert(__other.stride(1)
834 == __other.extents().extent(0));
837#endif // __glibcxx_padded_layouts
840 operator=(const mapping&) noexcept = default;
842 constexpr const extents_type&
843 extents() const noexcept { return _M_extents; }
846 required_span_size() const noexcept
847 { return __mdspan::__size(_M_extents); }
849 // _GLIBCXX_RESOLVE_LIB_DEFECTS
850 // 4314. Missing move in mdspan layout mapping::operator()
851 template<__mdspan::__valid_index_type<index_type>... _Indices>
852 requires (sizeof...(_Indices) == extents_type::rank())
854 operator()(_Indices... __indices) const noexcept
856 return __mdspan::__linear_index_left(_M_extents,
857 static_cast<index_type>(std::move(__indices))...);
860 static constexpr bool
861 is_always_unique() noexcept { return true; }
863 static constexpr bool
864 is_always_exhaustive() noexcept { return true; }
866 static constexpr bool
867 is_always_strided() noexcept { return true; }
869 static constexpr bool
870 is_unique() noexcept { return true; }
872 static constexpr bool
873 is_exhaustive() noexcept { return true; }
875 static constexpr bool
876 is_strided() noexcept { return true; }
879 stride(rank_type __i) const noexcept
880 requires (extents_type::rank() > 0)
882 __glibcxx_assert(__i < extents_type::rank());
883 return __mdspan::__fwd_prod(_M_extents, __i);
886 template<typename _OExtents>
887 requires (extents_type::rank() == _OExtents::rank())
888 friend constexpr bool
889 operator==(const mapping& __self, const mapping<_OExtents>& __other)
891 { return __self.extents() == __other.extents(); }
894 template<typename _OExtents>
896 mapping(const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
897 : _M_extents(__oexts)
899 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
900 "The size of OtherExtents must be representable as index_type");
901 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
904 [[no_unique_address]] extents_type _M_extents{};
909 template<typename _Extents, typename... _Indices>
910 constexpr typename _Extents::index_type
911 __linear_index_right(const _Extents& __exts, _Indices... __indices)
914 using _IndexType = typename _Extents::index_type;
915 array<_IndexType, sizeof...(__indices)> __ind_arr{__indices...};
916 _IndexType __res = 0;
917 if constexpr (sizeof...(__indices) > 0)
919 _IndexType __mult = 1;
920 auto __update = [&, __pos = __exts.rank()](_IndexType) mutable
923 _GLIBCXX_DEBUG_ASSERT(cmp_less(__ind_arr[__pos],
924 __exts.extent(__pos)));
925 __res += __ind_arr[__pos] * __mult;
926 __mult *= __exts.extent(__pos);
928 (__update(__indices), ...);
934 template<typename _Extents>
935 class layout_right::mapping
938 using extents_type = _Extents;
939 using index_type = typename extents_type::index_type;
940 using size_type = typename extents_type::size_type;
941 using rank_type = typename extents_type::rank_type;
942 using layout_type = layout_right;
944 static_assert(__mdspan::__representable_size<extents_type, index_type>,
945 "The size of extents_type must be representable as index_type");
948 mapping() noexcept = default;
951 mapping(const mapping&) noexcept = default;
954 mapping(const extents_type& __extents) noexcept
955 : _M_extents(__extents)
956 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
958 template<typename _OExtents>
959 requires is_constructible_v<extents_type, _OExtents>
960 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
961 mapping(const mapping<_OExtents>& __other) noexcept
962 : mapping(__other.extents(), __mdspan::__internal_ctor{})
965 template<typename _OExtents>
966 requires (extents_type::rank() <= 1)
967 && is_constructible_v<extents_type, _OExtents>
968 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
969 mapping(const layout_left::mapping<_OExtents>& __other) noexcept
970 : mapping(__other.extents(), __mdspan::__internal_ctor{})
973 template<typename _OExtents>
974 requires is_constructible_v<extents_type, _OExtents>
975 constexpr explicit(extents_type::rank() > 0)
976 mapping(const layout_stride::mapping<_OExtents>& __other) noexcept
977 : mapping(__other.extents(), __mdspan::__internal_ctor{})
978 { __glibcxx_assert(*this == __other); }
980#if __glibcxx_padded_layouts
981 template<typename _RightPaddedMapping>
982 requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
983 && is_constructible_v<extents_type,
984 typename _RightPaddedMapping::extents_type>
986 explicit(!is_convertible_v<typename _RightPaddedMapping::extents_type,
988 mapping(const _RightPaddedMapping& __other) noexcept
989 : mapping(__other.extents(), __mdspan::__internal_ctor{})
991 constexpr size_t __rank = extents_type::rank();
992 constexpr size_t __ostride_sta = __mdspan::__get_static_stride<
993 _RightPaddedMapping>();
995 if constexpr (__rank > 1)
997 if constexpr (extents_type::static_extent(__rank - 1) != dynamic_extent
998 && __ostride_sta != dynamic_extent)
999 static_assert(extents_type::static_extent(__rank - 1)
1002 __glibcxx_assert(__other.stride(__rank - 2)
1003 == __other.extents().extent(__rank - 1));
1009 operator=(const mapping&) noexcept = default;
1011 constexpr const extents_type&
1012 extents() const noexcept { return _M_extents; }
1014 constexpr index_type
1015 required_span_size() const noexcept
1016 { return __mdspan::__size(_M_extents); }
1018 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1019 // 4314. Missing move in mdspan layout mapping::operator()
1020 template<__mdspan::__valid_index_type<index_type>... _Indices>
1021 requires (sizeof...(_Indices) == extents_type::rank())
1022 constexpr index_type
1023 operator()(_Indices... __indices) const noexcept
1025 return __mdspan::__linear_index_right(
1026 _M_extents, static_cast<index_type>(std::move(__indices))...);
1029 static constexpr bool
1030 is_always_unique() noexcept
1033 static constexpr bool
1034 is_always_exhaustive() noexcept
1037 static constexpr bool
1038 is_always_strided() noexcept
1041 static constexpr bool
1042 is_unique() noexcept
1045 static constexpr bool
1046 is_exhaustive() noexcept
1049 static constexpr bool
1050 is_strided() noexcept
1053 constexpr index_type
1054 stride(rank_type __i) const noexcept
1055 requires (extents_type::rank() > 0)
1057 __glibcxx_assert(__i < extents_type::rank());
1058 return __mdspan::__rev_prod(_M_extents, __i);
1061 template<typename _OExtents>
1062 requires (extents_type::rank() == _OExtents::rank())
1063 friend constexpr bool
1064 operator==(const mapping& __self, const mapping<_OExtents>& __other)
1066 { return __self.extents() == __other.extents(); }
1069 template<typename _OExtents>
1071 mapping(const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
1072 : _M_extents(__oexts)
1074 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1075 "The size of OtherExtents must be representable as index_type");
1076 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
1079 [[no_unique_address]] extents_type _M_extents{};
1084 template<typename _Mp>
1085 concept __mapping_alike = requires
1087 requires __is_extents<typename _Mp::extents_type>;
1088 { _Mp::is_always_strided() } -> same_as<bool>;
1089 { _Mp::is_always_exhaustive() } -> same_as<bool>;
1090 { _Mp::is_always_unique() } -> same_as<bool>;
1091 bool_constant<_Mp::is_always_strided()>::value;
1092 bool_constant<_Mp::is_always_exhaustive()>::value;
1093 bool_constant<_Mp::is_always_unique()>::value;
1096 template<typename _Mapping>
1097 constexpr typename _Mapping::index_type
1098 __offset(const _Mapping& __m) noexcept
1100 using _IndexType = typename _Mapping::index_type;
1101 constexpr auto __rank = _Mapping::extents_type::rank();
1103 if constexpr (__standardized_mapping<_Mapping>)
1105 else if (__empty(__m.extents()))
1109 auto __impl = [&__m]<size_t... _Counts>(index_sequence<_Counts...>)
1110 { return __m(((void) _Counts, _IndexType(0))...); };
1111 return __impl(make_index_sequence<__rank>());
1115 template<typename _Mapping, typename... _Indices>
1116 constexpr typename _Mapping::index_type
1117 __linear_index_strides(const _Mapping& __m, _Indices... __indices)
1120 using _IndexType = typename _Mapping::index_type;
1121 _IndexType __res = 0;
1122 if constexpr (sizeof...(__indices) > 0)
1124 auto __update = [&, __pos = 0u](_IndexType __idx) mutable
1126 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx,
1127 __m.extents().extent(__pos)));
1128 __res += __idx * __m.stride(__pos++);
1130 (__update(__indices), ...);
1136 template<typename _Extents>
1137 class layout_stride::mapping
1140 using extents_type = _Extents;
1141 using index_type = typename extents_type::index_type;
1142 using size_type = typename extents_type::size_type;
1143 using rank_type = typename extents_type::rank_type;
1144 using layout_type = layout_stride;
1146 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1147 "The size of extents_type must be representable as index_type");
1152 // The precondition is either statically asserted, or automatically
1153 // satisfied because dynamic extents are zero-initialized.
1154 size_t __stride = 1;
1155 for (size_t __i = extents_type::rank(); __i > 0; --__i)
1157 _M_strides[__i - 1] = index_type(__stride);
1158 __stride *= size_t(_M_extents.extent(__i - 1));
1163 mapping(const mapping&) noexcept = default;
1165 template<typename _OIndexType>
1166 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1168 mapping(const extents_type& __exts,
1169 span<_OIndexType, extents_type::rank()> __strides) noexcept
1170 : _M_extents(__exts)
1172 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1173 _M_strides[__i] = index_type(as_const(__strides[__i]));
1176 template<typename _OIndexType>
1177 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1179 mapping(const extents_type& __exts,
1180 const array<_OIndexType, extents_type::rank()>& __strides)
1183 span<const _OIndexType, extents_type::rank()>(__strides))
1186 template<__mdspan::__mapping_alike _StridedMapping>
1187 requires (is_constructible_v<extents_type,
1188 typename _StridedMapping::extents_type>
1189 && _StridedMapping::is_always_unique()
1190 && _StridedMapping::is_always_strided())
1191 constexpr explicit(!(
1192 is_convertible_v<typename _StridedMapping::extents_type, extents_type>
1193 && __mdspan::__standardized_mapping<_StridedMapping>))
1194 mapping(const _StridedMapping& __other) noexcept
1195 : _M_extents(__other.extents())
1197 using _OIndexType = _StridedMapping::index_type;
1198 using _OExtents = _StridedMapping::extents_type;
1200 __glibcxx_assert(__mdspan::__offset(__other) == 0);
1201 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1202 "The size of StridedMapping::extents_type must be representable as"
1204 if constexpr (cmp_greater(__gnu_cxx::__int_traits<_OIndexType>::__max,
1205 __gnu_cxx::__int_traits<index_type>::__max))
1206 __glibcxx_assert(!cmp_less(
1207 __gnu_cxx::__int_traits<index_type>::__max,
1208 __other.required_span_size())
1209 && "other.required_span_size() must be representable"
1211 if constexpr (extents_type::rank() > 0)
1212 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1213 _M_strides[__i] = index_type(__other.stride(__i));
1217 operator=(const mapping&) noexcept = default;
1219 constexpr const extents_type&
1220 extents() const noexcept { return _M_extents; }
1222 constexpr array<index_type, extents_type::rank()>
1223 strides() const noexcept
1225 array<index_type, extents_type::rank()> __ret;
1226 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1227 __ret[__i] = _M_strides[__i];
1231 constexpr index_type
1232 required_span_size() const noexcept
1234 if (__mdspan::__empty(_M_extents))
1237 index_type __ret = 1;
1238 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1239 __ret += (_M_extents.extent(__i) - 1) * _M_strides[__i];
1243 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1244 // 4314. Missing move in mdspan layout mapping::operator()
1245 template<__mdspan::__valid_index_type<index_type>... _Indices>
1246 requires (sizeof...(_Indices) == extents_type::rank())
1247 constexpr index_type
1248 operator()(_Indices... __indices) const noexcept
1250 return __mdspan::__linear_index_strides(*this,
1251 static_cast<index_type>(std::move(__indices))...);
1254 static constexpr bool
1255 is_always_unique() noexcept { return true; }
1257 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1258 // 4266. layout_stride::mapping should treat empty mappings as exhaustive
1259 static constexpr bool
1260 is_always_exhaustive() noexcept
1262 return (_Extents::rank() == 0) || __mdspan::__contains_zero(
1263 __mdspan::__static_extents<extents_type>());
1266 static constexpr bool
1267 is_always_strided() noexcept { return true; }
1269 static constexpr bool
1270 is_unique() noexcept { return true; }
1272 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1273 // 4266. layout_stride::mapping should treat empty mappings as exhaustive
1275 is_exhaustive() const noexcept
1277 if constexpr (!is_always_exhaustive())
1279 auto __size = __mdspan::__size(_M_extents);
1281 return __size == required_span_size();
1286 static constexpr bool
1287 is_strided() noexcept { return true; }
1289 constexpr index_type
1290 stride(rank_type __r) const noexcept { return _M_strides[__r]; }
1292 template<__mdspan::__mapping_alike _OMapping>
1293 requires ((extents_type::rank() == _OMapping::extents_type::rank())
1294 && _OMapping::is_always_strided())
1295 friend constexpr bool
1296 operator==(const mapping& __self, const _OMapping& __other) noexcept
1298 if (__self.extents() != __other.extents())
1300 if constexpr (extents_type::rank() > 0)
1301 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1302 if (!cmp_equal(__self.stride(__i), __other.stride(__i)))
1304 return __mdspan::__offset(__other) == 0;
1308 using _Strides = typename __array_traits<index_type,
1309 extents_type::rank()>::_Type;
1310 [[no_unique_address]] extents_type _M_extents;
1311 [[no_unique_address]] _Strides _M_strides;
1314#ifdef __glibcxx_padded_layouts
1318 __least_multiple(size_t __x, size_t __y)
1322 return (__y / __x + (__y % __x != 0)) * __x ;
1325 template<typename _IndexType>
1327 __is_representable_least_multiple(size_t __x, size_t __y)
1329 constexpr auto __y_max = __gnu_cxx::__int_traits<_IndexType>::__max;
1330 if(std::cmp_greater(__y, __y_max))
1336 auto __max_delta = __y_max - static_cast<_IndexType>(__y);
1337 auto __y_mod_x = __y % __x;
1338 auto __delta = (__y_mod_x == 0) ? size_t(0) : (__x - __y_mod_x);
1339 return std::cmp_less_equal(__delta, __max_delta);
1342 template<typename _Extents, size_t _PaddingValue, typename _LayoutTraits,
1343 size_t _Rank = _Extents::rank()>
1344 concept __valid_static_stride = (_Extents::rank() <= 1)
1345 || (_PaddingValue == dynamic_extent)
1346 || (_Extents::static_extent(_LayoutTraits::_S_ext_idx) == dynamic_extent)
1347 || (__is_representable_least_multiple<size_t>(
1348 _PaddingValue, _Extents::static_extent(_LayoutTraits::_S_ext_idx)));
1350 template<size_t _PaddedStride, typename _Extents,
1351 typename _LayoutTraits>
1353 __is_representable_padded_size()
1355 using _IndexType = typename _Extents::index_type;
1356 auto __sta_exts = __static_extents<_Extents>(
1357 _LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
1358 size_t __max = __gnu_cxx::__int_traits<_IndexType>::__max;
1359 return __static_quotient(__sta_exts, __max / _PaddedStride) != 0;
1362 template<typename _Extents, size_t _PaddedStride, typename _LayoutTraits,
1363 size_t _Rank = _Extents::rank()>
1364 concept __valid_padded_size = (_Rank <= 1)
1365 || (_PaddedStride == dynamic_extent)
1366 || (!__all_static(__static_extents<_Extents>()))
1367 || (__contains_zero(__static_extents<_Extents>()))
1368 || (__is_representable_padded_size<_PaddedStride, _Extents,
1371 template<typename _Extents, typename _Stride, typename... _Indices>
1372 constexpr typename _Extents::index_type
1373 __linear_index_leftpad(const _Extents& __exts, _Stride __stride,
1374 _Indices... __indices)
1376 // i0 + stride*(i1 + extents.extent(1)*...)
1377 using _IndexType = typename _Extents::index_type;
1378 _IndexType __res = 0;
1379 if constexpr (sizeof...(__indices) > 0)
1381 _IndexType __mult = 1;
1383 auto __update_rest = [&, __pos = 1u](_IndexType __idx) mutable
1385 __res += __idx * __mult;
1386 __mult *= __exts.extent(__pos);
1390 auto __update = [&](_IndexType __idx, auto... __rest)
1393 __mult = __stride.extent(0);
1394 (__update_rest(__rest), ...);
1396 __update(__indices...);
1401 template<typename _Extents, typename _Stride, typename... _Indices>
1402 constexpr typename _Extents::index_type
1403 __linear_index_rightpad(const _Extents& __exts, _Stride __stride,
1404 _Indices... __indices)
1406 // i[n-1] + stride*(i[n-2] + extents.extent(n-2])*...)
1407 using _IndexType = typename _Extents::index_type;
1408 _IndexType __res = 0;
1409 if constexpr (sizeof...(__indices) > 0)
1411 _IndexType __mult = 1;
1412 array<_IndexType, sizeof...(__indices)> __ind_arr{__indices...};
1414 auto __update_rest = [&, __pos = __exts.rank()-1](_IndexType) mutable
1417 __res += __ind_arr[__pos] * __mult;
1418 __mult *= __exts.extent(__pos);
1421 auto __update = [&](_IndexType, auto... __rest)
1423 __res += __ind_arr[__exts.rank() - 1];
1424 __mult = __stride.extent(0);
1425 (__update_rest(__rest), ...);
1427 __update(__indices...);
1432 template<size_t _Rank>
1433 struct _LeftPaddedLayoutTraits
1435 using _LayoutSame = layout_left;
1436 using _LayoutOther = layout_right;
1438 constexpr static const size_t _S_ext_idx = 0;
1439 constexpr static const size_t _S_stride_idx = 1;
1440 constexpr static const size_t _S_unpad_begin = 1;
1441 constexpr static const size_t _S_unpad_end = _Rank;
1443 template<typename _IndexType, size_t _StaticStride, size_t..._Extents>
1444 constexpr static auto _S_make_padded_extent(
1445 extents<_IndexType, _StaticStride> __stride,
1446 const extents<_IndexType, _Extents...>& __exts)
1448 auto __impl = [&]<size_t... _Is>(integer_sequence<size_t, _Is...>)
1450 return extents<_IndexType, _StaticStride,
1451 (_Extents...[_Is + 1])...>{
1452 __stride.extent(0), __exts.extent(_Is + 1)...};
1454 return __impl(make_index_sequence<sizeof...(_Extents) - 1>());
1458 template<size_t _Rank>
1459 struct _RightPaddedLayoutTraits
1461 using _LayoutSame = layout_right;
1462 using _LayoutOther = layout_left;
1464 constexpr static size_t _S_ext_idx = _Rank - 1;
1465 constexpr static size_t _S_stride_idx = _Rank - 2;
1466 constexpr static size_t _S_unpad_begin = 0;
1467 constexpr static size_t _S_unpad_end = _Rank - 1;
1469 template<typename _IndexType, size_t _StaticStride, size_t..._Extents>
1470 constexpr static auto _S_make_padded_extent(
1471 extents<_IndexType, _StaticStride> __stride,
1472 const extents<_IndexType, _Extents...>& __exts)
1474 auto __impl = [&]<size_t... _Is>(integer_sequence<size_t, _Is...>)
1476 return extents<_IndexType, (_Extents...[_Is])..., _StaticStride>{
1477 __exts.extent(_Is)..., __stride.extent(0)};
1479 return __impl(make_index_sequence<sizeof...(_Extents) - 1>());
1483 template<size_t _PaddingValue, typename _Extents, typename _LayoutTraits>
1484 class _PaddedStorage
1486 using _LayoutSame = typename _LayoutTraits::_LayoutSame;
1489 using _IndexType = typename _Extents::index_type;
1490 constexpr static size_t _S_rank = _Extents::rank();
1492 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1493 // 4372. Weaken Mandates: for dynamic padding values in padded layouts
1494 static_assert((_PaddingValue == dynamic_extent)
1495 || (cmp_less_equal(_PaddingValue,
1496 __gnu_cxx::__int_traits<_IndexType>::__max)),
1497 "padding_value must be representable as index_type");
1499 static_assert(__representable_size<_Extents, _IndexType>,
1500 "The size of extents_type must be representable as index_type");
1502 static_assert(__valid_static_stride<_Extents, _PaddingValue,
1504 "The padded stride must be representable as size_t");
1506 static constexpr size_t _S_static_stride = [] consteval
1508 constexpr size_t __rank = _Extents::rank();
1509 if constexpr (__rank <= 1)
1513 constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
1514 constexpr size_t __sta_ext = _Extents::static_extent(__ext_idx);
1515 if constexpr (__sta_ext == 0)
1517 else if constexpr (_PaddingValue == dynamic_extent
1518 || __sta_ext == dynamic_extent)
1519 return dynamic_extent;
1521 return __least_multiple(_PaddingValue, __sta_ext);
1525 static_assert(_S_static_stride == dynamic_extent
1526 || cmp_less_equal(_S_static_stride,
1527 __gnu_cxx::__int_traits<_IndexType>::__max),
1528 "Padded stride must be representable as index_type");
1530 static_assert(__valid_padded_size<_Extents, _S_static_stride,
1534 _PaddedStorage() noexcept
1536 if constexpr (_S_rank > 1)
1537 if constexpr (_S_static_stride == dynamic_extent
1538 && _S_static_padextent() != dynamic_extent)
1539 _M_stride = _Stride{_S_static_padextent()};
1543 _PaddedStorage(const _Extents& __exts)
1544 : _M_extents(__exts)
1546 if constexpr (!__all_static(__static_extents<_Extents>()))
1547 __glibcxx_assert(__is_representable_extents(_M_extents));
1549 if constexpr (_S_rank > 1)
1551 _IndexType __stride;
1552 if constexpr (_PaddingValue == dynamic_extent)
1553 __stride = _M_padextent();
1554 else if constexpr (_S_static_padextent() != dynamic_extent)
1559 __is_representable_least_multiple<_IndexType>(
1560 _PaddingValue, _M_padextent()));
1562 __stride = static_cast<_IndexType>(
1563 __least_multiple(_PaddingValue, _M_padextent()));
1565 __glibcxx_assert(__is_representable_extents(
1566 _LayoutTraits::_S_make_padded_extent(
1567 std::dextents<_IndexType, 1>{__stride},
1570 _M_stride = _Stride{__stride};
1575 _PaddedStorage(const _Extents& __exts, _IndexType __pad)
1576 : _M_extents(__exts)
1578 if constexpr (_PaddingValue != dynamic_extent)
1579 __glibcxx_assert(cmp_equal(_PaddingValue, __pad));
1580 if constexpr (_S_rank > 1 && _S_static_stride == dynamic_extent)
1583 __is_representable_least_multiple<_IndexType>(
1584 __pad, _M_padextent()));
1586 _M_stride = _Stride{static_cast<_IndexType>(
1587 __least_multiple(__pad, _M_padextent()))};
1589 __glibcxx_assert(__is_representable_extents(
1590 _LayoutTraits::_S_make_padded_extent(
1591 _M_stride, _M_extents)));
1595 template<typename _OExtents>
1597 _PaddedStorage(const typename _LayoutSame::mapping<_OExtents>&
1599 : _PaddedStorage(_Extents(__other.extents()))
1601 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
1602 constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
1603 if constexpr (_S_rank > 1 && _PaddingValue != dynamic_extent)
1605 static_assert(_S_static_stride == dynamic_extent
1606 || _OExtents::static_extent(__ext_idx) == dynamic_extent
1607 || _S_static_stride == _OExtents::static_extent(__ext_idx),
1608 "The padded stride must be compatible with other");
1610 if constexpr (_S_static_stride == dynamic_extent
1611 || _OExtents::static_extent(__stride_idx) == dynamic_extent)
1612 __glibcxx_assert(std::cmp_equal(_M_padstride(),
1617 template<typename _OExtents>
1619 _PaddedStorage(const typename layout_stride::mapping<_OExtents>&
1621 : _M_extents(__other.extents())
1623 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
1624 __gnu_cxx::__int_traits<_IndexType>
1627 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
1628 if constexpr (_S_rank > 1)
1630 if constexpr (_PaddingValue != dynamic_extent)
1631 __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
1632 _M_calc_padstride())
1633 && "The padded stride must be compatible with other");
1634 if constexpr (_S_static_stride == dynamic_extent)
1635 _M_stride = _Stride{__other.stride(__stride_idx)};
1639 template<typename _SamePaddedMapping>
1641 _PaddedStorage(_LayoutTraits::_LayoutSame,
1642 const _SamePaddedMapping& __other)
1643 : _M_extents(__other.extents())
1645 if constexpr (_S_rank > 1)
1647 static_assert(_PaddingValue == dynamic_extent
1648 || _SamePaddedMapping::padding_value == dynamic_extent
1649 || _PaddingValue == _SamePaddedMapping::padding_value,
1650 "If neither PaddingValue is dynamic_extent, then they must "
1653 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
1654 if constexpr (_PaddingValue != dynamic_extent)
1655 __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
1656 _M_calc_padstride())
1657 && "The padded stride must be compatible with other");
1658 if constexpr (_S_static_stride == dynamic_extent)
1659 _M_stride = _Stride{__other.stride(__stride_idx)};
1661 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
1662 __gnu_cxx::__int_traits<_IndexType>::__max));
1665 template<typename _OtherPaddedMapping>
1667 _PaddedStorage(_LayoutTraits::_LayoutOther,
1668 const _OtherPaddedMapping& __other) noexcept
1669 : _M_extents(__other.extents())
1671 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
1672 __gnu_cxx::__int_traits<_IndexType>::__max));
1675 static constexpr bool
1676 _M_is_always_exhaustive() noexcept
1678 if constexpr (_S_rank <= 1)
1681 return _S_static_padextent() != dynamic_extent
1682 && _S_static_stride != dynamic_extent
1683 && _S_static_padextent() == _S_static_stride;
1687 _M_is_exhaustive() const noexcept
1689 if constexpr (_M_is_always_exhaustive())
1692 return cmp_equal(_M_padextent(), _M_padstride());
1695 constexpr static size_t
1696 _S_static_padextent() noexcept
1697 { return _Extents::static_extent(_LayoutTraits::_S_ext_idx); }
1699 constexpr _IndexType
1700 _M_padextent() const noexcept
1701 { return _M_extents.extent(_LayoutTraits::_S_ext_idx); }
1703 constexpr _IndexType
1704 _M_calc_padstride() const noexcept
1706 if constexpr (_S_static_stride != dynamic_extent)
1707 return _S_static_stride;
1708 else if constexpr (_PaddingValue != dynamic_extent)
1709 return __least_multiple(_PaddingValue, _M_padextent());
1711 return _M_padextent();
1714 constexpr _IndexType
1715 _M_padstride() const noexcept
1716 { return _M_stride.extent(0); }
1718 constexpr _IndexType
1719 _M_required_span_size() const noexcept
1721 if constexpr (_S_rank == 0)
1723 else if (__mdspan::__empty(_M_extents))
1727 size_t __stride = static_cast<size_t>(_M_padstride());
1728 size_t __prod_rest = __mdspan::__fwd_prod(_M_extents,
1729 _LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
1730 size_t __delta = _M_padstride() - _M_padextent();
1731 return static_cast<_IndexType>(__stride * __prod_rest - __delta);
1735 template<typename _SamePaddedMapping>
1737 _M_equal(const _SamePaddedMapping& __other) const noexcept
1739 return _M_extents == __other.extents()
1741 || cmp_equal(_M_stride.extent(0),
1742 __other.stride(_LayoutTraits::_S_stride_idx)));
1745 using _Stride = std::extents<_IndexType, _S_static_stride>;
1746 [[no_unique_address]] _Stride _M_stride;
1747 [[no_unique_address]] _Extents _M_extents;
1751 template<size_t _PaddingValue>
1752 template<typename _Extents>
1753 class layout_left_padded<_PaddingValue>::mapping
1756 static constexpr size_t padding_value = _PaddingValue;
1758 using extents_type = _Extents;
1759 using index_type = typename extents_type::index_type;
1760 using size_type = typename extents_type::size_type;
1761 using rank_type = typename extents_type::rank_type;
1762 using layout_type = layout_left_padded<padding_value>;
1765 static constexpr size_t _S_rank = extents_type::rank();
1766 using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
1767 _Extents, __mdspan::_LeftPaddedLayoutTraits<_S_rank>>;
1768 [[no_unique_address]] _PaddedStorage _M_storage;
1770 consteval friend size_t
1771 __mdspan::__get_static_stride<mapping>();
1773 constexpr index_type
1774 _M_extent(size_t __r) const noexcept
1775 { return _M_storage._M_extents.extent(__r); }
1777 constexpr index_type
1778 _M_padstride() const noexcept
1779 { return _M_storage._M_stride.extent(0); }
1787 mapping(const mapping&) noexcept = default;
1790 mapping(const extents_type& __exts)
1791 : _M_storage(__exts)
1794 template<__mdspan::__valid_index_type<index_type> _OIndexType>
1795 constexpr mapping(const extents_type& __exts, _OIndexType __pad)
1796 : _M_storage(__exts,
1797 __mdspan::__index_type_cast<index_type>(std::move(__pad)))
1800 template<typename _OExtents>
1801 requires is_constructible_v<extents_type, _OExtents>
1802 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1803 mapping(const layout_left::mapping<_OExtents>& __other)
1804 : _M_storage(__other)
1807 template<typename _OExtents>
1808 requires is_constructible_v<_OExtents, extents_type>
1809 constexpr explicit(_OExtents::rank() > 0)
1810 mapping(const typename layout_stride::mapping<_OExtents>& __other)
1811 : _M_storage(__other)
1812 { __glibcxx_assert(*this == __other); }
1814 template<typename _LeftpadMapping>
1815 requires __mdspan::__is_left_padded_mapping<_LeftpadMapping>
1816 && is_constructible_v<extents_type,
1817 typename _LeftpadMapping::extents_type>
1818 constexpr explicit(_S_rank > 1 && (padding_value != dynamic_extent
1819 || _LeftpadMapping::padding_value == dynamic_extent))
1820 mapping(const _LeftpadMapping& __other)
1821 : _M_storage(layout_left{}, __other)
1824 template<typename _RightPaddedMapping>
1825 requires (__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
1826 || __mdspan::__mapping_of<layout_right, _RightPaddedMapping>)
1828 && is_constructible_v<extents_type,
1829 typename _RightPaddedMapping::extents_type>
1830 constexpr explicit(!is_convertible_v<
1831 typename _RightPaddedMapping::extents_type, extents_type>)
1832 mapping(const _RightPaddedMapping& __other) noexcept
1833 : _M_storage(layout_right{}, __other)
1837 operator=(const mapping&) noexcept = default;
1839 constexpr const extents_type&
1840 extents() const noexcept { return _M_storage._M_extents; }
1842 constexpr array<index_type, _S_rank>
1843 strides() const noexcept
1845 array<index_type, _S_rank> __ret;
1846 if constexpr (_S_rank > 0)
1848 if constexpr (_S_rank > 1)
1849 __ret[1] = _M_padstride();
1850 if constexpr (_S_rank > 2)
1851 for(size_t __i = 2; __i < _S_rank; ++__i)
1852 __ret[__i] = __ret[__i - 1] * _M_extent(__i - 1);
1856 constexpr index_type
1857 required_span_size() const noexcept
1858 { return _M_storage._M_required_span_size(); }
1860 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1861 // 4314. Missing move in mdspan layout mapping::operator()
1862 template<__mdspan::__valid_index_type<index_type>... _Indices>
1863 requires (sizeof...(_Indices) == _S_rank)
1864 constexpr index_type
1865 operator()(_Indices... __indices) const noexcept
1867 return __mdspan::__linear_index_leftpad(
1868 extents(), _M_storage._M_stride,
1869 static_cast<index_type>(std::move(__indices))...);
1872 static constexpr bool
1873 is_always_exhaustive() noexcept
1874 { return _PaddedStorage::_M_is_always_exhaustive(); }
1877 is_exhaustive() noexcept
1878 { return _M_storage._M_is_exhaustive(); }
1880 static constexpr bool
1881 is_always_unique() noexcept { return true; }
1883 static constexpr bool
1884 is_always_strided() noexcept { return true; }
1886 static constexpr bool
1887 is_unique() noexcept { return true; }
1889 static constexpr bool
1890 is_strided() noexcept { return true; }
1892 constexpr index_type
1893 stride(rank_type __r) const noexcept
1895 __glibcxx_assert(__r < _S_rank);
1899 return static_cast<index_type>(
1900 static_cast<size_t>(_M_padstride()) *
1901 static_cast<size_t>(__mdspan::__fwd_prod(extents(), 1, __r)));
1904 template<typename _LeftpadMapping>
1905 requires(__mdspan::__is_left_padded_mapping<_LeftpadMapping>
1906 && _LeftpadMapping::extents_type::rank() == _S_rank)
1907 friend constexpr bool
1908 operator==(const mapping& __self, const _LeftpadMapping& __other)
1910 { return __self._M_storage._M_equal(__other); }
1913 template<size_t _PaddingValue>
1914 template<typename _Extents>
1915 class layout_right_padded<_PaddingValue>::mapping {
1917 static constexpr size_t padding_value = _PaddingValue;
1918 using extents_type = _Extents;
1919 using index_type = typename extents_type::index_type;
1920 using size_type = typename extents_type::size_type;
1921 using rank_type = typename extents_type::rank_type;
1922 using layout_type = layout_right_padded<_PaddingValue>;
1925 static constexpr size_t _S_rank = extents_type::rank();
1926 using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
1927 _Extents, __mdspan::_RightPaddedLayoutTraits<_S_rank>>;
1928 [[no_unique_address]] _PaddedStorage _M_storage;
1930 consteval friend size_t
1931 __mdspan::__get_static_stride<mapping>();
1933 constexpr index_type
1934 _M_extent(size_t __r) const noexcept
1935 { return _M_storage._M_extents.extent(__r); }
1937 constexpr index_type
1938 _M_padstride() const noexcept
1939 { return _M_storage._M_stride.extent(0); }
1947 mapping(const mapping&) noexcept = default;
1950 mapping(const extents_type& __exts)
1951 : _M_storage(__exts)
1954 template<__mdspan::__valid_index_type<index_type> _OIndexType>
1955 constexpr mapping(const extents_type& __exts, _OIndexType __pad)
1956 : _M_storage(__exts,
1957 __mdspan::__index_type_cast<index_type>(std::move(__pad)))
1960 template<typename _OExtents>
1961 requires is_constructible_v<extents_type, _OExtents>
1962 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1963 mapping(const layout_right::mapping<_OExtents>& __other)
1964 : _M_storage(__other)
1967 template<typename _OExtents>
1968 requires is_constructible_v<_OExtents, extents_type>
1969 constexpr explicit(_OExtents::rank() > 0)
1970 mapping(const typename layout_stride::mapping<_OExtents>& __other)
1971 : _M_storage(__other)
1972 { __glibcxx_assert(*this == __other); }
1974 template<typename _RightPaddedMapping>
1975 requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
1976 && is_constructible_v<extents_type,
1977 typename _RightPaddedMapping::extents_type>
1978 constexpr explicit(_S_rank > 1 && (padding_value != dynamic_extent
1979 || _RightPaddedMapping::padding_value == dynamic_extent))
1980 mapping(const _RightPaddedMapping& __other)
1981 : _M_storage(layout_right{}, __other)
1984 template<typename _LeftPaddedMapping>
1985 requires (__mdspan::__is_left_padded_mapping<_LeftPaddedMapping>
1986 || __mdspan::__mapping_of<layout_left, _LeftPaddedMapping>)
1988 && is_constructible_v<extents_type,
1989 typename _LeftPaddedMapping::extents_type>
1990 constexpr explicit(!is_convertible_v<
1991 typename _LeftPaddedMapping::extents_type, extents_type>)
1992 mapping(const _LeftPaddedMapping& __other) noexcept
1993 : _M_storage(layout_left{}, __other)
1996 constexpr mapping& operator=(const mapping&) noexcept = default;
1998 constexpr const extents_type&
1999 extents() const noexcept { return _M_storage._M_extents; }
2001 constexpr array<index_type, _S_rank>
2002 strides() const noexcept
2004 array<index_type, _S_rank> __ret;
2005 if constexpr (_S_rank > 0)
2006 __ret[_S_rank - 1] = 1;
2007 if constexpr (_S_rank > 1)
2008 __ret[_S_rank - 2] = _M_padstride();
2009 if constexpr (_S_rank > 2)
2010 for(size_t __i = _S_rank - 2; __i > 0; --__i)
2011 __ret[__i - 1] = __ret[__i] * _M_extent(__i);
2015 constexpr index_type
2016 required_span_size() const noexcept
2017 { return _M_storage._M_required_span_size(); }
2019 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2020 // 4314. Missing move in mdspan layout mapping::operator()
2021 template<__mdspan::__valid_index_type<index_type>... _Indices>
2022 requires (sizeof...(_Indices) == _S_rank)
2023 constexpr index_type
2024 operator()(_Indices... __indices) const noexcept
2026 return __mdspan::__linear_index_rightpad(
2027 extents(), _M_storage._M_stride,
2028 static_cast<index_type>(std::move(__indices))...);
2031 static constexpr bool
2032 is_always_exhaustive() noexcept
2033 { return _PaddedStorage::_M_is_always_exhaustive(); }
2036 is_exhaustive() noexcept
2037 { return _M_storage._M_is_exhaustive(); }
2039 static constexpr bool
2040 is_always_unique() noexcept { return true; }
2042 static constexpr bool
2043 is_always_strided() noexcept { return true; }
2045 static constexpr bool
2046 is_unique() noexcept { return true; }
2048 static constexpr bool
2049 is_strided() noexcept { return true; }
2051 constexpr index_type
2052 stride(rank_type __r) const noexcept
2054 __glibcxx_assert(__r < _S_rank);
2055 if constexpr (_S_rank <= 1)
2057 else if (__r == _S_rank - 1)
2059 else if (__r == _S_rank - 2)
2060 return _M_padstride();
2062 return static_cast<index_type>(
2063 static_cast<size_t>(_M_padstride()) *
2064 static_cast<size_t>(__mdspan::__fwd_prod(
2065 extents(), __r + 1, _S_rank - 1)));
2068 template<typename _RightPaddedMapping>
2069 requires(__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2070 && _RightPaddedMapping::extents_type::rank() == _S_rank)
2071 friend constexpr bool
2072 operator==(const mapping& __self, const _RightPaddedMapping& __other)
2074 { return __self._M_storage._M_equal(__other); }
2076#endif // __glibcxx_padded_layouts
2078 template<typename _ElementType>
2079 struct default_accessor
2081 static_assert(!is_array_v<_ElementType>,
2082 "ElementType must not be an array type");
2083 static_assert(!is_abstract_v<_ElementType>,
2084 "ElementType must not be an abstract class type");
2086 using offset_policy = default_accessor;
2087 using element_type = _ElementType;
2088 using reference = element_type&;
2089 using data_handle_type = element_type*;
2092 default_accessor() noexcept = default;
2094 template<typename _OElementType>
2095 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
2097 default_accessor(default_accessor<_OElementType>) noexcept
2101 access(data_handle_type __p, size_t __i) const noexcept
2102 { return __p[__i]; }
2104 constexpr data_handle_type
2105 offset(data_handle_type __p, size_t __i) const noexcept
2106 { return __p + __i; }
2109#ifdef __glibcxx_aligned_accessor
2110 template<typename _ElementType, size_t _ByteAlignment>
2111 struct aligned_accessor
2113 static_assert(has_single_bit(_ByteAlignment),
2114 "ByteAlignment must be a power of two");
2115 static_assert(_ByteAlignment >= alignof(_ElementType));
2117 using offset_policy = default_accessor<_ElementType>;
2118 using element_type = _ElementType;
2119 using reference = element_type&;
2120 using data_handle_type = element_type*;
2122 static constexpr size_t byte_alignment = _ByteAlignment;
2125 aligned_accessor() noexcept = default;
2127 template<typename _OElementType, size_t _OByteAlignment>
2128 requires (_OByteAlignment >= byte_alignment)
2129 && is_convertible_v<_OElementType(*)[], element_type(*)[]>
2131 aligned_accessor(aligned_accessor<_OElementType, _OByteAlignment>)
2135 template<typename _OElementType>
2136 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
2138 aligned_accessor(default_accessor<_OElementType>) noexcept
2141 template<typename _OElementType>
2142 requires is_convertible_v<element_type(*)[], _OElementType(*)[]>
2144 operator default_accessor<_OElementType>() const noexcept
2148 access(data_handle_type __p, size_t __i) const noexcept
2149 { return std::assume_aligned<byte_alignment>(__p)[__i]; }
2151 constexpr typename offset_policy::data_handle_type
2152 offset(data_handle_type __p, size_t __i) const noexcept
2153 { return std::assume_aligned<byte_alignment>(__p) + __i; }
2159 template<typename _Extents, typename _IndexType, size_t _Nm>
2161 __is_multi_index(const _Extents& __exts, span<_IndexType, _Nm> __indices)
2163 static_assert(__exts.rank() == _Nm);
2164 for (size_t __i = 0; __i < __exts.rank(); ++__i)
2165 if (__indices[__i] >= __exts.extent(__i))
2171 template<typename _ElementType, typename _Extents,
2172 typename _LayoutPolicy = layout_right,
2173 typename _AccessorPolicy = default_accessor<_ElementType>>
2176 static_assert(!is_array_v<_ElementType>,
2177 "ElementType must not be an array type");
2178 static_assert(!is_abstract_v<_ElementType>,
2179 "ElementType must not be an abstract class type");
2180 static_assert(__mdspan::__is_extents<_Extents>,
2181 "Extents must be a specialization of std::extents");
2182 static_assert(is_same_v<_ElementType,
2183 typename _AccessorPolicy::element_type>);
2186 using extents_type = _Extents;
2187 using layout_type = _LayoutPolicy;
2188 using accessor_type = _AccessorPolicy;
2189 using mapping_type = typename layout_type::template mapping<extents_type>;
2190 using element_type = _ElementType;
2191 using value_type = remove_cv_t<element_type>;
2192 using index_type = typename extents_type::index_type;
2193 using size_type = typename extents_type::size_type;
2194 using rank_type = typename extents_type::rank_type;
2195 using data_handle_type = typename accessor_type::data_handle_type;
2196 using reference = typename accessor_type::reference;
2198 static constexpr rank_type
2199 rank() noexcept { return extents_type::rank(); }
2201 static constexpr rank_type
2202 rank_dynamic() noexcept { return extents_type::rank_dynamic(); }
2204 static constexpr size_t
2205 static_extent(rank_type __r) noexcept
2206 { return extents_type::static_extent(__r); }
2208 constexpr index_type
2209 extent(rank_type __r) const noexcept { return extents().extent(__r); }
2213 requires (rank_dynamic() > 0)
2214 && is_default_constructible_v<data_handle_type>
2215 && is_default_constructible_v<mapping_type>
2216 && is_default_constructible_v<accessor_type> = default;
2219 mdspan(const mdspan& __other) = default;
2222 mdspan(mdspan&& __other) = default;
2224 template<__mdspan::__valid_index_type<index_type>... _OIndexTypes>
2225 requires (sizeof...(_OIndexTypes) == rank()
2226 || sizeof...(_OIndexTypes) == rank_dynamic())
2227 && is_constructible_v<mapping_type, extents_type>
2228 && is_default_constructible_v<accessor_type>
2230 mdspan(data_handle_type __handle, _OIndexTypes... __exts)
2232 _M_mapping(_Extents(static_cast<index_type>(std::move(__exts))...)),
2233 _M_handle(std::move(__handle))
2236 template<typename _OIndexType, size_t _Nm>
2237 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
2238 && (_Nm == rank() || _Nm == rank_dynamic())
2239 && is_constructible_v<mapping_type, extents_type>
2240 && is_default_constructible_v<accessor_type>
2241 constexpr explicit(_Nm != rank_dynamic())
2242 mdspan(data_handle_type __handle, span<_OIndexType, _Nm> __exts)
2243 : _M_accessor(), _M_mapping(extents_type(__exts)),
2244 _M_handle(std::move(__handle))
2247 template<typename _OIndexType, size_t _Nm>
2248 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
2249 && (_Nm == rank() || _Nm == rank_dynamic())
2250 && is_constructible_v<mapping_type, extents_type>
2251 && is_default_constructible_v<accessor_type>
2252 constexpr explicit(_Nm != rank_dynamic())
2253 mdspan(data_handle_type __handle, const array<_OIndexType, _Nm>& __exts)
2254 : _M_accessor(), _M_mapping(extents_type(__exts)),
2255 _M_handle(std::move(__handle))
2259 mdspan(data_handle_type __handle, const extents_type& __exts)
2260 requires is_constructible_v<mapping_type, const extents_type&>
2261 && is_default_constructible_v<accessor_type>
2262 : _M_accessor(), _M_mapping(__exts), _M_handle(std::move(__handle))
2266 mdspan(data_handle_type __handle, const mapping_type& __mapping)
2267 requires is_default_constructible_v<accessor_type>
2268 : _M_accessor(), _M_mapping(__mapping), _M_handle(std::move(__handle))
2272 mdspan(data_handle_type __handle, const mapping_type& __mapping,
2273 const accessor_type& __accessor)
2274 : _M_accessor(__accessor), _M_mapping(__mapping),
2275 _M_handle(std::move(__handle))
2278 template<typename _OElementType, typename _OExtents, typename _OLayout,
2279 typename _OAccessor>
2280 requires is_constructible_v<mapping_type,
2281 const typename _OLayout::template mapping<_OExtents>&>
2282 && is_constructible_v<accessor_type, const _OAccessor&>
2283 constexpr explicit(!is_convertible_v<
2284 const typename _OLayout::template mapping<_OExtents>&, mapping_type>
2285 || !is_convertible_v<const _OAccessor&, accessor_type>)
2286 mdspan(const mdspan<_OElementType, _OExtents, _OLayout, _OAccessor>&
2288 : _M_accessor(__other.accessor()), _M_mapping(__other.mapping()),
2289 _M_handle(__other.data_handle())
2291 static_assert(is_constructible_v<data_handle_type,
2292 const typename _OAccessor::data_handle_type&>);
2293 static_assert(is_constructible_v<extents_type, _OExtents>);
2297 operator=(const mdspan& __other) = default;
2300 operator=(mdspan&& __other) = default;
2302 template<__mdspan::__valid_index_type<index_type>... _OIndexTypes>
2303 requires (sizeof...(_OIndexTypes) == rank())
2305 operator[](_OIndexTypes... __indices) const
2307 auto __checked_call = [this](auto... __idxs) -> index_type
2309 if constexpr (sizeof...(__idxs) > 0)
2310 __glibcxx_assert(__mdspan::__is_multi_index(extents(),
2311 span<const index_type, sizeof...(__idxs)>({__idxs...})));
2312 return _M_mapping(__idxs...);
2315 auto __index = __checked_call(
2316 static_cast<index_type>(std::move(__indices))...);
2317 return _M_accessor.access(_M_handle, __index);
2320 template<typename _OIndexType>
2321 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
2323 operator[](span<_OIndexType, rank()> __indices) const
2325 auto __call = [&]<size_t... _Counts>(index_sequence<_Counts...>)
2327 { return (*this)[index_type(as_const(__indices[_Counts]))...]; };
2328 return __call(make_index_sequence<rank()>());
2331 template<typename _OIndexType>
2332 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
2334 operator[](const array<_OIndexType, rank()>& __indices) const
2335 { return (*this)[span<const _OIndexType, rank()>(__indices)]; }
2338 size() const noexcept
2340 __glibcxx_assert(cmp_less_equal(_M_mapping.required_span_size(),
2341 __gnu_cxx::__int_traits<size_t>
2343 return size_type(__mdspan::__size(extents()));
2348 empty() const noexcept
2349 { return __mdspan::__empty(extents()); }
2351 friend constexpr void
2352 swap(mdspan& __x, mdspan& __y) noexcept
2355 swap(__x._M_mapping, __y._M_mapping);
2356 swap(__x._M_accessor, __y._M_accessor);
2357 swap(__x._M_handle, __y._M_handle);
2360 constexpr const extents_type&
2361 extents() const noexcept { return _M_mapping.extents(); }
2363 constexpr const data_handle_type&
2364 data_handle() const noexcept { return _M_handle; }
2366 constexpr const mapping_type&
2367 mapping() const noexcept { return _M_mapping; }
2369 constexpr const accessor_type&
2370 accessor() const noexcept { return _M_accessor; }
2372 // Strengthened noexcept for all `is_*` methods.
2374 static constexpr bool
2375 is_always_unique() noexcept(noexcept(mapping_type::is_always_unique()))
2376 { return mapping_type::is_always_unique(); }
2378 static constexpr bool
2379 is_always_exhaustive()
2380 noexcept(noexcept(mapping_type::is_always_exhaustive()))
2381 { return mapping_type::is_always_exhaustive(); }
2383 static constexpr bool
2385 noexcept(noexcept(mapping_type::is_always_strided()))
2386 { return mapping_type::is_always_strided(); }
2389 is_unique() const noexcept(noexcept(_M_mapping.is_unique()))
2390 { return _M_mapping.is_unique(); }
2393 is_exhaustive() const noexcept(noexcept(_M_mapping.is_exhaustive()))
2394 { return _M_mapping.is_exhaustive(); }
2397 is_strided() const noexcept(noexcept(_M_mapping.is_strided()))
2398 { return _M_mapping.is_strided(); }
2400 constexpr index_type
2401 stride(rank_type __r) const { return _M_mapping.stride(__r); }
2404 [[no_unique_address]] accessor_type _M_accessor = accessor_type();
2405 [[no_unique_address]] mapping_type _M_mapping = mapping_type();
2406 [[no_unique_address]] data_handle_type _M_handle = data_handle_type();
2409 template<typename _CArray>
2410 requires is_array_v<_CArray> && (rank_v<_CArray> == 1)
2412 -> mdspan<remove_all_extents_t<_CArray>,
2413 extents<size_t, extent_v<_CArray, 0>>>;
2415 template<typename _Pointer>
2416 requires is_pointer_v<remove_reference_t<_Pointer>>
2418 -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;
2420 template<typename _ElementType, typename... _Integrals>
2421 requires (is_convertible_v<_Integrals, size_t> && ...)
2422 && (sizeof...(_Integrals) > 0)
2423 explicit mdspan(_ElementType*, _Integrals...)
2424 -> mdspan<_ElementType,
2425 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>>;
2427 template<typename _ElementType, typename _OIndexType, size_t _Nm>
2428 mdspan(_ElementType*, span<_OIndexType, _Nm>)
2429 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
2431 template<typename _ElementType, typename _OIndexType, size_t _Nm>
2432 mdspan(_ElementType*, const array<_OIndexType, _Nm>&)
2433 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
2435 template<typename _ElementType, typename _IndexType, size_t... _ExtentsPack>
2436 mdspan(_ElementType*, const extents<_IndexType, _ExtentsPack...>&)
2437 -> mdspan<_ElementType, extents<_IndexType, _ExtentsPack...>>;
2439 template<typename _ElementType, typename _MappingType>
2440 mdspan(_ElementType*, const _MappingType&)
2441 -> mdspan<_ElementType, typename _MappingType::extents_type,
2442 typename _MappingType::layout_type>;
2444 template<typename _MappingType, typename _AccessorType>
2445 mdspan(const typename _AccessorType::data_handle_type&, const _MappingType&,
2446 const _AccessorType&)
2447 -> mdspan<typename _AccessorType::element_type,
2448 typename _MappingType::extents_type,
2449 typename _MappingType::layout_type, _AccessorType>;
2451_GLIBCXX_END_NAMESPACE_VERSION