29#ifndef _GLIBCXX_MDSPAN
30#define _GLIBCXX_MDSPAN 1
33#pragma GCC system_header
41#define __glibcxx_want_mdspan
42#define __glibcxx_want_aligned_accessor
43#define __glibcxx_want_submdspan
46#if __glibcxx_aligned_accessor
50#if __glibcxx_submdspan
54#if __cplusplus > 202302L
58#ifdef __glibcxx_mdspan
60namespace std _GLIBCXX_VISIBILITY(default)
62_GLIBCXX_BEGIN_NAMESPACE_VERSION
66 __all_static(std::span<const size_t> __extents)
68 for(
auto __ext : __extents)
69 if (__ext == dynamic_extent)
75 __all_dynamic(std::span<const size_t> __extents)
77 for(
auto __ext : __extents)
78 if (__ext != dynamic_extent)
83 template<
typename _IndexType,
typename _OIndexTypeRef>
85 __index_type_cast(_OIndexTypeRef&& __other)
89 using _OIndexType = std::remove_cvref_t<_OIndexTypeRef>;
90 if constexpr (std::is_integral_v<_OIndexType>)
92 constexpr _IndexType __index_type_max
93 = __gnu_cxx::__int_traits<_IndexType>::__max;
94 constexpr _OIndexType __oindex_type_max
95 = __gnu_cxx::__int_traits<_OIndexType>::__max;
97 if constexpr (__index_type_max < __oindex_type_max)
98 __glibcxx_assert(cmp_less_equal(__other, __index_type_max));
100 if constexpr (std::is_signed_v<_OIndexType>)
101 __glibcxx_assert(__other >= 0);
102 return static_cast<_IndexType
>(__other);
107 if constexpr (std::is_signed_v<_IndexType>)
108 __glibcxx_assert(__ret >= 0);
113 template<array _Extents>
117 static constexpr size_t _S_rank = _Extents.size();
125 static constexpr size_t
126 _S_dynamic_index(
size_t __r)
noexcept
127 {
return _S_dynamic_index_data[__r]; }
129 static constexpr auto _S_dynamic_index_data = []
consteval
131 array<size_t, _S_rank+1> __ret;
133 for (
size_t __i = 0; __i < _S_rank; ++__i)
136 __dyn += (_Extents[__i] == dynamic_extent);
138 __ret[_S_rank] = __dyn;
142 static constexpr size_t _S_rank_dynamic = _S_dynamic_index(_S_rank);
146 static constexpr size_t
147 _S_dynamic_index_inv(
size_t __r)
noexcept
148 {
return _S_dynamic_index_inv_data[__r]; }
150 static constexpr auto _S_dynamic_index_inv_data = []
consteval
152 array<size_t, _S_rank_dynamic> __ret;
153 for (
size_t __i = 0, __r = 0; __i < _S_rank; ++__i)
154 if (_Extents[__i] == dynamic_extent)
159 static constexpr size_t
160 _S_static_extent(
size_t __r)
noexcept
161 {
return _Extents[__r]; }
164 template<array _Extents>
165 requires (__all_dynamic<_Extents>())
166 class _StaticExtents<_Extents>
169 static constexpr size_t _S_rank = _Extents.size();
171 static constexpr size_t
172 _S_dynamic_index(
size_t __r)
noexcept
175 static constexpr size_t _S_rank_dynamic = _S_rank;
177 static constexpr size_t
178 _S_dynamic_index_inv(
size_t __k)
noexcept
181 static constexpr size_t
182 _S_static_extent(
size_t)
noexcept
183 {
return dynamic_extent; }
186 template<
typename _IndexType, array _Extents>
187 class _ExtentsStorage :
public _StaticExtents<_Extents>
190 using _Base = _StaticExtents<_Extents>;
193 using _Base::_S_rank;
194 using _Base::_S_rank_dynamic;
195 using _Base::_S_dynamic_index;
196 using _Base::_S_dynamic_index_inv;
197 using _Base::_S_static_extent;
199 static constexpr bool
200 _S_is_dynamic(
size_t __r)
noexcept
202 if constexpr (__all_static(_Extents))
204 else if constexpr (__all_dynamic(_Extents))
207 return _Extents[__r] == dynamic_extent;
210 template<
typename _OIndexType>
211 static constexpr _IndexType
212 _S_int_cast(
const _OIndexType& __other)
noexcept
213 {
return _IndexType(__other); }
216 _M_extent(
size_t __r)
const noexcept
218 if (_S_is_dynamic(__r))
219 return _M_dyn_exts[_S_dynamic_index(__r)];
221 return _S_static_extent(__r);
224 template<
size_t _OtherRank,
typename _GetOtherExtent>
225 static constexpr bool
226 _S_is_compatible_extents(_GetOtherExtent __get_extent)
noexcept
228 if constexpr (_OtherRank == _S_rank)
229 for (
size_t __i = 0; __i < _S_rank; ++__i)
230 if (!_S_is_dynamic(__i)
231 && !cmp_equal(_Extents[__i], _S_int_cast(__get_extent(__i))))
236 template<
size_t _OtherRank,
typename _GetOtherExtent>
238 _M_init_dynamic_extents(_GetOtherExtent __get_extent)
noexcept
240 __glibcxx_assert(_S_is_compatible_extents<_OtherRank>(__get_extent));
241 for (
size_t __i = 0; __i < _S_rank_dynamic; ++__i)
244 if constexpr (_OtherRank != _S_rank_dynamic)
245 __di = _S_dynamic_index_inv(__i);
246 _M_dyn_exts[__i] = _S_int_cast(__get_extent(__di));
251 _ExtentsStorage() noexcept = default;
253 template<typename _OIndexType, array _OExtents>
255 _ExtentsStorage(const _ExtentsStorage<_OIndexType, _OExtents>&
258 _M_init_dynamic_extents<_S_rank>([&__other](
size_t __i)
259 {
return __other._M_extent(__i); });
262 template<
typename _OIndexType,
size_t _Nm>
264 _ExtentsStorage(span<const _OIndexType, _Nm> __exts)
noexcept
266 _M_init_dynamic_extents<_Nm>(
267 [&__exts](
size_t __i) ->
const _OIndexType&
268 {
return __exts[__i]; });
271 static constexpr const array<size_t, _S_rank>&
272 _S_static_extents() noexcept
275 constexpr span<const _IndexType>
276 _M_dynamic_extents(
size_t __begin,
size_t __end)
const noexcept
277 requires (_Extents.size() > 0)
279 return {_M_dyn_exts + _S_dynamic_index(__begin),
280 _S_dynamic_index(__end) - _S_dynamic_index(__begin)};
284 using _Storage = __array_traits<_IndexType, _S_rank_dynamic>::_Type;
285 [[no_unique_address]] _Storage _M_dyn_exts{};
288 template<
typename _OIndexType,
typename _SIndexType>
289 concept __valid_index_type =
290 is_convertible_v<_OIndexType, _SIndexType> &&
291 is_nothrow_constructible_v<_SIndexType, _OIndexType>;
293 template<
size_t _Extent,
typename _IndexType>
295 __valid_static_extent = _Extent == dynamic_extent
296 || _Extent <= __gnu_cxx::__int_traits<_IndexType>::__max;
298 template<
typename _Extents>
299 constexpr const array<size_t, _Extents::rank()>&
300 __static_extents() noexcept
301 {
return _Extents::_Storage::_S_static_extents(); }
303 template<
typename _Extents>
304 constexpr span<const size_t>
305 __static_extents(
size_t __begin,
size_t __end)
noexcept
307 const auto& __sta_exts = __static_extents<_Extents>();
308 return span<const size_t>(__sta_exts.data() + __begin, __end - __begin);
312 template<array _Extents>
313 constexpr auto __fwd_partial_prods = []
consteval
315 constexpr size_t __rank = _Extents.size();
316 std::array<size_t, __rank> __ret;
318 for (
size_t __r = 0; __r < __rank; ++__r)
321 if (
size_t __ext = _Extents[__r]; __ext != dynamic_extent)
328 template<array _Extents>
329 constexpr auto __rev_partial_prods = []
consteval
331 constexpr size_t __rank = _Extents.size();
332 std::array<size_t, __rank> __ret;
334 for (
size_t __r = __rank; __r > 0; --__r)
336 __ret[__r - 1] = __prod;
337 if (
size_t __ext = _Extents[__r - 1]; __ext != dynamic_extent)
343 template<
typename _Extents>
344 constexpr span<const typename _Extents::index_type>
345 __dynamic_extents(
const _Extents& __exts,
size_t __begin = 0,
346 size_t __end = _Extents::rank()) noexcept
347 {
return __exts._M_exts._M_dynamic_extents(__begin, __end); }
350#if __glibcxx_submdspan
353 explicit full_extent_t() =
default;
356 inline constexpr full_extent_t full_extent{};
358 template<
typename _OffsetType,
typename _ExtentType,
typename _Str
ideType>
361 static_assert(__is_signed_or_unsigned_integer<_OffsetType>::value
362 || __detail::__integral_constant_like<_OffsetType>);
363 static_assert(__is_signed_or_unsigned_integer<_ExtentType>::value
364 || __detail::__integral_constant_like<_ExtentType>);
365 static_assert(__is_signed_or_unsigned_integer<_StrideType>::value
366 || __detail::__integral_constant_like<_StrideType>);
368 using offset_type = _OffsetType;
369 using extent_type = _ExtentType;
370 using stride_type = _StrideType;
372 [[no_unique_address]] offset_type offset{};
373 [[no_unique_address]] extent_type extent{};
374 [[no_unique_address]] stride_type stride{};
377 template<
typename _Mapping>
378 struct submdspan_mapping_result
380 [[no_unique_address]] _Mapping mapping = _Mapping();
384 template<
typename _Tp>
385 constexpr bool __is_submdspan_mapping_result =
false;
387 template<
typename _Mapping>
388 constexpr bool __is_submdspan_mapping_result<submdspan_mapping_result<_Mapping>> =
true;
390 template<
typename _Mapping>
391 concept __submdspan_mapping_result = __is_submdspan_mapping_result<_Mapping>;
395 template<
typename _IndexType,
size_t... _Extents>
398 static_assert(__is_signed_or_unsigned_integer<_IndexType>::value,
399 "IndexType must be a signed or unsigned integer type");
401 (__mdspan::__valid_static_extent<_Extents, _IndexType> && ...),
402 "Extents must either be dynamic or representable as IndexType");
404 using _Storage = __mdspan::_ExtentsStorage<
405 _IndexType, array<size_t,
sizeof...(_Extents)>{_Extents...}>;
406 [[no_unique_address]] _Storage _M_exts;
409 using index_type = _IndexType;
410 using size_type = make_unsigned_t<index_type>;
411 using rank_type = size_t;
413 static constexpr rank_type
414 rank() noexcept {
return _Storage::_S_rank; }
416 static constexpr rank_type
417 rank_dynamic() noexcept {
return _Storage::_S_rank_dynamic; }
419 static constexpr size_t
420 static_extent(rank_type __r)
noexcept
422 __glibcxx_assert(__r < rank());
423 if constexpr (rank() == 0)
426 return _Storage::_S_static_extent(__r);
430 extent(rank_type __r)
const noexcept
432 __glibcxx_assert(__r < rank());
433 if constexpr (rank() == 0)
436 return _M_exts._M_extent(__r);
440 extents() noexcept = default;
443 static consteval
bool
444 _S_is_less_dynamic(
size_t __ext,
size_t __oext)
445 {
return (__ext != dynamic_extent) && (__oext == dynamic_extent); }
447 template<
typename _OIndexType,
size_t... _OExtents>
448 static consteval bool
451 return (_S_is_less_dynamic(_Extents, _OExtents) || ...)
452 || (__gnu_cxx::__int_traits<index_type>::__max
453 < __gnu_cxx::__int_traits<_OIndexType>::__max);
456 template<
size_t... _OExtents>
457 static consteval bool
458 _S_is_compatible_extents()
460 if constexpr (
sizeof...(_OExtents) != rank())
463 return ((_OExtents == dynamic_extent || _Extents == dynamic_extent
464 || _OExtents == _Extents) && ...);
468 template<
typename _OIndexType,
size_t... _OExtents>
469 requires (_S_is_compatible_extents<_OExtents...>())
470 constexpr explicit(_S_ctor_explicit<_OIndexType, _OExtents...>())
471 extents(
const extents<_OIndexType, _OExtents...>& __other) noexcept
472 : _M_exts(__other._M_exts)
475 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
476 requires (
sizeof...(_OIndexTypes) == rank()
477 ||
sizeof...(_OIndexTypes) == rank_dynamic())
478 constexpr explicit extents(_OIndexTypes... __exts) noexcept
479 : _M_exts(span<
const _IndexType,
sizeof...(_OIndexTypes)>(
480 initializer_list{
static_cast<_IndexType
>(
std::move(__exts))...}))
483 template<
typename _OIndexType,
size_t _Nm>
484 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
485 && (_Nm == rank() || _Nm == rank_dynamic())
486 constexpr explicit(_Nm != rank_dynamic())
487 extents(span<_OIndexType, _Nm> __exts) noexcept
488 : _M_exts(span<const _OIndexType, _Nm>(__exts))
491 template<
typename _OIndexType,
size_t _Nm>
492 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
493 && (_Nm == rank() || _Nm == rank_dynamic())
494 constexpr explicit(_Nm != rank_dynamic())
495 extents(
const array<_OIndexType, _Nm>& __exts) noexcept
496 : _M_exts(span<const _OIndexType, _Nm>(__exts))
499 template<
typename _OIndexType,
size_t... _OExtents>
500 friend constexpr bool
501 operator==(
const extents& __self,
502 const extents<_OIndexType, _OExtents...>& __other)
noexcept
504 if constexpr (!_S_is_compatible_extents<_OExtents...>())
508 auto __impl = [&__self, &__other]<
size_t... _Counts>(
510 {
return (cmp_equal(__self.extent(_Counts),
511 __other.extent(_Counts)) && ...); };
512 return __impl(make_index_sequence<__self.rank()>());
517 friend constexpr const array<size_t, rank()>&
518 __mdspan::__static_extents<extents>() noexcept;
520 friend constexpr span<const index_type>
521 __mdspan::__dynamic_extents<extents>(const extents&,
size_t,
size_t)
524 template<typename _OIndexType,
size_t... _OExtents>
525 friend class extents;
530 template<
typename _Tp,
size_t _Nm>
532 __contains_zero(span<_Tp, _Nm> __exts)
noexcept
534 for (
size_t __i = 0; __i < __exts.size(); ++__i)
535 if (__exts[__i] == 0)
540 template<
typename _Tp,
size_t _Nm>
542 __contains_zero(
const array<_Tp, _Nm>& __exts)
noexcept
543 {
return __contains_zero(span<const _Tp>(__exts)); }
545 template<
typename _Extents>
547 __empty(
const _Extents& __exts)
noexcept
549 if constexpr (__contains_zero(__static_extents<_Extents>()))
551 else if constexpr (_Extents::rank_dynamic() > 0)
552 return __contains_zero(__dynamic_extents(__exts));
557 template<
typename _Extents>
558 constexpr typename _Extents::index_type
559 __extents_prod(
const _Extents& __exts,
size_t __sta_prod,
size_t __begin,
560 size_t __end)
noexcept
565 size_t __ret = __sta_prod;
566 if constexpr (_Extents::rank_dynamic() > 0)
567 for (
auto __factor : __dynamic_extents(__exts, __begin, __end))
568 __ret *= size_t(__factor);
569 return static_cast<typename _Extents::index_type
>(__ret);
573 template<
typename _Extents>
574 constexpr typename _Extents::index_type
575 __fwd_prod(
const _Extents& __exts,
size_t __begin,
size_t __end)
noexcept
577 size_t __sta_prod = [__begin, __end] {
578 span<const size_t> __sta_exts
579 = __static_extents<_Extents>(__begin, __end);
581 for(
auto __ext : __sta_exts)
582 if (__ext != dynamic_extent)
586 return __extents_prod(__exts, __sta_prod, __begin, __end);
589 template<
typename _Extents>
590 constexpr typename _Extents::index_type
591 __fwd_prod(
const _Extents& __exts,
size_t __r)
noexcept
593 constexpr size_t __rank = _Extents::rank();
594 constexpr auto& __sta_exts = __static_extents<_Extents>();
595 if constexpr (__rank == 1)
597 else if constexpr (__rank == 2)
598 return __r == 0 ? 1 : __exts.extent(0);
599 else if constexpr (__all_dynamic(std::span(__sta_exts).first(__rank-1)))
600 return __extents_prod(__exts, 1, 0, __r);
603 size_t __sta_prod = __fwd_partial_prods<__sta_exts>[__r];
604 return __extents_prod(__exts, __sta_prod, 0, __r);
608 template<
typename _IndexType,
size_t _Nm>
610 __fwd_prod(span<const _IndexType, _Nm> __values)
612 _IndexType __ret = 1;
613 for(
auto __value : __values)
619 template<
typename _Extents>
620 constexpr typename _Extents::index_type
621 __rev_prod(
const _Extents& __exts,
size_t __r)
noexcept
623 constexpr size_t __rank = _Extents::rank();
624 constexpr auto& __sta_exts = __static_extents<_Extents>();
625 if constexpr (__rank == 1)
627 else if constexpr (__rank == 2)
628 return __r == 0 ? __exts.extent(1) : 1;
629 else if constexpr (__all_dynamic(std::span(__sta_exts).last(__rank-1)))
630 return __extents_prod(__exts, 1, __r + 1, __rank);
633 size_t __sta_prod = __rev_partial_prods<__sta_exts>[__r];
634 return __extents_prod(__exts, __sta_prod, __r + 1, __rank);
638 template<
typename _Extents>
639 constexpr typename _Extents::index_type
640 __size(
const _Extents& __exts)
noexcept
642 constexpr size_t __sta_prod = [] {
643 span<const size_t> __sta_exts = __static_extents<_Extents>();
645 for(
auto __ext : __sta_exts)
646 if (__ext != dynamic_extent)
650 return __extents_prod(__exts, __sta_prod, 0, _Extents::rank());
653 template<
typename _IndexType,
size_t... _Counts>
654 auto __build_dextents_type(integer_sequence<size_t, _Counts...>)
655 -> extents<_IndexType, ((void) _Counts, dynamic_extent)...>;
658 template<
typename _IndexType,
size_t _Rank>
659 using dextents =
decltype(__mdspan::__build_dextents_type<_IndexType>(
660 make_index_sequence<_Rank>()));
662#if __glibcxx_mdspan >= 202406L
663 template<
size_t _Rank,
typename _IndexType =
size_t>
664 using dims = dextents<_IndexType, _Rank>;
667 template<
typename... _Integrals>
668 requires (is_convertible_v<_Integrals, size_t> && ...)
669 explicit extents(_Integrals...) ->
670 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>;
674 template<
typename _Extents>
680 template<
typename _Extents>
686 template<
typename _Extents>
690#ifdef __glibcxx_padded_layouts
691 template<
size_t _PaddingValue>
692 struct layout_left_padded
694 template<
typename _Extents>
698 template<
size_t _PaddingValue>
699 struct layout_right_padded
701 template<
typename _Extents>
708 template<
typename _Tp>
709 constexpr bool __is_extents =
false;
711 template<
typename _IndexType,
size_t... _Extents>
712 constexpr bool __is_extents<extents<_IndexType, _Extents...>> =
true;
714 template<
typename _Extents,
typename... _Indices>
715 constexpr typename _Extents::index_type
716 __linear_index_left(
const _Extents& __exts, _Indices... __indices)
719 using _IndexType =
typename _Extents::index_type;
720 _IndexType __res = 0;
721 if constexpr (
sizeof...(__indices) > 0)
723 _IndexType __mult = 1;
724 auto __update = [&, __pos = 0u](_IndexType __idx)
mutable
726 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx, __exts.extent(__pos)));
727 __res += __idx * __mult;
728 __mult *= __exts.extent(__pos);
731 (__update(__indices), ...);
736 template<
typename _IndexType>
738 __static_quotient(std::span<const size_t> __sta_exts,
739 _IndexType __nom = __gnu_cxx::__int_traits<_IndexType>::__max)
741 for (
auto __factor : __sta_exts)
743 if (__factor != dynamic_extent)
744 __nom /= _IndexType(__factor);
751 template<
typename _Extents,
752 typename _IndexType =
typename _Extents::index_type>
753 requires __is_extents<_Extents>
755 __static_quotient(_IndexType __nom
756 = __gnu_cxx::__int_traits<_IndexType>::__max)
758 std::span<const size_t> __sta_exts = __static_extents<_Extents>();
759 return __static_quotient<_IndexType>(__sta_exts, __nom);
762 template<
typename _Extents>
764 __is_representable_extents(
const _Extents& __exts)
noexcept
766 using _IndexType = _Extents::index_type;
768 if constexpr (__contains_zero(__static_extents<_Extents>()))
772 constexpr auto __sta_quo = __static_quotient<_Extents>();
773 if constexpr (_Extents::rank_dynamic() == 0)
774 return __sta_quo != 0;
777 auto __dyn_exts = __dynamic_extents(__exts);
778 if (__contains_zero(__dyn_exts))
781 if constexpr (__sta_quo == 0)
785 auto __dyn_quo = _IndexType(__sta_quo);
786 for (
auto __factor : __dyn_exts)
788 __dyn_quo /= __factor;
798 template<
typename _Extents,
typename _IndexType>
799 concept __representable_size = _Extents::rank_dynamic() != 0
800 || __contains_zero(__static_extents<_Extents>())
801 || (__static_quotient<_Extents, _IndexType>() != 0);
803 template<
typename _Layout,
typename _Mapping>
804 concept __mapping_of =
805 is_same_v<
typename _Layout::template mapping<
806 typename _Mapping::extents_type>,
809 template<
template<
size_t>
typename _Layout,
typename _Mapping>
810 concept __padded_mapping_of = __mapping_of<
811 _Layout<_Mapping::padding_value>, _Mapping>;
813#ifdef __glibcxx_padded_layouts
814 template<
typename _Mapping>
815 constexpr bool __is_left_padded_mapping = __padded_mapping_of<
816 layout_left_padded, _Mapping>;
818 template<
typename _Mapping>
819 constexpr bool __is_right_padded_mapping = __padded_mapping_of<
820 layout_right_padded, _Mapping>;
822 template<
typename _Mapping>
823 constexpr bool __is_padded_mapping = __is_left_padded_mapping<_Mapping>
824 || __is_right_padded_mapping<_Mapping>;
827 template<
typename _PaddedMapping>
829 __get_static_stride()
830 {
return _PaddedMapping::_PaddedStorage::_S_static_stride; }
832 template<
typename _Mapping>
833 concept __standardized_mapping = __mapping_of<layout_left, _Mapping>
834 || __mapping_of<layout_right, _Mapping>
835 || __mapping_of<layout_stride, _Mapping>
836#ifdef __glibcxx_padded_layouts
837 || __is_left_padded_mapping<_Mapping>
838 || __is_right_padded_mapping<_Mapping>
843 class __internal_ctor
846 template<
typename _Mapping>
847 constexpr typename _Mapping::index_type
848 __offset(
const _Mapping& __m)
noexcept
850 using _IndexType =
typename _Mapping::index_type;
851 constexpr auto __rank = _Mapping::extents_type::rank();
853 if constexpr (__standardized_mapping<_Mapping>)
855 else if (__empty(__m.extents()))
859 auto __impl = [&__m]<
size_t... _Counts>(
index_sequence<_Counts...>)
860 {
return __m(((
void) _Counts, _IndexType(0))...); };
861 return __impl(make_index_sequence<__rank>());
865#ifdef __glibcxx_submdspan
866 template<
typename _Tp>
867 constexpr bool __is_strided_slice =
false;
869 template<
typename _OffsetType,
typename _ExtentType,
typename _Str
ideType>
870 constexpr bool __is_strided_slice<strided_slice<_OffsetType,
871 _ExtentType, _StrideType>> =
true;
873 template<
typename _IndexType,
typename _OIndexType>
875 __is_representable_integer(_OIndexType __value)
877 constexpr auto __min = __gnu_cxx::__int_traits<_IndexType>::__min;
878 constexpr auto __max = __gnu_cxx::__int_traits<_IndexType>::__max;
879 return std::cmp_less_equal(__min, __value)
880 && std::cmp_less_equal(__value, __max);
883 template<
typename _Tp>
884 constexpr bool __is_constant_wrapper =
false;
886 template<_CwFixedValue _Xv,
typename _Tp>
887 constexpr bool __is_constant_wrapper<constant_wrapper<_Xv, _Tp>>
890 template<
size_t _Index,
typename _Extents>
892 __extract_extent(
const _Extents& __exts)
894 using _IndexType =
typename _Extents::index_type;
895 return extents<_IndexType, _Extents::static_extent(_Index)>{
896 __exts.extent(_Index)};
899 template<
typename _Slice,
typename _IndexType>
900 concept __acceptable_slice_type = same_as<_Slice, full_extent_t>
901 || same_as<_Slice, _IndexType> || __is_constant_wrapper<_Slice>
902 || __is_strided_slice<_Slice>;
904 template<
typename _IndexType,
typename... _Slices>
908 return (
static_cast<size_t>(!convertible_to<_Slices, _IndexType>)
912 template<
typename _IndexType,
typename... _Slices>
916 constexpr auto __rank =
sizeof...(_Slices);
917 constexpr auto __sub_rank = __subrank<_IndexType, _Slices...>();
918 auto __map = std::array<size_t, __sub_rank>{};
919 auto __is_int_like = std::array<bool, __rank>{
920 convertible_to<_Slices, _IndexType>...};
923 for (
size_t __k = 0; __k < __rank; ++__k)
924 if (!__is_int_like[__k])
929 template<
typename _Slice>
931 __slice_begin(_Slice __slice)
933 if constexpr (same_as<_Slice, full_extent_t>)
935 else if constexpr (__is_strided_slice<_Slice>)
936 return __slice.offset;
941 template<
typename _Mapping,
typename... _Slices>
943 __suboffset(
const _Mapping& __mapping,
const _Slices&... __slices)
945 using _IndexType =
typename _Mapping::index_type;
946 auto __any_past_the_end = [&]<
size_t... _Is>(
index_sequence<_Is...>)
948 auto __is_past_the_end = [](
const auto& __slice,
const auto& __ext)
950 using _Slice = remove_cvref_t<
decltype(__slice)>;
951 if constexpr (is_convertible_v<_Slice, _IndexType>)
953 else if constexpr (same_as<_Slice, full_extent_t>
954 && __ext.static_extent(0) != 0
955 && __ext.static_extent(0) != dynamic_extent)
958 return __mdspan::__slice_begin(__slice) == __ext.extent(0);
961 const auto& __exts = __mapping.extents();
962 return ((__is_past_the_end(__slices...[_Is],
963 __mdspan::__extract_extent<_Is>(__exts))) || ...);
966 if constexpr ((same_as<_Slices, full_extent_t> && ...))
967 return __mdspan::__offset(__mapping);
970 return __mapping.required_span_size();
971 return __mapping(__mdspan::__slice_begin(__slices)...);
974 template<
typename _IndexType,
size_t _Extent,
typename _Slice>
976 __static_slice_extent()
978 if constexpr (same_as<_Slice, full_extent_t>)
980 else if constexpr (same_as<_Slice, constant_wrapper<_IndexType(0)>>)
982 else if constexpr (__is_constant_wrapper<typename _Slice::extent_type>
983 && __is_constant_wrapper<typename _Slice::stride_type>)
984 return 1 + ((
typename _Slice::extent_type{}) - 1)
985 / (
typename _Slice::stride_type{});
987 return dynamic_extent;
990 template<
size_t _K,
typename _Extents,
typename _Slice>
991 constexpr typename _Extents::index_type
992 __dynamic_slice_extent(
const _Extents& __exts, _Slice __slice)
994 if constexpr (__is_strided_slice<_Slice>)
995 return __slice.extent == 0 ? 0 : 1 + (__slice.extent - 1) / __slice.stride;
997 return __exts.extent(_K);
1000 template<
typename _IndexType,
size_t... _Extents,
typename... _Slices>
1001 requires (
sizeof...(_Slices) ==
sizeof...(_Extents))
1003 __subextents(
const extents<_IndexType, _Extents...>& __exts,
1004 _Slices... __slices)
1006 constexpr auto __inv_map = __mdspan::__inv_map_rank<_IndexType, _Slices...>();
1009 using _SubExts = extents<_IndexType,
1010 __mdspan::__static_slice_extent<_IndexType,
1011 _Extents...[__inv_map[_Indices]],
1012 _Slices...[__inv_map[_Indices]]>()...>;
1013 if constexpr (_SubExts::rank_dynamic() == 0)
1017 using _StaticSubExtents = __mdspan::_StaticExtents<
1018 __mdspan::__static_extents<_SubExts>()>;
1021 constexpr auto __slice_idx = [__inv_map](
size_t __i)
consteval
1023 return __inv_map[_StaticSubExtents::_S_dynamic_index_inv(__i)];
1026 return _SubExts{__mdspan::__dynamic_slice_extent<__slice_idx(_Is)>(
1027 __exts, __slices...[__slice_idx(_Is)])...};
1029 constexpr auto __dyn_subrank = _SubExts::rank_dynamic();
1037 enum class _LayoutSide
1044 template<
typename _Mapping>
1045 consteval _LayoutSide
1048 if constexpr (__is_left_padded_mapping<_Mapping>
1049 || __mapping_of<layout_left, _Mapping>)
1050 return _LayoutSide::__left;
1051 if constexpr (__is_right_padded_mapping<_Mapping>
1052 || __mapping_of<layout_right, _Mapping>)
1053 return _LayoutSide::__right;
1055 return _LayoutSide::__unknown;
1058 template<_LayoutS
ide _S
ide,
size_t _Rank>
1059 struct _StridesTrait
1061 static constexpr const _LayoutSide _S_side = _Side;
1063 static constexpr size_t
1064 _S_idx(
size_t __k)
noexcept
1066 if constexpr (_Side == _LayoutSide::__left)
1069 return _Rank - 1 - __k;
1074 template<
typename _Mapping>
1075 static constexpr typename _Mapping::index_type
1076 _S_padded_extent(
const _Mapping& __mapping,
size_t __k)
1079 return __mapping.stride(_S_idx(1));
1081 return __mapping.extents().extent(_S_idx(__k));
1084 template<
typename _IndexType,
typename... _Slices>
1085 static consteval auto
1088 static_assert(_Side != _LayoutSide::__unknown);
1091 return __mdspan::__inv_map_rank<_IndexType, _Slices...[_S_idx(_Is)]...>();
1097 template<
typename _SubExts,
typename _Mapping,
typename... _Slices>
1099 __substrides_generic(
const _Mapping& __mapping,
const _Slices&... __slices)
1101 using _IndexType =
typename _Mapping::index_type;
1102 if constexpr (_SubExts::rank() == 0)
1103 return array<_IndexType, _SubExts::rank()>{};
1106 auto __stride = [&__mapping](
size_t __k,
auto __slice) -> _IndexType
1108 if constexpr (__is_strided_slice<
decltype(__slice)>)
1109 if (__slice.stride < __slice.extent)
1110 return __mapping.stride(__k) * __slice.stride;
1111 return __mapping.stride(__k);
1116 constexpr auto __inv_map
1117 = __mdspan::__inv_map_rank<_IndexType, _Slices...>();
1118 return array<_IndexType, _SubExts::rank()>{
1119 __stride(__inv_map[_Is], __slices...[__inv_map[_Is]])...};
1125 template<
typename _SubExts,
typename _Mapping,
typename... _Slices>
1127 __substrides_standardized(
const _Mapping& __mapping,
1128 const _Slices&... __slices)
1130 using _IndexType =
typename _Mapping::index_type;
1131 using _Trait = _StridesTrait<__mapping_side<_Mapping>(),
1132 _Mapping::extents_type::rank()>;
1133 using _SubTrait = _StridesTrait<__mapping_side<_Mapping>(), _SubExts::rank()>;
1135 constexpr size_t __sub_rank = _SubExts::rank();
1137 std::array<_IndexType, __sub_rank> __ret;
1138 if constexpr (__sub_rank > 0)
1140 constexpr auto __inv_map
1141 = _Trait::template _S_inv_map<_IndexType, _Slices...>();
1145 size_t __stride = 1;
1146 auto __body = [&](
size_t __k,
auto __slice)
1148 for (
size_t __i = __i0; __i < __inv_map[__k]; ++__i)
1149 __stride *= _Trait::_S_padded_extent(__mapping, __i);
1151 size_t __krev = _SubTrait::_S_idx(__k);
1152 if constexpr (__is_strided_slice<
decltype(__slice)>)
1154 if (__slice.stride < __slice.extent)
1155 __ret[__krev] = __stride * __slice.stride;
1157 __ret[__krev] = __stride;
1160 __ret[__krev] = __stride;
1162 __i0 = __inv_map[__k];
1165 ((__body(_Ks, __slices...[_Trait::_S_idx(__inv_map[_Ks])])),...);
1173 template<
typename _SubExts,
typename _Mapping,
typename... _Slices>
1175 __substrides(
const _Mapping& __mapping,
const _Slices&... __slices)
1177 if constexpr (__mdspan::__mapping_side<_Mapping>() == _LayoutSide::__unknown)
1178 return __mdspan::__substrides_generic<_SubExts>(__mapping, __slices...);
1180 return __mdspan::__substrides_standardized<_SubExts>(__mapping, __slices...);
1183 template<
typename _Slice>
1184 concept __is_unit_stride_slice = (__mdspan::__is_strided_slice<_Slice>
1185 && __mdspan::__is_constant_wrapper<typename _Slice::stride_type>
1186 && _Slice::stride_type::value == 1)
1187 || std::same_as<_Slice, full_extent_t>;
1193 enum class _SliceKind
1196 __unit_strided_slice,
1201 template<
typename _Slice>
1202 consteval _SliceKind
1205 if constexpr (std::same_as<_Slice, full_extent_t>)
1206 return _SliceKind::__full;
1207 else if constexpr (__mdspan::__is_strided_slice<_Slice>)
1209 if constexpr (__mdspan::__is_unit_stride_slice<_Slice>)
1210 return _SliceKind::__unit_strided_slice;
1212 return _SliceKind::__strided_slice;
1215 return _SliceKind::__collapsing;
1218 template<
typename... _Slices>
1219 consteval array<_SliceKind,
sizeof...(_Slices)>
1220 __make_slice_kind_array()
1222 return array<_SliceKind,
sizeof...(_Slices)>{
1223 __mdspan::__make_slice_kind<_Slices>()...};
1229 __is_block(span<const _SliceKind> __slice_kinds,
size_t __block_size)
1231 if (__block_size == 0)
1234 if (__block_size > __slice_kinds.size())
1237 for (
size_t __i = 0; __i < __block_size - 1; ++__i)
1238 if (__slice_kinds[__i] != _SliceKind::__full)
1241 auto __last = __slice_kinds[__block_size - 1];
1242 return __last == _SliceKind::__full
1243 || __last == _SliceKind::__unit_strided_slice;
1248 static consteval size_t
1249 __padded_block_begin_generic(span<const _SliceKind> __slice_kinds,
1252 if (__slice_kinds[0] != _SliceKind::__full
1253 && __slice_kinds[0] != _SliceKind::__unit_strided_slice)
1254 return dynamic_extent;
1255 else if (__slice_kinds.size() == 1)
1256 return dynamic_extent;
1260 while(__u < __slice_kinds.size()
1261 && __slice_kinds[__u] == _SliceKind::__collapsing)
1264 if (__mdspan::__is_block(__slice_kinds.subspan(__u), __sub_rank -1))
1266 return dynamic_extent;
1270 template<_LayoutS
ide _S
ide,
size_t _Nm>
1271 static consteval size_t
1272 __padded_block_begin(span<const _SliceKind, _Nm> __slice_kinds,
size_t __sub_rank)
1274 if constexpr (_Side == _LayoutSide::__left)
1275 return __mdspan::__padded_block_begin_generic(__slice_kinds, __sub_rank);
1278 std::array<_SliceKind, _Nm> __rev_slices;
1279 for(
size_t __i = 0; __i < _Nm; ++__i)
1280 __rev_slices[__i] = __slice_kinds[_Nm - 1 - __i];
1281 auto __rev_slice_kinds = span<const _SliceKind>(__rev_slices);
1283 auto __u = __mdspan::__padded_block_begin_generic(__rev_slice_kinds,
1285 return __u == dynamic_extent ? dynamic_extent : _Nm - 1 - __u;
1289 template<_LayoutS
ide _S
ide,
bool _Padded>
1290 struct _SubMdspanMapping;
1293 struct _SubMdspanMapping<_LayoutSide::__left, false>
1295 using _Layout = layout_left;
1296 template<
size_t _Pad>
using _PaddedLayout = layout_left_padded<_Pad>;
1298 template<
typename _Mapping,
size_t _Us>
1299 static consteval size_t
1302 using _Extents =
typename _Mapping::extents_type;
1303 constexpr auto __sta_exts = __mdspan::__static_extents<_Extents>(0, _Us);
1304 if constexpr (!__mdspan::__all_static(__sta_exts))
1305 return dynamic_extent;
1307 return __mdspan::__fwd_prod(__sta_exts);
1310 template<
size_t _Nm>
1311 static consteval bool
1312 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
size_t __sub_rank)
1313 {
return __mdspan::__is_block(__slice_kinds, __sub_rank); }
1317 struct _SubMdspanMapping<_LayoutSide::__left, true>
1319 using _Layout = layout_left;
1320 template<
size_t _Pad>
using _PaddedLayout = layout_left_padded<_Pad>;
1322 template<
typename _Mapping,
size_t _Us>
1323 static consteval size_t
1326 using _Extents =
typename _Mapping::extents_type;
1327 constexpr auto __sta_exts
1328 = __mdspan::__static_extents<_Extents>(1, _Us);
1329 constexpr auto __sta_padstride
1330 = __mdspan::__get_static_stride<_Mapping>();
1331 if constexpr (__sta_padstride == dynamic_extent
1332 || !__mdspan::__all_static(__sta_exts))
1333 return dynamic_extent;
1335 return __sta_padstride * __mdspan::__fwd_prod(__sta_exts);
1338 template<
size_t _Nm>
1339 static consteval bool
1340 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
1343 if (__sub_rank == 1)
1344 return __slice_kinds[0] == _SliceKind::__unit_strided_slice
1345 || __slice_kinds[0] == _SliceKind::__full;
1352 struct _SubMdspanMapping<_LayoutSide::__right, false>
1354 using _Layout = layout_right;
1355 template<
size_t _Pad>
using _PaddedLayout = layout_right_padded<_Pad>;
1357 template<
typename _Mapping,
size_t _Us>
1358 static consteval size_t
1361 using _Extents =
typename _Mapping::extents_type;
1362 constexpr auto __rank = _Extents::rank();
1363 constexpr auto __sta_exts
1364 = __mdspan::__static_extents<_Extents>(_Us + 1, __rank);
1365 if constexpr (!__mdspan::__all_static(__sta_exts))
1366 return dynamic_extent;
1368 return __fwd_prod(__sta_exts);
1371 template<
size_t _Nm>
1372 static consteval bool
1373 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
1376 auto __rev_slice_kinds = array<_SliceKind, _Nm>{};
1377 for(
size_t __i = 0; __i < _Nm; ++__i)
1378 __rev_slice_kinds[__i] = __slice_kinds[_Nm - 1 - __i];
1379 return __mdspan::__is_block(span(__rev_slice_kinds), __sub_rank);
1384 struct _SubMdspanMapping<_LayoutSide::__right, true>
1386 using _Layout = layout_right;
1387 template<
size_t _Pad>
using _PaddedLayout = layout_right_padded<_Pad>;
1389 template<
typename _Mapping,
size_t _Us>
1390 static consteval size_t
1393 using _Extents =
typename _Mapping::extents_type;
1394 constexpr auto __rank = _Extents::rank();
1395 constexpr auto __sta_exts
1396 = __mdspan::__static_extents<_Extents>(_Us + 1, __rank - 1);
1397 constexpr auto __sta_padstride
1398 = __mdspan::__get_static_stride<_Mapping>();
1399 if constexpr (__sta_padstride == dynamic_extent
1400 || !__mdspan::__all_static(__sta_exts))
1401 return dynamic_extent;
1403 return __sta_padstride * __mdspan::__fwd_prod(__sta_exts);
1406 template<
size_t _Nm>
1407 static consteval bool
1408 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
1411 if (__sub_rank == 1)
1412 return __slice_kinds[_Nm - 1] == _SliceKind::__unit_strided_slice
1413 || __slice_kinds[_Nm - 1] == _SliceKind::__full;
1420 template<
typename _Mapping>
1422 __submdspan_mapping_impl(
const _Mapping& __mapping)
1423 {
return submdspan_mapping_result{__mapping, 0}; }
1425 template<
typename _Mapping,
typename... _Slices>
1426 requires (
sizeof...(_Slices) > 0)
1428 __submdspan_mapping_impl(
const _Mapping& __mapping, _Slices... __slices)
1430 using _IndexType =
typename _Mapping::index_type;
1431 static_assert((__acceptable_slice_type<_Slices, _IndexType> && ...));
1433 constexpr auto __side = __mdspan::__mapping_side<_Mapping>();
1434 constexpr auto __rank =
sizeof...(_Slices);
1435 using _Trait = _SubMdspanMapping<__side, __is_padded_mapping<_Mapping>>;
1436 using _SliceView = span<const _SliceKind, __rank>;
1438 constexpr auto __slice_kinds = __mdspan::__make_slice_kind_array<_Slices...>();
1439 auto __offset = __mdspan::__suboffset(__mapping, __slices...);
1440 auto __sub_exts = __mdspan::__subextents(__mapping.extents(), __slices...);
1441 using _SubExts =
decltype(__sub_exts);
1442 constexpr auto __sub_rank = _SubExts::rank();
1443 if constexpr (__sub_rank == 0)
1444 return submdspan_mapping_result{
1445 typename _Trait::_Layout::mapping(__sub_exts), __offset};
1446 else if constexpr (_Trait::_S_is_unpadded_submdspan(
1447 _SliceView(__slice_kinds), __sub_rank))
1448 return submdspan_mapping_result{
1449 typename _Trait::_Layout::mapping(__sub_exts), __offset};
1451 constexpr auto __u = __padded_block_begin<__side>(
1452 _SliceView(__slice_kinds), __sub_rank);
1453 __u != dynamic_extent)
1455 constexpr auto __pad = _Trait::template _S_pad<_Mapping, __u>();
1456 using _Layout =
typename _Trait::template _PaddedLayout<__pad>;
1457 return submdspan_mapping_result{
1458 typename _Layout::mapping(__sub_exts, __mapping.stride(__u)),
1464 = __mdspan::__substrides<_SubExts>(__mapping, __slices...);
1465 return submdspan_mapping_result{
1466 layout_stride::mapping(__sub_exts, __sub_strides), __offset};
1472 template<
typename _Extents>
1473 class layout_left::mapping
1476 using extents_type = _Extents;
1477 using index_type =
typename extents_type::index_type;
1478 using size_type =
typename extents_type::size_type;
1479 using rank_type =
typename extents_type::rank_type;
1480 using layout_type = layout_left;
1482 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1483 "The size of extents_type must be representable as index_type");
1486 mapping() noexcept = default;
1489 mapping(const mapping&) noexcept = default;
1492 mapping(const extents_type& __extents) noexcept
1493 : _M_extents(__extents)
1494 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
1496 template<
typename _OExtents>
1497 requires is_constructible_v<extents_type, _OExtents>
1498 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1499 mapping(
const mapping<_OExtents>& __other) noexcept
1500 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1503 template<
typename _OExtents>
1504 requires (extents_type::rank() <= 1)
1505 && is_constructible_v<extents_type, _OExtents>
1506 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1507 mapping(
const layout_right::mapping<_OExtents>& __other) noexcept
1508 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1512 template<
typename _OExtents>
1513 requires is_constructible_v<extents_type, _OExtents>
1514 constexpr explicit(!(extents_type::rank() == 0
1515 && is_convertible_v<_OExtents, extents_type>))
1516 mapping(
const layout_stride::mapping<_OExtents>& __other) noexcept
1517 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1518 { __glibcxx_assert(*
this == __other); }
1520#if __glibcxx_padded_layouts
1521 template<
typename _LeftpadMapping>
1522 requires __mdspan::__is_left_padded_mapping<_LeftpadMapping>
1523 && is_constructible_v<extents_type,
1524 typename _LeftpadMapping::extents_type>
1526 explicit(!is_convertible_v<
typename _LeftpadMapping::extents_type,
1528 mapping(
const _LeftpadMapping& __other) noexcept
1529 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1531 constexpr size_t __ostride_sta
1532 = __mdspan::__get_static_stride<_LeftpadMapping>();
1534 if constexpr (extents_type::rank() > 1)
1536 if constexpr (extents_type::static_extent(0) != dynamic_extent
1537 && __ostride_sta != dynamic_extent)
1538 static_assert(extents_type::static_extent(0) == __ostride_sta);
1540 __glibcxx_assert(__other.stride(1)
1541 == __other.extents().extent(0));
1547 operator=(
const mapping&)
noexcept =
default;
1549 constexpr const extents_type&
1550 extents() const noexcept {
return _M_extents; }
1552 constexpr index_type
1553 required_span_size() const noexcept
1554 {
return __mdspan::__size(_M_extents); }
1558 template<__mdspan::__val
id_index_type<index_type>... _Indices>
1559 requires (
sizeof...(_Indices) == extents_type::rank())
1560 constexpr index_type
1561 operator()(_Indices... __indices)
const noexcept
1563 return __mdspan::__linear_index_left(_M_extents,
1564 static_cast<index_type
>(
std::move(__indices))...);
1567 static constexpr bool
1568 is_always_unique() noexcept {
return true; }
1570 static constexpr bool
1571 is_always_exhaustive() noexcept {
return true; }
1573 static constexpr bool
1574 is_always_strided() noexcept {
return true; }
1576 static constexpr bool
1577 is_unique() noexcept {
return true; }
1579 static constexpr bool
1580 is_exhaustive() noexcept {
return true; }
1582 static constexpr bool
1583 is_strided() noexcept {
return true; }
1585 constexpr index_type
1586 stride(rank_type __i)
const noexcept
1587 requires (extents_type::rank() > 0)
1589 __glibcxx_assert(__i < extents_type::rank());
1590 return __mdspan::__fwd_prod(_M_extents, __i);
1593 template<
typename _OExtents>
1594 requires (extents_type::rank() == _OExtents::rank())
1595 friend constexpr bool
1596 operator==(
const mapping& __self,
const mapping<_OExtents>& __other)
1598 {
return __self.extents() == __other.extents(); }
1601 template<
typename _OExtents>
1603 mapping(
const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
1604 : _M_extents(__oexts)
1606 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1607 "The size of OtherExtents must be representable as index_type");
1608 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
1611#if __glibcxx_submdspan
1612 template<
typename... _Slices>
1613 requires (extents_type::rank() ==
sizeof...(_Slices))
1614 friend constexpr auto
1615 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
1616 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
1619 [[no_unique_address]] extents_type _M_extents{};
1624 template<
typename _Extents,
typename... _Indices>
1625 constexpr typename _Extents::index_type
1626 __linear_index_right(
const _Extents& __exts, _Indices... __indices)
1629 using _IndexType =
typename _Extents::index_type;
1630 array<_IndexType,
sizeof...(__indices)> __ind_arr{__indices...};
1631 _IndexType __res = 0;
1632 if constexpr (
sizeof...(__indices) > 0)
1634 _IndexType __mult = 1;
1635 auto __update = [&, __pos = __exts.rank()](_IndexType)
mutable
1638 _GLIBCXX_DEBUG_ASSERT(cmp_less(__ind_arr[__pos],
1639 __exts.extent(__pos)));
1640 __res += __ind_arr[__pos] * __mult;
1641 __mult *= __exts.extent(__pos);
1643 (__update(__indices), ...);
1649 template<
typename _Extents>
1650 class layout_right::mapping
1653 using extents_type = _Extents;
1654 using index_type =
typename extents_type::index_type;
1655 using size_type =
typename extents_type::size_type;
1656 using rank_type =
typename extents_type::rank_type;
1657 using layout_type = layout_right;
1659 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1660 "The size of extents_type must be representable as index_type");
1663 mapping() noexcept = default;
1666 mapping(const mapping&) noexcept = default;
1669 mapping(const extents_type& __extents) noexcept
1670 : _M_extents(__extents)
1671 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
1673 template<
typename _OExtents>
1674 requires is_constructible_v<extents_type, _OExtents>
1675 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1676 mapping(
const mapping<_OExtents>& __other) noexcept
1677 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1680 template<
typename _OExtents>
1681 requires (extents_type::rank() <= 1)
1682 && is_constructible_v<extents_type, _OExtents>
1683 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1684 mapping(
const layout_left::mapping<_OExtents>& __other) noexcept
1685 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1688 template<
typename _OExtents>
1689 requires is_constructible_v<extents_type, _OExtents>
1690 constexpr explicit(!(extents_type::rank() == 0
1691 && is_convertible_v<_OExtents, extents_type>))
1692 mapping(
const layout_stride::mapping<_OExtents>& __other) noexcept
1693 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1694 { __glibcxx_assert(*
this == __other); }
1696#if __glibcxx_padded_layouts
1697 template<
typename _RightPaddedMapping>
1698 requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
1699 && is_constructible_v<extents_type,
1700 typename _RightPaddedMapping::extents_type>
1702 explicit(!is_convertible_v<
typename _RightPaddedMapping::extents_type,
1704 mapping(
const _RightPaddedMapping& __other) noexcept
1705 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1707 constexpr size_t __rank = extents_type::rank();
1708 constexpr size_t __ostride_sta
1709 = __mdspan::__get_static_stride<_RightPaddedMapping>();
1711 if constexpr (__rank > 1)
1713 if constexpr (extents_type::static_extent(__rank - 1) != dynamic_extent
1714 && __ostride_sta != dynamic_extent)
1715 static_assert(extents_type::static_extent(__rank - 1)
1718 __glibcxx_assert(__other.stride(__rank - 2)
1719 == __other.extents().extent(__rank - 1));
1725 operator=(
const mapping&)
noexcept =
default;
1727 constexpr const extents_type&
1728 extents() const noexcept {
return _M_extents; }
1730 constexpr index_type
1731 required_span_size() const noexcept
1732 {
return __mdspan::__size(_M_extents); }
1736 template<__mdspan::__val
id_index_type<index_type>... _Indices>
1737 requires (
sizeof...(_Indices) == extents_type::rank())
1738 constexpr index_type
1739 operator()(_Indices... __indices)
const noexcept
1741 return __mdspan::__linear_index_right(
1742 _M_extents,
static_cast<index_type
>(
std::move(__indices))...);
1745 static constexpr bool
1746 is_always_unique() noexcept
1749 static constexpr bool
1750 is_always_exhaustive() noexcept
1753 static constexpr bool
1754 is_always_strided() noexcept
1757 static constexpr bool
1758 is_unique() noexcept
1761 static constexpr bool
1762 is_exhaustive() noexcept
1765 static constexpr bool
1766 is_strided() noexcept
1769 constexpr index_type
1770 stride(rank_type __i)
const noexcept
1771 requires (extents_type::rank() > 0)
1773 __glibcxx_assert(__i < extents_type::rank());
1774 return __mdspan::__rev_prod(_M_extents, __i);
1777 template<
typename _OExtents>
1778 requires (extents_type::rank() == _OExtents::rank())
1779 friend constexpr bool
1780 operator==(
const mapping& __self,
const mapping<_OExtents>& __other)
1782 {
return __self.extents() == __other.extents(); }
1785 template<
typename _OExtents>
1787 mapping(
const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
1788 : _M_extents(__oexts)
1790 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1791 "The size of OtherExtents must be representable as index_type");
1792 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
1795#if __glibcxx_submdspan
1796 template<
typename... _Slices>
1797 requires (extents_type::rank() ==
sizeof...(_Slices))
1798 friend constexpr auto
1799 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
1800 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
1803 [[no_unique_address]] extents_type _M_extents{};
1808 template<
typename _Mp>
1809 concept __mapping_alike =
requires
1811 requires __is_extents<typename _Mp::extents_type>;
1812 { _Mp::is_always_strided() } -> same_as<bool>;
1813 { _Mp::is_always_exhaustive() } -> same_as<bool>;
1814 { _Mp::is_always_unique() } -> same_as<bool>;
1815 bool_constant<_Mp::is_always_strided()>::value;
1816 bool_constant<_Mp::is_always_exhaustive()>::value;
1817 bool_constant<_Mp::is_always_unique()>::value;
1820 template<
typename _Mapping,
typename... _Indices>
1821 constexpr typename _Mapping::index_type
1822 __linear_index_strides(
const _Mapping& __m, _Indices... __indices)
1825 using _IndexType =
typename _Mapping::index_type;
1826 _IndexType __res = 0;
1827 if constexpr (
sizeof...(__indices) > 0)
1829 auto __update = [&, __pos = 0u](_IndexType __idx)
mutable
1831 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx,
1832 __m.extents().extent(__pos)));
1833 __res += __idx * __m.stride(__pos++);
1835 (__update(__indices), ...);
1841 template<
typename _Extents>
1842 class layout_stride::mapping
1845 using extents_type = _Extents;
1846 using index_type =
typename extents_type::index_type;
1847 using size_type =
typename extents_type::size_type;
1848 using rank_type =
typename extents_type::rank_type;
1849 using layout_type = layout_stride;
1851 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1852 "The size of extents_type must be representable as index_type");
1859 size_t __stride = 1;
1860 for (
size_t __i = extents_type::rank(); __i > 0; --__i)
1862 _M_strides[__i - 1] = index_type(__stride);
1863 __stride *= size_t(_M_extents.extent(__i - 1));
1868 mapping(
const mapping&)
noexcept =
default;
1870 template<
typename _OIndexType>
1871 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1873 mapping(
const extents_type& __exts,
1874 span<_OIndexType, extents_type::rank()> __strides) noexcept
1875 : _M_extents(__exts)
1877 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1878 _M_strides[__i] = index_type(as_const(__strides[__i]));
1881 template<
typename _OIndexType>
1882 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1884 mapping(
const extents_type& __exts,
1885 const array<_OIndexType, extents_type::rank()>& __strides)
1888 span<
const _OIndexType, extents_type::rank()>(__strides))
1891 template<__mdspan::__mapping_alike _Str
idedMapping>
1892 requires (is_constructible_v<extents_type,
1893 typename _StridedMapping::extents_type>
1894 && _StridedMapping::is_always_unique()
1895 && _StridedMapping::is_always_strided())
1896 constexpr explicit(!(
1897 is_convertible_v<typename _StridedMapping::extents_type, extents_type>
1898 && __mdspan::__standardized_mapping<_StridedMapping>))
1899 mapping(
const _StridedMapping& __other) noexcept
1900 : _M_extents(__other.extents())
1902 using _OIndexType = _StridedMapping::index_type;
1903 using _OExtents = _StridedMapping::extents_type;
1905 __glibcxx_assert(__mdspan::__offset(__other) == 0);
1906 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1907 "The size of StridedMapping::extents_type must be representable as"
1909 if constexpr (cmp_greater(__gnu_cxx::__int_traits<_OIndexType>::__max,
1910 __gnu_cxx::__int_traits<index_type>::__max))
1911 __glibcxx_assert(!cmp_less(
1912 __gnu_cxx::__int_traits<index_type>::__max,
1913 __other.required_span_size())
1914 &&
"other.required_span_size() must be representable"
1916 if constexpr (extents_type::rank() > 0)
1917 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1918 _M_strides[__i] = index_type(__other.stride(__i));
1922 operator=(
const mapping&)
noexcept =
default;
1924 constexpr const extents_type&
1925 extents() const noexcept {
return _M_extents; }
1927 constexpr array<index_type, extents_type::rank()>
1928 strides() const noexcept
1930 array<index_type, extents_type::rank()> __ret;
1931 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1932 __ret[__i] = _M_strides[__i];
1936 constexpr index_type
1937 required_span_size() const noexcept
1939 if (__mdspan::__empty(_M_extents))
1942 index_type __ret = 1;
1943 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1944 __ret += (_M_extents.extent(__i) - 1) * _M_strides[__i];
1950 template<__mdspan::__val
id_index_type<index_type>... _Indices>
1951 requires (
sizeof...(_Indices) == extents_type::rank())
1952 constexpr index_type
1953 operator()(_Indices... __indices)
const noexcept
1955 return __mdspan::__linear_index_strides(*
this,
1956 static_cast<index_type
>(
std::move(__indices))...);
1959 static constexpr bool
1960 is_always_unique() noexcept {
return true; }
1964 static constexpr bool
1965 is_always_exhaustive() noexcept
1967 return (_Extents::rank() == 0) || __mdspan::__contains_zero(
1968 __mdspan::__static_extents<extents_type>());
1971 static constexpr bool
1972 is_always_strided() noexcept {
return true; }
1974 static constexpr bool
1975 is_unique() noexcept {
return true; }
1980 is_exhaustive() const noexcept
1982 if constexpr (!is_always_exhaustive())
1984 auto __size = __mdspan::__size(_M_extents);
1986 return __size == required_span_size();
1991 static constexpr bool
1992 is_strided() noexcept {
return true; }
1994 constexpr index_type
1995 stride(rank_type __r)
const noexcept {
return _M_strides[__r]; }
1997 template<__mdspan::__mapping_alike _OMapping>
1998 requires ((extents_type::rank() == _OMapping::extents_type::rank())
1999 && _OMapping::is_always_strided())
2000 friend constexpr bool
2001 operator==(
const mapping& __self,
const _OMapping& __other)
noexcept
2003 if (__self.extents() != __other.extents())
2005 if constexpr (extents_type::rank() > 0)
2006 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
2007 if (!cmp_equal(__self.stride(__i), __other.stride(__i)))
2009 return __mdspan::__offset(__other) == 0;
2013#if __glibcxx_submdspan
2014 template<
typename... _Slices>
2015 requires (extents_type::rank() ==
sizeof...(_Slices))
2016 friend constexpr auto
2017 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
2019 if constexpr (
sizeof...(_Slices) == 0)
2020 return submdspan_mapping_result{__mapping, 0};
2023 auto __offset = __mdspan::__suboffset(__mapping, __slices...);
2024 auto __sub_exts = __mdspan::__subextents(__mapping.extents(), __slices...);
2026 = __mdspan::__substrides<decltype(__sub_exts)>(__mapping, __slices...);
2027 return submdspan_mapping_result{
2028 layout_stride::mapping(__sub_exts, __sub_strides), __offset};
2033 using _Strides =
typename __array_traits<index_type,
2034 extents_type::rank()>::_Type;
2035 [[no_unique_address]] extents_type _M_extents;
2036 [[no_unique_address]] _Strides _M_strides;
2039#ifdef __glibcxx_padded_layouts
2043 __least_multiple(
size_t __x,
size_t __y)
2047 return (__y / __x + (__y % __x != 0)) * __x ;
2050 template<
typename _IndexType>
2052 __is_representable_least_multiple(
size_t __x,
size_t __y)
2054 constexpr auto __y_max = __gnu_cxx::__int_traits<_IndexType>::__max;
2055 if(std::cmp_greater(__y, __y_max))
2061 auto __max_delta = __y_max -
static_cast<_IndexType
>(__y);
2062 auto __y_mod_x = __y % __x;
2063 auto __delta = (__y_mod_x == 0) ?
size_t(0) : (__x - __y_mod_x);
2064 return std::cmp_less_equal(__delta, __max_delta);
2067 template<
typename _Extents,
size_t _PaddingValue,
typename _LayoutTraits,
2068 size_t _Rank = _Extents::rank()>
2069 concept __valid_static_stride = (_Extents::rank() <= 1)
2070 || (_PaddingValue == dynamic_extent)
2071 || (_Extents::static_extent(_LayoutTraits::_S_ext_idx) == dynamic_extent)
2072 || (__is_representable_least_multiple<size_t>(
2073 _PaddingValue, _Extents::static_extent(_LayoutTraits::_S_ext_idx)));
2075 template<
size_t _PaddedStride,
typename _Extents,
2076 typename _LayoutTraits>
2078 __is_representable_padded_size()
2080 using _IndexType =
typename _Extents::index_type;
2081 auto __sta_exts = __static_extents<_Extents>(
2082 _LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
2083 size_t __max = __gnu_cxx::__int_traits<_IndexType>::__max;
2084 return __static_quotient(__sta_exts, __max / _PaddedStride) != 0;
2087 template<
typename _Extents,
size_t _PaddedStride,
typename _LayoutTraits,
2088 size_t _Rank = _Extents::rank()>
2089 concept __valid_padded_size = (_Rank <= 1)
2090 || (_PaddedStride == dynamic_extent)
2091 || (!__all_static(__static_extents<_Extents>()))
2092 || (__contains_zero(__static_extents<_Extents>()))
2093 || (__is_representable_padded_size<_PaddedStride, _Extents,
2096 template<
typename _Extents,
typename _Stride,
typename... _Indices>
2097 constexpr typename _Extents::index_type
2098 __linear_index_leftpad(
const _Extents& __exts, _Stride __stride,
2099 _Indices... __indices)
2102 using _IndexType =
typename _Extents::index_type;
2103 _IndexType __res = 0;
2104 if constexpr (
sizeof...(__indices) > 0)
2106 _IndexType __mult = 1;
2108 auto __update_rest = [&, __pos = 1u](_IndexType __idx)
mutable
2110 __res += __idx * __mult;
2111 __mult *= __exts.extent(__pos);
2115 auto __update = [&](_IndexType __idx,
auto... __rest)
2118 __mult = __stride.extent(0);
2119 (__update_rest(__rest), ...);
2121 __update(__indices...);
2126 template<
typename _Extents,
typename _Stride,
typename... _Indices>
2127 constexpr typename _Extents::index_type
2128 __linear_index_rightpad(
const _Extents& __exts, _Stride __stride,
2129 _Indices... __indices)
2132 using _IndexType =
typename _Extents::index_type;
2133 _IndexType __res = 0;
2134 if constexpr (
sizeof...(__indices) > 0)
2136 _IndexType __mult = 1;
2137 array<_IndexType,
sizeof...(__indices)> __ind_arr{__indices...};
2139 auto __update_rest = [&, __pos = __exts.rank()-1](_IndexType)
mutable
2142 __res += __ind_arr[__pos] * __mult;
2143 __mult *= __exts.extent(__pos);
2146 auto __update = [&](_IndexType,
auto... __rest)
2148 __res += __ind_arr[__exts.rank() - 1];
2149 __mult = __stride.extent(0);
2150 (__update_rest(__rest), ...);
2152 __update(__indices...);
2157 template<
size_t _Rank>
2158 struct _LeftPaddedLayoutTraits
2160 using _LayoutSame = layout_left;
2161 using _LayoutOther = layout_right;
2163 constexpr static const size_t _S_ext_idx = 0;
2164 constexpr static const size_t _S_stride_idx = 1;
2165 constexpr static const size_t _S_unpad_begin = 1;
2166 constexpr static const size_t _S_unpad_end = _Rank;
2168 template<
typename _IndexType,
size_t _StaticStride,
size_t..._Extents>
2169 constexpr static auto
2170 _S_make_padded_extent(
2171 extents<_IndexType, _StaticStride> __stride,
2172 const extents<_IndexType, _Extents...>& __exts)
2174 auto __impl = [&]<
size_t... _Is>(integer_sequence<size_t, _Is...>)
2176 return extents<_IndexType, _StaticStride,
2177 (_Extents...[_Is + 1])...>{
2178 __stride.extent(0), __exts.extent(_Is + 1)...};
2180 return __impl(make_index_sequence<
sizeof...(_Extents) - 1>());
2184 template<
size_t _Rank>
2185 struct _RightPaddedLayoutTraits
2187 using _LayoutSame = layout_right;
2188 using _LayoutOther = layout_left;
2190 constexpr static size_t _S_ext_idx = _Rank - 1;
2191 constexpr static size_t _S_stride_idx = _Rank - 2;
2192 constexpr static size_t _S_unpad_begin = 0;
2193 constexpr static size_t _S_unpad_end = _Rank - 1;
2195 template<
typename _IndexType,
size_t _StaticStride,
size_t..._Extents>
2196 constexpr static auto
2197 _S_make_padded_extent(
2198 extents<_IndexType, _StaticStride> __stride,
2199 const extents<_IndexType, _Extents...>& __exts)
2201 auto __impl = [&]<
size_t... _Is>(integer_sequence<size_t, _Is...>)
2203 return extents<_IndexType, (_Extents...[_Is])..., _StaticStride>{
2204 __exts.extent(_Is)..., __stride.extent(0)};
2206 return __impl(make_index_sequence<
sizeof...(_Extents) - 1>());
2210 template<
size_t _PaddingValue,
typename _Extents,
typename _LayoutTraits>
2211 class _PaddedStorage
2213 using _LayoutSame =
typename _LayoutTraits::_LayoutSame;
2216 using _IndexType =
typename _Extents::index_type;
2217 constexpr static size_t _S_rank = _Extents::rank();
2221 static_assert((_PaddingValue == dynamic_extent)
2222 || (cmp_less_equal(_PaddingValue,
2223 __gnu_cxx::__int_traits<_IndexType>::__max)),
2224 "padding_value must be representable as index_type");
2226 static_assert(__representable_size<_Extents, _IndexType>,
2227 "The size of extents_type must be representable as index_type");
2229 static_assert(__valid_static_stride<_Extents, _PaddingValue,
2231 "The padded stride must be representable as size_t");
2233 static constexpr size_t _S_static_stride = []
consteval
2235 constexpr size_t __rank = _Extents::rank();
2236 if constexpr (__rank <= 1)
2240 constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
2241 constexpr size_t __sta_ext = _Extents::static_extent(__ext_idx);
2242 if constexpr (__sta_ext == 0)
2244 else if constexpr (_PaddingValue == dynamic_extent
2245 || __sta_ext == dynamic_extent)
2246 return dynamic_extent;
2248 return __least_multiple(_PaddingValue, __sta_ext);
2252 static_assert(_S_static_stride == dynamic_extent
2253 || cmp_less_equal(_S_static_stride,
2254 __gnu_cxx::__int_traits<_IndexType>::__max),
2255 "Padded stride must be representable as index_type");
2257 static_assert(__valid_padded_size<_Extents, _S_static_stride,
2261 _PaddedStorage() noexcept
2263 if constexpr (_S_rank > 1)
2264 if constexpr (_S_static_stride == dynamic_extent
2265 && _S_static_padextent() != dynamic_extent)
2266 _M_stride = _Stride{_S_static_padextent()};
2270 _PaddedStorage(
const _Extents& __exts)
2271 : _M_extents(__exts)
2273 if constexpr (!__all_static(__static_extents<_Extents>()))
2274 __glibcxx_assert(__is_representable_extents(_M_extents));
2276 if constexpr (_S_rank > 1)
2278 _IndexType __stride;
2279 if constexpr (_PaddingValue == dynamic_extent)
2280 __stride = _M_padextent();
2281 else if constexpr (_S_static_padextent() != dynamic_extent)
2286 __is_representable_least_multiple<_IndexType>(
2287 _PaddingValue, _M_padextent()));
2289 __stride =
static_cast<_IndexType
>(
2290 __least_multiple(_PaddingValue, _M_padextent()));
2292 __glibcxx_assert(__is_representable_extents(
2293 _LayoutTraits::_S_make_padded_extent(
2294 std::dextents<_IndexType, 1>{__stride},
2297 _M_stride = _Stride{__stride};
2302 _PaddedStorage(
const _Extents& __exts, _IndexType __pad)
2303 : _M_extents(__exts)
2305 if constexpr (_PaddingValue != dynamic_extent)
2306 __glibcxx_assert(cmp_equal(_PaddingValue, __pad));
2307 if constexpr (_S_rank > 1 && _S_static_stride == dynamic_extent)
2310 __is_representable_least_multiple<_IndexType>(
2311 __pad, _M_padextent()));
2313 _M_stride = _Stride{
static_cast<_IndexType
>(
2314 __least_multiple(__pad, _M_padextent()))};
2316 __glibcxx_assert(__is_representable_extents(
2317 _LayoutTraits::_S_make_padded_extent(
2318 _M_stride, _M_extents)));
2322 template<
typename _OExtents>
2325 const typename _LayoutSame::template mapping<_OExtents>& __other)
2326 : _PaddedStorage(_Extents(__other.extents()))
2328 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
2329 constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
2330 if constexpr (_S_rank > 1 && _PaddingValue != dynamic_extent)
2332 static_assert(_S_static_stride == dynamic_extent
2333 || _OExtents::static_extent(__ext_idx) == dynamic_extent
2334 || _S_static_stride == _OExtents::static_extent(__ext_idx),
2335 "The padded stride must be compatible with other");
2337 if constexpr (_S_static_stride == dynamic_extent
2338 || _OExtents::static_extent(__stride_idx) == dynamic_extent)
2339 __glibcxx_assert(std::cmp_equal(_M_padstride(),
2344 template<
typename _OExtents>
2346 _PaddedStorage(
const typename layout_stride::mapping<_OExtents>&
2348 : _M_extents(__other.extents())
2350 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
2354 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
2355 if constexpr (_S_rank > 1)
2357 if constexpr (_PaddingValue != dynamic_extent)
2358 __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
2359 _M_calc_padstride())
2360 &&
"The padded stride must be compatible with other");
2361 if constexpr (_S_static_stride == dynamic_extent)
2362 _M_stride = _Stride{__other.stride(__stride_idx)};
2366 template<
typename _SamePaddedMapping>
2368 _PaddedStorage(_LayoutTraits::_LayoutSame,
2369 const _SamePaddedMapping& __other)
2370 : _M_extents(__other.extents())
2372 if constexpr (_S_rank > 1)
2374 static_assert(_PaddingValue == dynamic_extent
2375 || _SamePaddedMapping::padding_value == dynamic_extent
2376 || _PaddingValue == _SamePaddedMapping::padding_value,
2377 "If neither PaddingValue is dynamic_extent, then they must "
2380 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
2381 if constexpr (_PaddingValue != dynamic_extent)
2382 __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
2383 _M_calc_padstride())
2384 &&
"The padded stride must be compatible with other");
2385 if constexpr (_S_static_stride == dynamic_extent)
2386 _M_stride = _Stride{__other.stride(__stride_idx)};
2388 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
2389 __gnu_cxx::__int_traits<_IndexType>::__max));
2392 template<
typename _OtherPaddedMapping>
2394 _PaddedStorage(_LayoutTraits::_LayoutOther,
2395 const _OtherPaddedMapping& __other) noexcept
2396 : _M_extents(__other.extents())
2398 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
2399 __gnu_cxx::__int_traits<_IndexType>::__max));
2402 static constexpr bool
2403 _M_is_always_exhaustive() noexcept
2405 if constexpr (_S_rank <= 1)
2408 return _S_static_padextent() != dynamic_extent
2409 && _S_static_stride != dynamic_extent
2410 && _S_static_padextent() == _S_static_stride;
2414 _M_is_exhaustive() const noexcept
2416 if constexpr (_M_is_always_exhaustive())
2419 return cmp_equal(_M_padextent(), _M_padstride());
2422 constexpr static size_t
2423 _S_static_padextent() noexcept
2424 {
return _Extents::static_extent(_LayoutTraits::_S_ext_idx); }
2426 constexpr _IndexType
2427 _M_padextent() const noexcept
2428 {
return _M_extents.extent(_LayoutTraits::_S_ext_idx); }
2430 constexpr _IndexType
2431 _M_calc_padstride() const noexcept
2433 if constexpr (_S_static_stride != dynamic_extent)
2434 return _S_static_stride;
2435 else if constexpr (_PaddingValue != dynamic_extent)
2436 return __least_multiple(_PaddingValue, _M_padextent());
2438 return _M_padextent();
2441 constexpr _IndexType
2442 _M_padstride() const noexcept
2443 {
return _M_stride.extent(0); }
2445 constexpr _IndexType
2446 _M_required_span_size() const noexcept
2448 if constexpr (_S_rank == 0)
2450 else if (__mdspan::__empty(_M_extents))
2454 size_t __stride =
static_cast<size_t>(_M_padstride());
2455 size_t __prod_rest = __mdspan::__fwd_prod(_M_extents,
2456 _LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
2457 size_t __delta = _M_padstride() - _M_padextent();
2458 return static_cast<_IndexType
>(__stride * __prod_rest - __delta);
2462 template<
typename _SamePaddedMapping>
2464 _M_equal(
const _SamePaddedMapping& __other)
const noexcept
2466 return _M_extents == __other.extents()
2468 || cmp_equal(_M_stride.extent(0),
2469 __other.stride(_LayoutTraits::_S_stride_idx)));
2472 using _Stride = std::extents<_IndexType, _S_static_stride>;
2473 [[no_unique_address]] _Stride _M_stride;
2474 [[no_unique_address]] _Extents _M_extents;
2478 template<
size_t _PaddingValue>
2479 template<
typename _Extents>
2480 class layout_left_padded<_PaddingValue>::mapping
2483 static constexpr size_t padding_value = _PaddingValue;
2485 using extents_type = _Extents;
2486 using index_type =
typename extents_type::index_type;
2487 using size_type =
typename extents_type::size_type;
2488 using rank_type =
typename extents_type::rank_type;
2489 using layout_type = layout_left_padded<padding_value>;
2492 static constexpr size_t _S_rank = extents_type::rank();
2493 using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
2494 _Extents, __mdspan::_LeftPaddedLayoutTraits<_S_rank>>;
2495 [[no_unique_address]] _PaddedStorage _M_storage;
2497 consteval friend size_t
2498 __mdspan::__get_static_stride<mapping>();
2500 constexpr index_type
2501 _M_extent(
size_t __r)
const noexcept
2502 {
return _M_storage._M_extents.extent(__r); }
2504 constexpr index_type
2505 _M_padstride() const noexcept
2506 {
return _M_storage._M_stride.extent(0); }
2514 mapping(
const mapping&)
noexcept =
default;
2517 mapping(
const extents_type& __exts)
2518 : _M_storage(__exts)
2521 template<__mdspan::__val
id_index_type<index_type> _OIndexType>
2523 mapping(
const extents_type& __exts, _OIndexType __pad)
2524 : _M_storage(__exts,
2525 __mdspan::__index_type_cast<index_type>(std::move(__pad)))
2528 template<
typename _OExtents>
2529 requires is_constructible_v<extents_type, _OExtents>
2530 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
2531 mapping(
const layout_left::mapping<_OExtents>& __other)
2532 : _M_storage(__other)
2535 template<
typename _OExtents>
2536 requires is_constructible_v<_OExtents, extents_type>
2537 constexpr explicit(!(_OExtents::rank() == 0
2538 && is_convertible_v<_OExtents, extents_type>))
2539 mapping(
const typename layout_stride::mapping<_OExtents>& __other)
2540 : _M_storage(__other)
2541 { __glibcxx_assert(*
this == __other); }
2543 template<
typename _LeftPaddedMapping>
2544 requires __mdspan::__is_left_padded_mapping<_LeftPaddedMapping>
2545 && is_constructible_v<extents_type,
2546 typename _LeftPaddedMapping::extents_type>
2548 !is_convertible_v<
typename _LeftPaddedMapping::extents_type,
2550 || _S_rank > 1 && (padding_value != dynamic_extent
2551 || _LeftPaddedMapping::padding_value == dynamic_extent))
2552 mapping(
const _LeftPaddedMapping& __other)
2553 : _M_storage(layout_left{}, __other)
2556 template<
typename _RightPaddedMapping>
2557 requires (__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2558 || __mdspan::__mapping_of<layout_right, _RightPaddedMapping>)
2560 && is_constructible_v<extents_type,
2561 typename _RightPaddedMapping::extents_type>
2562 constexpr explicit(!is_convertible_v<
2563 typename _RightPaddedMapping::extents_type, extents_type>)
2564 mapping(
const _RightPaddedMapping& __other) noexcept
2565 : _M_storage(layout_right{}, __other)
2569 operator=(
const mapping&)
noexcept =
default;
2571 constexpr const extents_type&
2572 extents() const noexcept {
return _M_storage._M_extents; }
2574 constexpr array<index_type, _S_rank>
2575 strides() const noexcept
2577 array<index_type, _S_rank> __ret;
2578 if constexpr (_S_rank > 0)
2580 if constexpr (_S_rank > 1)
2581 __ret[1] = _M_padstride();
2582 if constexpr (_S_rank > 2)
2583 for(
size_t __i = 2; __i < _S_rank; ++__i)
2584 __ret[__i] = __ret[__i - 1] * _M_extent(__i - 1);
2588 constexpr index_type
2589 required_span_size() const noexcept
2590 {
return _M_storage._M_required_span_size(); }
2594 template<__mdspan::__val
id_index_type<index_type>... _Indices>
2595 requires (
sizeof...(_Indices) == _S_rank)
2596 constexpr index_type
2597 operator()(_Indices... __indices)
const noexcept
2599 return __mdspan::__linear_index_leftpad(
2600 extents(), _M_storage._M_stride,
2601 static_cast<index_type
>(
std::move(__indices))...);
2604 static constexpr bool
2605 is_always_exhaustive() noexcept
2606 {
return _PaddedStorage::_M_is_always_exhaustive(); }
2609 is_exhaustive() noexcept
2610 {
return _M_storage._M_is_exhaustive(); }
2612 static constexpr bool
2613 is_always_unique() noexcept {
return true; }
2615 static constexpr bool
2616 is_unique() noexcept {
return true; }
2618 static constexpr bool
2619 is_always_strided() noexcept {
return true; }
2621 static constexpr bool
2622 is_strided() noexcept {
return true; }
2624 constexpr index_type
2625 stride(rank_type __r)
const noexcept
2627 __glibcxx_assert(__r < _S_rank);
2631 return static_cast<index_type
>(
2632 static_cast<size_t>(_M_padstride()) *
2633 static_cast<size_t>(__mdspan::__fwd_prod(extents(), 1, __r)));
2636 template<
typename _LeftpadMapping>
2637 requires(__mdspan::__is_left_padded_mapping<_LeftpadMapping>
2638 && _LeftpadMapping::extents_type::rank() == _S_rank)
2639 friend constexpr bool
2640 operator==(
const mapping& __self,
const _LeftpadMapping& __other)
2642 {
return __self._M_storage._M_equal(__other); }
2645#if __glibcxx_submdspan
2646 template<
typename... _Slices>
2647 requires (extents_type::rank() ==
sizeof...(_Slices))
2648 friend constexpr auto
2649 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
2650 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
2654 template<
size_t _PaddingValue>
2655 template<
typename _Extents>
2656 class layout_right_padded<_PaddingValue>::mapping {
2658 static constexpr size_t padding_value = _PaddingValue;
2659 using extents_type = _Extents;
2660 using index_type =
typename extents_type::index_type;
2661 using size_type =
typename extents_type::size_type;
2662 using rank_type =
typename extents_type::rank_type;
2663 using layout_type = layout_right_padded<_PaddingValue>;
2666 static constexpr size_t _S_rank = extents_type::rank();
2667 using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
2668 _Extents, __mdspan::_RightPaddedLayoutTraits<_S_rank>>;
2669 [[no_unique_address]] _PaddedStorage _M_storage;
2671 consteval friend size_t
2672 __mdspan::__get_static_stride<mapping>();
2674 constexpr index_type
2675 _M_extent(
size_t __r)
const noexcept
2676 {
return _M_storage._M_extents.extent(__r); }
2678 constexpr index_type
2679 _M_padstride() const noexcept
2680 {
return _M_storage._M_stride.extent(0); }
2688 mapping(
const mapping&)
noexcept =
default;
2691 mapping(
const extents_type& __exts)
2692 : _M_storage(__exts)
2695 template<__mdspan::__val
id_index_type<index_type> _OIndexType>
2697 mapping(
const extents_type& __exts, _OIndexType __pad)
2698 : _M_storage(__exts,
2699 __mdspan::__index_type_cast<index_type>(std::move(__pad)))
2702 template<
typename _OExtents>
2703 requires is_constructible_v<extents_type, _OExtents>
2704 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
2705 mapping(
const layout_right::mapping<_OExtents>& __other)
2706 : _M_storage(__other)
2709 template<
typename _OExtents>
2710 requires is_constructible_v<_OExtents, extents_type>
2711 constexpr explicit(!(_OExtents::rank() == 0
2712 && is_convertible_v<_OExtents, extents_type>))
2713 mapping(
const typename layout_stride::mapping<_OExtents>& __other)
2714 : _M_storage(__other)
2715 { __glibcxx_assert(*
this == __other); }
2717 template<
typename _RightPaddedMapping>
2718 requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2719 && is_constructible_v<extents_type,
2720 typename _RightPaddedMapping::extents_type>
2722 !is_convertible_v<
typename _RightPaddedMapping::extents_type,
2724 || _S_rank > 1 && (padding_value != dynamic_extent
2725 || _RightPaddedMapping::padding_value == dynamic_extent))
2726 mapping(
const _RightPaddedMapping& __other)
2727 : _M_storage(layout_right{}, __other)
2730 template<
typename _LeftPaddedMapping>
2731 requires (__mdspan::__is_left_padded_mapping<_LeftPaddedMapping>
2732 || __mdspan::__mapping_of<layout_left, _LeftPaddedMapping>)
2734 && is_constructible_v<extents_type,
2735 typename _LeftPaddedMapping::extents_type>
2736 constexpr explicit(!is_convertible_v<
2737 typename _LeftPaddedMapping::extents_type, extents_type>)
2738 mapping(
const _LeftPaddedMapping& __other) noexcept
2739 : _M_storage(layout_left{}, __other)
2742 constexpr mapping& operator=(
const mapping&)
noexcept =
default;
2744 constexpr const extents_type&
2745 extents() const noexcept {
return _M_storage._M_extents; }
2747 constexpr array<index_type, _S_rank>
2748 strides() const noexcept
2750 array<index_type, _S_rank> __ret;
2751 if constexpr (_S_rank > 0)
2752 __ret[_S_rank - 1] = 1;
2753 if constexpr (_S_rank > 1)
2754 __ret[_S_rank - 2] = _M_padstride();
2755 if constexpr (_S_rank > 2)
2756 for(
size_t __i = _S_rank - 2; __i > 0; --__i)
2757 __ret[__i - 1] = __ret[__i] * _M_extent(__i);
2761 constexpr index_type
2762 required_span_size() const noexcept
2763 {
return _M_storage._M_required_span_size(); }
2767 template<__mdspan::__val
id_index_type<index_type>... _Indices>
2768 requires (
sizeof...(_Indices) == _S_rank)
2769 constexpr index_type
2770 operator()(_Indices... __indices)
const noexcept
2772 return __mdspan::__linear_index_rightpad(
2773 extents(), _M_storage._M_stride,
2774 static_cast<index_type
>(
std::move(__indices))...);
2777 static constexpr bool
2778 is_always_exhaustive() noexcept
2779 {
return _PaddedStorage::_M_is_always_exhaustive(); }
2782 is_exhaustive() noexcept
2783 {
return _M_storage._M_is_exhaustive(); }
2785 static constexpr bool
2786 is_always_unique() noexcept {
return true; }
2788 static constexpr bool
2789 is_unique() noexcept {
return true; }
2791 static constexpr bool
2792 is_always_strided() noexcept {
return true; }
2794 static constexpr bool
2795 is_strided() noexcept {
return true; }
2797 constexpr index_type
2798 stride(rank_type __r)
const noexcept
2800 __glibcxx_assert(__r < _S_rank);
2801 if constexpr (_S_rank <= 1)
2803 else if (__r == _S_rank - 1)
2805 else if (__r == _S_rank - 2)
2806 return _M_padstride();
2808 return static_cast<index_type
>(
2809 static_cast<size_t>(_M_padstride()) *
2810 static_cast<size_t>(__mdspan::__fwd_prod(
2811 extents(), __r + 1, _S_rank - 1)));
2814 template<
typename _RightPaddedMapping>
2815 requires(__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2816 && _RightPaddedMapping::extents_type::rank() == _S_rank)
2817 friend constexpr bool
2818 operator==(
const mapping& __self,
const _RightPaddedMapping& __other)
2820 {
return __self._M_storage._M_equal(__other); }
2822#if __glibcxx_submdspan
2824 template<
typename... _Slices>
2825 requires (extents_type::rank() ==
sizeof...(_Slices))
2826 friend constexpr auto
2827 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
2828 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
2833 template<
typename _ElementType>
2834 struct default_accessor
2836 static_assert(!is_array_v<_ElementType>,
2837 "ElementType must not be an array type");
2838 static_assert(!is_abstract_v<_ElementType>,
2839 "ElementType must not be an abstract class type");
2841 using offset_policy = default_accessor;
2842 using element_type = _ElementType;
2843 using reference = element_type&;
2844 using data_handle_type = element_type*;
2847 default_accessor() noexcept = default;
2849 template<typename _OElementType>
2850 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
2852 default_accessor(default_accessor<_OElementType>) noexcept
2856 access(data_handle_type __p,
size_t __i)
const noexcept
2857 {
return __p[__i]; }
2859 constexpr data_handle_type
2860 offset(data_handle_type __p,
size_t __i)
const noexcept
2861 {
return __p + __i; }
2864#ifdef __glibcxx_aligned_accessor
2865 template<
typename _ElementType,
size_t _ByteAlignment>
2866 struct aligned_accessor
2868 static_assert(has_single_bit(_ByteAlignment),
2869 "ByteAlignment must be a power of two");
2870 static_assert(_ByteAlignment >=
alignof(_ElementType));
2872 using offset_policy = default_accessor<_ElementType>;
2873 using element_type = _ElementType;
2874 using reference = element_type&;
2875 using data_handle_type = element_type*;
2877 static constexpr size_t byte_alignment = _ByteAlignment;
2880 aligned_accessor() noexcept = default;
2882 template<typename _OElementType,
size_t _OByteAlignment>
2883 requires (_OByteAlignment >= byte_alignment)
2884 && is_convertible_v<_OElementType(*)[], element_type(*)[]>
2886 aligned_accessor(aligned_accessor<_OElementType, _OByteAlignment>)
2890 template<
typename _OElementType>
2891 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
2893 aligned_accessor(default_accessor<_OElementType>)
noexcept
2896 template<
typename _OElementType>
2897 requires is_convertible_v<element_type(*)[], _OElementType(*)[]>
2899 operator default_accessor<_OElementType>() const noexcept
2903 access(data_handle_type __p,
size_t __i)
const noexcept
2906 constexpr typename offset_policy::data_handle_type
2907 offset(data_handle_type __p,
size_t __i)
const noexcept
2912 template<
typename _ElementType,
typename _Extents,
2913 typename _LayoutPolicy = layout_right,
2914 typename _AccessorPolicy = default_accessor<_ElementType>>
2917 static_assert(!is_array_v<_ElementType>,
2918 "ElementType must not be an array type");
2919 static_assert(!is_abstract_v<_ElementType>,
2920 "ElementType must not be an abstract class type");
2921 static_assert(__mdspan::__is_extents<_Extents>,
2922 "Extents must be a specialization of std::extents");
2923 static_assert(is_same_v<_ElementType,
2924 typename _AccessorPolicy::element_type>);
2927 using extents_type = _Extents;
2928 using layout_type = _LayoutPolicy;
2929 using accessor_type = _AccessorPolicy;
2930 using mapping_type =
typename layout_type::template mapping<extents_type>;
2931 using element_type = _ElementType;
2932 using value_type = remove_cv_t<element_type>;
2933 using index_type =
typename extents_type::index_type;
2934 using size_type =
typename extents_type::size_type;
2935 using rank_type =
typename extents_type::rank_type;
2936 using data_handle_type =
typename accessor_type::data_handle_type;
2937 using reference =
typename accessor_type::reference;
2939 static constexpr rank_type
2940 rank() noexcept {
return extents_type::rank(); }
2942 static constexpr rank_type
2943 rank_dynamic() noexcept {
return extents_type::rank_dynamic(); }
2945 static constexpr size_t
2946 static_extent(rank_type __r)
noexcept
2947 {
return extents_type::static_extent(__r); }
2949 constexpr index_type
2950 extent(rank_type __r)
const noexcept {
return extents().extent(__r); }
2954 requires (rank_dynamic() > 0)
2955 && is_default_constructible_v<data_handle_type>
2956 && is_default_constructible_v<mapping_type>
2957 && is_default_constructible_v<accessor_type> =
default;
2960 mdspan(
const mdspan& __other) =
default;
2963 mdspan(mdspan&& __other) =
default;
2965 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
2966 requires (
sizeof...(_OIndexTypes) == rank()
2967 ||
sizeof...(_OIndexTypes) == rank_dynamic())
2968 && is_constructible_v<mapping_type, extents_type>
2969 && is_default_constructible_v<accessor_type>
2971 mdspan(data_handle_type __handle, _OIndexTypes... __exts)
2973 _M_mapping(_Extents(
static_cast<index_type
>(
std::move(__exts))...)),
2977 template<
typename _OIndexType,
size_t _Nm>
2978 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
2979 && (_Nm == rank() || _Nm == rank_dynamic())
2980 && is_constructible_v<mapping_type, extents_type>
2981 && is_default_constructible_v<accessor_type>
2982 constexpr explicit(_Nm != rank_dynamic())
2983 mdspan(data_handle_type __handle, span<_OIndexType, _Nm> __exts)
2984 : _M_accessor(), _M_mapping(extents_type(__exts)),
2985 _M_handle(std::move(__handle))
2988 template<
typename _OIndexType,
size_t _Nm>
2989 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
2990 && (_Nm == rank() || _Nm == rank_dynamic())
2991 && is_constructible_v<mapping_type, extents_type>
2992 && is_default_constructible_v<accessor_type>
2993 constexpr explicit(_Nm != rank_dynamic())
2994 mdspan(data_handle_type __handle,
const array<_OIndexType, _Nm>& __exts)
2995 : _M_accessor(), _M_mapping(extents_type(__exts)),
2996 _M_handle(std::move(__handle))
3000 mdspan(data_handle_type __handle,
const extents_type& __exts)
3001 requires is_constructible_v<mapping_type, const extents_type&>
3002 && is_default_constructible_v<accessor_type>
3003 : _M_accessor(), _M_mapping(__exts), _M_handle(
std::move(__handle))
3007 mdspan(data_handle_type __handle,
const mapping_type& __mapping)
3008 requires is_default_constructible_v<accessor_type>
3009 : _M_accessor(), _M_mapping(__mapping), _M_handle(
std::move(__handle))
3013 mdspan(data_handle_type __handle,
const mapping_type& __mapping,
3014 const accessor_type& __accessor)
3015 : _M_accessor(__accessor), _M_mapping(__mapping),
3016 _M_handle(std::move(__handle))
3019 template<
typename _OElementType,
typename _OExtents,
typename _OLayout,
3020 typename _OAccessor>
3021 requires is_constructible_v<mapping_type,
3022 const typename _OLayout::template mapping<_OExtents>&>
3023 && is_constructible_v<accessor_type, const _OAccessor&>
3024 constexpr explicit(!is_convertible_v<
3025 const typename _OLayout::template mapping<_OExtents>&, mapping_type>
3026 || !is_convertible_v<const _OAccessor&, accessor_type>)
3027 mdspan(
const mdspan<_OElementType, _OExtents, _OLayout, _OAccessor>&
3029 : _M_accessor(__other.accessor()), _M_mapping(__other.mapping()),
3030 _M_handle(__other.data_handle())
3032 static_assert(is_constructible_v<data_handle_type,
3033 const typename _OAccessor::data_handle_type&>);
3034 static_assert(is_constructible_v<extents_type, _OExtents>);
3038 operator=(
const mdspan& __other) =
default;
3041 operator=(mdspan&& __other) =
default;
3043 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
3044 requires (
sizeof...(_OIndexTypes) == rank())
3046 operator[](_OIndexTypes... __indices)
const
3048 if constexpr (rank() == 0)
3049 return _M_accessor.access(_M_handle, _M_mapping());
3050 else if constexpr (!(is_same_v<_OIndexTypes, index_type> && ...))
3052 __mdspan::__index_type_cast<index_type>(
std::move(__indices))...);
3055 auto __is_multi_index = [&]<
size_t... _Counts>(
index_sequence<_Counts...>)
3056 {
return ((__indices < extents().extent(_Counts)) && ...); };
3058 __glibcxx_assert(__is_multi_index(make_index_sequence<rank()>()));
3059 return _M_accessor.access(_M_handle, _M_mapping(__indices...));
3063 template<
typename _OIndexType>
3064 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3066 operator[](span<_OIndexType, rank()> __indices)
const
3072 __mdspan::__index_type_cast<index_type>(as_const(__indices[_Counts]))...);
3074 return __call(make_index_sequence<rank()>());
3077 template<
typename _OIndexType>
3078 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3080 operator[](
const array<_OIndexType, rank()>& __indices)
const
3081 {
return operator[](span<
const _OIndexType, rank()>(__indices)); }
3083#if __cplusplus > 202302L
3084 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
3085 requires (
sizeof...(_OIndexTypes) == rank())
3087 at(_OIndexTypes... __indices)
const
3089 if constexpr (rank() == 0)
3090 return _M_accessor.access(_M_handle, _M_mapping());
3091 else if constexpr (!(is_integral_v<_OIndexTypes> && ...))
3092 return at(__index_int_t<_OIndexTypes>(
std::move(__indices))...);
3095 auto __check_bound = [&]<
typename _OIntType>(
size_t __dim, _OIntType __index)
3097 if constexpr (is_signed_v<_OIntType>)
3099 std::__throw_out_of_range_fmt(
3100 __N(
"mdspan::at: %zuth index is negative"), __dim);
3102 const auto __ext = extents().extent(__dim);
3103 if (std::cmp_greater_equal(__index, __ext))
3104 std::__throw_out_of_range_fmt(
3105 __N(
"mdspan::at: %zuth index (which is %zu)"
3106 " >= extent(%zu) (which is %zu)"),
3107 __dim,
size_t(__index), __dim,
size_t(__ext));
3109 auto __check_bounds = [&]<
size_t... _Counts>(
index_sequence<_Counts...>)
3110 { (__check_bound(_Counts, __indices), ...); };
3112 __check_bounds(make_index_sequence<rank()>());
3113 auto __index = _M_mapping(
static_cast<index_type
>(__indices)...);
3114 return _M_accessor.access(_M_handle, __index);
3118 template<
typename _OIndexType>
3119 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3121 at(span<_OIndexType, rank()> __indices)
const
3127 __index_int_t<_OIndexType>(as_const(__indices[_Counts]))...);
3129 return __call(make_index_sequence<rank()>());
3132 template<
typename _OIndexType>
3133 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3135 at(
const array<_OIndexType, rank()>& __indices)
const
3136 {
return at(span<
const _OIndexType, rank()>(__indices)); }
3140 size() const noexcept
3142 __glibcxx_assert(cmp_less_equal(_M_mapping.required_span_size(),
3145 return size_type(__mdspan::__size(extents()));
3150 empty() const noexcept
3151 {
return __mdspan::__empty(extents()); }
3153 friend constexpr void
3154 swap(mdspan& __x, mdspan& __y)
noexcept
3157 swap(__x._M_mapping, __y._M_mapping);
3158 swap(__x._M_accessor, __y._M_accessor);
3159 swap(__x._M_handle, __y._M_handle);
3162 constexpr const extents_type&
3163 extents() const noexcept {
return _M_mapping.extents(); }
3165 constexpr const data_handle_type&
3166 data_handle() const noexcept {
return _M_handle; }
3168 constexpr const mapping_type&
3169 mapping() const noexcept {
return _M_mapping; }
3171 constexpr const accessor_type&
3172 accessor() const noexcept {
return _M_accessor; }
3176 static constexpr bool
3177 is_always_unique() noexcept(noexcept(mapping_type::is_always_unique()))
3178 {
return mapping_type::is_always_unique(); }
3180 static constexpr bool
3181 is_always_exhaustive()
3182 noexcept(noexcept(mapping_type::is_always_exhaustive()))
3183 {
return mapping_type::is_always_exhaustive(); }
3185 static constexpr bool
3187 noexcept(noexcept(mapping_type::is_always_strided()))
3188 {
return mapping_type::is_always_strided(); }
3191 is_unique() const noexcept(noexcept(_M_mapping.is_unique()))
3192 {
return _M_mapping.is_unique(); }
3195 is_exhaustive() const noexcept(noexcept(_M_mapping.is_exhaustive()))
3196 {
return _M_mapping.is_exhaustive(); }
3199 is_strided() const noexcept(noexcept(_M_mapping.is_strided()))
3200 {
return _M_mapping.is_strided(); }
3202 constexpr index_type
3203 stride(rank_type __r)
const {
return _M_mapping.stride(__r); }
3206 template<
typename _OIndexType>
3207 using __index_int_t = std::__conditional_t<
3208 is_integral_v<_OIndexType>, _OIndexType, index_type>;
3210 [[no_unique_address]] accessor_type _M_accessor = accessor_type();
3211 [[no_unique_address]] mapping_type _M_mapping = mapping_type();
3212 [[no_unique_address]] data_handle_type _M_handle = data_handle_type();
3215 template<
typename _CArray>
3216 requires is_array_v<_CArray> && (rank_v<_CArray> == 1)
3218 -> mdspan<remove_all_extents_t<_CArray>,
3219 extents<size_t, extent_v<_CArray, 0>>>;
3221 template<
typename _Po
inter>
3222 requires is_pointer_v<remove_reference_t<_Pointer>>
3224 -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;
3226 template<
typename _ElementType,
typename... _Integrals>
3227 requires (is_convertible_v<_Integrals, size_t> && ...)
3228 && (
sizeof...(_Integrals) > 0)
3229 explicit mdspan(_ElementType*, _Integrals...)
3230 -> mdspan<_ElementType,
3231 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>>;
3233 template<
typename _ElementType,
typename _OIndexType,
size_t _Nm>
3234 mdspan(_ElementType*, span<_OIndexType, _Nm>)
3235 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
3237 template<
typename _ElementType,
typename _OIndexType,
size_t _Nm>
3238 mdspan(_ElementType*,
const array<_OIndexType, _Nm>&)
3239 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
3241 template<
typename _ElementType,
typename _IndexType,
size_t... _ExtentsPack>
3242 mdspan(_ElementType*,
const extents<_IndexType, _ExtentsPack...>&)
3243 -> mdspan<_ElementType, extents<_IndexType, _ExtentsPack...>>;
3245 template<
typename _ElementType,
typename _MappingType>
3246 mdspan(_ElementType*,
const _MappingType&)
3247 -> mdspan<_ElementType,
typename _MappingType::extents_type,
3248 typename _MappingType::layout_type>;
3250 template<
typename _MappingType,
typename _AccessorType>
3251 mdspan(
const typename _AccessorType::data_handle_type&,
const _MappingType&,
3252 const _AccessorType&)
3253 -> mdspan<
typename _AccessorType::element_type,
3254 typename _MappingType::extents_type,
3255 typename _MappingType::layout_type, _AccessorType>;
3257#if __glibcxx_submdspan
3260 template<
typename _IndexType,
typename _Slice>
3262 __canonical_index(_Slice&& __slice)
3264 if constexpr (__detail::__integral_constant_like<_Slice>)
3266 static_assert(__is_representable_integer<_IndexType>(_Slice::value));
3267 static_assert(_Slice::value >= 0);
3268 return std::cw<_IndexType(_Slice::value)>;
3271 return __mdspan::__index_type_cast<_IndexType>(
std::move(__slice));
3274 template<
typename _IndexType,
typename _Slice>
3276 __slice_cast(_Slice&& __slice)
3278 using _SliceType = remove_cvref_t<_Slice>;
3279 if constexpr (is_convertible_v<_SliceType, full_extent_t>)
3280 return static_cast<full_extent_t
>(
std::move(__slice));
3281 else if constexpr (is_convertible_v<_SliceType, _IndexType>)
3282 return __mdspan::__canonical_index<_IndexType>(
std::move(__slice));
3283 else if constexpr (__is_strided_slice<_SliceType>)
3286 = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.extent));
3288 = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.offset));
3289 if constexpr (is_same_v<
decltype(__extent),
3290 constant_wrapper<_IndexType(0)>>)
3291 return strided_slice{
3294 .stride = cw<_IndexType(1)>
3297 return strided_slice{
3301 = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.stride))
3306 auto [__sbegin, __send] =
std::move(__slice);
3308 = __mdspan::__canonical_index<_IndexType>(
std::move(__sbegin));
3310 = __mdspan::__canonical_index<_IndexType>(
std::move(__send));
3311 return strided_slice{
3313 .extent = __mdspan::__canonical_index<_IndexType>(__end - __offset),
3314 .stride = cw<_IndexType(1)>
3319 template<
typename _IndexType,
size_t _Extent,
typename _OIndexType>
3321 __check_valid_index(
const extents<_IndexType, _Extent>& __ext,
3322 const _OIndexType& __idx)
3324 if constexpr (__is_constant_wrapper<_OIndexType>
3325 && _Extent != dynamic_extent)
3327 static_assert(_OIndexType::value >= 0);
3328 static_assert(std::cmp_less_equal(_OIndexType::value, _Extent));
3331 __glibcxx_assert(__idx <= __ext.extent(0));
3334 template<
typename _IndexType,
size_t _Extent,
typename _Slice>
3336 __check_valid_slice(
const extents<_IndexType, _Extent>& __ext,
3337 const _Slice& __slice)
3339 if constexpr (__is_strided_slice<_Slice>)
3345 __mdspan::__check_valid_index(__ext, __slice.offset);
3346 __mdspan::__check_valid_index(__ext, __slice.extent);
3348 if constexpr (__is_constant_wrapper<typename _Slice::extent_type>
3349 && __is_constant_wrapper<typename _Slice::stride_type>)
3350 static_assert(_Slice::stride_type::value > 0);
3352 __glibcxx_assert(__slice.extent == 0 || __slice.stride > 0);
3354 if constexpr (__is_constant_wrapper<typename _Slice::offset_type>
3355 && __is_constant_wrapper<typename _Slice::extent_type>
3356 && _Extent != dynamic_extent)
3357 static_assert(std::cmp_greater_equal(
3358 _Extent - _Slice::offset_type::value,
3359 _Slice::extent_type::value));
3361 __glibcxx_assert(__ext.extent(0) - __slice.offset
3364 else if constexpr (__is_constant_wrapper<_Slice>
3365 && _Extent != dynamic_extent)
3366 static_assert(std::cmp_less(_Slice::value, _Extent));
3367 else if constexpr (convertible_to<_Slice, _IndexType>)
3368 __glibcxx_assert(__slice < __ext.extent(0));
3371 template<
typename _Extents,
typename... _Slices>
3373 __check_valid_slices(
const _Extents& __exts,
const _Slices&... __slices)
3375 constexpr auto __rank = _Extents::rank();
3378 ((__mdspan::__check_valid_slice(__extract_extent<_Is>(__exts),
3379 __slices...[_Is])),...);
3381 __impl(make_index_sequence<__rank>());
3384 template<
typename _Slice>
3385 using __full_extent_t = std::full_extent_t;
3388 void submdspan_mapping() =
delete;
3390 template<
typename _Mapping,
typename... _Slices>
3391 concept __sliceable_mapping =
requires(
const _Mapping __m, _Slices... __slices)
3393 { submdspan_mapping(__m, __slices...) } -> __submdspan_mapping_result;
3396 template<
typename _Mapping,
typename... _Slices>
3398 __submapping(
const _Mapping& __mapping, _Slices... __slices)
3400 __mdspan::__check_valid_slices(__mapping.extents(), __slices...);
3401 return submdspan_mapping(__mapping, __slices...);
3405 template<
typename _IndexType,
size_t... _Extents,
typename... _RawSlices>
3406 requires (
sizeof...(_RawSlices) ==
sizeof...(_Extents))
3408 subextents(
const extents<_IndexType, _Extents...>& __exts,
3409 _RawSlices... __raw_slices)
3411 auto __impl = [&__exts](
auto... __slices)
3413 __mdspan::__check_valid_slices(__exts, __slices...);
3414 return __mdspan::__subextents(__exts, __slices...);
3416 return __impl(__mdspan::__slice_cast<_IndexType>(__raw_slices)...);
3419 template<
typename _IndexType,
size_t... _Extents,
typename... _RawSlices>
3420 requires (
sizeof...(_Extents) ==
sizeof...(_RawSlices))
3422 canonical_slices(
const extents<_IndexType, _Extents...>& __exts,
3423 _RawSlices... __raw_slices)
3425 auto __impl = [&__exts](
auto... __slices)
3427 __mdspan::__check_valid_slices(__exts, __slices...);
3430 return __impl(__mdspan::__slice_cast<_IndexType>(__raw_slices)...);
3433 template<
typename _ElementType,
typename _Extents,
typename _Layout,
3434 typename _Accessor,
typename... _RawSlices>
3435 requires (
sizeof...(_RawSlices) == _Extents::rank()
3436 && __mdspan::__sliceable_mapping<typename _Layout::template mapping<_Extents>,
3437 __mdspan::__full_extent_t<_RawSlices>...>)
3440 const mdspan<_ElementType, _Extents, _Layout, _Accessor>& __md,
3441 _RawSlices... __raw_slices)
3443 using _IndexType =
typename _Extents::index_type;
3444 auto [__mapping, __offset] = __mdspan::__submapping(
3445 __md.mapping(), __mdspan::__slice_cast<_IndexType>(__raw_slices)...);
3447 __md.accessor().offset(__md.data_handle(), __offset),
3449 typename _Accessor::offset_policy(__md.accessor()));
3453_GLIBCXX_END_NAMESPACE_VERSION
constexpr _Tp * assume_aligned(_Tp *__ptr) noexcept
Inform the compiler that a pointer is aligned.
constexpr tuple< typename __decay_and_strip< _Elements >::__type... > make_tuple(_Elements &&... __args)
Create a tuple containing copies of the arguments.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.
make_integer_sequence< size_t, _Num > make_index_sequence
Alias template make_index_sequence.
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.