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
55#ifdef __glibcxx_mdspan
57namespace std _GLIBCXX_VISIBILITY(default)
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
63 __all_static(std::span<const size_t> __extents)
65 for(
auto __ext : __extents)
66 if (__ext == dynamic_extent)
72 __all_dynamic(std::span<const size_t> __extents)
74 for(
auto __ext : __extents)
75 if (__ext != dynamic_extent)
80 template<
typename _IndexType,
typename _OIndexType>
82 __index_type_cast(_OIndexType&& __other)
84 if constexpr (std::is_integral_v<_OIndexType>)
86 constexpr _IndexType __index_type_max
87 = __gnu_cxx::__int_traits<_IndexType>::__max;
88 constexpr _OIndexType __oindex_type_max
89 = __gnu_cxx::__int_traits<_OIndexType>::__max;
91 if constexpr (__index_type_max < __oindex_type_max)
92 __glibcxx_assert(cmp_less_equal(__other, __index_type_max));
94 if constexpr (std::is_signed_v<_OIndexType>)
95 __glibcxx_assert(__other >= 0);
96 return static_cast<_IndexType
>(__other);
100 auto __ret =
static_cast<_IndexType
>(
std::move(__other));
101 if constexpr (std::is_signed_v<_IndexType>)
102 __glibcxx_assert(__ret >= 0);
107 template<array _Extents>
111 static constexpr size_t _S_rank = _Extents.size();
119 static constexpr size_t
120 _S_dynamic_index(
size_t __r)
noexcept
121 {
return _S_dynamic_index_data[__r]; }
123 static constexpr auto _S_dynamic_index_data = []
consteval
125 array<size_t, _S_rank+1> __ret;
127 for (
size_t __i = 0; __i < _S_rank; ++__i)
130 __dyn += (_Extents[__i] == dynamic_extent);
132 __ret[_S_rank] = __dyn;
136 static constexpr size_t _S_rank_dynamic = _S_dynamic_index(_S_rank);
140 static constexpr size_t
141 _S_dynamic_index_inv(
size_t __r)
noexcept
142 {
return _S_dynamic_index_inv_data[__r]; }
144 static constexpr auto _S_dynamic_index_inv_data = []
consteval
146 array<size_t, _S_rank_dynamic> __ret;
147 for (
size_t __i = 0, __r = 0; __i < _S_rank; ++__i)
148 if (_Extents[__i] == dynamic_extent)
153 static constexpr size_t
154 _S_static_extent(
size_t __r)
noexcept
155 {
return _Extents[__r]; }
158 template<array _Extents>
159 requires (__all_dynamic<_Extents>())
160 class _StaticExtents<_Extents>
163 static constexpr size_t _S_rank = _Extents.size();
165 static constexpr size_t
166 _S_dynamic_index(
size_t __r)
noexcept
169 static constexpr size_t _S_rank_dynamic = _S_rank;
171 static constexpr size_t
172 _S_dynamic_index_inv(
size_t __k)
noexcept
175 static constexpr size_t
176 _S_static_extent(
size_t)
noexcept
177 {
return dynamic_extent; }
180 template<
typename _IndexType, array _Extents>
181 class _ExtentsStorage :
public _StaticExtents<_Extents>
184 using _Base = _StaticExtents<_Extents>;
187 using _Base::_S_rank;
188 using _Base::_S_rank_dynamic;
189 using _Base::_S_dynamic_index;
190 using _Base::_S_dynamic_index_inv;
191 using _Base::_S_static_extent;
193 static constexpr bool
194 _S_is_dynamic(
size_t __r)
noexcept
196 if constexpr (__all_static(_Extents))
198 else if constexpr (__all_dynamic(_Extents))
201 return _Extents[__r] == dynamic_extent;
204 template<
typename _OIndexType>
205 static constexpr _IndexType
206 _S_int_cast(
const _OIndexType& __other)
noexcept
207 {
return _IndexType(__other); }
210 _M_extent(
size_t __r)
const noexcept
212 if (_S_is_dynamic(__r))
213 return _M_dyn_exts[_S_dynamic_index(__r)];
215 return _S_static_extent(__r);
218 template<
size_t _OtherRank,
typename _GetOtherExtent>
219 static constexpr bool
220 _S_is_compatible_extents(_GetOtherExtent __get_extent)
noexcept
222 if constexpr (_OtherRank == _S_rank)
223 for (
size_t __i = 0; __i < _S_rank; ++__i)
224 if (!_S_is_dynamic(__i)
225 && !cmp_equal(_Extents[__i], _S_int_cast(__get_extent(__i))))
230 template<
size_t _OtherRank,
typename _GetOtherExtent>
232 _M_init_dynamic_extents(_GetOtherExtent __get_extent)
noexcept
234 __glibcxx_assert(_S_is_compatible_extents<_OtherRank>(__get_extent));
235 for (
size_t __i = 0; __i < _S_rank_dynamic; ++__i)
238 if constexpr (_OtherRank != _S_rank_dynamic)
239 __di = _S_dynamic_index_inv(__i);
240 _M_dyn_exts[__i] = _S_int_cast(__get_extent(__di));
245 _ExtentsStorage() noexcept = default;
247 template<typename _OIndexType, array _OExtents>
249 _ExtentsStorage(const _ExtentsStorage<_OIndexType, _OExtents>&
252 _M_init_dynamic_extents<_S_rank>([&__other](
size_t __i)
253 {
return __other._M_extent(__i); });
256 template<
typename _OIndexType,
size_t _Nm>
258 _ExtentsStorage(span<const _OIndexType, _Nm> __exts)
noexcept
260 _M_init_dynamic_extents<_Nm>(
261 [&__exts](
size_t __i) ->
const _OIndexType&
262 {
return __exts[__i]; });
265 static constexpr const array<size_t, _S_rank>&
266 _S_static_extents() noexcept
269 constexpr span<const _IndexType>
270 _M_dynamic_extents(
size_t __begin,
size_t __end)
const noexcept
271 requires (_Extents.size() > 0)
273 return {_M_dyn_exts + _S_dynamic_index(__begin),
274 _S_dynamic_index(__end) - _S_dynamic_index(__begin)};
278 using _Storage = __array_traits<_IndexType, _S_rank_dynamic>::_Type;
279 [[no_unique_address]] _Storage _M_dyn_exts{};
282 template<
typename _OIndexType,
typename _SIndexType>
283 concept __valid_index_type =
284 is_convertible_v<_OIndexType, _SIndexType> &&
285 is_nothrow_constructible_v<_SIndexType, _OIndexType>;
287 template<
size_t _Extent,
typename _IndexType>
289 __valid_static_extent = _Extent == dynamic_extent
290 || _Extent <= __gnu_cxx::__int_traits<_IndexType>::__max;
292 template<
typename _Extents>
293 constexpr const array<size_t, _Extents::rank()>&
294 __static_extents() noexcept
295 {
return _Extents::_Storage::_S_static_extents(); }
297 template<
typename _Extents>
298 constexpr span<const size_t>
299 __static_extents(
size_t __begin,
size_t __end)
noexcept
301 const auto& __sta_exts = __static_extents<_Extents>();
302 return span<const size_t>(__sta_exts.data() + __begin, __end - __begin);
306 template<array _Extents>
307 constexpr auto __fwd_partial_prods = []
consteval
309 constexpr size_t __rank = _Extents.size();
310 std::array<size_t, __rank> __ret;
312 for (
size_t __r = 0; __r < __rank; ++__r)
315 if (
size_t __ext = _Extents[__r]; __ext != dynamic_extent)
322 template<array _Extents>
323 constexpr auto __rev_partial_prods = []
consteval
325 constexpr size_t __rank = _Extents.size();
326 std::array<size_t, __rank> __ret;
328 for (
size_t __r = __rank; __r > 0; --__r)
330 __ret[__r - 1] = __prod;
331 if (
size_t __ext = _Extents[__r - 1]; __ext != dynamic_extent)
337 template<
typename _Extents>
338 constexpr span<const typename _Extents::index_type>
339 __dynamic_extents(
const _Extents& __exts,
size_t __begin = 0,
340 size_t __end = _Extents::rank()) noexcept
341 {
return __exts._M_exts._M_dynamic_extents(__begin, __end); }
344#if __glibcxx_submdspan
347 explicit full_extent_t() =
default;
350 inline constexpr full_extent_t full_extent{};
352 template<
typename _OffsetType,
typename _ExtentType,
typename _Str
ideType>
355 static_assert(__is_signed_or_unsigned_integer<_OffsetType>::value
356 || __detail::__integral_constant_like<_OffsetType>);
357 static_assert(__is_signed_or_unsigned_integer<_ExtentType>::value
358 || __detail::__integral_constant_like<_ExtentType>);
359 static_assert(__is_signed_or_unsigned_integer<_StrideType>::value
360 || __detail::__integral_constant_like<_StrideType>);
362 using offset_type = _OffsetType;
363 using extent_type = _ExtentType;
364 using stride_type = _StrideType;
366 [[no_unique_address]] offset_type offset{};
367 [[no_unique_address]] extent_type extent{};
368 [[no_unique_address]] stride_type stride{};
371 template<
typename _Mapping>
372 struct submdspan_mapping_result
374 [[no_unique_address]] _Mapping mapping = _Mapping();
378 template<
typename _Tp>
379 constexpr bool __is_submdspan_mapping_result =
false;
381 template<
typename _Mapping>
382 constexpr bool __is_submdspan_mapping_result<submdspan_mapping_result<_Mapping>> =
true;
384 template<
typename _Mapping>
385 concept __submdspan_mapping_result = __is_submdspan_mapping_result<_Mapping>;
389 template<
typename _IndexType,
size_t... _Extents>
392 static_assert(__is_signed_or_unsigned_integer<_IndexType>::value,
393 "IndexType must be a signed or unsigned integer type");
395 (__mdspan::__valid_static_extent<_Extents, _IndexType> && ...),
396 "Extents must either be dynamic or representable as IndexType");
398 using _Storage = __mdspan::_ExtentsStorage<
399 _IndexType, array<size_t,
sizeof...(_Extents)>{_Extents...}>;
400 [[no_unique_address]] _Storage _M_exts;
403 using index_type = _IndexType;
404 using size_type = make_unsigned_t<index_type>;
405 using rank_type = size_t;
407 static constexpr rank_type
408 rank() noexcept {
return _Storage::_S_rank; }
410 static constexpr rank_type
411 rank_dynamic() noexcept {
return _Storage::_S_rank_dynamic; }
413 static constexpr size_t
414 static_extent(rank_type __r)
noexcept
416 __glibcxx_assert(__r < rank());
417 if constexpr (rank() == 0)
420 return _Storage::_S_static_extent(__r);
424 extent(rank_type __r)
const noexcept
426 __glibcxx_assert(__r < rank());
427 if constexpr (rank() == 0)
430 return _M_exts._M_extent(__r);
434 extents() noexcept = default;
437 static consteval
bool
438 _S_is_less_dynamic(
size_t __ext,
size_t __oext)
439 {
return (__ext != dynamic_extent) && (__oext == dynamic_extent); }
441 template<
typename _OIndexType,
size_t... _OExtents>
442 static consteval bool
445 return (_S_is_less_dynamic(_Extents, _OExtents) || ...)
446 || (__gnu_cxx::__int_traits<index_type>::__max
447 < __gnu_cxx::__int_traits<_OIndexType>::__max);
450 template<
size_t... _OExtents>
451 static consteval bool
452 _S_is_compatible_extents()
454 if constexpr (
sizeof...(_OExtents) != rank())
457 return ((_OExtents == dynamic_extent || _Extents == dynamic_extent
458 || _OExtents == _Extents) && ...);
462 template<
typename _OIndexType,
size_t... _OExtents>
463 requires (_S_is_compatible_extents<_OExtents...>())
464 constexpr explicit(_S_ctor_explicit<_OIndexType, _OExtents...>())
465 extents(
const extents<_OIndexType, _OExtents...>& __other) noexcept
466 : _M_exts(__other._M_exts)
469 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
470 requires (
sizeof...(_OIndexTypes) == rank()
471 ||
sizeof...(_OIndexTypes) == rank_dynamic())
472 constexpr explicit extents(_OIndexTypes... __exts) noexcept
473 : _M_exts(span<
const _IndexType,
sizeof...(_OIndexTypes)>(
474 initializer_list{
static_cast<_IndexType
>(
std::move(__exts))...}))
477 template<
typename _OIndexType,
size_t _Nm>
478 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
479 && (_Nm == rank() || _Nm == rank_dynamic())
480 constexpr explicit(_Nm != rank_dynamic())
481 extents(span<_OIndexType, _Nm> __exts) noexcept
482 : _M_exts(span<const _OIndexType, _Nm>(__exts))
485 template<
typename _OIndexType,
size_t _Nm>
486 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
487 && (_Nm == rank() || _Nm == rank_dynamic())
488 constexpr explicit(_Nm != rank_dynamic())
489 extents(
const array<_OIndexType, _Nm>& __exts) noexcept
490 : _M_exts(span<const _OIndexType, _Nm>(__exts))
493 template<
typename _OIndexType,
size_t... _OExtents>
494 friend constexpr bool
495 operator==(
const extents& __self,
496 const extents<_OIndexType, _OExtents...>& __other)
noexcept
498 if constexpr (!_S_is_compatible_extents<_OExtents...>())
502 auto __impl = [&__self, &__other]<
size_t... _Counts>(
504 {
return (cmp_equal(__self.extent(_Counts),
505 __other.extent(_Counts)) && ...); };
506 return __impl(make_index_sequence<__self.rank()>());
511 friend constexpr const array<size_t, rank()>&
512 __mdspan::__static_extents<extents>() noexcept;
514 friend constexpr span<const index_type>
515 __mdspan::__dynamic_extents<extents>(const extents&,
size_t,
size_t)
518 template<typename _OIndexType,
size_t... _OExtents>
519 friend class extents;
524 template<
typename _Tp,
size_t _Nm>
526 __contains_zero(span<_Tp, _Nm> __exts)
noexcept
528 for (
size_t __i = 0; __i < __exts.size(); ++__i)
529 if (__exts[__i] == 0)
534 template<
typename _Tp,
size_t _Nm>
536 __contains_zero(
const array<_Tp, _Nm>& __exts)
noexcept
537 {
return __contains_zero(span<const _Tp>(__exts)); }
539 template<
typename _Extents>
541 __empty(
const _Extents& __exts)
noexcept
543 if constexpr (__contains_zero(__static_extents<_Extents>()))
545 else if constexpr (_Extents::rank_dynamic() > 0)
546 return __contains_zero(__dynamic_extents(__exts));
551 template<
typename _Extents>
552 constexpr typename _Extents::index_type
553 __extents_prod(
const _Extents& __exts,
size_t __sta_prod,
size_t __begin,
554 size_t __end)
noexcept
559 size_t __ret = __sta_prod;
560 if constexpr (_Extents::rank_dynamic() > 0)
561 for (
auto __factor : __dynamic_extents(__exts, __begin, __end))
562 __ret *= size_t(__factor);
563 return static_cast<typename _Extents::index_type
>(__ret);
567 template<
typename _Extents>
568 constexpr typename _Extents::index_type
569 __fwd_prod(
const _Extents& __exts,
size_t __begin,
size_t __end)
noexcept
571 size_t __sta_prod = [__begin, __end] {
572 span<const size_t> __sta_exts
573 = __static_extents<_Extents>(__begin, __end);
575 for(
auto __ext : __sta_exts)
576 if (__ext != dynamic_extent)
580 return __extents_prod(__exts, __sta_prod, __begin, __end);
583 template<
typename _Extents>
584 constexpr typename _Extents::index_type
585 __fwd_prod(
const _Extents& __exts,
size_t __r)
noexcept
587 constexpr size_t __rank = _Extents::rank();
588 constexpr auto& __sta_exts = __static_extents<_Extents>();
589 if constexpr (__rank == 1)
591 else if constexpr (__rank == 2)
592 return __r == 0 ? 1 : __exts.extent(0);
593 else if constexpr (__all_dynamic(std::span(__sta_exts).first(__rank-1)))
594 return __extents_prod(__exts, 1, 0, __r);
597 size_t __sta_prod = __fwd_partial_prods<__sta_exts>[__r];
598 return __extents_prod(__exts, __sta_prod, 0, __r);
602 template<
typename _IndexType,
size_t _Nm>
604 __fwd_prod(span<const _IndexType, _Nm> __values)
606 _IndexType __ret = 1;
607 for(
auto __value : __values)
613 template<
typename _Extents>
614 constexpr typename _Extents::index_type
615 __rev_prod(
const _Extents& __exts,
size_t __r)
noexcept
617 constexpr size_t __rank = _Extents::rank();
618 constexpr auto& __sta_exts = __static_extents<_Extents>();
619 if constexpr (__rank == 1)
621 else if constexpr (__rank == 2)
622 return __r == 0 ? __exts.extent(1) : 1;
623 else if constexpr (__all_dynamic(std::span(__sta_exts).last(__rank-1)))
624 return __extents_prod(__exts, 1, __r + 1, __rank);
627 size_t __sta_prod = __rev_partial_prods<__sta_exts>[__r];
628 return __extents_prod(__exts, __sta_prod, __r + 1, __rank);
632 template<
typename _Extents>
633 constexpr typename _Extents::index_type
634 __size(
const _Extents& __exts)
noexcept
636 constexpr size_t __sta_prod = [] {
637 span<const size_t> __sta_exts = __static_extents<_Extents>();
639 for(
auto __ext : __sta_exts)
640 if (__ext != dynamic_extent)
644 return __extents_prod(__exts, __sta_prod, 0, _Extents::rank());
647 template<
typename _IndexType,
size_t... _Counts>
648 auto __build_dextents_type(integer_sequence<size_t, _Counts...>)
649 -> extents<_IndexType, ((void) _Counts, dynamic_extent)...>;
652 template<
typename _IndexType,
size_t _Rank>
653 using dextents =
decltype(__mdspan::__build_dextents_type<_IndexType>(
654 make_index_sequence<_Rank>()));
656#if __glibcxx_mdspan >= 202406L
657 template<
size_t _Rank,
typename _IndexType =
size_t>
658 using dims = dextents<_IndexType, _Rank>;
661 template<
typename... _Integrals>
662 requires (is_convertible_v<_Integrals, size_t> && ...)
663 explicit extents(_Integrals...) ->
664 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>;
668 template<
typename _Extents>
674 template<
typename _Extents>
680 template<
typename _Extents>
684#ifdef __glibcxx_padded_layouts
685 template<
size_t _PaddingValue>
686 struct layout_left_padded
688 template<
typename _Extents>
692 template<
size_t _PaddingValue>
693 struct layout_right_padded
695 template<
typename _Extents>
702 template<
typename _Tp>
703 constexpr bool __is_extents =
false;
705 template<
typename _IndexType,
size_t... _Extents>
706 constexpr bool __is_extents<extents<_IndexType, _Extents...>> =
true;
708 template<
typename _Extents,
typename... _Indices>
709 constexpr typename _Extents::index_type
710 __linear_index_left(
const _Extents& __exts, _Indices... __indices)
713 using _IndexType =
typename _Extents::index_type;
714 _IndexType __res = 0;
715 if constexpr (
sizeof...(__indices) > 0)
717 _IndexType __mult = 1;
718 auto __update = [&, __pos = 0u](_IndexType __idx)
mutable
720 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx, __exts.extent(__pos)));
721 __res += __idx * __mult;
722 __mult *= __exts.extent(__pos);
725 (__update(__indices), ...);
730 template<
typename _IndexType>
732 __static_quotient(std::span<const size_t> __sta_exts,
733 _IndexType __nom = __gnu_cxx::__int_traits<_IndexType>::__max)
735 for (
auto __factor : __sta_exts)
737 if (__factor != dynamic_extent)
738 __nom /= _IndexType(__factor);
745 template<
typename _Extents,
746 typename _IndexType =
typename _Extents::index_type>
747 requires __is_extents<_Extents>
749 __static_quotient(_IndexType __nom
750 = __gnu_cxx::__int_traits<_IndexType>::__max)
752 std::span<const size_t> __sta_exts = __static_extents<_Extents>();
753 return __static_quotient<_IndexType>(__sta_exts, __nom);
756 template<
typename _Extents>
758 __is_representable_extents(
const _Extents& __exts)
noexcept
760 using _IndexType = _Extents::index_type;
762 if constexpr (__contains_zero(__static_extents<_Extents>()))
766 constexpr auto __sta_quo = __static_quotient<_Extents>();
767 if constexpr (_Extents::rank_dynamic() == 0)
768 return __sta_quo != 0;
771 auto __dyn_exts = __dynamic_extents(__exts);
772 if (__contains_zero(__dyn_exts))
775 if constexpr (__sta_quo == 0)
779 auto __dyn_quo = _IndexType(__sta_quo);
780 for (
auto __factor : __dyn_exts)
782 __dyn_quo /= __factor;
792 template<
typename _Extents,
typename _IndexType>
793 concept __representable_size = _Extents::rank_dynamic() != 0
794 || __contains_zero(__static_extents<_Extents>())
795 || (__static_quotient<_Extents, _IndexType>() != 0);
797 template<
typename _Layout,
typename _Mapping>
798 concept __mapping_of =
799 is_same_v<
typename _Layout::template mapping<
800 typename _Mapping::extents_type>,
803 template<
template<
size_t>
typename _Layout,
typename _Mapping>
804 concept __padded_mapping_of = __mapping_of<
805 _Layout<_Mapping::padding_value>, _Mapping>;
807#ifdef __glibcxx_padded_layouts
808 template<
typename _Mapping>
809 constexpr bool __is_left_padded_mapping = __padded_mapping_of<
810 layout_left_padded, _Mapping>;
812 template<
typename _Mapping>
813 constexpr bool __is_right_padded_mapping = __padded_mapping_of<
814 layout_right_padded, _Mapping>;
816 template<
typename _Mapping>
817 constexpr bool __is_padded_mapping = __is_left_padded_mapping<_Mapping>
818 || __is_right_padded_mapping<_Mapping>;
821 template<
typename _PaddedMapping>
823 __get_static_stride()
824 {
return _PaddedMapping::_PaddedStorage::_S_static_stride; }
826 template<
typename _Mapping>
827 concept __standardized_mapping = __mapping_of<layout_left, _Mapping>
828 || __mapping_of<layout_right, _Mapping>
829 || __mapping_of<layout_stride, _Mapping>
830#ifdef __glibcxx_padded_layouts
831 || __is_left_padded_mapping<_Mapping>
832 || __is_right_padded_mapping<_Mapping>
837 class __internal_ctor
840 template<
typename _Mapping>
841 constexpr typename _Mapping::index_type
842 __offset(
const _Mapping& __m)
noexcept
844 using _IndexType =
typename _Mapping::index_type;
845 constexpr auto __rank = _Mapping::extents_type::rank();
847 if constexpr (__standardized_mapping<_Mapping>)
849 else if (__empty(__m.extents()))
853 auto __impl = [&__m]<
size_t... _Counts>(
index_sequence<_Counts...>)
854 {
return __m(((
void) _Counts, _IndexType(0))...); };
855 return __impl(make_index_sequence<__rank>());
859#ifdef __glibcxx_submdspan
860 template<
typename _Tp>
861 constexpr bool __is_strided_slice =
false;
863 template<
typename _OffsetType,
typename _ExtentType,
typename _Str
ideType>
864 constexpr bool __is_strided_slice<strided_slice<_OffsetType,
865 _ExtentType, _StrideType>> =
true;
867 template<
typename _IndexType,
typename _OIndexType>
869 __is_representable_integer(_OIndexType __value)
871 constexpr auto __min = __gnu_cxx::__int_traits<_IndexType>::__min;
872 constexpr auto __max = __gnu_cxx::__int_traits<_IndexType>::__max;
873 return std::cmp_less_equal(__min, __value)
874 && std::cmp_less_equal(__value, __max);
877 template<
typename _Tp>
878 constexpr bool __is_constant_wrapper =
false;
880 template<_CwFixedValue _Xv,
typename _Tp>
881 constexpr bool __is_constant_wrapper<constant_wrapper<_Xv, _Tp>>
884 template<
size_t _Index,
typename _Extents>
886 __extract_extent(
const _Extents& __exts)
888 using _IndexType =
typename _Extents::index_type;
889 return extents<_IndexType, _Extents::static_extent(_Index)>{
890 __exts.extent(_Index)};
893 template<
typename _Slice,
typename _IndexType>
894 concept __acceptable_slice_type = same_as<_Slice, full_extent_t>
895 || same_as<_Slice, _IndexType> || __is_constant_wrapper<_Slice>
896 || __is_strided_slice<_Slice>;
898 template<
typename _IndexType,
typename... _Slices>
902 return (
static_cast<size_t>(!convertible_to<_Slices, _IndexType>)
906 template<
typename _IndexType,
typename... _Slices>
910 constexpr auto __rank =
sizeof...(_Slices);
911 constexpr auto __sub_rank = __subrank<_IndexType, _Slices...>();
912 auto __map = std::array<size_t, __sub_rank>{};
913 auto __is_int_like = std::array<bool, __rank>{
914 convertible_to<_Slices, _IndexType>...};
917 for (
size_t __k = 0; __k < __rank; ++__k)
918 if (!__is_int_like[__k])
923 template<
typename _Slice>
925 __slice_begin(_Slice __slice)
927 if constexpr (same_as<_Slice, full_extent_t>)
929 else if constexpr (__is_strided_slice<_Slice>)
930 return __slice.offset;
935 template<
typename _Mapping,
typename... _Slices>
937 __suboffset(
const _Mapping& __mapping,
const _Slices&... __slices)
939 using _IndexType =
typename _Mapping::index_type;
940 auto __any_past_the_end = [&]<
size_t... _Is>(
index_sequence<_Is...>)
942 auto __is_past_the_end = [](
const auto& __slice,
const auto& __ext)
944 using _Slice = remove_cvref_t<
decltype(__slice)>;
945 if constexpr (is_convertible_v<_Slice, _IndexType>)
947 else if constexpr (same_as<_Slice, full_extent_t>
948 && __ext.static_extent(0) != 0
949 && __ext.static_extent(0) != dynamic_extent)
952 return __mdspan::__slice_begin(__slice) == __ext.extent(0);
955 const auto& __exts = __mapping.extents();
956 return ((__is_past_the_end(__slices...[_Is],
957 __mdspan::__extract_extent<_Is>(__exts))) || ...);
960 if constexpr ((same_as<_Slices, full_extent_t> && ...))
961 return __mdspan::__offset(__mapping);
964 return __mapping.required_span_size();
965 return __mapping(__mdspan::__slice_begin(__slices)...);
968 template<
typename _IndexType,
size_t _Extent,
typename _Slice>
970 __static_slice_extent()
972 if constexpr (same_as<_Slice, full_extent_t>)
974 else if constexpr (same_as<_Slice, constant_wrapper<_IndexType(0)>>)
976 else if constexpr (__is_constant_wrapper<typename _Slice::extent_type>
977 && __is_constant_wrapper<typename _Slice::stride_type>)
978 return 1 + ((
typename _Slice::extent_type{}) - 1)
979 / (
typename _Slice::stride_type{});
981 return dynamic_extent;
984 template<
size_t _K,
typename _Extents,
typename _Slice>
985 constexpr typename _Extents::index_type
986 __dynamic_slice_extent(
const _Extents& __exts, _Slice __slice)
988 if constexpr (__is_strided_slice<_Slice>)
989 return __slice.extent == 0 ? 0 : 1 + (__slice.extent - 1) / __slice.stride;
991 return __exts.extent(_K);
994 template<
typename _IndexType,
size_t... _Extents,
typename... _Slices>
995 requires (
sizeof...(_Slices) ==
sizeof...(_Extents))
997 __subextents(
const extents<_IndexType, _Extents...>& __exts,
1000 constexpr auto __inv_map = __mdspan::__inv_map_rank<_IndexType, _Slices...>();
1003 using _SubExts = extents<_IndexType,
1004 __mdspan::__static_slice_extent<_IndexType,
1005 _Extents...[__inv_map[_Indices]],
1006 _Slices...[__inv_map[_Indices]]>()...>;
1007 if constexpr (_SubExts::rank_dynamic() == 0)
1011 using _StaticSubExtents = __mdspan::_StaticExtents<
1012 __mdspan::__static_extents<_SubExts>()>;
1015 constexpr auto __slice_idx = [__inv_map](
size_t __i)
consteval
1017 return __inv_map[_StaticSubExtents::_S_dynamic_index_inv(__i)];
1020 return _SubExts{__mdspan::__dynamic_slice_extent<__slice_idx(_Is)>(
1021 __exts, __slices...[__slice_idx(_Is)])...};
1023 constexpr auto __dyn_subrank = _SubExts::rank_dynamic();
1031 enum class _LayoutSide
1038 template<
typename _Mapping>
1039 consteval _LayoutSide
1042 if constexpr (__is_left_padded_mapping<_Mapping>
1043 || __mapping_of<layout_left, _Mapping>)
1044 return _LayoutSide::__left;
1045 if constexpr (__is_right_padded_mapping<_Mapping>
1046 || __mapping_of<layout_right, _Mapping>)
1047 return _LayoutSide::__right;
1049 return _LayoutSide::__unknown;
1052 template<_LayoutS
ide _S
ide,
size_t _Rank>
1053 struct _StridesTrait
1055 static constexpr const _LayoutSide _S_side = _Side;
1057 static constexpr size_t
1058 _S_idx(
size_t __k)
noexcept
1060 if constexpr (_Side == _LayoutSide::__left)
1063 return _Rank - 1 - __k;
1068 template<
typename _Mapping>
1069 static constexpr typename _Mapping::index_type
1070 _S_padded_extent(
const _Mapping& __mapping,
size_t __k)
1073 return __mapping.stride(_S_idx(1));
1075 return __mapping.extents().extent(_S_idx(__k));
1078 template<
typename _IndexType,
typename... _Slices>
1079 static consteval auto
1082 static_assert(_Side != _LayoutSide::__unknown);
1085 return __mdspan::__inv_map_rank<_IndexType, _Slices...[_S_idx(_Is)]...>();
1091 template<
typename _SubExts,
typename _Mapping,
typename... _Slices>
1093 __substrides_generic(
const _Mapping& __mapping,
const _Slices&... __slices)
1095 using _IndexType =
typename _Mapping::index_type;
1096 if constexpr (_SubExts::rank() == 0)
1097 return array<_IndexType, _SubExts::rank()>{};
1100 auto __stride = [&__mapping](
size_t __k,
auto __slice) -> _IndexType
1102 if constexpr (__is_strided_slice<
decltype(__slice)>)
1103 if (__slice.stride < __slice.extent)
1104 return __mapping.stride(__k) * __slice.stride;
1105 return __mapping.stride(__k);
1110 constexpr auto __inv_map
1111 = __mdspan::__inv_map_rank<_IndexType, _Slices...>();
1112 return array<_IndexType, _SubExts::rank()>{
1113 __stride(__inv_map[_Is], __slices...[__inv_map[_Is]])...};
1119 template<
typename _SubExts,
typename _Mapping,
typename... _Slices>
1121 __substrides_standardized(
const _Mapping& __mapping,
1122 const _Slices&... __slices)
1124 using _IndexType =
typename _Mapping::index_type;
1125 using _Trait = _StridesTrait<__mapping_side<_Mapping>(),
1126 _Mapping::extents_type::rank()>;
1127 using _SubTrait = _StridesTrait<__mapping_side<_Mapping>(), _SubExts::rank()>;
1129 constexpr size_t __sub_rank = _SubExts::rank();
1131 std::array<_IndexType, __sub_rank> __ret;
1132 if constexpr (__sub_rank > 0)
1134 constexpr auto __inv_map
1135 = _Trait::template _S_inv_map<_IndexType, _Slices...>();
1139 size_t __stride = 1;
1140 auto __body = [&](
size_t __k,
auto __slice)
1142 for (
size_t __i = __i0; __i < __inv_map[__k]; ++__i)
1143 __stride *= _Trait::_S_padded_extent(__mapping, __i);
1145 size_t __krev = _SubTrait::_S_idx(__k);
1146 if constexpr (__is_strided_slice<
decltype(__slice)>)
1148 if (__slice.stride < __slice.extent)
1149 __ret[__krev] = __stride * __slice.stride;
1151 __ret[__krev] = __stride;
1154 __ret[__krev] = __stride;
1156 __i0 = __inv_map[__k];
1159 ((__body(_Ks, __slices...[_Trait::_S_idx(__inv_map[_Ks])])),...);
1167 template<
typename _SubExts,
typename _Mapping,
typename... _Slices>
1169 __substrides(
const _Mapping& __mapping,
const _Slices&... __slices)
1171 if constexpr (__mdspan::__mapping_side<_Mapping>() == _LayoutSide::__unknown)
1172 return __mdspan::__substrides_generic<_SubExts>(__mapping, __slices...);
1174 return __mdspan::__substrides_standardized<_SubExts>(__mapping, __slices...);
1177 template<
typename _Slice>
1178 concept __is_unit_stride_slice = (__mdspan::__is_strided_slice<_Slice>
1179 && __mdspan::__is_constant_wrapper<typename _Slice::stride_type>
1180 && _Slice::stride_type::value == 1)
1181 || std::same_as<_Slice, full_extent_t>;
1187 enum class _SliceKind
1190 __unit_strided_slice,
1195 template<
typename _Slice>
1196 consteval _SliceKind
1199 if constexpr (std::same_as<_Slice, full_extent_t>)
1200 return _SliceKind::__full;
1201 else if constexpr (__mdspan::__is_strided_slice<_Slice>)
1203 if constexpr (__mdspan::__is_unit_stride_slice<_Slice>)
1204 return _SliceKind::__unit_strided_slice;
1206 return _SliceKind::__strided_slice;
1209 return _SliceKind::__collapsing;
1212 template<
typename... _Slices>
1213 consteval array<_SliceKind,
sizeof...(_Slices)>
1214 __make_slice_kind_array()
1216 return array<_SliceKind,
sizeof...(_Slices)>{
1217 __mdspan::__make_slice_kind<_Slices>()...};
1223 __is_block(span<const _SliceKind> __slice_kinds,
size_t __block_size)
1225 if (__block_size == 0)
1228 if (__block_size > __slice_kinds.size())
1231 for (
size_t __i = 0; __i < __block_size - 1; ++__i)
1232 if (__slice_kinds[__i] != _SliceKind::__full)
1235 auto __last = __slice_kinds[__block_size - 1];
1236 return __last == _SliceKind::__full
1237 || __last == _SliceKind::__unit_strided_slice;
1242 static consteval size_t
1243 __padded_block_begin_generic(span<const _SliceKind> __slice_kinds,
1246 if (__slice_kinds[0] != _SliceKind::__full
1247 && __slice_kinds[0] != _SliceKind::__unit_strided_slice)
1248 return dynamic_extent;
1249 else if (__slice_kinds.size() == 1)
1250 return dynamic_extent;
1254 while(__u < __slice_kinds.size()
1255 && __slice_kinds[__u] == _SliceKind::__collapsing)
1258 if (__mdspan::__is_block(__slice_kinds.subspan(__u), __sub_rank -1))
1260 return dynamic_extent;
1264 template<_LayoutS
ide _S
ide,
size_t _Nm>
1265 static consteval size_t
1266 __padded_block_begin(span<const _SliceKind, _Nm> __slice_kinds,
size_t __sub_rank)
1268 if constexpr (_Side == _LayoutSide::__left)
1269 return __mdspan::__padded_block_begin_generic(__slice_kinds, __sub_rank);
1272 std::array<_SliceKind, _Nm> __rev_slices;
1273 for(
size_t __i = 0; __i < _Nm; ++__i)
1274 __rev_slices[__i] = __slice_kinds[_Nm - 1 - __i];
1275 auto __rev_slice_kinds = span<const _SliceKind>(__rev_slices);
1277 auto __u = __mdspan::__padded_block_begin_generic(__rev_slice_kinds,
1279 return __u == dynamic_extent ? dynamic_extent : _Nm - 1 - __u;
1283 template<_LayoutS
ide _S
ide,
bool _Padded>
1284 struct _SubMdspanMapping;
1287 struct _SubMdspanMapping<_LayoutSide::__left, false>
1289 using _Layout = layout_left;
1290 template<
size_t _Pad>
using _PaddedLayout = layout_left_padded<_Pad>;
1292 template<
typename _Mapping,
size_t _Us>
1293 static consteval size_t
1296 using _Extents =
typename _Mapping::extents_type;
1297 constexpr auto __sta_exts = __mdspan::__static_extents<_Extents>(0, _Us);
1298 if constexpr (!__mdspan::__all_static(__sta_exts))
1299 return dynamic_extent;
1301 return __mdspan::__fwd_prod(__sta_exts);
1304 template<
size_t _Nm>
1305 static consteval bool
1306 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
size_t __sub_rank)
1307 {
return __mdspan::__is_block(__slice_kinds, __sub_rank); }
1311 struct _SubMdspanMapping<_LayoutSide::__left, true>
1313 using _Layout = layout_left;
1314 template<
size_t _Pad>
using _PaddedLayout = layout_left_padded<_Pad>;
1316 template<
typename _Mapping,
size_t _Us>
1317 static consteval size_t
1320 using _Extents =
typename _Mapping::extents_type;
1321 constexpr auto __sta_exts
1322 = __mdspan::__static_extents<_Extents>(1, _Us);
1323 constexpr auto __sta_padstride
1324 = __mdspan::__get_static_stride<_Mapping>();
1325 if constexpr (__sta_padstride == dynamic_extent
1326 || !__mdspan::__all_static(__sta_exts))
1327 return dynamic_extent;
1329 return __sta_padstride * __mdspan::__fwd_prod(__sta_exts);
1332 template<
size_t _Nm>
1333 static consteval bool
1334 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
1337 if (__sub_rank == 1)
1338 return __slice_kinds[0] == _SliceKind::__unit_strided_slice
1339 || __slice_kinds[0] == _SliceKind::__full;
1346 struct _SubMdspanMapping<_LayoutSide::__right, false>
1348 using _Layout = layout_right;
1349 template<
size_t _Pad>
using _PaddedLayout = layout_right_padded<_Pad>;
1351 template<
typename _Mapping,
size_t _Us>
1352 static consteval size_t
1355 using _Extents =
typename _Mapping::extents_type;
1356 constexpr auto __rank = _Extents::rank();
1357 constexpr auto __sta_exts
1358 = __mdspan::__static_extents<_Extents>(_Us + 1, __rank);
1359 if constexpr (!__mdspan::__all_static(__sta_exts))
1360 return dynamic_extent;
1362 return __fwd_prod(__sta_exts);
1365 template<
size_t _Nm>
1366 static consteval bool
1367 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
1370 auto __rev_slice_kinds = array<_SliceKind, _Nm>{};
1371 for(
size_t __i = 0; __i < _Nm; ++__i)
1372 __rev_slice_kinds[__i] = __slice_kinds[_Nm - 1 - __i];
1373 return __mdspan::__is_block(span(__rev_slice_kinds), __sub_rank);
1378 struct _SubMdspanMapping<_LayoutSide::__right, true>
1380 using _Layout = layout_right;
1381 template<
size_t _Pad>
using _PaddedLayout = layout_right_padded<_Pad>;
1383 template<
typename _Mapping,
size_t _Us>
1384 static consteval size_t
1387 using _Extents =
typename _Mapping::extents_type;
1388 constexpr auto __rank = _Extents::rank();
1389 constexpr auto __sta_exts
1390 = __mdspan::__static_extents<_Extents>(_Us + 1, __rank - 1);
1391 constexpr auto __sta_padstride
1392 = __mdspan::__get_static_stride<_Mapping>();
1393 if constexpr (__sta_padstride == dynamic_extent
1394 || !__mdspan::__all_static(__sta_exts))
1395 return dynamic_extent;
1397 return __sta_padstride * __mdspan::__fwd_prod(__sta_exts);
1400 template<
size_t _Nm>
1401 static consteval bool
1402 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
1405 if (__sub_rank == 1)
1406 return __slice_kinds[_Nm - 1] == _SliceKind::__unit_strided_slice
1407 || __slice_kinds[_Nm - 1] == _SliceKind::__full;
1414 template<
typename _Mapping>
1416 __submdspan_mapping_impl(
const _Mapping& __mapping)
1417 {
return submdspan_mapping_result{__mapping, 0}; }
1419 template<
typename _Mapping,
typename... _Slices>
1420 requires (
sizeof...(_Slices) > 0)
1422 __submdspan_mapping_impl(
const _Mapping& __mapping, _Slices... __slices)
1424 using _IndexType =
typename _Mapping::index_type;
1425 static_assert((__acceptable_slice_type<_Slices, _IndexType> && ...));
1427 constexpr auto __side = __mdspan::__mapping_side<_Mapping>();
1428 constexpr auto __rank =
sizeof...(_Slices);
1429 using _Trait = _SubMdspanMapping<__side, __is_padded_mapping<_Mapping>>;
1430 using _SliceView = span<const _SliceKind, __rank>;
1432 constexpr auto __slice_kinds = __mdspan::__make_slice_kind_array<_Slices...>();
1433 auto __offset = __mdspan::__suboffset(__mapping, __slices...);
1434 auto __sub_exts = __mdspan::__subextents(__mapping.extents(), __slices...);
1435 using _SubExts =
decltype(__sub_exts);
1436 constexpr auto __sub_rank = _SubExts::rank();
1437 if constexpr (__sub_rank == 0)
1438 return submdspan_mapping_result{
1439 typename _Trait::_Layout::mapping(__sub_exts), __offset};
1440 else if constexpr (_Trait::_S_is_unpadded_submdspan(
1441 _SliceView(__slice_kinds), __sub_rank))
1442 return submdspan_mapping_result{
1443 typename _Trait::_Layout::mapping(__sub_exts), __offset};
1445 constexpr auto __u = __padded_block_begin<__side>(
1446 _SliceView(__slice_kinds), __sub_rank);
1447 __u != dynamic_extent)
1449 constexpr auto __pad = _Trait::template _S_pad<_Mapping, __u>();
1450 using _Layout =
typename _Trait::template _PaddedLayout<__pad>;
1451 return submdspan_mapping_result{
1452 typename _Layout::mapping(__sub_exts, __mapping.stride(__u)),
1458 = __mdspan::__substrides<_SubExts>(__mapping, __slices...);
1459 return submdspan_mapping_result{
1460 layout_stride::mapping(__sub_exts, __sub_strides), __offset};
1466 template<
typename _Extents>
1467 class layout_left::mapping
1470 using extents_type = _Extents;
1471 using index_type =
typename extents_type::index_type;
1472 using size_type =
typename extents_type::size_type;
1473 using rank_type =
typename extents_type::rank_type;
1474 using layout_type = layout_left;
1476 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1477 "The size of extents_type must be representable as index_type");
1480 mapping() noexcept = default;
1483 mapping(const mapping&) noexcept = default;
1486 mapping(const extents_type& __extents) noexcept
1487 : _M_extents(__extents)
1488 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
1490 template<
typename _OExtents>
1491 requires is_constructible_v<extents_type, _OExtents>
1492 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1493 mapping(
const mapping<_OExtents>& __other) noexcept
1494 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1497 template<
typename _OExtents>
1498 requires (extents_type::rank() <= 1)
1499 && is_constructible_v<extents_type, _OExtents>
1500 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1501 mapping(
const layout_right::mapping<_OExtents>& __other) noexcept
1502 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1506 template<
typename _OExtents>
1507 requires is_constructible_v<extents_type, _OExtents>
1508 constexpr explicit(!(extents_type::rank() == 0
1509 && is_convertible_v<_OExtents, extents_type>))
1510 mapping(
const layout_stride::mapping<_OExtents>& __other) noexcept
1511 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1512 { __glibcxx_assert(*
this == __other); }
1514#if __glibcxx_padded_layouts
1515 template<
typename _LeftpadMapping>
1516 requires __mdspan::__is_left_padded_mapping<_LeftpadMapping>
1517 && is_constructible_v<extents_type,
1518 typename _LeftpadMapping::extents_type>
1520 explicit(!is_convertible_v<
typename _LeftpadMapping::extents_type,
1522 mapping(
const _LeftpadMapping& __other) noexcept
1523 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1525 constexpr size_t __ostride_sta
1526 = __mdspan::__get_static_stride<_LeftpadMapping>();
1528 if constexpr (extents_type::rank() > 1)
1530 if constexpr (extents_type::static_extent(0) != dynamic_extent
1531 && __ostride_sta != dynamic_extent)
1532 static_assert(extents_type::static_extent(0) == __ostride_sta);
1534 __glibcxx_assert(__other.stride(1)
1535 == __other.extents().extent(0));
1541 operator=(
const mapping&)
noexcept =
default;
1543 constexpr const extents_type&
1544 extents() const noexcept {
return _M_extents; }
1546 constexpr index_type
1547 required_span_size() const noexcept
1548 {
return __mdspan::__size(_M_extents); }
1552 template<__mdspan::__val
id_index_type<index_type>... _Indices>
1553 requires (
sizeof...(_Indices) == extents_type::rank())
1554 constexpr index_type
1555 operator()(_Indices... __indices)
const noexcept
1557 return __mdspan::__linear_index_left(_M_extents,
1558 static_cast<index_type
>(
std::move(__indices))...);
1561 static constexpr bool
1562 is_always_unique() noexcept {
return true; }
1564 static constexpr bool
1565 is_always_exhaustive() noexcept {
return true; }
1567 static constexpr bool
1568 is_always_strided() noexcept {
return true; }
1570 static constexpr bool
1571 is_unique() noexcept {
return true; }
1573 static constexpr bool
1574 is_exhaustive() noexcept {
return true; }
1576 static constexpr bool
1577 is_strided() noexcept {
return true; }
1579 constexpr index_type
1580 stride(rank_type __i)
const noexcept
1581 requires (extents_type::rank() > 0)
1583 __glibcxx_assert(__i < extents_type::rank());
1584 return __mdspan::__fwd_prod(_M_extents, __i);
1587 template<
typename _OExtents>
1588 requires (extents_type::rank() == _OExtents::rank())
1589 friend constexpr bool
1590 operator==(
const mapping& __self,
const mapping<_OExtents>& __other)
1592 {
return __self.extents() == __other.extents(); }
1595 template<
typename _OExtents>
1597 mapping(
const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
1598 : _M_extents(__oexts)
1600 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1601 "The size of OtherExtents must be representable as index_type");
1602 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
1605#if __glibcxx_submdspan
1606 template<
typename... _Slices>
1607 requires (extents_type::rank() ==
sizeof...(_Slices))
1608 friend constexpr auto
1609 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
1610 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
1613 [[no_unique_address]] extents_type _M_extents{};
1618 template<
typename _Extents,
typename... _Indices>
1619 constexpr typename _Extents::index_type
1620 __linear_index_right(
const _Extents& __exts, _Indices... __indices)
1623 using _IndexType =
typename _Extents::index_type;
1624 array<_IndexType,
sizeof...(__indices)> __ind_arr{__indices...};
1625 _IndexType __res = 0;
1626 if constexpr (
sizeof...(__indices) > 0)
1628 _IndexType __mult = 1;
1629 auto __update = [&, __pos = __exts.rank()](_IndexType)
mutable
1632 _GLIBCXX_DEBUG_ASSERT(cmp_less(__ind_arr[__pos],
1633 __exts.extent(__pos)));
1634 __res += __ind_arr[__pos] * __mult;
1635 __mult *= __exts.extent(__pos);
1637 (__update(__indices), ...);
1643 template<
typename _Extents>
1644 class layout_right::mapping
1647 using extents_type = _Extents;
1648 using index_type =
typename extents_type::index_type;
1649 using size_type =
typename extents_type::size_type;
1650 using rank_type =
typename extents_type::rank_type;
1651 using layout_type = layout_right;
1653 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1654 "The size of extents_type must be representable as index_type");
1657 mapping() noexcept = default;
1660 mapping(const mapping&) noexcept = default;
1663 mapping(const extents_type& __extents) noexcept
1664 : _M_extents(__extents)
1665 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
1667 template<
typename _OExtents>
1668 requires is_constructible_v<extents_type, _OExtents>
1669 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1670 mapping(
const mapping<_OExtents>& __other) noexcept
1671 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1674 template<
typename _OExtents>
1675 requires (extents_type::rank() <= 1)
1676 && is_constructible_v<extents_type, _OExtents>
1677 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1678 mapping(
const layout_left::mapping<_OExtents>& __other) noexcept
1679 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1682 template<
typename _OExtents>
1683 requires is_constructible_v<extents_type, _OExtents>
1684 constexpr explicit(!(extents_type::rank() == 0
1685 && is_convertible_v<_OExtents, extents_type>))
1686 mapping(
const layout_stride::mapping<_OExtents>& __other) noexcept
1687 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1688 { __glibcxx_assert(*
this == __other); }
1690#if __glibcxx_padded_layouts
1691 template<
typename _RightPaddedMapping>
1692 requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
1693 && is_constructible_v<extents_type,
1694 typename _RightPaddedMapping::extents_type>
1696 explicit(!is_convertible_v<
typename _RightPaddedMapping::extents_type,
1698 mapping(
const _RightPaddedMapping& __other) noexcept
1699 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1701 constexpr size_t __rank = extents_type::rank();
1702 constexpr size_t __ostride_sta
1703 = __mdspan::__get_static_stride<_RightPaddedMapping>();
1705 if constexpr (__rank > 1)
1707 if constexpr (extents_type::static_extent(__rank - 1) != dynamic_extent
1708 && __ostride_sta != dynamic_extent)
1709 static_assert(extents_type::static_extent(__rank - 1)
1712 __glibcxx_assert(__other.stride(__rank - 2)
1713 == __other.extents().extent(__rank - 1));
1719 operator=(
const mapping&)
noexcept =
default;
1721 constexpr const extents_type&
1722 extents() const noexcept {
return _M_extents; }
1724 constexpr index_type
1725 required_span_size() const noexcept
1726 {
return __mdspan::__size(_M_extents); }
1730 template<__mdspan::__val
id_index_type<index_type>... _Indices>
1731 requires (
sizeof...(_Indices) == extents_type::rank())
1732 constexpr index_type
1733 operator()(_Indices... __indices)
const noexcept
1735 return __mdspan::__linear_index_right(
1736 _M_extents,
static_cast<index_type
>(
std::move(__indices))...);
1739 static constexpr bool
1740 is_always_unique() noexcept
1743 static constexpr bool
1744 is_always_exhaustive() noexcept
1747 static constexpr bool
1748 is_always_strided() noexcept
1751 static constexpr bool
1752 is_unique() noexcept
1755 static constexpr bool
1756 is_exhaustive() noexcept
1759 static constexpr bool
1760 is_strided() noexcept
1763 constexpr index_type
1764 stride(rank_type __i)
const noexcept
1765 requires (extents_type::rank() > 0)
1767 __glibcxx_assert(__i < extents_type::rank());
1768 return __mdspan::__rev_prod(_M_extents, __i);
1771 template<
typename _OExtents>
1772 requires (extents_type::rank() == _OExtents::rank())
1773 friend constexpr bool
1774 operator==(
const mapping& __self,
const mapping<_OExtents>& __other)
1776 {
return __self.extents() == __other.extents(); }
1779 template<
typename _OExtents>
1781 mapping(
const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
1782 : _M_extents(__oexts)
1784 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1785 "The size of OtherExtents must be representable as index_type");
1786 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
1789#if __glibcxx_submdspan
1790 template<
typename... _Slices>
1791 requires (extents_type::rank() ==
sizeof...(_Slices))
1792 friend constexpr auto
1793 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
1794 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
1797 [[no_unique_address]] extents_type _M_extents{};
1802 template<
typename _Mp>
1803 concept __mapping_alike =
requires
1805 requires __is_extents<typename _Mp::extents_type>;
1806 { _Mp::is_always_strided() } -> same_as<bool>;
1807 { _Mp::is_always_exhaustive() } -> same_as<bool>;
1808 { _Mp::is_always_unique() } -> same_as<bool>;
1809 bool_constant<_Mp::is_always_strided()>::value;
1810 bool_constant<_Mp::is_always_exhaustive()>::value;
1811 bool_constant<_Mp::is_always_unique()>::value;
1814 template<
typename _Mapping,
typename... _Indices>
1815 constexpr typename _Mapping::index_type
1816 __linear_index_strides(
const _Mapping& __m, _Indices... __indices)
1819 using _IndexType =
typename _Mapping::index_type;
1820 _IndexType __res = 0;
1821 if constexpr (
sizeof...(__indices) > 0)
1823 auto __update = [&, __pos = 0u](_IndexType __idx)
mutable
1825 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx,
1826 __m.extents().extent(__pos)));
1827 __res += __idx * __m.stride(__pos++);
1829 (__update(__indices), ...);
1835 template<
typename _Extents>
1836 class layout_stride::mapping
1839 using extents_type = _Extents;
1840 using index_type =
typename extents_type::index_type;
1841 using size_type =
typename extents_type::size_type;
1842 using rank_type =
typename extents_type::rank_type;
1843 using layout_type = layout_stride;
1845 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1846 "The size of extents_type must be representable as index_type");
1853 size_t __stride = 1;
1854 for (
size_t __i = extents_type::rank(); __i > 0; --__i)
1856 _M_strides[__i - 1] = index_type(__stride);
1857 __stride *= size_t(_M_extents.extent(__i - 1));
1862 mapping(
const mapping&)
noexcept =
default;
1864 template<
typename _OIndexType>
1865 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1867 mapping(
const extents_type& __exts,
1868 span<_OIndexType, extents_type::rank()> __strides) noexcept
1869 : _M_extents(__exts)
1871 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1872 _M_strides[__i] = index_type(as_const(__strides[__i]));
1875 template<
typename _OIndexType>
1876 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1878 mapping(
const extents_type& __exts,
1879 const array<_OIndexType, extents_type::rank()>& __strides)
1882 span<
const _OIndexType, extents_type::rank()>(__strides))
1885 template<__mdspan::__mapping_alike _Str
idedMapping>
1886 requires (is_constructible_v<extents_type,
1887 typename _StridedMapping::extents_type>
1888 && _StridedMapping::is_always_unique()
1889 && _StridedMapping::is_always_strided())
1890 constexpr explicit(!(
1891 is_convertible_v<typename _StridedMapping::extents_type, extents_type>
1892 && __mdspan::__standardized_mapping<_StridedMapping>))
1893 mapping(
const _StridedMapping& __other) noexcept
1894 : _M_extents(__other.extents())
1896 using _OIndexType = _StridedMapping::index_type;
1897 using _OExtents = _StridedMapping::extents_type;
1899 __glibcxx_assert(__mdspan::__offset(__other) == 0);
1900 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1901 "The size of StridedMapping::extents_type must be representable as"
1903 if constexpr (cmp_greater(__gnu_cxx::__int_traits<_OIndexType>::__max,
1904 __gnu_cxx::__int_traits<index_type>::__max))
1905 __glibcxx_assert(!cmp_less(
1906 __gnu_cxx::__int_traits<index_type>::__max,
1907 __other.required_span_size())
1908 &&
"other.required_span_size() must be representable"
1910 if constexpr (extents_type::rank() > 0)
1911 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1912 _M_strides[__i] = index_type(__other.stride(__i));
1916 operator=(
const mapping&)
noexcept =
default;
1918 constexpr const extents_type&
1919 extents() const noexcept {
return _M_extents; }
1921 constexpr array<index_type, extents_type::rank()>
1922 strides() const noexcept
1924 array<index_type, extents_type::rank()> __ret;
1925 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1926 __ret[__i] = _M_strides[__i];
1930 constexpr index_type
1931 required_span_size() const noexcept
1933 if (__mdspan::__empty(_M_extents))
1936 index_type __ret = 1;
1937 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1938 __ret += (_M_extents.extent(__i) - 1) * _M_strides[__i];
1944 template<__mdspan::__val
id_index_type<index_type>... _Indices>
1945 requires (
sizeof...(_Indices) == extents_type::rank())
1946 constexpr index_type
1947 operator()(_Indices... __indices)
const noexcept
1949 return __mdspan::__linear_index_strides(*
this,
1950 static_cast<index_type
>(
std::move(__indices))...);
1953 static constexpr bool
1954 is_always_unique() noexcept {
return true; }
1958 static constexpr bool
1959 is_always_exhaustive() noexcept
1961 return (_Extents::rank() == 0) || __mdspan::__contains_zero(
1962 __mdspan::__static_extents<extents_type>());
1965 static constexpr bool
1966 is_always_strided() noexcept {
return true; }
1968 static constexpr bool
1969 is_unique() noexcept {
return true; }
1974 is_exhaustive() const noexcept
1976 if constexpr (!is_always_exhaustive())
1978 auto __size = __mdspan::__size(_M_extents);
1980 return __size == required_span_size();
1985 static constexpr bool
1986 is_strided() noexcept {
return true; }
1988 constexpr index_type
1989 stride(rank_type __r)
const noexcept {
return _M_strides[__r]; }
1991 template<__mdspan::__mapping_alike _OMapping>
1992 requires ((extents_type::rank() == _OMapping::extents_type::rank())
1993 && _OMapping::is_always_strided())
1994 friend constexpr bool
1995 operator==(
const mapping& __self,
const _OMapping& __other)
noexcept
1997 if (__self.extents() != __other.extents())
1999 if constexpr (extents_type::rank() > 0)
2000 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
2001 if (!cmp_equal(__self.stride(__i), __other.stride(__i)))
2003 return __mdspan::__offset(__other) == 0;
2007#if __glibcxx_submdspan
2008 template<
typename... _Slices>
2009 requires (extents_type::rank() ==
sizeof...(_Slices))
2010 friend constexpr auto
2011 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
2013 if constexpr (
sizeof...(_Slices) == 0)
2014 return submdspan_mapping_result{__mapping, 0};
2017 auto __offset = __mdspan::__suboffset(__mapping, __slices...);
2018 auto __sub_exts = __mdspan::__subextents(__mapping.extents(), __slices...);
2020 = __mdspan::__substrides<decltype(__sub_exts)>(__mapping, __slices...);
2021 return submdspan_mapping_result{
2022 layout_stride::mapping(__sub_exts, __sub_strides), __offset};
2027 using _Strides =
typename __array_traits<index_type,
2028 extents_type::rank()>::_Type;
2029 [[no_unique_address]] extents_type _M_extents;
2030 [[no_unique_address]] _Strides _M_strides;
2033#ifdef __glibcxx_padded_layouts
2037 __least_multiple(
size_t __x,
size_t __y)
2041 return (__y / __x + (__y % __x != 0)) * __x ;
2044 template<
typename _IndexType>
2046 __is_representable_least_multiple(
size_t __x,
size_t __y)
2048 constexpr auto __y_max = __gnu_cxx::__int_traits<_IndexType>::__max;
2049 if(std::cmp_greater(__y, __y_max))
2055 auto __max_delta = __y_max -
static_cast<_IndexType
>(__y);
2056 auto __y_mod_x = __y % __x;
2057 auto __delta = (__y_mod_x == 0) ?
size_t(0) : (__x - __y_mod_x);
2058 return std::cmp_less_equal(__delta, __max_delta);
2061 template<
typename _Extents,
size_t _PaddingValue,
typename _LayoutTraits,
2062 size_t _Rank = _Extents::rank()>
2063 concept __valid_static_stride = (_Extents::rank() <= 1)
2064 || (_PaddingValue == dynamic_extent)
2065 || (_Extents::static_extent(_LayoutTraits::_S_ext_idx) == dynamic_extent)
2066 || (__is_representable_least_multiple<size_t>(
2067 _PaddingValue, _Extents::static_extent(_LayoutTraits::_S_ext_idx)));
2069 template<
size_t _PaddedStride,
typename _Extents,
2070 typename _LayoutTraits>
2072 __is_representable_padded_size()
2074 using _IndexType =
typename _Extents::index_type;
2075 auto __sta_exts = __static_extents<_Extents>(
2076 _LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
2077 size_t __max = __gnu_cxx::__int_traits<_IndexType>::__max;
2078 return __static_quotient(__sta_exts, __max / _PaddedStride) != 0;
2081 template<
typename _Extents,
size_t _PaddedStride,
typename _LayoutTraits,
2082 size_t _Rank = _Extents::rank()>
2083 concept __valid_padded_size = (_Rank <= 1)
2084 || (_PaddedStride == dynamic_extent)
2085 || (!__all_static(__static_extents<_Extents>()))
2086 || (__contains_zero(__static_extents<_Extents>()))
2087 || (__is_representable_padded_size<_PaddedStride, _Extents,
2090 template<
typename _Extents,
typename _Stride,
typename... _Indices>
2091 constexpr typename _Extents::index_type
2092 __linear_index_leftpad(
const _Extents& __exts, _Stride __stride,
2093 _Indices... __indices)
2096 using _IndexType =
typename _Extents::index_type;
2097 _IndexType __res = 0;
2098 if constexpr (
sizeof...(__indices) > 0)
2100 _IndexType __mult = 1;
2102 auto __update_rest = [&, __pos = 1u](_IndexType __idx)
mutable
2104 __res += __idx * __mult;
2105 __mult *= __exts.extent(__pos);
2109 auto __update = [&](_IndexType __idx,
auto... __rest)
2112 __mult = __stride.extent(0);
2113 (__update_rest(__rest), ...);
2115 __update(__indices...);
2120 template<
typename _Extents,
typename _Stride,
typename... _Indices>
2121 constexpr typename _Extents::index_type
2122 __linear_index_rightpad(
const _Extents& __exts, _Stride __stride,
2123 _Indices... __indices)
2126 using _IndexType =
typename _Extents::index_type;
2127 _IndexType __res = 0;
2128 if constexpr (
sizeof...(__indices) > 0)
2130 _IndexType __mult = 1;
2131 array<_IndexType,
sizeof...(__indices)> __ind_arr{__indices...};
2133 auto __update_rest = [&, __pos = __exts.rank()-1](_IndexType)
mutable
2136 __res += __ind_arr[__pos] * __mult;
2137 __mult *= __exts.extent(__pos);
2140 auto __update = [&](_IndexType,
auto... __rest)
2142 __res += __ind_arr[__exts.rank() - 1];
2143 __mult = __stride.extent(0);
2144 (__update_rest(__rest), ...);
2146 __update(__indices...);
2151 template<
size_t _Rank>
2152 struct _LeftPaddedLayoutTraits
2154 using _LayoutSame = layout_left;
2155 using _LayoutOther = layout_right;
2157 constexpr static const size_t _S_ext_idx = 0;
2158 constexpr static const size_t _S_stride_idx = 1;
2159 constexpr static const size_t _S_unpad_begin = 1;
2160 constexpr static const size_t _S_unpad_end = _Rank;
2162 template<
typename _IndexType,
size_t _StaticStride,
size_t..._Extents>
2163 constexpr static auto
2164 _S_make_padded_extent(
2165 extents<_IndexType, _StaticStride> __stride,
2166 const extents<_IndexType, _Extents...>& __exts)
2168 auto __impl = [&]<
size_t... _Is>(integer_sequence<size_t, _Is...>)
2170 return extents<_IndexType, _StaticStride,
2171 (_Extents...[_Is + 1])...>{
2172 __stride.extent(0), __exts.extent(_Is + 1)...};
2174 return __impl(make_index_sequence<
sizeof...(_Extents) - 1>());
2178 template<
size_t _Rank>
2179 struct _RightPaddedLayoutTraits
2181 using _LayoutSame = layout_right;
2182 using _LayoutOther = layout_left;
2184 constexpr static size_t _S_ext_idx = _Rank - 1;
2185 constexpr static size_t _S_stride_idx = _Rank - 2;
2186 constexpr static size_t _S_unpad_begin = 0;
2187 constexpr static size_t _S_unpad_end = _Rank - 1;
2189 template<
typename _IndexType,
size_t _StaticStride,
size_t..._Extents>
2190 constexpr static auto
2191 _S_make_padded_extent(
2192 extents<_IndexType, _StaticStride> __stride,
2193 const extents<_IndexType, _Extents...>& __exts)
2195 auto __impl = [&]<
size_t... _Is>(integer_sequence<size_t, _Is...>)
2197 return extents<_IndexType, (_Extents...[_Is])..., _StaticStride>{
2198 __exts.extent(_Is)..., __stride.extent(0)};
2200 return __impl(make_index_sequence<
sizeof...(_Extents) - 1>());
2204 template<
size_t _PaddingValue,
typename _Extents,
typename _LayoutTraits>
2205 class _PaddedStorage
2207 using _LayoutSame =
typename _LayoutTraits::_LayoutSame;
2210 using _IndexType =
typename _Extents::index_type;
2211 constexpr static size_t _S_rank = _Extents::rank();
2215 static_assert((_PaddingValue == dynamic_extent)
2216 || (cmp_less_equal(_PaddingValue,
2217 __gnu_cxx::__int_traits<_IndexType>::__max)),
2218 "padding_value must be representable as index_type");
2220 static_assert(__representable_size<_Extents, _IndexType>,
2221 "The size of extents_type must be representable as index_type");
2223 static_assert(__valid_static_stride<_Extents, _PaddingValue,
2225 "The padded stride must be representable as size_t");
2227 static constexpr size_t _S_static_stride = []
consteval
2229 constexpr size_t __rank = _Extents::rank();
2230 if constexpr (__rank <= 1)
2234 constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
2235 constexpr size_t __sta_ext = _Extents::static_extent(__ext_idx);
2236 if constexpr (__sta_ext == 0)
2238 else if constexpr (_PaddingValue == dynamic_extent
2239 || __sta_ext == dynamic_extent)
2240 return dynamic_extent;
2242 return __least_multiple(_PaddingValue, __sta_ext);
2246 static_assert(_S_static_stride == dynamic_extent
2247 || cmp_less_equal(_S_static_stride,
2248 __gnu_cxx::__int_traits<_IndexType>::__max),
2249 "Padded stride must be representable as index_type");
2251 static_assert(__valid_padded_size<_Extents, _S_static_stride,
2255 _PaddedStorage() noexcept
2257 if constexpr (_S_rank > 1)
2258 if constexpr (_S_static_stride == dynamic_extent
2259 && _S_static_padextent() != dynamic_extent)
2260 _M_stride = _Stride{_S_static_padextent()};
2264 _PaddedStorage(
const _Extents& __exts)
2265 : _M_extents(__exts)
2267 if constexpr (!__all_static(__static_extents<_Extents>()))
2268 __glibcxx_assert(__is_representable_extents(_M_extents));
2270 if constexpr (_S_rank > 1)
2272 _IndexType __stride;
2273 if constexpr (_PaddingValue == dynamic_extent)
2274 __stride = _M_padextent();
2275 else if constexpr (_S_static_padextent() != dynamic_extent)
2280 __is_representable_least_multiple<_IndexType>(
2281 _PaddingValue, _M_padextent()));
2283 __stride =
static_cast<_IndexType
>(
2284 __least_multiple(_PaddingValue, _M_padextent()));
2286 __glibcxx_assert(__is_representable_extents(
2287 _LayoutTraits::_S_make_padded_extent(
2288 std::dextents<_IndexType, 1>{__stride},
2291 _M_stride = _Stride{__stride};
2296 _PaddedStorage(
const _Extents& __exts, _IndexType __pad)
2297 : _M_extents(__exts)
2299 if constexpr (_PaddingValue != dynamic_extent)
2300 __glibcxx_assert(cmp_equal(_PaddingValue, __pad));
2301 if constexpr (_S_rank > 1 && _S_static_stride == dynamic_extent)
2304 __is_representable_least_multiple<_IndexType>(
2305 __pad, _M_padextent()));
2307 _M_stride = _Stride{
static_cast<_IndexType
>(
2308 __least_multiple(__pad, _M_padextent()))};
2310 __glibcxx_assert(__is_representable_extents(
2311 _LayoutTraits::_S_make_padded_extent(
2312 _M_stride, _M_extents)));
2316 template<
typename _OExtents>
2319 const typename _LayoutSame::template mapping<_OExtents>& __other)
2320 : _PaddedStorage(_Extents(__other.extents()))
2322 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
2323 constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
2324 if constexpr (_S_rank > 1 && _PaddingValue != dynamic_extent)
2326 static_assert(_S_static_stride == dynamic_extent
2327 || _OExtents::static_extent(__ext_idx) == dynamic_extent
2328 || _S_static_stride == _OExtents::static_extent(__ext_idx),
2329 "The padded stride must be compatible with other");
2331 if constexpr (_S_static_stride == dynamic_extent
2332 || _OExtents::static_extent(__stride_idx) == dynamic_extent)
2333 __glibcxx_assert(std::cmp_equal(_M_padstride(),
2338 template<
typename _OExtents>
2340 _PaddedStorage(
const typename layout_stride::mapping<_OExtents>&
2342 : _M_extents(__other.extents())
2344 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
2348 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
2349 if constexpr (_S_rank > 1)
2351 if constexpr (_PaddingValue != dynamic_extent)
2352 __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
2353 _M_calc_padstride())
2354 &&
"The padded stride must be compatible with other");
2355 if constexpr (_S_static_stride == dynamic_extent)
2356 _M_stride = _Stride{__other.stride(__stride_idx)};
2360 template<
typename _SamePaddedMapping>
2362 _PaddedStorage(_LayoutTraits::_LayoutSame,
2363 const _SamePaddedMapping& __other)
2364 : _M_extents(__other.extents())
2366 if constexpr (_S_rank > 1)
2368 static_assert(_PaddingValue == dynamic_extent
2369 || _SamePaddedMapping::padding_value == dynamic_extent
2370 || _PaddingValue == _SamePaddedMapping::padding_value,
2371 "If neither PaddingValue is dynamic_extent, then they must "
2374 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
2375 if constexpr (_PaddingValue != dynamic_extent)
2376 __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
2377 _M_calc_padstride())
2378 &&
"The padded stride must be compatible with other");
2379 if constexpr (_S_static_stride == dynamic_extent)
2380 _M_stride = _Stride{__other.stride(__stride_idx)};
2382 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
2383 __gnu_cxx::__int_traits<_IndexType>::__max));
2386 template<
typename _OtherPaddedMapping>
2388 _PaddedStorage(_LayoutTraits::_LayoutOther,
2389 const _OtherPaddedMapping& __other) noexcept
2390 : _M_extents(__other.extents())
2392 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
2393 __gnu_cxx::__int_traits<_IndexType>::__max));
2396 static constexpr bool
2397 _M_is_always_exhaustive() noexcept
2399 if constexpr (_S_rank <= 1)
2402 return _S_static_padextent() != dynamic_extent
2403 && _S_static_stride != dynamic_extent
2404 && _S_static_padextent() == _S_static_stride;
2408 _M_is_exhaustive() const noexcept
2410 if constexpr (_M_is_always_exhaustive())
2413 return cmp_equal(_M_padextent(), _M_padstride());
2416 constexpr static size_t
2417 _S_static_padextent() noexcept
2418 {
return _Extents::static_extent(_LayoutTraits::_S_ext_idx); }
2420 constexpr _IndexType
2421 _M_padextent() const noexcept
2422 {
return _M_extents.extent(_LayoutTraits::_S_ext_idx); }
2424 constexpr _IndexType
2425 _M_calc_padstride() const noexcept
2427 if constexpr (_S_static_stride != dynamic_extent)
2428 return _S_static_stride;
2429 else if constexpr (_PaddingValue != dynamic_extent)
2430 return __least_multiple(_PaddingValue, _M_padextent());
2432 return _M_padextent();
2435 constexpr _IndexType
2436 _M_padstride() const noexcept
2437 {
return _M_stride.extent(0); }
2439 constexpr _IndexType
2440 _M_required_span_size() const noexcept
2442 if constexpr (_S_rank == 0)
2444 else if (__mdspan::__empty(_M_extents))
2448 size_t __stride =
static_cast<size_t>(_M_padstride());
2449 size_t __prod_rest = __mdspan::__fwd_prod(_M_extents,
2450 _LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
2451 size_t __delta = _M_padstride() - _M_padextent();
2452 return static_cast<_IndexType
>(__stride * __prod_rest - __delta);
2456 template<
typename _SamePaddedMapping>
2458 _M_equal(
const _SamePaddedMapping& __other)
const noexcept
2460 return _M_extents == __other.extents()
2462 || cmp_equal(_M_stride.extent(0),
2463 __other.stride(_LayoutTraits::_S_stride_idx)));
2466 using _Stride = std::extents<_IndexType, _S_static_stride>;
2467 [[no_unique_address]] _Stride _M_stride;
2468 [[no_unique_address]] _Extents _M_extents;
2472 template<
size_t _PaddingValue>
2473 template<
typename _Extents>
2474 class layout_left_padded<_PaddingValue>::mapping
2477 static constexpr size_t padding_value = _PaddingValue;
2479 using extents_type = _Extents;
2480 using index_type =
typename extents_type::index_type;
2481 using size_type =
typename extents_type::size_type;
2482 using rank_type =
typename extents_type::rank_type;
2483 using layout_type = layout_left_padded<padding_value>;
2486 static constexpr size_t _S_rank = extents_type::rank();
2487 using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
2488 _Extents, __mdspan::_LeftPaddedLayoutTraits<_S_rank>>;
2489 [[no_unique_address]] _PaddedStorage _M_storage;
2491 consteval friend size_t
2492 __mdspan::__get_static_stride<mapping>();
2494 constexpr index_type
2495 _M_extent(
size_t __r)
const noexcept
2496 {
return _M_storage._M_extents.extent(__r); }
2498 constexpr index_type
2499 _M_padstride() const noexcept
2500 {
return _M_storage._M_stride.extent(0); }
2508 mapping(
const mapping&)
noexcept =
default;
2511 mapping(
const extents_type& __exts)
2512 : _M_storage(__exts)
2515 template<__mdspan::__val
id_index_type<index_type> _OIndexType>
2517 mapping(
const extents_type& __exts, _OIndexType __pad)
2518 : _M_storage(__exts,
2519 __mdspan::__index_type_cast<index_type>(std::move(__pad)))
2522 template<
typename _OExtents>
2523 requires is_constructible_v<extents_type, _OExtents>
2524 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
2525 mapping(
const layout_left::mapping<_OExtents>& __other)
2526 : _M_storage(__other)
2529 template<
typename _OExtents>
2530 requires is_constructible_v<_OExtents, extents_type>
2531 constexpr explicit(!(_OExtents::rank() == 0
2532 && is_convertible_v<_OExtents, extents_type>))
2533 mapping(
const typename layout_stride::mapping<_OExtents>& __other)
2534 : _M_storage(__other)
2535 { __glibcxx_assert(*
this == __other); }
2537 template<
typename _LeftPaddedMapping>
2538 requires __mdspan::__is_left_padded_mapping<_LeftPaddedMapping>
2539 && is_constructible_v<extents_type,
2540 typename _LeftPaddedMapping::extents_type>
2542 !is_convertible_v<
typename _LeftPaddedMapping::extents_type,
2544 || _S_rank > 1 && (padding_value != dynamic_extent
2545 || _LeftPaddedMapping::padding_value == dynamic_extent))
2546 mapping(
const _LeftPaddedMapping& __other)
2547 : _M_storage(layout_left{}, __other)
2550 template<
typename _RightPaddedMapping>
2551 requires (__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2552 || __mdspan::__mapping_of<layout_right, _RightPaddedMapping>)
2554 && is_constructible_v<extents_type,
2555 typename _RightPaddedMapping::extents_type>
2556 constexpr explicit(!is_convertible_v<
2557 typename _RightPaddedMapping::extents_type, extents_type>)
2558 mapping(
const _RightPaddedMapping& __other) noexcept
2559 : _M_storage(layout_right{}, __other)
2563 operator=(
const mapping&)
noexcept =
default;
2565 constexpr const extents_type&
2566 extents() const noexcept {
return _M_storage._M_extents; }
2568 constexpr array<index_type, _S_rank>
2569 strides() const noexcept
2571 array<index_type, _S_rank> __ret;
2572 if constexpr (_S_rank > 0)
2574 if constexpr (_S_rank > 1)
2575 __ret[1] = _M_padstride();
2576 if constexpr (_S_rank > 2)
2577 for(
size_t __i = 2; __i < _S_rank; ++__i)
2578 __ret[__i] = __ret[__i - 1] * _M_extent(__i - 1);
2582 constexpr index_type
2583 required_span_size() const noexcept
2584 {
return _M_storage._M_required_span_size(); }
2588 template<__mdspan::__val
id_index_type<index_type>... _Indices>
2589 requires (
sizeof...(_Indices) == _S_rank)
2590 constexpr index_type
2591 operator()(_Indices... __indices)
const noexcept
2593 return __mdspan::__linear_index_leftpad(
2594 extents(), _M_storage._M_stride,
2595 static_cast<index_type
>(
std::move(__indices))...);
2598 static constexpr bool
2599 is_always_exhaustive() noexcept
2600 {
return _PaddedStorage::_M_is_always_exhaustive(); }
2603 is_exhaustive() noexcept
2604 {
return _M_storage._M_is_exhaustive(); }
2606 static constexpr bool
2607 is_always_unique() noexcept {
return true; }
2609 static constexpr bool
2610 is_unique() noexcept {
return true; }
2612 static constexpr bool
2613 is_always_strided() noexcept {
return true; }
2615 static constexpr bool
2616 is_strided() noexcept {
return true; }
2618 constexpr index_type
2619 stride(rank_type __r)
const noexcept
2621 __glibcxx_assert(__r < _S_rank);
2625 return static_cast<index_type
>(
2626 static_cast<size_t>(_M_padstride()) *
2627 static_cast<size_t>(__mdspan::__fwd_prod(extents(), 1, __r)));
2630 template<
typename _LeftpadMapping>
2631 requires(__mdspan::__is_left_padded_mapping<_LeftpadMapping>
2632 && _LeftpadMapping::extents_type::rank() == _S_rank)
2633 friend constexpr bool
2634 operator==(
const mapping& __self,
const _LeftpadMapping& __other)
2636 {
return __self._M_storage._M_equal(__other); }
2639#if __glibcxx_submdspan
2640 template<
typename... _Slices>
2641 requires (extents_type::rank() ==
sizeof...(_Slices))
2642 friend constexpr auto
2643 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
2644 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
2648 template<
size_t _PaddingValue>
2649 template<
typename _Extents>
2650 class layout_right_padded<_PaddingValue>::mapping {
2652 static constexpr size_t padding_value = _PaddingValue;
2653 using extents_type = _Extents;
2654 using index_type =
typename extents_type::index_type;
2655 using size_type =
typename extents_type::size_type;
2656 using rank_type =
typename extents_type::rank_type;
2657 using layout_type = layout_right_padded<_PaddingValue>;
2660 static constexpr size_t _S_rank = extents_type::rank();
2661 using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
2662 _Extents, __mdspan::_RightPaddedLayoutTraits<_S_rank>>;
2663 [[no_unique_address]] _PaddedStorage _M_storage;
2665 consteval friend size_t
2666 __mdspan::__get_static_stride<mapping>();
2668 constexpr index_type
2669 _M_extent(
size_t __r)
const noexcept
2670 {
return _M_storage._M_extents.extent(__r); }
2672 constexpr index_type
2673 _M_padstride() const noexcept
2674 {
return _M_storage._M_stride.extent(0); }
2682 mapping(
const mapping&)
noexcept =
default;
2685 mapping(
const extents_type& __exts)
2686 : _M_storage(__exts)
2689 template<__mdspan::__val
id_index_type<index_type> _OIndexType>
2691 mapping(
const extents_type& __exts, _OIndexType __pad)
2692 : _M_storage(__exts,
2693 __mdspan::__index_type_cast<index_type>(std::move(__pad)))
2696 template<
typename _OExtents>
2697 requires is_constructible_v<extents_type, _OExtents>
2698 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
2699 mapping(
const layout_right::mapping<_OExtents>& __other)
2700 : _M_storage(__other)
2703 template<
typename _OExtents>
2704 requires is_constructible_v<_OExtents, extents_type>
2705 constexpr explicit(!(_OExtents::rank() == 0
2706 && is_convertible_v<_OExtents, extents_type>))
2707 mapping(
const typename layout_stride::mapping<_OExtents>& __other)
2708 : _M_storage(__other)
2709 { __glibcxx_assert(*
this == __other); }
2711 template<
typename _RightPaddedMapping>
2712 requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2713 && is_constructible_v<extents_type,
2714 typename _RightPaddedMapping::extents_type>
2716 !is_convertible_v<
typename _RightPaddedMapping::extents_type,
2718 || _S_rank > 1 && (padding_value != dynamic_extent
2719 || _RightPaddedMapping::padding_value == dynamic_extent))
2720 mapping(
const _RightPaddedMapping& __other)
2721 : _M_storage(layout_right{}, __other)
2724 template<
typename _LeftPaddedMapping>
2725 requires (__mdspan::__is_left_padded_mapping<_LeftPaddedMapping>
2726 || __mdspan::__mapping_of<layout_left, _LeftPaddedMapping>)
2728 && is_constructible_v<extents_type,
2729 typename _LeftPaddedMapping::extents_type>
2730 constexpr explicit(!is_convertible_v<
2731 typename _LeftPaddedMapping::extents_type, extents_type>)
2732 mapping(
const _LeftPaddedMapping& __other) noexcept
2733 : _M_storage(layout_left{}, __other)
2736 constexpr mapping& operator=(
const mapping&)
noexcept =
default;
2738 constexpr const extents_type&
2739 extents() const noexcept {
return _M_storage._M_extents; }
2741 constexpr array<index_type, _S_rank>
2742 strides() const noexcept
2744 array<index_type, _S_rank> __ret;
2745 if constexpr (_S_rank > 0)
2746 __ret[_S_rank - 1] = 1;
2747 if constexpr (_S_rank > 1)
2748 __ret[_S_rank - 2] = _M_padstride();
2749 if constexpr (_S_rank > 2)
2750 for(
size_t __i = _S_rank - 2; __i > 0; --__i)
2751 __ret[__i - 1] = __ret[__i] * _M_extent(__i);
2755 constexpr index_type
2756 required_span_size() const noexcept
2757 {
return _M_storage._M_required_span_size(); }
2761 template<__mdspan::__val
id_index_type<index_type>... _Indices>
2762 requires (
sizeof...(_Indices) == _S_rank)
2763 constexpr index_type
2764 operator()(_Indices... __indices)
const noexcept
2766 return __mdspan::__linear_index_rightpad(
2767 extents(), _M_storage._M_stride,
2768 static_cast<index_type
>(
std::move(__indices))...);
2771 static constexpr bool
2772 is_always_exhaustive() noexcept
2773 {
return _PaddedStorage::_M_is_always_exhaustive(); }
2776 is_exhaustive() noexcept
2777 {
return _M_storage._M_is_exhaustive(); }
2779 static constexpr bool
2780 is_always_unique() noexcept {
return true; }
2782 static constexpr bool
2783 is_unique() noexcept {
return true; }
2785 static constexpr bool
2786 is_always_strided() noexcept {
return true; }
2788 static constexpr bool
2789 is_strided() noexcept {
return true; }
2791 constexpr index_type
2792 stride(rank_type __r)
const noexcept
2794 __glibcxx_assert(__r < _S_rank);
2795 if constexpr (_S_rank <= 1)
2797 else if (__r == _S_rank - 1)
2799 else if (__r == _S_rank - 2)
2800 return _M_padstride();
2802 return static_cast<index_type
>(
2803 static_cast<size_t>(_M_padstride()) *
2804 static_cast<size_t>(__mdspan::__fwd_prod(
2805 extents(), __r + 1, _S_rank - 1)));
2808 template<
typename _RightPaddedMapping>
2809 requires(__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2810 && _RightPaddedMapping::extents_type::rank() == _S_rank)
2811 friend constexpr bool
2812 operator==(
const mapping& __self,
const _RightPaddedMapping& __other)
2814 {
return __self._M_storage._M_equal(__other); }
2816#if __glibcxx_submdspan
2818 template<
typename... _Slices>
2819 requires (extents_type::rank() ==
sizeof...(_Slices))
2820 friend constexpr auto
2821 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
2822 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
2827 template<
typename _ElementType>
2828 struct default_accessor
2830 static_assert(!is_array_v<_ElementType>,
2831 "ElementType must not be an array type");
2832 static_assert(!is_abstract_v<_ElementType>,
2833 "ElementType must not be an abstract class type");
2835 using offset_policy = default_accessor;
2836 using element_type = _ElementType;
2837 using reference = element_type&;
2838 using data_handle_type = element_type*;
2841 default_accessor() noexcept = default;
2843 template<typename _OElementType>
2844 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
2846 default_accessor(default_accessor<_OElementType>) noexcept
2850 access(data_handle_type __p,
size_t __i)
const noexcept
2851 {
return __p[__i]; }
2853 constexpr data_handle_type
2854 offset(data_handle_type __p,
size_t __i)
const noexcept
2855 {
return __p + __i; }
2858#ifdef __glibcxx_aligned_accessor
2859 template<
typename _ElementType,
size_t _ByteAlignment>
2860 struct aligned_accessor
2862 static_assert(has_single_bit(_ByteAlignment),
2863 "ByteAlignment must be a power of two");
2864 static_assert(_ByteAlignment >=
alignof(_ElementType));
2866 using offset_policy = default_accessor<_ElementType>;
2867 using element_type = _ElementType;
2868 using reference = element_type&;
2869 using data_handle_type = element_type*;
2871 static constexpr size_t byte_alignment = _ByteAlignment;
2874 aligned_accessor() noexcept = default;
2876 template<typename _OElementType,
size_t _OByteAlignment>
2877 requires (_OByteAlignment >= byte_alignment)
2878 && is_convertible_v<_OElementType(*)[], element_type(*)[]>
2880 aligned_accessor(aligned_accessor<_OElementType, _OByteAlignment>)
2884 template<
typename _OElementType>
2885 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
2887 aligned_accessor(default_accessor<_OElementType>)
noexcept
2890 template<
typename _OElementType>
2891 requires is_convertible_v<element_type(*)[], _OElementType(*)[]>
2893 operator default_accessor<_OElementType>() const noexcept
2897 access(data_handle_type __p,
size_t __i)
const noexcept
2900 constexpr typename offset_policy::data_handle_type
2901 offset(data_handle_type __p,
size_t __i)
const noexcept
2908 template<
typename _Extents,
typename _IndexType,
size_t _Nm>
2910 __is_multi_index(
const _Extents& __exts, span<_IndexType, _Nm> __indices)
2912 static_assert(__exts.rank() == _Nm);
2913 for (
size_t __i = 0; __i < __exts.rank(); ++__i)
2914 if (__indices[__i] >= __exts.extent(__i))
2920 template<
typename _ElementType,
typename _Extents,
2921 typename _LayoutPolicy = layout_right,
2922 typename _AccessorPolicy = default_accessor<_ElementType>>
2925 static_assert(!is_array_v<_ElementType>,
2926 "ElementType must not be an array type");
2927 static_assert(!is_abstract_v<_ElementType>,
2928 "ElementType must not be an abstract class type");
2929 static_assert(__mdspan::__is_extents<_Extents>,
2930 "Extents must be a specialization of std::extents");
2931 static_assert(is_same_v<_ElementType,
2932 typename _AccessorPolicy::element_type>);
2935 using extents_type = _Extents;
2936 using layout_type = _LayoutPolicy;
2937 using accessor_type = _AccessorPolicy;
2938 using mapping_type =
typename layout_type::template mapping<extents_type>;
2939 using element_type = _ElementType;
2940 using value_type = remove_cv_t<element_type>;
2941 using index_type =
typename extents_type::index_type;
2942 using size_type =
typename extents_type::size_type;
2943 using rank_type =
typename extents_type::rank_type;
2944 using data_handle_type =
typename accessor_type::data_handle_type;
2945 using reference =
typename accessor_type::reference;
2947 static constexpr rank_type
2948 rank() noexcept {
return extents_type::rank(); }
2950 static constexpr rank_type
2951 rank_dynamic() noexcept {
return extents_type::rank_dynamic(); }
2953 static constexpr size_t
2954 static_extent(rank_type __r)
noexcept
2955 {
return extents_type::static_extent(__r); }
2957 constexpr index_type
2958 extent(rank_type __r)
const noexcept {
return extents().extent(__r); }
2962 requires (rank_dynamic() > 0)
2963 && is_default_constructible_v<data_handle_type>
2964 && is_default_constructible_v<mapping_type>
2965 && is_default_constructible_v<accessor_type> =
default;
2968 mdspan(
const mdspan& __other) =
default;
2971 mdspan(mdspan&& __other) =
default;
2973 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
2974 requires (
sizeof...(_OIndexTypes) == rank()
2975 ||
sizeof...(_OIndexTypes) == rank_dynamic())
2976 && is_constructible_v<mapping_type, extents_type>
2977 && is_default_constructible_v<accessor_type>
2979 mdspan(data_handle_type __handle, _OIndexTypes... __exts)
2981 _M_mapping(_Extents(
static_cast<index_type
>(
std::move(__exts))...)),
2985 template<
typename _OIndexType,
size_t _Nm>
2986 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
2987 && (_Nm == rank() || _Nm == rank_dynamic())
2988 && is_constructible_v<mapping_type, extents_type>
2989 && is_default_constructible_v<accessor_type>
2990 constexpr explicit(_Nm != rank_dynamic())
2991 mdspan(data_handle_type __handle, span<_OIndexType, _Nm> __exts)
2992 : _M_accessor(), _M_mapping(extents_type(__exts)),
2993 _M_handle(std::move(__handle))
2996 template<
typename _OIndexType,
size_t _Nm>
2997 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
2998 && (_Nm == rank() || _Nm == rank_dynamic())
2999 && is_constructible_v<mapping_type, extents_type>
3000 && is_default_constructible_v<accessor_type>
3001 constexpr explicit(_Nm != rank_dynamic())
3002 mdspan(data_handle_type __handle,
const array<_OIndexType, _Nm>& __exts)
3003 : _M_accessor(), _M_mapping(extents_type(__exts)),
3004 _M_handle(std::move(__handle))
3008 mdspan(data_handle_type __handle,
const extents_type& __exts)
3009 requires is_constructible_v<mapping_type, const extents_type&>
3010 && is_default_constructible_v<accessor_type>
3011 : _M_accessor(), _M_mapping(__exts), _M_handle(
std::move(__handle))
3015 mdspan(data_handle_type __handle,
const mapping_type& __mapping)
3016 requires is_default_constructible_v<accessor_type>
3017 : _M_accessor(), _M_mapping(__mapping), _M_handle(
std::move(__handle))
3021 mdspan(data_handle_type __handle,
const mapping_type& __mapping,
3022 const accessor_type& __accessor)
3023 : _M_accessor(__accessor), _M_mapping(__mapping),
3024 _M_handle(std::move(__handle))
3027 template<
typename _OElementType,
typename _OExtents,
typename _OLayout,
3028 typename _OAccessor>
3029 requires is_constructible_v<mapping_type,
3030 const typename _OLayout::template mapping<_OExtents>&>
3031 && is_constructible_v<accessor_type, const _OAccessor&>
3032 constexpr explicit(!is_convertible_v<
3033 const typename _OLayout::template mapping<_OExtents>&, mapping_type>
3034 || !is_convertible_v<const _OAccessor&, accessor_type>)
3035 mdspan(
const mdspan<_OElementType, _OExtents, _OLayout, _OAccessor>&
3037 : _M_accessor(__other.accessor()), _M_mapping(__other.mapping()),
3038 _M_handle(__other.data_handle())
3040 static_assert(is_constructible_v<data_handle_type,
3041 const typename _OAccessor::data_handle_type&>);
3042 static_assert(is_constructible_v<extents_type, _OExtents>);
3046 operator=(
const mdspan& __other) =
default;
3049 operator=(mdspan&& __other) =
default;
3051 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
3052 requires (
sizeof...(_OIndexTypes) == rank())
3054 operator[](_OIndexTypes... __indices)
const
3056 auto __checked_call = [
this](
auto... __idxs) -> index_type
3058 if constexpr (
sizeof...(__idxs) > 0)
3059 __glibcxx_assert(__mdspan::__is_multi_index(extents(),
3060 span<
const index_type,
sizeof...(__idxs)>({__idxs...})));
3061 return _M_mapping(__idxs...);
3064 auto __index = __checked_call(
3065 static_cast<index_type
>(
std::move(__indices))...);
3066 return _M_accessor.access(_M_handle, __index);
3069 template<
typename _OIndexType>
3070 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3072 operator[](span<_OIndexType, rank()> __indices)
const
3076 {
return (*
this)[index_type(as_const(__indices[_Counts]))...]; };
3077 return __call(make_index_sequence<rank()>());
3080 template<
typename _OIndexType>
3081 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3083 operator[](
const array<_OIndexType, rank()>& __indices)
const
3084 {
return (*
this)[span<const _OIndexType, rank()>(__indices)]; }
3087 size() const noexcept
3089 __glibcxx_assert(cmp_less_equal(_M_mapping.required_span_size(),
3092 return size_type(__mdspan::__size(extents()));
3097 empty() const noexcept
3098 {
return __mdspan::__empty(extents()); }
3100 friend constexpr void
3101 swap(mdspan& __x, mdspan& __y)
noexcept
3104 swap(__x._M_mapping, __y._M_mapping);
3105 swap(__x._M_accessor, __y._M_accessor);
3106 swap(__x._M_handle, __y._M_handle);
3109 constexpr const extents_type&
3110 extents() const noexcept {
return _M_mapping.extents(); }
3112 constexpr const data_handle_type&
3113 data_handle() const noexcept {
return _M_handle; }
3115 constexpr const mapping_type&
3116 mapping() const noexcept {
return _M_mapping; }
3118 constexpr const accessor_type&
3119 accessor() const noexcept {
return _M_accessor; }
3123 static constexpr bool
3124 is_always_unique() noexcept(noexcept(mapping_type::is_always_unique()))
3125 {
return mapping_type::is_always_unique(); }
3127 static constexpr bool
3128 is_always_exhaustive()
3129 noexcept(noexcept(mapping_type::is_always_exhaustive()))
3130 {
return mapping_type::is_always_exhaustive(); }
3132 static constexpr bool
3134 noexcept(noexcept(mapping_type::is_always_strided()))
3135 {
return mapping_type::is_always_strided(); }
3138 is_unique() const noexcept(noexcept(_M_mapping.is_unique()))
3139 {
return _M_mapping.is_unique(); }
3142 is_exhaustive() const noexcept(noexcept(_M_mapping.is_exhaustive()))
3143 {
return _M_mapping.is_exhaustive(); }
3146 is_strided() const noexcept(noexcept(_M_mapping.is_strided()))
3147 {
return _M_mapping.is_strided(); }
3149 constexpr index_type
3150 stride(rank_type __r)
const {
return _M_mapping.stride(__r); }
3153 [[no_unique_address]] accessor_type _M_accessor = accessor_type();
3154 [[no_unique_address]] mapping_type _M_mapping = mapping_type();
3155 [[no_unique_address]] data_handle_type _M_handle = data_handle_type();
3158 template<
typename _CArray>
3159 requires is_array_v<_CArray> && (rank_v<_CArray> == 1)
3161 -> mdspan<remove_all_extents_t<_CArray>,
3162 extents<size_t, extent_v<_CArray, 0>>>;
3164 template<
typename _Po
inter>
3165 requires is_pointer_v<remove_reference_t<_Pointer>>
3167 -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;
3169 template<
typename _ElementType,
typename... _Integrals>
3170 requires (is_convertible_v<_Integrals, size_t> && ...)
3171 && (
sizeof...(_Integrals) > 0)
3172 explicit mdspan(_ElementType*, _Integrals...)
3173 -> mdspan<_ElementType,
3174 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>>;
3176 template<
typename _ElementType,
typename _OIndexType,
size_t _Nm>
3177 mdspan(_ElementType*, span<_OIndexType, _Nm>)
3178 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
3180 template<
typename _ElementType,
typename _OIndexType,
size_t _Nm>
3181 mdspan(_ElementType*,
const array<_OIndexType, _Nm>&)
3182 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
3184 template<
typename _ElementType,
typename _IndexType,
size_t... _ExtentsPack>
3185 mdspan(_ElementType*,
const extents<_IndexType, _ExtentsPack...>&)
3186 -> mdspan<_ElementType, extents<_IndexType, _ExtentsPack...>>;
3188 template<
typename _ElementType,
typename _MappingType>
3189 mdspan(_ElementType*,
const _MappingType&)
3190 -> mdspan<_ElementType,
typename _MappingType::extents_type,
3191 typename _MappingType::layout_type>;
3193 template<
typename _MappingType,
typename _AccessorType>
3194 mdspan(
const typename _AccessorType::data_handle_type&,
const _MappingType&,
3195 const _AccessorType&)
3196 -> mdspan<
typename _AccessorType::element_type,
3197 typename _MappingType::extents_type,
3198 typename _MappingType::layout_type, _AccessorType>;
3200#if __glibcxx_submdspan
3203 template<
typename _IndexType,
typename _Slice>
3205 __canonical_index(_Slice&& __slice)
3207 if constexpr (__detail::__integral_constant_like<_Slice>)
3209 static_assert(__is_representable_integer<_IndexType>(_Slice::value));
3210 static_assert(_Slice::value >= 0);
3211 return std::cw<_IndexType(_Slice::value)>;
3214 return __mdspan::__index_type_cast<_IndexType>(
std::move(__slice));
3217 template<
typename _IndexType,
typename _Slice>
3219 __slice_cast(_Slice&& __slice)
3221 using _SliceType = remove_cvref_t<_Slice>;
3222 if constexpr (is_convertible_v<_SliceType, full_extent_t>)
3223 return static_cast<full_extent_t
>(
std::move(__slice));
3224 else if constexpr (is_convertible_v<_SliceType, _IndexType>)
3225 return __mdspan::__canonical_index<_IndexType>(
std::move(__slice));
3226 else if constexpr (__is_strided_slice<_SliceType>)
3229 = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.extent));
3231 = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.offset));
3232 if constexpr (is_same_v<
decltype(__extent),
3233 constant_wrapper<_IndexType(0)>>)
3234 return strided_slice{
3237 .stride = cw<_IndexType(1)>
3240 return strided_slice{
3244 = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.stride))
3249 auto [__sbegin, __send] =
std::move(__slice);
3251 = __mdspan::__canonical_index<_IndexType>(
std::move(__sbegin));
3253 = __mdspan::__canonical_index<_IndexType>(
std::move(__send));
3254 return strided_slice{
3256 .extent = __mdspan::__canonical_index<_IndexType>(__end - __offset),
3257 .stride = cw<_IndexType(1)>
3262 template<
typename _IndexType,
size_t _Extent,
typename _OIndexType>
3264 __check_valid_index(
const extents<_IndexType, _Extent>& __ext,
3265 const _OIndexType& __idx)
3267 if constexpr (__is_constant_wrapper<_OIndexType>
3268 && _Extent != dynamic_extent)
3270 static_assert(_OIndexType::value >= 0);
3271 static_assert(std::cmp_less_equal(_OIndexType::value, _Extent));
3274 __glibcxx_assert(__idx <= __ext.extent(0));
3277 template<
typename _IndexType,
size_t _Extent,
typename _Slice>
3279 __check_valid_slice(
const extents<_IndexType, _Extent>& __ext,
3280 const _Slice& __slice)
3282 if constexpr (__is_strided_slice<_Slice>)
3288 __mdspan::__check_valid_index(__ext, __slice.offset);
3289 __mdspan::__check_valid_index(__ext, __slice.extent);
3291 if constexpr (__is_constant_wrapper<typename _Slice::extent_type>
3292 && __is_constant_wrapper<typename _Slice::stride_type>)
3293 static_assert(_Slice::stride_type::value > 0);
3295 __glibcxx_assert(__slice.extent == 0 || __slice.stride > 0);
3297 if constexpr (__is_constant_wrapper<typename _Slice::offset_type>
3298 && __is_constant_wrapper<typename _Slice::extent_type>
3299 && _Extent != dynamic_extent)
3300 static_assert(std::cmp_greater_equal(
3301 _Extent - _Slice::offset_type::value,
3302 _Slice::extent_type::value));
3304 __glibcxx_assert(__ext.extent(0) - __slice.offset
3307 else if constexpr (__is_constant_wrapper<_Slice>
3308 && _Extent != dynamic_extent)
3309 static_assert(std::cmp_less(_Slice::value, _Extent));
3310 else if constexpr (convertible_to<_Slice, _IndexType>)
3311 __glibcxx_assert(__slice < __ext.extent(0));
3314 template<
typename _Extents,
typename... _Slices>
3316 __check_valid_slices(
const _Extents& __exts,
const _Slices&... __slices)
3318 constexpr auto __rank = _Extents::rank();
3321 ((__mdspan::__check_valid_slice(__extract_extent<_Is>(__exts),
3322 __slices...[_Is])),...);
3324 __impl(make_index_sequence<__rank>());
3327 template<
typename _Slice>
3328 using __full_extent_t = std::full_extent_t;
3331 void submdspan_mapping() =
delete;
3333 template<
typename _Mapping,
typename... _Slices>
3334 concept __sliceable_mapping =
requires(
const _Mapping __m, _Slices... __slices)
3336 { submdspan_mapping(__m, __slices...) } -> __submdspan_mapping_result;
3339 template<
typename _Mapping,
typename... _Slices>
3341 __submapping(
const _Mapping& __mapping, _Slices... __slices)
3343 __mdspan::__check_valid_slices(__mapping.extents(), __slices...);
3344 return submdspan_mapping(__mapping, __slices...);
3348 template<
typename _IndexType,
size_t... _Extents,
typename... _RawSlices>
3349 requires (
sizeof...(_RawSlices) ==
sizeof...(_Extents))
3351 submdspan_extents(
const extents<_IndexType, _Extents...>& __exts,
3352 _RawSlices... __raw_slices)
3354 auto __impl = [&__exts](
auto... __slices)
3356 __mdspan::__check_valid_slices(__exts, __slices...);
3357 return __mdspan::__subextents(__exts, __slices...);
3359 return __impl(__mdspan::__slice_cast<_IndexType>(__raw_slices)...);
3362 template<
typename _IndexType,
size_t... _Extents,
typename... _RawSlices>
3363 requires (
sizeof...(_Extents) ==
sizeof...(_RawSlices))
3365 submdspan_canonicalize_slices(
const extents<_IndexType, _Extents...>& __exts,
3366 _RawSlices... __raw_slices)
3368 auto __impl = [&__exts](
auto... __slices)
3370 __mdspan::__check_valid_slices(__exts, __slices...);
3373 return __impl(__mdspan::__slice_cast<_IndexType>(__raw_slices)...);
3376 template<
typename _ElementType,
typename _Extents,
typename _Layout,
3377 typename _Accessor,
typename... _RawSlices>
3378 requires (
sizeof...(_RawSlices) == _Extents::rank()
3379 && __mdspan::__sliceable_mapping<typename _Layout::template mapping<_Extents>,
3380 __mdspan::__full_extent_t<_RawSlices>...>)
3383 const mdspan<_ElementType, _Extents, _Layout, _Accessor>& __md,
3384 _RawSlices... __raw_slices)
3386 using _IndexType =
typename _Extents::index_type;
3387 auto [__mapping, __offset] = __mdspan::__submapping(
3388 __md.mapping(), __mdspan::__slice_cast<_IndexType>(__raw_slices)...);
3390 __md.accessor().offset(__md.data_handle(), __offset),
3392 typename _Accessor::offset_policy(__md.accessor()));
3396_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.
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>.