29#ifndef _GLIBCXX_MDSPAN
30#define _GLIBCXX_MDSPAN 1
33#pragma GCC system_header
41#define __glibcxx_want_mdspan
42#define __glibcxx_want_aligned_accessor
43#define __glibcxx_want_submdspan
46#if __glibcxx_aligned_accessor
50#if __glibcxx_submdspan
54#if __cplusplus > 202302L
58#ifdef __glibcxx_mdspan
60namespace std _GLIBCXX_VISIBILITY(default)
62_GLIBCXX_BEGIN_NAMESPACE_VERSION
66 __all_static(std::span<const size_t> __extents)
68 for(
auto __ext : __extents)
69 if (__ext == dynamic_extent)
75 __all_dynamic(std::span<const size_t> __extents)
77 for(
auto __ext : __extents)
78 if (__ext != dynamic_extent)
83 template<
typename _IndexType,
typename _OIndexTypeRef>
85 __index_type_cast(_OIndexTypeRef&& __other)
89 using _OIndexType = std::remove_cvref_t<_OIndexTypeRef>;
90 if constexpr (std::is_integral_v<_OIndexType>)
92 constexpr _IndexType __index_type_max
93 = __gnu_cxx::__int_traits<_IndexType>::__max;
94 constexpr _OIndexType __oindex_type_max
95 = __gnu_cxx::__int_traits<_OIndexType>::__max;
97 if constexpr (__index_type_max < __oindex_type_max)
98 __glibcxx_assert(cmp_less_equal(__other, __index_type_max));
100 if constexpr (std::is_signed_v<_OIndexType>)
101 __glibcxx_assert(__other >= 0);
102 return static_cast<_IndexType
>(__other);
107 if constexpr (std::is_signed_v<_IndexType>)
108 __glibcxx_assert(__ret >= 0);
113 template<array _Extents>
117 static constexpr size_t _S_rank = _Extents.size();
125 static constexpr size_t
126 _S_dynamic_index(
size_t __r)
noexcept
127 {
return _S_dynamic_index_data[__r]; }
129 static constexpr auto _S_dynamic_index_data = []
consteval
131 array<size_t, _S_rank+1> __ret;
133 for (
size_t __i = 0; __i < _S_rank; ++__i)
136 __dyn += (_Extents[__i] == dynamic_extent);
138 __ret[_S_rank] = __dyn;
142 static constexpr size_t _S_rank_dynamic = _S_dynamic_index(_S_rank);
146 static constexpr size_t
147 _S_dynamic_index_inv(
size_t __r)
noexcept
148 {
return _S_dynamic_index_inv_data[__r]; }
150 static constexpr auto _S_dynamic_index_inv_data = []
consteval
152 array<size_t, _S_rank_dynamic> __ret;
153 for (
size_t __i = 0, __r = 0; __i < _S_rank; ++__i)
154 if (_Extents[__i] == dynamic_extent)
159 static constexpr size_t
160 _S_static_extent(
size_t __r)
noexcept
161 {
return _Extents[__r]; }
164 template<array _Extents>
165 requires (__all_dynamic<_Extents>())
166 class _StaticExtents<_Extents>
169 static constexpr size_t _S_rank = _Extents.size();
171 static constexpr size_t
172 _S_dynamic_index(
size_t __r)
noexcept
175 static constexpr size_t _S_rank_dynamic = _S_rank;
177 static constexpr size_t
178 _S_dynamic_index_inv(
size_t __k)
noexcept
181 static constexpr size_t
182 _S_static_extent(
size_t)
noexcept
183 {
return dynamic_extent; }
186 template<
typename _IndexType, array _Extents>
187 class _ExtentsStorage :
public _StaticExtents<_Extents>
190 using _Base = _StaticExtents<_Extents>;
193 using _Base::_S_rank;
194 using _Base::_S_rank_dynamic;
195 using _Base::_S_dynamic_index;
196 using _Base::_S_dynamic_index_inv;
197 using _Base::_S_static_extent;
199 static constexpr bool
200 _S_is_dynamic(
size_t __r)
noexcept
202 if constexpr (__all_static(_Extents))
204 else if constexpr (__all_dynamic(_Extents))
207 return _Extents[__r] == dynamic_extent;
210 template<
typename _OIndexType>
211 static constexpr _IndexType
212 _S_int_cast(
const _OIndexType& __other)
noexcept
213 {
return _IndexType(__other); }
216 _M_extent(
size_t __r)
const noexcept
218 if (_S_is_dynamic(__r))
219 return _M_dyn_exts[_S_dynamic_index(__r)];
221 return _S_static_extent(__r);
224 template<
size_t _OtherRank,
typename _GetOtherExtent>
225 static constexpr bool
226 _S_is_compatible_extents(_GetOtherExtent __get_extent)
noexcept
228 if constexpr (_OtherRank == _S_rank)
229 for (
size_t __i = 0; __i < _S_rank; ++__i)
230 if (!_S_is_dynamic(__i)
231 && !cmp_equal(_Extents[__i], _S_int_cast(__get_extent(__i))))
236 template<
size_t _OtherRank,
typename _GetOtherExtent>
238 _M_init_dynamic_extents(_GetOtherExtent __get_extent)
noexcept
240 __glibcxx_assert(_S_is_compatible_extents<_OtherRank>(__get_extent));
241 for (
size_t __i = 0; __i < _S_rank_dynamic; ++__i)
244 if constexpr (_OtherRank != _S_rank_dynamic)
245 __di = _S_dynamic_index_inv(__i);
246 _M_dyn_exts[__i] = _S_int_cast(__get_extent(__di));
251 _ExtentsStorage() noexcept = default;
253 template<typename _OIndexType, array _OExtents>
255 _ExtentsStorage(const _ExtentsStorage<_OIndexType, _OExtents>&
258 _M_init_dynamic_extents<_S_rank>([&__other](
size_t __i)
259 {
return __other._M_extent(__i); });
262 template<
typename _OIndexType,
size_t _Nm>
264 _ExtentsStorage(span<const _OIndexType, _Nm> __exts)
noexcept
266 _M_init_dynamic_extents<_Nm>(
267 [&__exts](
size_t __i) ->
const _OIndexType&
268 {
return __exts[__i]; });
271 static constexpr const array<size_t, _S_rank>&
272 _S_static_extents() noexcept
275 constexpr span<const _IndexType>
276 _M_dynamic_extents(
size_t __begin,
size_t __end)
const noexcept
277 requires (_Extents.size() > 0)
279 return {_M_dyn_exts + _S_dynamic_index(__begin),
280 _S_dynamic_index(__end) - _S_dynamic_index(__begin)};
284 using _Storage = __array_traits<_IndexType, _S_rank_dynamic>::_Type;
285 [[no_unique_address]] _Storage _M_dyn_exts{};
288 template<
typename _OIndexType,
typename _SIndexType>
289 concept __valid_index_type =
290 is_convertible_v<_OIndexType, _SIndexType> &&
291 is_nothrow_constructible_v<_SIndexType, _OIndexType>;
293 template<
size_t _Extent,
typename _IndexType>
295 __valid_static_extent = _Extent == dynamic_extent
296 || _Extent <= __gnu_cxx::__int_traits<_IndexType>::__max;
298 template<
typename _Extents>
299 constexpr const array<size_t, _Extents::rank()>&
300 __static_extents() noexcept
301 {
return _Extents::_Storage::_S_static_extents(); }
303 template<
typename _Extents>
304 constexpr span<const size_t>
305 __static_extents(
size_t __begin,
size_t __end)
noexcept
307 const auto& __sta_exts = __static_extents<_Extents>();
308 return span<const size_t>(__sta_exts.data() + __begin, __end - __begin);
312 template<array _Extents>
313 constexpr auto __fwd_partial_prods = []
consteval
315 constexpr size_t __rank = _Extents.size();
316 std::array<size_t, __rank> __ret;
318 for (
size_t __r = 0; __r < __rank; ++__r)
321 if (
size_t __ext = _Extents[__r]; __ext != dynamic_extent)
328 template<array _Extents>
329 constexpr auto __rev_partial_prods = []
consteval
331 constexpr size_t __rank = _Extents.size();
332 std::array<size_t, __rank> __ret;
334 for (
size_t __r = __rank; __r > 0; --__r)
336 __ret[__r - 1] = __prod;
337 if (
size_t __ext = _Extents[__r - 1]; __ext != dynamic_extent)
343 template<
typename _Extents>
344 constexpr span<const typename _Extents::index_type>
345 __dynamic_extents(
const _Extents& __exts,
size_t __begin = 0,
346 size_t __end = _Extents::rank()) noexcept
347 {
return __exts._M_exts._M_dynamic_extents(__begin, __end); }
350#if __glibcxx_submdspan
353 explicit full_extent_t() =
default;
356 inline constexpr full_extent_t full_extent{};
358 template<
typename _OffsetType,
typename _ExtentType,
typename _Str
ideType>
361 static_assert(__is_signed_or_unsigned_integer<_OffsetType>::value
362 || __detail::__integral_constant_like<_OffsetType>);
363 static_assert(__is_signed_or_unsigned_integer<_ExtentType>::value
364 || __detail::__integral_constant_like<_ExtentType>);
365 static_assert(__is_signed_or_unsigned_integer<_StrideType>::value
366 || __detail::__integral_constant_like<_StrideType>);
368 using offset_type = _OffsetType;
369 using extent_type = _ExtentType;
370 using stride_type = _StrideType;
372 [[no_unique_address]] offset_type offset{};
373 [[no_unique_address]] extent_type extent{};
374 [[no_unique_address]] stride_type stride{};
377 template<
typename _FirstType,
typename _LastType,
typename _Str
ideType = constant_wrapper<1zu>>
380 static_assert(__is_signed_or_unsigned_integer<_FirstType>::value
381 || __detail::__integral_constant_like<_FirstType>);
382 static_assert(__is_signed_or_unsigned_integer<_LastType>::value
383 || __detail::__integral_constant_like<_LastType>);
384 static_assert(__is_signed_or_unsigned_integer<_StrideType>::value
385 || __detail::__integral_constant_like<_StrideType>);
387 [[no_unique_address]] _FirstType first{};
388 [[no_unique_address]] _LastType last{};
389 [[no_unique_address]] _StrideType stride{};
392 template<
typename _Mapping>
393 struct submdspan_mapping_result
395 [[no_unique_address]] _Mapping mapping = _Mapping();
399 template<
typename _Tp>
400 constexpr bool __is_submdspan_mapping_result =
false;
402 template<
typename _Mapping>
403 constexpr bool __is_submdspan_mapping_result<submdspan_mapping_result<_Mapping>> =
true;
405 template<
typename _Mapping>
406 concept __submdspan_mapping_result = __is_submdspan_mapping_result<_Mapping>;
410 template<
typename _IndexType,
size_t... _Extents>
413 static_assert(__is_signed_or_unsigned_integer<_IndexType>::value,
414 "IndexType must be a signed or unsigned integer type");
416 (__mdspan::__valid_static_extent<_Extents, _IndexType> && ...),
417 "Extents must either be dynamic or representable as IndexType");
419 using _Storage = __mdspan::_ExtentsStorage<
420 _IndexType, array<size_t,
sizeof...(_Extents)>{_Extents...}>;
421 [[no_unique_address]] _Storage _M_exts;
424 using index_type = _IndexType;
426 using rank_type = size_t;
428 static constexpr rank_type
429 rank() noexcept {
return _Storage::_S_rank; }
431 static constexpr rank_type
432 rank_dynamic() noexcept {
return _Storage::_S_rank_dynamic; }
434 static constexpr size_t
435 static_extent(rank_type __r)
noexcept
437 __glibcxx_assert(__r < rank());
438 if constexpr (rank() == 0)
441 return _Storage::_S_static_extent(__r);
445 extent(rank_type __r)
const noexcept
447 __glibcxx_assert(__r < rank());
448 if constexpr (rank() == 0)
451 return _M_exts._M_extent(__r);
455 extents() noexcept = default;
458 static consteval
bool
459 _S_is_less_dynamic(
size_t __ext,
size_t __oext)
460 {
return (__ext != dynamic_extent) && (__oext == dynamic_extent); }
462 template<
typename _OIndexType,
size_t... _OExtents>
463 static consteval bool
466 return (_S_is_less_dynamic(_Extents, _OExtents) || ...)
467 || (__gnu_cxx::__int_traits<index_type>::__max
468 < __gnu_cxx::__int_traits<_OIndexType>::__max);
471 template<
size_t... _OExtents>
472 static consteval bool
473 _S_is_compatible_extents()
475 if constexpr (
sizeof...(_OExtents) != rank())
478 return ((_OExtents == dynamic_extent || _Extents == dynamic_extent
479 || _OExtents == _Extents) && ...);
483 template<
typename _OIndexType,
size_t... _OExtents>
484 requires (_S_is_compatible_extents<_OExtents...>())
485 constexpr explicit(_S_ctor_explicit<_OIndexType, _OExtents...>())
486 extents(
const extents<_OIndexType, _OExtents...>& __other) noexcept
487 : _M_exts(__other._M_exts)
490 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
491 requires (
sizeof...(_OIndexTypes) == rank()
492 ||
sizeof...(_OIndexTypes) == rank_dynamic())
493 constexpr explicit extents(_OIndexTypes... __exts) noexcept
494 : _M_exts(span<
const _IndexType,
sizeof...(_OIndexTypes)>(
495 initializer_list{
static_cast<_IndexType
>(
std::move(__exts))...}))
498 template<
typename _OIndexType,
size_t _Nm>
499 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
500 && (_Nm == rank() || _Nm == rank_dynamic())
501 constexpr explicit(_Nm != rank_dynamic())
502 extents(span<_OIndexType, _Nm> __exts) noexcept
503 : _M_exts(span<const _OIndexType, _Nm>(__exts))
506 template<
typename _OIndexType,
size_t _Nm>
507 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
508 && (_Nm == rank() || _Nm == rank_dynamic())
509 constexpr explicit(_Nm != rank_dynamic())
510 extents(
const array<_OIndexType, _Nm>& __exts) noexcept
511 : _M_exts(span<const _OIndexType, _Nm>(__exts))
514 template<
typename _OIndexType,
size_t... _OExtents>
515 friend constexpr bool
516 operator==(
const extents& __self,
517 const extents<_OIndexType, _OExtents...>& __other)
noexcept
519 if constexpr (!_S_is_compatible_extents<_OExtents...>())
523 auto __impl = [&__self, &__other]<
size_t... _Counts>(
525 {
return (cmp_equal(__self.extent(_Counts),
526 __other.extent(_Counts)) && ...); };
532 friend constexpr const array<size_t, rank()>&
533 __mdspan::__static_extents<extents>() noexcept;
535 friend constexpr span<const index_type>
536 __mdspan::__dynamic_extents<extents>(const extents&,
size_t,
size_t)
539 template<typename _OIndexType,
size_t... _OExtents>
540 friend class extents;
545 template<
typename _Tp,
size_t _Nm>
547 __contains_zero(span<_Tp, _Nm> __exts)
noexcept
549 for (
size_t __i = 0; __i < __exts.size(); ++__i)
550 if (__exts[__i] == 0)
555 template<
typename _Tp,
size_t _Nm>
557 __contains_zero(
const array<_Tp, _Nm>& __exts)
noexcept
558 {
return __contains_zero(span<const _Tp>(__exts)); }
560 template<
typename _Extents>
562 __empty(
const _Extents& __exts)
noexcept
564 if constexpr (__contains_zero(__static_extents<_Extents>()))
566 else if constexpr (_Extents::rank_dynamic() > 0)
567 return __contains_zero(__dynamic_extents(__exts));
572 template<
typename _Extents>
573 constexpr typename _Extents::index_type
574 __extents_prod(
const _Extents& __exts,
size_t __sta_prod,
size_t __begin,
575 size_t __end)
noexcept
580 size_t __ret = __sta_prod;
581 if constexpr (_Extents::rank_dynamic() > 0)
582 for (
auto __factor : __dynamic_extents(__exts, __begin, __end))
583 __ret *= size_t(__factor);
584 return static_cast<typename _Extents::index_type
>(__ret);
588 template<
typename _Extents>
589 constexpr typename _Extents::index_type
590 __fwd_prod(
const _Extents& __exts,
size_t __begin,
size_t __end)
noexcept
592 size_t __sta_prod = [__begin, __end] {
593 span<const size_t> __sta_exts
594 = __static_extents<_Extents>(__begin, __end);
596 for(
auto __ext : __sta_exts)
597 if (__ext != dynamic_extent)
601 return __extents_prod(__exts, __sta_prod, __begin, __end);
604 template<
typename _Extents>
605 constexpr typename _Extents::index_type
606 __fwd_prod(
const _Extents& __exts,
size_t __r)
noexcept
608 constexpr size_t __rank = _Extents::rank();
609 constexpr auto& __sta_exts = __static_extents<_Extents>();
610 if constexpr (__rank == 1)
612 else if constexpr (__rank == 2)
613 return __r == 0 ? 1 : __exts.extent(0);
614 else if constexpr (__all_dynamic(std::span(__sta_exts).first(__rank-1)))
615 return __extents_prod(__exts, 1, 0, __r);
618 size_t __sta_prod = __fwd_partial_prods<__sta_exts>[__r];
619 return __extents_prod(__exts, __sta_prod, 0, __r);
623 template<
typename _IndexType,
size_t _Nm>
625 __fwd_prod(span<const _IndexType, _Nm> __values)
627 _IndexType __ret = 1;
628 for(
auto __value : __values)
634 template<
typename _Extents>
635 constexpr typename _Extents::index_type
636 __rev_prod(
const _Extents& __exts,
size_t __r)
noexcept
638 constexpr size_t __rank = _Extents::rank();
639 constexpr auto& __sta_exts = __static_extents<_Extents>();
640 if constexpr (__rank == 1)
642 else if constexpr (__rank == 2)
643 return __r == 0 ? __exts.extent(1) : 1;
644 else if constexpr (__all_dynamic(std::span(__sta_exts).last(__rank-1)))
645 return __extents_prod(__exts, 1, __r + 1, __rank);
648 size_t __sta_prod = __rev_partial_prods<__sta_exts>[__r];
649 return __extents_prod(__exts, __sta_prod, __r + 1, __rank);
653 template<
typename _Extents>
654 constexpr typename _Extents::index_type
655 __size(
const _Extents& __exts)
noexcept
657 constexpr size_t __sta_prod = [] {
658 span<const size_t> __sta_exts = __static_extents<_Extents>();
660 for(
auto __ext : __sta_exts)
661 if (__ext != dynamic_extent)
665 return __extents_prod(__exts, __sta_prod, 0, _Extents::rank());
668 template<
typename _IndexType,
size_t... _Counts>
669 auto __build_dextents_type(integer_sequence<size_t, _Counts...>)
670 -> extents<_IndexType, ((void) _Counts, dynamic_extent)...>;
673 template<
typename _IndexType,
size_t _Rank>
674 using dextents =
decltype(__mdspan::__build_dextents_type<_IndexType>(
677#if __glibcxx_mdspan >= 202406L
678 template<
size_t _Rank,
typename _IndexType =
size_t>
679 using dims = dextents<_IndexType, _Rank>;
682 template<
typename... _Integrals>
683 requires (is_convertible_v<_Integrals, size_t> && ...)
684 explicit extents(_Integrals...) ->
685 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>;
689 template<
typename _Extents>
695 template<
typename _Extents>
701 template<
typename _Extents>
705#ifdef __glibcxx_padded_layouts
706 template<
size_t _PaddingValue>
707 struct layout_left_padded
709 template<
typename _Extents>
713 template<
size_t _PaddingValue>
714 struct layout_right_padded
716 template<
typename _Extents>
723 template<
typename _Tp>
724 constexpr bool __is_extents =
false;
726 template<
typename _IndexType,
size_t... _Extents>
727 constexpr bool __is_extents<extents<_IndexType, _Extents...>> =
true;
729 template<
typename _Extents,
typename... _Indices>
730 constexpr typename _Extents::index_type
731 __linear_index_left(
const _Extents& __exts, _Indices... __indices)
734 using _IndexType =
typename _Extents::index_type;
735 _IndexType __res = 0;
736 if constexpr (
sizeof...(__indices) > 0)
738 _IndexType __mult = 1;
739 auto __update = [&, __pos = 0u](_IndexType __idx)
mutable
741 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx, __exts.extent(__pos)));
742 __res += __idx * __mult;
743 __mult *= __exts.extent(__pos);
746 (__update(__indices), ...);
751 template<
typename _IndexType>
753 __static_quotient(std::span<const size_t> __sta_exts,
754 _IndexType __nom = __gnu_cxx::__int_traits<_IndexType>::__max)
756 for (
auto __factor : __sta_exts)
758 if (__factor != dynamic_extent)
759 __nom /= _IndexType(__factor);
766 template<
typename _Extents,
767 typename _IndexType =
typename _Extents::index_type>
768 requires __is_extents<_Extents>
770 __static_quotient(_IndexType __nom
771 = __gnu_cxx::__int_traits<_IndexType>::__max)
773 std::span<const size_t> __sta_exts = __static_extents<_Extents>();
774 return __static_quotient<_IndexType>(__sta_exts, __nom);
777 template<
typename _Extents>
779 __is_representable_extents(
const _Extents& __exts)
noexcept
781 using _IndexType = _Extents::index_type;
783 if constexpr (__contains_zero(__static_extents<_Extents>()))
787 constexpr auto __sta_quo = __static_quotient<_Extents>();
788 if constexpr (_Extents::rank_dynamic() == 0)
789 return __sta_quo != 0;
792 auto __dyn_exts = __dynamic_extents(__exts);
793 if (__contains_zero(__dyn_exts))
796 if constexpr (__sta_quo == 0)
800 auto __dyn_quo = _IndexType(__sta_quo);
801 for (
auto __factor : __dyn_exts)
803 __dyn_quo /= __factor;
813 template<
typename _Extents,
typename _IndexType>
814 concept __representable_size = _Extents::rank_dynamic() != 0
815 || __contains_zero(__static_extents<_Extents>())
816 || (__static_quotient<_Extents, _IndexType>() != 0);
818 template<
typename _Layout,
typename _Mapping>
819 concept __mapping_of =
820 is_same_v<
typename _Layout::template mapping<
821 typename _Mapping::extents_type>,
824 template<
template<
size_t>
typename _Layout,
typename _Mapping>
825 concept __padded_mapping_of = __mapping_of<
826 _Layout<_Mapping::padding_value>, _Mapping>;
828#ifdef __glibcxx_padded_layouts
829 template<
typename _Mapping>
830 constexpr bool __is_left_padded_mapping = __padded_mapping_of<
831 layout_left_padded, _Mapping>;
833 template<
typename _Mapping>
834 constexpr bool __is_right_padded_mapping = __padded_mapping_of<
835 layout_right_padded, _Mapping>;
837 template<
typename _Mapping>
838 constexpr bool __is_padded_mapping = __is_left_padded_mapping<_Mapping>
839 || __is_right_padded_mapping<_Mapping>;
842 template<
typename _PaddedMapping>
844 __get_static_stride()
845 {
return _PaddedMapping::_PaddedStorage::_S_static_stride; }
847 template<
typename _Mapping>
848 concept __standardized_mapping = __mapping_of<layout_left, _Mapping>
849 || __mapping_of<layout_right, _Mapping>
850 || __mapping_of<layout_stride, _Mapping>
851#ifdef __glibcxx_padded_layouts
852 || __is_left_padded_mapping<_Mapping>
853 || __is_right_padded_mapping<_Mapping>
858 class __internal_ctor
861 template<
typename _Mapping>
862 constexpr typename _Mapping::index_type
863 __offset(
const _Mapping& __m)
noexcept
865 using _IndexType =
typename _Mapping::index_type;
866 constexpr auto __rank = _Mapping::extents_type::rank();
868 if constexpr (__standardized_mapping<_Mapping>)
870 else if (__empty(__m.extents()))
874 auto __impl = [&__m]<
size_t... _Counts>(
index_sequence<_Counts...>)
875 {
return __m(((
void) _Counts, _IndexType(0))...); };
876 return __impl(make_index_sequence<__rank>());
880#ifdef __glibcxx_submdspan
881 template<
typename _Tp>
882 constexpr bool __is_extent_slice =
false;
884 template<
typename _OffsetType,
typename _ExtentType,
typename _Str
ideType>
885 constexpr bool __is_extent_slice<extent_slice<_OffsetType,
886 _ExtentType, _StrideType>> =
true;
888 template<
typename _Tp>
889 constexpr bool __is_range_slice =
false;
891 template<
typename _FirstType,
typename _LastType,
typename _Str
ideType>
892 constexpr bool __is_range_slice<range_slice<_FirstType,
893 _LastType, _StrideType>> =
true;
895 template<
typename _IndexType,
typename _OIndexType>
897 __is_representable_integer(_OIndexType __value)
899 constexpr auto __min = __gnu_cxx::__int_traits<_IndexType>::__min;
900 constexpr auto __max = __gnu_cxx::__int_traits<_IndexType>::__max;
901 return std::cmp_less_equal(__min, __value)
902 && std::cmp_less_equal(__value, __max);
905 template<
typename _Tp>
906 constexpr bool __is_constant_wrapper =
false;
908 template<_CwFixedValue _Xv,
typename _Tp>
909 constexpr bool __is_constant_wrapper<constant_wrapper<_Xv, _Tp>>
912 template<
size_t _Index,
typename _Extents>
914 __extract_extent(
const _Extents& __exts)
916 using _IndexType =
typename _Extents::index_type;
917 return extents<_IndexType, _Extents::static_extent(_Index)>{
918 __exts.extent(_Index)};
921 template<
typename _Slice,
typename _IndexType>
922 concept __acceptable_slice_type = same_as<_Slice, full_extent_t>
923 || same_as<_Slice, _IndexType> || __is_constant_wrapper<_Slice>
924 || __is_extent_slice<_Slice>;
926 template<
typename _IndexType,
typename... _Slices>
930 return (
static_cast<size_t>(!convertible_to<_Slices, _IndexType>)
934 template<
typename _IndexType,
typename... _Slices>
938 constexpr auto __rank =
sizeof...(_Slices);
939 constexpr auto __sub_rank = __subrank<_IndexType, _Slices...>();
940 auto __map = std::array<size_t, __sub_rank>{};
941 auto __is_int_like = std::array<bool, __rank>{
942 convertible_to<_Slices, _IndexType>...};
945 for (
size_t __k = 0; __k < __rank; ++__k)
946 if (!__is_int_like[__k])
951 template<
typename _Slice>
953 __slice_begin(_Slice __slice)
955 if constexpr (same_as<_Slice, full_extent_t>)
957 else if constexpr (__is_extent_slice<_Slice>)
958 return __slice.offset;
963 template<
typename _Mapping,
typename... _Slices>
965 __suboffset(
const _Mapping& __mapping,
const _Slices&... __slices)
967 using _IndexType =
typename _Mapping::index_type;
968 auto __any_past_the_end = [&]<
size_t... _Is>(
index_sequence<_Is...>)
970 auto __is_past_the_end = [](
const auto& __slice,
const auto& __ext)
972 using _Slice = remove_cvref_t<
decltype(__slice)>;
973 if constexpr (is_convertible_v<_Slice, _IndexType>)
975 else if constexpr (same_as<_Slice, full_extent_t>
976 && __ext.static_extent(0) != 0
977 && __ext.static_extent(0) != dynamic_extent)
980 return __mdspan::__slice_begin(__slice) == __ext.extent(0);
983 const auto& __exts = __mapping.extents();
984 return ((__is_past_the_end(__slices...[_Is],
985 __mdspan::__extract_extent<_Is>(__exts))) || ...);
988 if constexpr ((same_as<_Slices, full_extent_t> && ...))
989 return __mdspan::__offset(__mapping);
992 return __mapping.required_span_size();
993 return __mapping(__mdspan::__slice_begin(__slices)...);
996 template<
typename _IndexType,
size_t _Extent,
typename _Slice>
998 __static_slice_extent()
1000 if constexpr (same_as<_Slice, full_extent_t>)
1002 else if constexpr (same_as<_Slice, constant_wrapper<_IndexType(0)>>)
1004 else if constexpr (__is_constant_wrapper<typename _Slice::extent_type>)
1005 return _Slice::extent_type::value;
1007 return dynamic_extent;
1010 template<
size_t _K,
typename _Extents,
typename _Slice>
1011 constexpr typename _Extents::index_type
1012 __dynamic_slice_extent(
const _Extents& __exts, _Slice __slice)
1014 if constexpr (__is_extent_slice<_Slice>)
1015 return __slice.extent;
1017 return __exts.extent(_K);
1020 template<
typename _IndexType,
size_t... _Extents,
typename... _Slices>
1021 requires (
sizeof...(_Slices) ==
sizeof...(_Extents))
1023 __subextents(
const extents<_IndexType, _Extents...>& __exts,
1024 _Slices... __slices)
1026 constexpr auto __inv_map = __mdspan::__inv_map_rank<_IndexType, _Slices...>();
1029 using _SubExts = extents<_IndexType,
1030 __mdspan::__static_slice_extent<_IndexType,
1031 _Extents...[__inv_map[_Indices]],
1032 _Slices...[__inv_map[_Indices]]>()...>;
1033 if constexpr (_SubExts::rank_dynamic() == 0)
1037 using _StaticSubExtents = __mdspan::_StaticExtents<
1038 __mdspan::__static_extents<_SubExts>()>;
1041 constexpr auto __slice_idx = [__inv_map](
size_t __i)
consteval
1043 return __inv_map[_StaticSubExtents::_S_dynamic_index_inv(__i)];
1046 return _SubExts{__mdspan::__dynamic_slice_extent<__slice_idx(_Is)>(
1047 __exts, __slices...[__slice_idx(_Is)])...};
1049 constexpr auto __dyn_subrank = _SubExts::rank_dynamic();
1057 enum class _LayoutSide
1064 template<
typename _Mapping>
1065 consteval _LayoutSide
1068 if constexpr (__is_left_padded_mapping<_Mapping>
1069 || __mapping_of<layout_left, _Mapping>)
1070 return _LayoutSide::__left;
1071 if constexpr (__is_right_padded_mapping<_Mapping>
1072 || __mapping_of<layout_right, _Mapping>)
1073 return _LayoutSide::__right;
1075 return _LayoutSide::__unknown;
1078 template<_LayoutS
ide _S
ide,
size_t _Rank>
1079 struct _StridesTrait
1081 static constexpr const _LayoutSide _S_side = _Side;
1083 static constexpr size_t
1084 _S_idx(
size_t __k)
noexcept
1086 if constexpr (_Side == _LayoutSide::__left)
1089 return _Rank - 1 - __k;
1094 template<
typename _Mapping>
1095 static constexpr typename _Mapping::index_type
1096 _S_padded_extent(
const _Mapping& __mapping,
size_t __k)
1099 return __mapping.stride(_S_idx(1));
1101 return __mapping.extents().extent(_S_idx(__k));
1104 template<
typename _IndexType,
typename... _Slices>
1105 static consteval auto
1108 static_assert(_Side != _LayoutSide::__unknown);
1111 return __mdspan::__inv_map_rank<_IndexType, _Slices...[_S_idx(_Is)]...>();
1117 template<
typename _SubExts,
typename _Mapping,
typename... _Slices>
1119 __substrides_generic(
const _Mapping& __mapping,
const _Slices&... __slices)
1121 using _IndexType =
typename _Mapping::index_type;
1122 if constexpr (_SubExts::rank() == 0)
1123 return array<_IndexType, _SubExts::rank()>{};
1126 auto __stride = [&__mapping](
size_t __k,
auto __slice) -> _IndexType
1128 if constexpr (__is_extent_slice<
decltype(__slice)>)
1129 if (__slice.extent > 1)
1130 return __mapping.stride(__k) * __slice.stride;
1131 return __mapping.stride(__k);
1136 constexpr auto __inv_map
1137 = __mdspan::__inv_map_rank<_IndexType, _Slices...>();
1138 return array<_IndexType, _SubExts::rank()>{
1139 __stride(__inv_map[_Is], __slices...[__inv_map[_Is]])...};
1145 template<
typename _SubExts,
typename _Mapping,
typename... _Slices>
1147 __substrides_standardized(
const _Mapping& __mapping,
1148 const _Slices&... __slices)
1150 using _IndexType =
typename _Mapping::index_type;
1151 using _Trait = _StridesTrait<__mapping_side<_Mapping>(),
1152 _Mapping::extents_type::rank()>;
1153 using _SubTrait = _StridesTrait<__mapping_side<_Mapping>(), _SubExts::rank()>;
1155 constexpr size_t __sub_rank = _SubExts::rank();
1157 std::array<_IndexType, __sub_rank> __ret;
1158 if constexpr (__sub_rank > 0)
1160 constexpr auto __inv_map
1161 = _Trait::template _S_inv_map<_IndexType, _Slices...>();
1165 size_t __stride = 1;
1166 auto __body = [&](
size_t __k,
auto __slice)
1168 for (
size_t __i = __i0; __i < __inv_map[__k]; ++__i)
1169 __stride *= _Trait::_S_padded_extent(__mapping, __i);
1171 size_t __krev = _SubTrait::_S_idx(__k);
1172 if constexpr (__is_extent_slice<
decltype(__slice)>)
1174 if (__slice.extent > 1)
1175 __ret[__krev] = __stride * __slice.stride;
1177 __ret[__krev] = __stride;
1180 __ret[__krev] = __stride;
1182 __i0 = __inv_map[__k];
1185 ((__body(_Ks, __slices...[_Trait::_S_idx(__inv_map[_Ks])])),...);
1193 template<
typename _SubExts,
typename _Mapping,
typename... _Slices>
1195 __substrides(
const _Mapping& __mapping,
const _Slices&... __slices)
1197 if constexpr (__mdspan::__mapping_side<_Mapping>() == _LayoutSide::__unknown)
1198 return __mdspan::__substrides_generic<_SubExts>(__mapping, __slices...);
1200 return __mdspan::__substrides_standardized<_SubExts>(__mapping, __slices...);
1203 template<
typename _Slice>
1204 concept __is_unit_stride_slice = (__mdspan::__is_extent_slice<_Slice>
1205 && __mdspan::__is_constant_wrapper<typename _Slice::stride_type>
1206 && _Slice::stride_type::value == 1)
1207 || std::same_as<_Slice, full_extent_t>;
1213 enum class _SliceKind
1216 __unit_stride_slice,
1221 template<
typename _Slice>
1222 consteval _SliceKind
1225 if constexpr (std::same_as<_Slice, full_extent_t>)
1226 return _SliceKind::__full;
1227 else if constexpr (__mdspan::__is_extent_slice<_Slice>)
1229 if constexpr (__mdspan::__is_unit_stride_slice<_Slice>)
1230 return _SliceKind::__unit_stride_slice;
1232 return _SliceKind::__extent_slice;
1235 return _SliceKind::__collapsing;
1238 template<
typename... _Slices>
1239 consteval array<_SliceKind,
sizeof...(_Slices)>
1240 __make_slice_kind_array()
1242 return array<_SliceKind,
sizeof...(_Slices)>{
1243 __mdspan::__make_slice_kind<_Slices>()...};
1249 __is_block(span<const _SliceKind> __slice_kinds,
size_t __block_size)
1251 if (__block_size == 0)
1254 if (__block_size > __slice_kinds.size())
1257 for (
size_t __i = 0; __i < __block_size - 1; ++__i)
1258 if (__slice_kinds[__i] != _SliceKind::__full)
1261 auto __last = __slice_kinds[__block_size - 1];
1262 return __last == _SliceKind::__full
1263 || __last == _SliceKind::__unit_stride_slice;
1268 static consteval size_t
1269 __padded_block_begin_generic(span<const _SliceKind> __slice_kinds,
1272 if (__slice_kinds[0] != _SliceKind::__full
1273 && __slice_kinds[0] != _SliceKind::__unit_stride_slice)
1274 return dynamic_extent;
1275 else if (__slice_kinds.size() == 1)
1276 return dynamic_extent;
1280 while(__u < __slice_kinds.size()
1281 && __slice_kinds[__u] == _SliceKind::__collapsing)
1284 if (__mdspan::__is_block(__slice_kinds.subspan(__u), __sub_rank -1))
1286 return dynamic_extent;
1290 template<_LayoutS
ide _S
ide,
size_t _Nm>
1291 static consteval size_t
1292 __padded_block_begin(span<const _SliceKind, _Nm> __slice_kinds,
size_t __sub_rank)
1294 if constexpr (_Side == _LayoutSide::__left)
1295 return __mdspan::__padded_block_begin_generic(__slice_kinds, __sub_rank);
1298 std::array<_SliceKind, _Nm> __rev_slices;
1299 for(
size_t __i = 0; __i < _Nm; ++__i)
1300 __rev_slices[__i] = __slice_kinds[_Nm - 1 - __i];
1301 auto __rev_slice_kinds = span<const _SliceKind>(__rev_slices);
1303 auto __u = __mdspan::__padded_block_begin_generic(__rev_slice_kinds,
1305 return __u == dynamic_extent ? dynamic_extent : _Nm - 1 - __u;
1309 template<_LayoutS
ide _S
ide,
bool _Padded>
1310 struct _SubMdspanMapping;
1313 struct _SubMdspanMapping<_LayoutSide::__left, false>
1315 using _Layout = layout_left;
1316 template<
size_t _Pad>
using _PaddedLayout = layout_left_padded<_Pad>;
1318 template<
typename _Mapping,
size_t _Us>
1319 static consteval size_t
1322 using _Extents =
typename _Mapping::extents_type;
1323 constexpr auto __sta_exts = __mdspan::__static_extents<_Extents>(0, _Us);
1324 if constexpr (!__mdspan::__all_static(__sta_exts))
1325 return dynamic_extent;
1327 return __mdspan::__fwd_prod(__sta_exts);
1330 template<
size_t _Nm>
1331 static consteval bool
1332 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
size_t __sub_rank)
1333 {
return __mdspan::__is_block(__slice_kinds, __sub_rank); }
1337 struct _SubMdspanMapping<_LayoutSide::__left, true>
1339 using _Layout = layout_left;
1340 template<
size_t _Pad>
using _PaddedLayout = layout_left_padded<_Pad>;
1342 template<
typename _Mapping,
size_t _Us>
1343 static consteval size_t
1346 using _Extents =
typename _Mapping::extents_type;
1347 constexpr auto __sta_exts
1348 = __mdspan::__static_extents<_Extents>(1, _Us);
1349 constexpr auto __sta_padstride
1350 = __mdspan::__get_static_stride<_Mapping>();
1351 if constexpr (__sta_padstride == dynamic_extent
1352 || !__mdspan::__all_static(__sta_exts))
1353 return dynamic_extent;
1355 return __sta_padstride * __mdspan::__fwd_prod(__sta_exts);
1358 template<
size_t _Nm>
1359 static consteval bool
1360 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
1363 if (__sub_rank == 1)
1364 return __slice_kinds[0] == _SliceKind::__unit_stride_slice
1365 || __slice_kinds[0] == _SliceKind::__full;
1372 struct _SubMdspanMapping<_LayoutSide::__right, false>
1374 using _Layout = layout_right;
1375 template<
size_t _Pad>
using _PaddedLayout = layout_right_padded<_Pad>;
1377 template<
typename _Mapping,
size_t _Us>
1378 static consteval size_t
1381 using _Extents =
typename _Mapping::extents_type;
1382 constexpr auto __rank = _Extents::rank();
1383 constexpr auto __sta_exts
1384 = __mdspan::__static_extents<_Extents>(_Us + 1, __rank);
1385 if constexpr (!__mdspan::__all_static(__sta_exts))
1386 return dynamic_extent;
1388 return __fwd_prod(__sta_exts);
1391 template<
size_t _Nm>
1392 static consteval bool
1393 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
1396 auto __rev_slice_kinds = array<_SliceKind, _Nm>{};
1397 for(
size_t __i = 0; __i < _Nm; ++__i)
1398 __rev_slice_kinds[__i] = __slice_kinds[_Nm - 1 - __i];
1399 return __mdspan::__is_block(span(__rev_slice_kinds), __sub_rank);
1404 struct _SubMdspanMapping<_LayoutSide::__right, true>
1406 using _Layout = layout_right;
1407 template<
size_t _Pad>
using _PaddedLayout = layout_right_padded<_Pad>;
1409 template<
typename _Mapping,
size_t _Us>
1410 static consteval size_t
1413 using _Extents =
typename _Mapping::extents_type;
1414 constexpr auto __rank = _Extents::rank();
1415 constexpr auto __sta_exts
1416 = __mdspan::__static_extents<_Extents>(_Us + 1, __rank - 1);
1417 constexpr auto __sta_padstride
1418 = __mdspan::__get_static_stride<_Mapping>();
1419 if constexpr (__sta_padstride == dynamic_extent
1420 || !__mdspan::__all_static(__sta_exts))
1421 return dynamic_extent;
1423 return __sta_padstride * __mdspan::__fwd_prod(__sta_exts);
1426 template<
size_t _Nm>
1427 static consteval bool
1428 _S_is_unpadded_submdspan(span<const _SliceKind, _Nm> __slice_kinds,
1431 if (__sub_rank == 1)
1432 return __slice_kinds[_Nm - 1] == _SliceKind::__unit_stride_slice
1433 || __slice_kinds[_Nm - 1] == _SliceKind::__full;
1440 template<
typename _Mapping>
1442 __submdspan_mapping_impl(
const _Mapping& __mapping)
1443 {
return submdspan_mapping_result{__mapping, 0}; }
1445 template<
typename _Mapping,
typename... _Slices>
1446 requires (
sizeof...(_Slices) > 0)
1448 __submdspan_mapping_impl(
const _Mapping& __mapping, _Slices... __slices)
1450 using _IndexType =
typename _Mapping::index_type;
1451 static_assert((__acceptable_slice_type<_Slices, _IndexType> && ...));
1453 constexpr auto __side = __mdspan::__mapping_side<_Mapping>();
1454 constexpr auto __rank =
sizeof...(_Slices);
1455 using _Trait = _SubMdspanMapping<__side, __is_padded_mapping<_Mapping>>;
1456 using _SliceView = span<const _SliceKind, __rank>;
1458 constexpr auto __slice_kinds = __mdspan::__make_slice_kind_array<_Slices...>();
1459 auto __offset = __mdspan::__suboffset(__mapping, __slices...);
1460 auto __sub_exts = __mdspan::__subextents(__mapping.extents(), __slices...);
1461 using _SubExts =
decltype(__sub_exts);
1462 constexpr auto __sub_rank = _SubExts::rank();
1463 if constexpr (__sub_rank == 0)
1464 return submdspan_mapping_result{
1465 typename _Trait::_Layout::mapping(__sub_exts), __offset};
1466 else if constexpr (_Trait::_S_is_unpadded_submdspan(
1467 _SliceView(__slice_kinds), __sub_rank))
1468 return submdspan_mapping_result{
1469 typename _Trait::_Layout::mapping(__sub_exts), __offset};
1471 constexpr auto __u = __padded_block_begin<__side>(
1472 _SliceView(__slice_kinds), __sub_rank);
1473 __u != dynamic_extent)
1475 constexpr auto __pad = _Trait::template _S_pad<_Mapping, __u>();
1476 using _Layout =
typename _Trait::template _PaddedLayout<__pad>;
1477 return submdspan_mapping_result{
1478 typename _Layout::mapping(__sub_exts, __mapping.stride(__u)),
1484 = __mdspan::__substrides<_SubExts>(__mapping, __slices...);
1485 return submdspan_mapping_result{
1486 layout_stride::mapping(__sub_exts, __sub_strides), __offset};
1492 template<
typename _Extents>
1493 class layout_left::mapping
1496 using extents_type = _Extents;
1497 using index_type =
typename extents_type::index_type;
1498 using size_type =
typename extents_type::size_type;
1499 using rank_type =
typename extents_type::rank_type;
1500 using layout_type = layout_left;
1502 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1503 "The size of extents_type must be representable as index_type");
1506 mapping() noexcept = default;
1509 mapping(const mapping&) noexcept = default;
1512 mapping(const extents_type& __extents) noexcept
1513 : _M_extents(__extents)
1514 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
1516 template<
typename _OExtents>
1517 requires is_constructible_v<extents_type, _OExtents>
1518 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1519 mapping(
const mapping<_OExtents>& __other) noexcept
1520 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1523 template<
typename _OExtents>
1524 requires (extents_type::rank() <= 1)
1525 && is_constructible_v<extents_type, _OExtents>
1526 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1527 mapping(
const layout_right::mapping<_OExtents>& __other) noexcept
1528 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1532 template<
typename _OExtents>
1533 requires is_constructible_v<extents_type, _OExtents>
1534 constexpr explicit(!(extents_type::rank() == 0
1535 && is_convertible_v<_OExtents, extents_type>))
1536 mapping(
const layout_stride::mapping<_OExtents>& __other) noexcept
1537 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1538 { __glibcxx_assert(*
this == __other); }
1540#if __glibcxx_padded_layouts
1541 template<
typename _LeftpadMapping>
1542 requires __mdspan::__is_left_padded_mapping<_LeftpadMapping>
1543 && is_constructible_v<extents_type,
1544 typename _LeftpadMapping::extents_type>
1546 explicit(!is_convertible_v<
typename _LeftpadMapping::extents_type,
1548 mapping(
const _LeftpadMapping& __other) noexcept
1549 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1551 constexpr size_t __ostride_sta
1552 = __mdspan::__get_static_stride<_LeftpadMapping>();
1554 if constexpr (extents_type::rank() > 1)
1556 if constexpr (extents_type::static_extent(0) != dynamic_extent
1557 && __ostride_sta != dynamic_extent)
1558 static_assert(extents_type::static_extent(0) == __ostride_sta);
1560 __glibcxx_assert(__other.stride(1)
1561 == __other.extents().extent(0));
1567 operator=(
const mapping&)
noexcept =
default;
1569 constexpr const extents_type&
1570 extents() const noexcept {
return _M_extents; }
1572 constexpr index_type
1573 required_span_size() const noexcept
1574 {
return __mdspan::__size(_M_extents); }
1578 template<__mdspan::__val
id_index_type<index_type>... _Indices>
1579 requires (
sizeof...(_Indices) == extents_type::rank())
1580 constexpr index_type
1581 operator()(_Indices... __indices)
const noexcept
1583 return __mdspan::__linear_index_left(_M_extents,
1584 static_cast<index_type
>(
std::move(__indices))...);
1587 static constexpr bool
1588 is_always_unique() noexcept {
return true; }
1590 static constexpr bool
1591 is_always_exhaustive() noexcept {
return true; }
1593 static constexpr bool
1594 is_always_strided() noexcept {
return true; }
1596 static constexpr bool
1597 is_unique() noexcept {
return true; }
1599 static constexpr bool
1600 is_exhaustive() noexcept {
return true; }
1602 static constexpr bool
1603 is_strided() noexcept {
return true; }
1605 constexpr index_type
1606 stride(rank_type __i)
const noexcept
1607 requires (extents_type::rank() > 0)
1609 __glibcxx_assert(__i < extents_type::rank());
1610 return __mdspan::__fwd_prod(_M_extents, __i);
1613 template<
typename _OExtents>
1614 requires (extents_type::rank() == _OExtents::rank())
1615 friend constexpr bool
1616 operator==(
const mapping& __self,
const mapping<_OExtents>& __other)
1618 {
return __self.extents() == __other.extents(); }
1621 template<
typename _OExtents>
1623 mapping(
const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
1624 : _M_extents(__oexts)
1626 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1627 "The size of OtherExtents must be representable as index_type");
1628 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
1631#if __glibcxx_submdspan
1632 template<
typename... _Slices>
1633 requires (extents_type::rank() ==
sizeof...(_Slices))
1634 friend constexpr auto
1635 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
1636 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
1639 [[no_unique_address]] extents_type _M_extents{};
1644 template<
typename _Extents,
typename... _Indices>
1645 constexpr typename _Extents::index_type
1646 __linear_index_right(
const _Extents& __exts, _Indices... __indices)
1649 using _IndexType =
typename _Extents::index_type;
1650 array<_IndexType,
sizeof...(__indices)> __ind_arr{__indices...};
1651 _IndexType __res = 0;
1652 if constexpr (
sizeof...(__indices) > 0)
1654 _IndexType __mult = 1;
1655 auto __update = [&, __pos = __exts.rank()](_IndexType)
mutable
1658 _GLIBCXX_DEBUG_ASSERT(cmp_less(__ind_arr[__pos],
1659 __exts.extent(__pos)));
1660 __res += __ind_arr[__pos] * __mult;
1661 __mult *= __exts.extent(__pos);
1663 (__update(__indices), ...);
1669 template<
typename _Extents>
1670 class layout_right::mapping
1673 using extents_type = _Extents;
1674 using index_type =
typename extents_type::index_type;
1675 using size_type =
typename extents_type::size_type;
1676 using rank_type =
typename extents_type::rank_type;
1677 using layout_type = layout_right;
1679 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1680 "The size of extents_type must be representable as index_type");
1683 mapping() noexcept = default;
1686 mapping(const mapping&) noexcept = default;
1689 mapping(const extents_type& __extents) noexcept
1690 : _M_extents(__extents)
1691 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
1693 template<
typename _OExtents>
1694 requires is_constructible_v<extents_type, _OExtents>
1695 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1696 mapping(
const mapping<_OExtents>& __other) noexcept
1697 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1700 template<
typename _OExtents>
1701 requires (extents_type::rank() <= 1)
1702 && is_constructible_v<extents_type, _OExtents>
1703 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
1704 mapping(
const layout_left::mapping<_OExtents>& __other) noexcept
1705 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1708 template<
typename _OExtents>
1709 requires is_constructible_v<extents_type, _OExtents>
1710 constexpr explicit(!(extents_type::rank() == 0
1711 && is_convertible_v<_OExtents, extents_type>))
1712 mapping(
const layout_stride::mapping<_OExtents>& __other) noexcept
1713 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1714 { __glibcxx_assert(*
this == __other); }
1716#if __glibcxx_padded_layouts
1717 template<
typename _RightPaddedMapping>
1718 requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
1719 && is_constructible_v<extents_type,
1720 typename _RightPaddedMapping::extents_type>
1722 explicit(!is_convertible_v<
typename _RightPaddedMapping::extents_type,
1724 mapping(
const _RightPaddedMapping& __other) noexcept
1725 : mapping(__other.extents(), __mdspan::__internal_ctor{})
1727 constexpr size_t __rank = extents_type::rank();
1728 constexpr size_t __ostride_sta
1729 = __mdspan::__get_static_stride<_RightPaddedMapping>();
1731 if constexpr (__rank > 1)
1733 if constexpr (extents_type::static_extent(__rank - 1) != dynamic_extent
1734 && __ostride_sta != dynamic_extent)
1735 static_assert(extents_type::static_extent(__rank - 1)
1738 __glibcxx_assert(__other.stride(__rank - 2)
1739 == __other.extents().extent(__rank - 1));
1745 operator=(
const mapping&)
noexcept =
default;
1747 constexpr const extents_type&
1748 extents() const noexcept {
return _M_extents; }
1750 constexpr index_type
1751 required_span_size() const noexcept
1752 {
return __mdspan::__size(_M_extents); }
1756 template<__mdspan::__val
id_index_type<index_type>... _Indices>
1757 requires (
sizeof...(_Indices) == extents_type::rank())
1758 constexpr index_type
1759 operator()(_Indices... __indices)
const noexcept
1761 return __mdspan::__linear_index_right(
1762 _M_extents,
static_cast<index_type
>(
std::move(__indices))...);
1765 static constexpr bool
1766 is_always_unique() noexcept
1769 static constexpr bool
1770 is_always_exhaustive() noexcept
1773 static constexpr bool
1774 is_always_strided() noexcept
1777 static constexpr bool
1778 is_unique() noexcept
1781 static constexpr bool
1782 is_exhaustive() noexcept
1785 static constexpr bool
1786 is_strided() noexcept
1789 constexpr index_type
1790 stride(rank_type __i)
const noexcept
1791 requires (extents_type::rank() > 0)
1793 __glibcxx_assert(__i < extents_type::rank());
1794 return __mdspan::__rev_prod(_M_extents, __i);
1797 template<
typename _OExtents>
1798 requires (extents_type::rank() == _OExtents::rank())
1799 friend constexpr bool
1800 operator==(
const mapping& __self,
const mapping<_OExtents>& __other)
1802 {
return __self.extents() == __other.extents(); }
1805 template<
typename _OExtents>
1807 mapping(
const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
1808 : _M_extents(__oexts)
1810 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1811 "The size of OtherExtents must be representable as index_type");
1812 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
1815#if __glibcxx_submdspan
1816 template<
typename... _Slices>
1817 requires (extents_type::rank() ==
sizeof...(_Slices))
1818 friend constexpr auto
1819 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
1820 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
1823 [[no_unique_address]] extents_type _M_extents{};
1828 template<
typename _Mp>
1829 concept __mapping_alike =
requires
1831 requires __is_extents<typename _Mp::extents_type>;
1832 { _Mp::is_always_strided() } -> same_as<bool>;
1833 { _Mp::is_always_exhaustive() } -> same_as<bool>;
1834 { _Mp::is_always_unique() } -> same_as<bool>;
1835 bool_constant<_Mp::is_always_strided()>::value;
1836 bool_constant<_Mp::is_always_exhaustive()>::value;
1837 bool_constant<_Mp::is_always_unique()>::value;
1840 template<
typename _Mapping,
typename... _Indices>
1841 constexpr typename _Mapping::index_type
1842 __linear_index_strides(
const _Mapping& __m, _Indices... __indices)
1845 using _IndexType =
typename _Mapping::index_type;
1846 _IndexType __res = 0;
1847 if constexpr (
sizeof...(__indices) > 0)
1849 auto __update = [&, __pos = 0u](_IndexType __idx)
mutable
1851 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx,
1852 __m.extents().extent(__pos)));
1853 __res += __idx * __m.stride(__pos++);
1855 (__update(__indices), ...);
1861 template<
typename _Extents>
1862 class layout_stride::mapping
1865 using extents_type = _Extents;
1866 using index_type =
typename extents_type::index_type;
1867 using size_type =
typename extents_type::size_type;
1868 using rank_type =
typename extents_type::rank_type;
1869 using layout_type = layout_stride;
1871 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1872 "The size of extents_type must be representable as index_type");
1879 size_t __stride = 1;
1880 for (
size_t __i = extents_type::rank(); __i > 0; --__i)
1882 _M_strides[__i - 1] = index_type(__stride);
1883 __stride *= size_t(_M_extents.extent(__i - 1));
1888 mapping(
const mapping&)
noexcept =
default;
1890 template<
typename _OIndexType>
1891 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1893 mapping(
const extents_type& __exts,
1894 span<_OIndexType, extents_type::rank()> __strides) noexcept
1895 : _M_extents(__exts)
1897 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1898 _M_strides[__i] = index_type(as_const(__strides[__i]));
1901 template<
typename _OIndexType>
1902 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1904 mapping(
const extents_type& __exts,
1905 const array<_OIndexType, extents_type::rank()>& __strides)
1908 span<
const _OIndexType, extents_type::rank()>(__strides))
1911 template<__mdspan::__mapping_alike _Str
idedMapping>
1912 requires (is_constructible_v<extents_type,
1913 typename _StridedMapping::extents_type>
1914 && _StridedMapping::is_always_unique()
1915 && _StridedMapping::is_always_strided())
1916 constexpr explicit(!(
1917 is_convertible_v<typename _StridedMapping::extents_type, extents_type>
1918 && __mdspan::__standardized_mapping<_StridedMapping>))
1919 mapping(
const _StridedMapping& __other) noexcept
1920 : _M_extents(__other.extents())
1922 using _OIndexType = _StridedMapping::index_type;
1923 using _OExtents = _StridedMapping::extents_type;
1925 __glibcxx_assert(__mdspan::__offset(__other) == 0);
1926 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1927 "The size of StridedMapping::extents_type must be representable as"
1929 if constexpr (cmp_greater(__gnu_cxx::__int_traits<_OIndexType>::__max,
1930 __gnu_cxx::__int_traits<index_type>::__max))
1931 __glibcxx_assert(!cmp_less(
1932 __gnu_cxx::__int_traits<index_type>::__max,
1933 __other.required_span_size())
1934 &&
"other.required_span_size() must be representable"
1936 if constexpr (extents_type::rank() > 0)
1937 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1938 _M_strides[__i] = index_type(__other.stride(__i));
1942 operator=(
const mapping&)
noexcept =
default;
1944 constexpr const extents_type&
1945 extents() const noexcept {
return _M_extents; }
1947 constexpr array<index_type, extents_type::rank()>
1948 strides() const noexcept
1950 array<index_type, extents_type::rank()> __ret;
1951 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1952 __ret[__i] = _M_strides[__i];
1956 constexpr index_type
1957 required_span_size() const noexcept
1959 if (__mdspan::__empty(_M_extents))
1962 index_type __ret = 1;
1963 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
1964 __ret += (_M_extents.extent(__i) - 1) * _M_strides[__i];
1970 template<__mdspan::__val
id_index_type<index_type>... _Indices>
1971 requires (
sizeof...(_Indices) == extents_type::rank())
1972 constexpr index_type
1973 operator()(_Indices... __indices)
const noexcept
1975 return __mdspan::__linear_index_strides(*
this,
1976 static_cast<index_type
>(
std::move(__indices))...);
1979 static constexpr bool
1980 is_always_unique() noexcept {
return true; }
1984 static constexpr bool
1985 is_always_exhaustive() noexcept
1987 return (_Extents::rank() == 0) || __mdspan::__contains_zero(
1988 __mdspan::__static_extents<extents_type>());
1991 static constexpr bool
1992 is_always_strided() noexcept {
return true; }
1994 static constexpr bool
1995 is_unique() noexcept {
return true; }
2000 is_exhaustive() const noexcept
2002 if constexpr (!is_always_exhaustive())
2004 auto __size = __mdspan::__size(_M_extents);
2006 return __size == required_span_size();
2011 static constexpr bool
2012 is_strided() noexcept {
return true; }
2014 constexpr index_type
2015 stride(rank_type __r)
const noexcept {
return _M_strides[__r]; }
2017 template<__mdspan::__mapping_alike _OMapping>
2018 requires ((extents_type::rank() == _OMapping::extents_type::rank())
2019 && _OMapping::is_always_strided())
2020 friend constexpr bool
2021 operator==(
const mapping& __self,
const _OMapping& __other)
noexcept
2023 if (__self.extents() != __other.extents())
2025 if constexpr (extents_type::rank() > 0)
2026 for (
size_t __i = 0; __i < extents_type::rank(); ++__i)
2027 if (!cmp_equal(__self.stride(__i), __other.stride(__i)))
2029 return __mdspan::__offset(__other) == 0;
2033#if __glibcxx_submdspan
2034 template<
typename... _Slices>
2035 requires (extents_type::rank() ==
sizeof...(_Slices))
2036 friend constexpr auto
2037 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
2039 if constexpr (
sizeof...(_Slices) == 0)
2040 return submdspan_mapping_result{__mapping, 0};
2043 auto __offset = __mdspan::__suboffset(__mapping, __slices...);
2044 auto __sub_exts = __mdspan::__subextents(__mapping.extents(), __slices...);
2046 = __mdspan::__substrides<decltype(__sub_exts)>(__mapping, __slices...);
2047 return submdspan_mapping_result{
2048 layout_stride::mapping(__sub_exts, __sub_strides), __offset};
2053 using _Strides =
typename __array_traits<index_type,
2054 extents_type::rank()>::_Type;
2055 [[no_unique_address]] extents_type _M_extents;
2056 [[no_unique_address]] _Strides _M_strides;
2059#ifdef __glibcxx_padded_layouts
2063 __least_multiple(
size_t __x,
size_t __y)
2067 return (__y / __x + (__y % __x != 0)) * __x ;
2070 template<
typename _IndexType>
2072 __is_representable_least_multiple(
size_t __x,
size_t __y)
2074 constexpr auto __y_max = __gnu_cxx::__int_traits<_IndexType>::__max;
2075 if(std::cmp_greater(__y, __y_max))
2081 auto __max_delta = __y_max -
static_cast<_IndexType
>(__y);
2082 auto __y_mod_x = __y % __x;
2083 auto __delta = (__y_mod_x == 0) ?
size_t(0) : (__x - __y_mod_x);
2084 return std::cmp_less_equal(__delta, __max_delta);
2087 template<
typename _Extents,
size_t _PaddingValue,
typename _LayoutTraits,
2088 size_t _Rank = _Extents::rank()>
2089 concept __valid_static_stride = (_Extents::rank() <= 1)
2090 || (_PaddingValue == dynamic_extent)
2091 || (_Extents::static_extent(_LayoutTraits::_S_ext_idx) == dynamic_extent)
2092 || (__is_representable_least_multiple<size_t>(
2093 _PaddingValue, _Extents::static_extent(_LayoutTraits::_S_ext_idx)));
2095 template<
size_t _PaddedStride,
typename _Extents,
2096 typename _LayoutTraits>
2098 __is_representable_padded_size()
2100 using _IndexType =
typename _Extents::index_type;
2101 auto __sta_exts = __static_extents<_Extents>(
2102 _LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
2103 size_t __max = __gnu_cxx::__int_traits<_IndexType>::__max;
2104 return __static_quotient(__sta_exts, __max / _PaddedStride) != 0;
2107 template<
typename _Extents,
size_t _PaddedStride,
typename _LayoutTraits,
2108 size_t _Rank = _Extents::rank()>
2109 concept __valid_padded_size = (_Rank <= 1)
2110 || (_PaddedStride == dynamic_extent)
2111 || (!__all_static(__static_extents<_Extents>()))
2112 || (__contains_zero(__static_extents<_Extents>()))
2113 || (__is_representable_padded_size<_PaddedStride, _Extents,
2116 template<
typename _Extents,
typename _Stride,
typename... _Indices>
2117 constexpr typename _Extents::index_type
2118 __linear_index_leftpad(
const _Extents& __exts, _Stride __stride,
2119 _Indices... __indices)
2122 using _IndexType =
typename _Extents::index_type;
2123 _IndexType __res = 0;
2124 if constexpr (
sizeof...(__indices) > 0)
2126 _IndexType __mult = 1;
2128 auto __update_rest = [&, __pos = 1u](_IndexType __idx)
mutable
2130 __res += __idx * __mult;
2131 __mult *= __exts.extent(__pos);
2135 auto __update = [&](_IndexType __idx,
auto... __rest)
2138 __mult = __stride.extent(0);
2139 (__update_rest(__rest), ...);
2141 __update(__indices...);
2146 template<
typename _Extents,
typename _Stride,
typename... _Indices>
2147 constexpr typename _Extents::index_type
2148 __linear_index_rightpad(
const _Extents& __exts, _Stride __stride,
2149 _Indices... __indices)
2152 using _IndexType =
typename _Extents::index_type;
2153 _IndexType __res = 0;
2154 if constexpr (
sizeof...(__indices) > 0)
2156 _IndexType __mult = 1;
2157 array<_IndexType,
sizeof...(__indices)> __ind_arr{__indices...};
2159 auto __update_rest = [&, __pos = __exts.rank()-1](_IndexType)
mutable
2162 __res += __ind_arr[__pos] * __mult;
2163 __mult *= __exts.extent(__pos);
2166 auto __update = [&](_IndexType,
auto... __rest)
2168 __res += __ind_arr[__exts.rank() - 1];
2169 __mult = __stride.extent(0);
2170 (__update_rest(__rest), ...);
2172 __update(__indices...);
2177 template<
size_t _Rank>
2178 struct _LeftPaddedLayoutTraits
2180 using _LayoutSame = layout_left;
2181 using _LayoutOther = layout_right;
2183 constexpr static const size_t _S_ext_idx = 0;
2184 constexpr static const size_t _S_stride_idx = 1;
2185 constexpr static const size_t _S_unpad_begin = 1;
2186 constexpr static const size_t _S_unpad_end = _Rank;
2188 template<
typename _IndexType,
size_t _StaticStride,
size_t..._Extents>
2189 constexpr static auto
2190 _S_make_padded_extent(
2191 extents<_IndexType, _StaticStride> __stride,
2192 const extents<_IndexType, _Extents...>& __exts)
2194 auto __impl = [&]<
size_t... _Is>(integer_sequence<size_t, _Is...>)
2196 return extents<_IndexType, _StaticStride,
2197 (_Extents...[_Is + 1])...>{
2198 __stride.extent(0), __exts.extent(_Is + 1)...};
2200 return __impl(make_index_sequence<
sizeof...(_Extents) - 1>());
2204 template<
size_t _Rank>
2205 struct _RightPaddedLayoutTraits
2207 using _LayoutSame = layout_right;
2208 using _LayoutOther = layout_left;
2210 constexpr static size_t _S_ext_idx = _Rank - 1;
2211 constexpr static size_t _S_stride_idx = _Rank - 2;
2212 constexpr static size_t _S_unpad_begin = 0;
2213 constexpr static size_t _S_unpad_end = _Rank - 1;
2215 template<
typename _IndexType,
size_t _StaticStride,
size_t..._Extents>
2216 constexpr static auto
2217 _S_make_padded_extent(
2218 extents<_IndexType, _StaticStride> __stride,
2219 const extents<_IndexType, _Extents...>& __exts)
2221 auto __impl = [&]<
size_t... _Is>(integer_sequence<size_t, _Is...>)
2223 return extents<_IndexType, (_Extents...[_Is])..., _StaticStride>{
2224 __exts.extent(_Is)..., __stride.extent(0)};
2226 return __impl(make_index_sequence<
sizeof...(_Extents) - 1>());
2230 template<
size_t _PaddingValue,
typename _Extents,
typename _LayoutTraits>
2231 class _PaddedStorage
2233 using _LayoutSame =
typename _LayoutTraits::_LayoutSame;
2236 using _IndexType =
typename _Extents::index_type;
2237 constexpr static size_t _S_rank = _Extents::rank();
2241 static_assert((_PaddingValue == dynamic_extent)
2242 || (cmp_less_equal(_PaddingValue,
2243 __gnu_cxx::__int_traits<_IndexType>::__max)),
2244 "padding_value must be representable as index_type");
2246 static_assert(__representable_size<_Extents, _IndexType>,
2247 "The size of extents_type must be representable as index_type");
2249 static_assert(__valid_static_stride<_Extents, _PaddingValue,
2251 "The padded stride must be representable as size_t");
2253 static constexpr size_t _S_static_stride = []
consteval
2255 constexpr size_t __rank = _Extents::rank();
2256 if constexpr (__rank <= 1)
2260 constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
2261 constexpr size_t __sta_ext = _Extents::static_extent(__ext_idx);
2262 if constexpr (__sta_ext == 0)
2264 else if constexpr (_PaddingValue == dynamic_extent
2265 || __sta_ext == dynamic_extent)
2266 return dynamic_extent;
2268 return __least_multiple(_PaddingValue, __sta_ext);
2272 static_assert(_S_static_stride == dynamic_extent
2273 || cmp_less_equal(_S_static_stride,
2274 __gnu_cxx::__int_traits<_IndexType>::__max),
2275 "Padded stride must be representable as index_type");
2277 static_assert(__valid_padded_size<_Extents, _S_static_stride,
2281 _PaddedStorage() noexcept
2283 if constexpr (_S_rank > 1)
2284 if constexpr (_S_static_stride == dynamic_extent
2285 && _S_static_padextent() != dynamic_extent)
2286 _M_stride = _Stride{_S_static_padextent()};
2290 _PaddedStorage(
const _Extents& __exts)
2291 : _M_extents(__exts)
2293 if constexpr (!__all_static(__static_extents<_Extents>()))
2294 __glibcxx_assert(__is_representable_extents(_M_extents));
2296 if constexpr (_S_rank > 1)
2298 _IndexType __stride;
2299 if constexpr (_PaddingValue == dynamic_extent)
2300 __stride = _M_padextent();
2301 else if constexpr (_S_static_padextent() != dynamic_extent)
2306 __is_representable_least_multiple<_IndexType>(
2307 _PaddingValue, _M_padextent()));
2309 __stride =
static_cast<_IndexType
>(
2310 __least_multiple(_PaddingValue, _M_padextent()));
2312 __glibcxx_assert(__is_representable_extents(
2313 _LayoutTraits::_S_make_padded_extent(
2314 std::dextents<_IndexType, 1>{__stride},
2317 _M_stride = _Stride{__stride};
2322 _PaddedStorage(
const _Extents& __exts, _IndexType __pad)
2323 : _M_extents(__exts)
2325 if constexpr (_PaddingValue != dynamic_extent)
2326 __glibcxx_assert(cmp_equal(_PaddingValue, __pad));
2327 if constexpr (_S_rank > 1 && _S_static_stride == dynamic_extent)
2330 __is_representable_least_multiple<_IndexType>(
2331 __pad, _M_padextent()));
2333 _M_stride = _Stride{
static_cast<_IndexType
>(
2334 __least_multiple(__pad, _M_padextent()))};
2336 __glibcxx_assert(__is_representable_extents(
2337 _LayoutTraits::_S_make_padded_extent(
2338 _M_stride, _M_extents)));
2342 template<
typename _OExtents>
2345 const typename _LayoutSame::template mapping<_OExtents>& __other)
2346 : _PaddedStorage(_Extents(__other.extents()))
2348 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
2349 constexpr size_t __ext_idx = _LayoutTraits::_S_ext_idx;
2350 if constexpr (_S_rank > 1 && _PaddingValue != dynamic_extent)
2352 static_assert(_S_static_stride == dynamic_extent
2353 || _OExtents::static_extent(__ext_idx) == dynamic_extent
2354 || _S_static_stride == _OExtents::static_extent(__ext_idx),
2355 "The padded stride must be compatible with other");
2357 if constexpr (_S_static_stride == dynamic_extent
2358 || _OExtents::static_extent(__stride_idx) == dynamic_extent)
2359 __glibcxx_assert(std::cmp_equal(_M_padstride(),
2364 template<
typename _OExtents>
2366 _PaddedStorage(
const typename layout_stride::mapping<_OExtents>&
2368 : _M_extents(__other.extents())
2370 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
2374 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
2375 if constexpr (_S_rank > 1)
2377 if constexpr (_PaddingValue != dynamic_extent)
2378 __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
2379 _M_calc_padstride())
2380 &&
"The padded stride must be compatible with other");
2381 if constexpr (_S_static_stride == dynamic_extent)
2382 _M_stride = _Stride{__other.stride(__stride_idx)};
2386 template<
typename _SamePaddedMapping>
2388 _PaddedStorage(_LayoutTraits::_LayoutSame,
2389 const _SamePaddedMapping& __other)
2390 : _M_extents(__other.extents())
2392 if constexpr (_S_rank > 1)
2394 static_assert(_PaddingValue == dynamic_extent
2395 || _SamePaddedMapping::padding_value == dynamic_extent
2396 || _PaddingValue == _SamePaddedMapping::padding_value,
2397 "If neither PaddingValue is dynamic_extent, then they must "
2400 constexpr size_t __stride_idx = _LayoutTraits::_S_stride_idx;
2401 if constexpr (_PaddingValue != dynamic_extent)
2402 __glibcxx_assert(cmp_equal(__other.stride(__stride_idx),
2403 _M_calc_padstride())
2404 &&
"The padded stride must be compatible with other");
2405 if constexpr (_S_static_stride == dynamic_extent)
2406 _M_stride = _Stride{__other.stride(__stride_idx)};
2408 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
2409 __gnu_cxx::__int_traits<_IndexType>::__max));
2412 template<
typename _OtherPaddedMapping>
2414 _PaddedStorage(_LayoutTraits::_LayoutOther,
2415 const _OtherPaddedMapping& __other) noexcept
2416 : _M_extents(__other.extents())
2418 __glibcxx_assert(cmp_less_equal(__other.required_span_size(),
2419 __gnu_cxx::__int_traits<_IndexType>::__max));
2422 static constexpr bool
2423 _M_is_always_exhaustive() noexcept
2425 if constexpr (_S_rank <= 1)
2428 return _S_static_padextent() != dynamic_extent
2429 && _S_static_stride != dynamic_extent
2430 && _S_static_padextent() == _S_static_stride;
2434 _M_is_exhaustive() const noexcept
2436 if constexpr (_M_is_always_exhaustive())
2439 return cmp_equal(_M_padextent(), _M_padstride());
2442 constexpr static size_t
2443 _S_static_padextent() noexcept
2444 {
return _Extents::static_extent(_LayoutTraits::_S_ext_idx); }
2446 constexpr _IndexType
2447 _M_padextent() const noexcept
2448 {
return _M_extents.extent(_LayoutTraits::_S_ext_idx); }
2450 constexpr _IndexType
2451 _M_calc_padstride() const noexcept
2453 if constexpr (_S_static_stride != dynamic_extent)
2454 return _S_static_stride;
2455 else if constexpr (_PaddingValue != dynamic_extent)
2456 return __least_multiple(_PaddingValue, _M_padextent());
2458 return _M_padextent();
2461 constexpr _IndexType
2462 _M_padstride() const noexcept
2463 {
return _M_stride.extent(0); }
2465 constexpr _IndexType
2466 _M_required_span_size() const noexcept
2468 if constexpr (_S_rank == 0)
2470 else if (__mdspan::__empty(_M_extents))
2474 size_t __stride =
static_cast<size_t>(_M_padstride());
2475 size_t __prod_rest = __mdspan::__fwd_prod(_M_extents,
2476 _LayoutTraits::_S_unpad_begin, _LayoutTraits::_S_unpad_end);
2477 size_t __delta = _M_padstride() - _M_padextent();
2478 return static_cast<_IndexType
>(__stride * __prod_rest - __delta);
2482 template<
typename _SamePaddedMapping>
2484 _M_equal(
const _SamePaddedMapping& __other)
const noexcept
2486 return _M_extents == __other.extents()
2488 || cmp_equal(_M_stride.extent(0),
2489 __other.stride(_LayoutTraits::_S_stride_idx)));
2492 using _Stride = std::extents<_IndexType, _S_static_stride>;
2493 [[no_unique_address]] _Stride _M_stride;
2494 [[no_unique_address]] _Extents _M_extents;
2498 template<
size_t _PaddingValue>
2499 template<
typename _Extents>
2500 class layout_left_padded<_PaddingValue>::mapping
2503 static constexpr size_t padding_value = _PaddingValue;
2505 using extents_type = _Extents;
2506 using index_type =
typename extents_type::index_type;
2507 using size_type =
typename extents_type::size_type;
2508 using rank_type =
typename extents_type::rank_type;
2509 using layout_type = layout_left_padded<padding_value>;
2512 static constexpr size_t _S_rank = extents_type::rank();
2513 using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
2514 _Extents, __mdspan::_LeftPaddedLayoutTraits<_S_rank>>;
2515 [[no_unique_address]] _PaddedStorage _M_storage;
2517 consteval friend size_t
2518 __mdspan::__get_static_stride<mapping>();
2520 constexpr index_type
2521 _M_extent(
size_t __r)
const noexcept
2522 {
return _M_storage._M_extents.extent(__r); }
2524 constexpr index_type
2525 _M_padstride() const noexcept
2526 {
return _M_storage._M_stride.extent(0); }
2534 mapping(
const mapping&)
noexcept =
default;
2537 mapping(
const extents_type& __exts)
2538 : _M_storage(__exts)
2541 template<__mdspan::__val
id_index_type<index_type> _OIndexType>
2543 mapping(
const extents_type& __exts, _OIndexType __pad)
2544 : _M_storage(__exts,
2545 __mdspan::__index_type_cast<index_type>(std::move(__pad)))
2548 template<
typename _OExtents>
2549 requires is_constructible_v<extents_type, _OExtents>
2550 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
2551 mapping(
const layout_left::mapping<_OExtents>& __other)
2552 : _M_storage(__other)
2555 template<
typename _OExtents>
2556 requires is_constructible_v<_OExtents, extents_type>
2557 constexpr explicit(!(_OExtents::rank() == 0
2558 && is_convertible_v<_OExtents, extents_type>))
2559 mapping(
const typename layout_stride::mapping<_OExtents>& __other)
2560 : _M_storage(__other)
2561 { __glibcxx_assert(*
this == __other); }
2563 template<
typename _LeftPaddedMapping>
2564 requires __mdspan::__is_left_padded_mapping<_LeftPaddedMapping>
2565 && is_constructible_v<extents_type,
2566 typename _LeftPaddedMapping::extents_type>
2568 !is_convertible_v<
typename _LeftPaddedMapping::extents_type,
2570 || _S_rank > 1 && (padding_value != dynamic_extent
2571 || _LeftPaddedMapping::padding_value == dynamic_extent))
2572 mapping(
const _LeftPaddedMapping& __other)
2573 : _M_storage(layout_left{}, __other)
2576 template<
typename _RightPaddedMapping>
2577 requires (__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2578 || __mdspan::__mapping_of<layout_right, _RightPaddedMapping>)
2580 && is_constructible_v<extents_type,
2581 typename _RightPaddedMapping::extents_type>
2582 constexpr explicit(!is_convertible_v<
2583 typename _RightPaddedMapping::extents_type, extents_type>)
2584 mapping(
const _RightPaddedMapping& __other) noexcept
2585 : _M_storage(layout_right{}, __other)
2589 operator=(
const mapping&)
noexcept =
default;
2591 constexpr const extents_type&
2592 extents() const noexcept {
return _M_storage._M_extents; }
2594 constexpr array<index_type, _S_rank>
2595 strides() const noexcept
2597 array<index_type, _S_rank> __ret;
2598 if constexpr (_S_rank > 0)
2600 if constexpr (_S_rank > 1)
2601 __ret[1] = _M_padstride();
2602 if constexpr (_S_rank > 2)
2603 for(
size_t __i = 2; __i < _S_rank; ++__i)
2604 __ret[__i] = __ret[__i - 1] * _M_extent(__i - 1);
2608 constexpr index_type
2609 required_span_size() const noexcept
2610 {
return _M_storage._M_required_span_size(); }
2614 template<__mdspan::__val
id_index_type<index_type>... _Indices>
2615 requires (
sizeof...(_Indices) == _S_rank)
2616 constexpr index_type
2617 operator()(_Indices... __indices)
const noexcept
2619 return __mdspan::__linear_index_leftpad(
2620 extents(), _M_storage._M_stride,
2621 static_cast<index_type
>(
std::move(__indices))...);
2624 static constexpr bool
2625 is_always_exhaustive() noexcept
2626 {
return _PaddedStorage::_M_is_always_exhaustive(); }
2629 is_exhaustive() noexcept
2630 {
return _M_storage._M_is_exhaustive(); }
2632 static constexpr bool
2633 is_always_unique() noexcept {
return true; }
2635 static constexpr bool
2636 is_unique() noexcept {
return true; }
2638 static constexpr bool
2639 is_always_strided() noexcept {
return true; }
2641 static constexpr bool
2642 is_strided() noexcept {
return true; }
2644 constexpr index_type
2645 stride(rank_type __r)
const noexcept
2647 __glibcxx_assert(__r < _S_rank);
2651 return static_cast<index_type
>(
2652 static_cast<size_t>(_M_padstride()) *
2653 static_cast<size_t>(__mdspan::__fwd_prod(extents(), 1, __r)));
2656 template<
typename _LeftpadMapping>
2657 requires(__mdspan::__is_left_padded_mapping<_LeftpadMapping>
2658 && _LeftpadMapping::extents_type::rank() == _S_rank)
2659 friend constexpr bool
2660 operator==(
const mapping& __self,
const _LeftpadMapping& __other)
2662 {
return __self._M_storage._M_equal(__other); }
2665#if __glibcxx_submdspan
2666 template<
typename... _Slices>
2667 requires (extents_type::rank() ==
sizeof...(_Slices))
2668 friend constexpr auto
2669 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
2670 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
2674 template<
size_t _PaddingValue>
2675 template<
typename _Extents>
2676 class layout_right_padded<_PaddingValue>::mapping {
2678 static constexpr size_t padding_value = _PaddingValue;
2679 using extents_type = _Extents;
2680 using index_type =
typename extents_type::index_type;
2681 using size_type =
typename extents_type::size_type;
2682 using rank_type =
typename extents_type::rank_type;
2683 using layout_type = layout_right_padded<_PaddingValue>;
2686 static constexpr size_t _S_rank = extents_type::rank();
2687 using _PaddedStorage = __mdspan::_PaddedStorage<_PaddingValue,
2688 _Extents, __mdspan::_RightPaddedLayoutTraits<_S_rank>>;
2689 [[no_unique_address]] _PaddedStorage _M_storage;
2691 consteval friend size_t
2692 __mdspan::__get_static_stride<mapping>();
2694 constexpr index_type
2695 _M_extent(
size_t __r)
const noexcept
2696 {
return _M_storage._M_extents.extent(__r); }
2698 constexpr index_type
2699 _M_padstride() const noexcept
2700 {
return _M_storage._M_stride.extent(0); }
2708 mapping(
const mapping&)
noexcept =
default;
2711 mapping(
const extents_type& __exts)
2712 : _M_storage(__exts)
2715 template<__mdspan::__val
id_index_type<index_type> _OIndexType>
2717 mapping(
const extents_type& __exts, _OIndexType __pad)
2718 : _M_storage(__exts,
2719 __mdspan::__index_type_cast<index_type>(std::move(__pad)))
2722 template<
typename _OExtents>
2723 requires is_constructible_v<extents_type, _OExtents>
2724 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
2725 mapping(
const layout_right::mapping<_OExtents>& __other)
2726 : _M_storage(__other)
2729 template<
typename _OExtents>
2730 requires is_constructible_v<_OExtents, extents_type>
2731 constexpr explicit(!(_OExtents::rank() == 0
2732 && is_convertible_v<_OExtents, extents_type>))
2733 mapping(
const typename layout_stride::mapping<_OExtents>& __other)
2734 : _M_storage(__other)
2735 { __glibcxx_assert(*
this == __other); }
2737 template<
typename _RightPaddedMapping>
2738 requires __mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2739 && is_constructible_v<extents_type,
2740 typename _RightPaddedMapping::extents_type>
2742 !is_convertible_v<
typename _RightPaddedMapping::extents_type,
2744 || _S_rank > 1 && (padding_value != dynamic_extent
2745 || _RightPaddedMapping::padding_value == dynamic_extent))
2746 mapping(
const _RightPaddedMapping& __other)
2747 : _M_storage(layout_right{}, __other)
2750 template<
typename _LeftPaddedMapping>
2751 requires (__mdspan::__is_left_padded_mapping<_LeftPaddedMapping>
2752 || __mdspan::__mapping_of<layout_left, _LeftPaddedMapping>)
2754 && is_constructible_v<extents_type,
2755 typename _LeftPaddedMapping::extents_type>
2756 constexpr explicit(!is_convertible_v<
2757 typename _LeftPaddedMapping::extents_type, extents_type>)
2758 mapping(
const _LeftPaddedMapping& __other) noexcept
2759 : _M_storage(layout_left{}, __other)
2762 constexpr mapping& operator=(
const mapping&)
noexcept =
default;
2764 constexpr const extents_type&
2765 extents() const noexcept {
return _M_storage._M_extents; }
2767 constexpr array<index_type, _S_rank>
2768 strides() const noexcept
2770 array<index_type, _S_rank> __ret;
2771 if constexpr (_S_rank > 0)
2772 __ret[_S_rank - 1] = 1;
2773 if constexpr (_S_rank > 1)
2774 __ret[_S_rank - 2] = _M_padstride();
2775 if constexpr (_S_rank > 2)
2776 for(
size_t __i = _S_rank - 2; __i > 0; --__i)
2777 __ret[__i - 1] = __ret[__i] * _M_extent(__i);
2781 constexpr index_type
2782 required_span_size() const noexcept
2783 {
return _M_storage._M_required_span_size(); }
2787 template<__mdspan::__val
id_index_type<index_type>... _Indices>
2788 requires (
sizeof...(_Indices) == _S_rank)
2789 constexpr index_type
2790 operator()(_Indices... __indices)
const noexcept
2792 return __mdspan::__linear_index_rightpad(
2793 extents(), _M_storage._M_stride,
2794 static_cast<index_type
>(
std::move(__indices))...);
2797 static constexpr bool
2798 is_always_exhaustive() noexcept
2799 {
return _PaddedStorage::_M_is_always_exhaustive(); }
2802 is_exhaustive() noexcept
2803 {
return _M_storage._M_is_exhaustive(); }
2805 static constexpr bool
2806 is_always_unique() noexcept {
return true; }
2808 static constexpr bool
2809 is_unique() noexcept {
return true; }
2811 static constexpr bool
2812 is_always_strided() noexcept {
return true; }
2814 static constexpr bool
2815 is_strided() noexcept {
return true; }
2817 constexpr index_type
2818 stride(rank_type __r)
const noexcept
2820 __glibcxx_assert(__r < _S_rank);
2821 if constexpr (_S_rank <= 1)
2823 else if (__r == _S_rank - 1)
2825 else if (__r == _S_rank - 2)
2826 return _M_padstride();
2828 return static_cast<index_type
>(
2829 static_cast<size_t>(_M_padstride()) *
2830 static_cast<size_t>(__mdspan::__fwd_prod(
2831 extents(), __r + 1, _S_rank - 1)));
2834 template<
typename _RightPaddedMapping>
2835 requires(__mdspan::__is_right_padded_mapping<_RightPaddedMapping>
2836 && _RightPaddedMapping::extents_type::rank() == _S_rank)
2837 friend constexpr bool
2838 operator==(
const mapping& __self,
const _RightPaddedMapping& __other)
2840 {
return __self._M_storage._M_equal(__other); }
2842#if __glibcxx_submdspan
2844 template<
typename... _Slices>
2845 requires (extents_type::rank() ==
sizeof...(_Slices))
2846 friend constexpr auto
2847 submdspan_mapping(
const mapping& __mapping, _Slices... __slices)
2848 {
return __mdspan::__submdspan_mapping_impl(__mapping, __slices...); }
2853 template<
typename _ElementType>
2854 struct default_accessor
2856 static_assert(!is_array_v<_ElementType>,
2857 "ElementType must not be an array type");
2858 static_assert(!is_abstract_v<_ElementType>,
2859 "ElementType must not be an abstract class type");
2861 using offset_policy = default_accessor;
2862 using element_type = _ElementType;
2863 using reference = element_type&;
2864 using data_handle_type = element_type*;
2867 default_accessor() noexcept = default;
2869 template<typename _OElementType>
2870 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
2872 default_accessor(default_accessor<_OElementType>) noexcept
2876 access(data_handle_type __p,
size_t __i)
const noexcept
2877 {
return __p[__i]; }
2879 constexpr data_handle_type
2880 offset(data_handle_type __p,
size_t __i)
const noexcept
2881 {
return __p + __i; }
2884#ifdef __glibcxx_aligned_accessor
2885 template<
typename _ElementType,
size_t _ByteAlignment>
2886 struct aligned_accessor
2888 static_assert(has_single_bit(_ByteAlignment),
2889 "ByteAlignment must be a power of two");
2890 static_assert(_ByteAlignment >=
alignof(_ElementType));
2892 using offset_policy = default_accessor<_ElementType>;
2893 using element_type = _ElementType;
2894 using reference = element_type&;
2895 using data_handle_type = element_type*;
2897 static constexpr size_t byte_alignment = _ByteAlignment;
2900 aligned_accessor() noexcept = default;
2902 template<typename _OElementType,
size_t _OByteAlignment>
2903 requires (_OByteAlignment >= byte_alignment)
2904 && is_convertible_v<_OElementType(*)[], element_type(*)[]>
2906 aligned_accessor(aligned_accessor<_OElementType, _OByteAlignment>)
2910 template<
typename _OElementType>
2911 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
2913 aligned_accessor(default_accessor<_OElementType>)
noexcept
2916 template<
typename _OElementType>
2917 requires is_convertible_v<element_type(*)[], _OElementType(*)[]>
2919 operator default_accessor<_OElementType>() const noexcept
2923 access(data_handle_type __p,
size_t __i)
const noexcept
2926 constexpr typename offset_policy::data_handle_type
2927 offset(data_handle_type __p,
size_t __i)
const noexcept
2932 template<
typename _ElementType,
typename _Extents,
2933 typename _LayoutPolicy = layout_right,
2934 typename _AccessorPolicy = default_accessor<_ElementType>>
2937 static_assert(!is_array_v<_ElementType>,
2938 "ElementType must not be an array type");
2939 static_assert(!is_abstract_v<_ElementType>,
2940 "ElementType must not be an abstract class type");
2941 static_assert(__mdspan::__is_extents<_Extents>,
2942 "Extents must be a specialization of std::extents");
2943 static_assert(is_same_v<_ElementType,
2944 typename _AccessorPolicy::element_type>);
2947 using extents_type = _Extents;
2948 using layout_type = _LayoutPolicy;
2949 using accessor_type = _AccessorPolicy;
2950 using mapping_type =
typename layout_type::template mapping<extents_type>;
2951 using element_type = _ElementType;
2952 using value_type = remove_cv_t<element_type>;
2953 using index_type =
typename extents_type::index_type;
2954 using size_type =
typename extents_type::size_type;
2955 using rank_type =
typename extents_type::rank_type;
2956 using data_handle_type =
typename accessor_type::data_handle_type;
2957 using reference =
typename accessor_type::reference;
2959 static constexpr rank_type
2960 rank() noexcept {
return extents_type::rank(); }
2962 static constexpr rank_type
2963 rank_dynamic() noexcept {
return extents_type::rank_dynamic(); }
2965 static constexpr size_t
2966 static_extent(rank_type __r)
noexcept
2967 {
return extents_type::static_extent(__r); }
2969 constexpr index_type
2970 extent(rank_type __r)
const noexcept {
return extents().extent(__r); }
2974 requires (rank_dynamic() > 0)
2975 && is_default_constructible_v<data_handle_type>
2976 && is_default_constructible_v<mapping_type>
2977 && is_default_constructible_v<accessor_type> =
default;
2980 mdspan(
const mdspan& __other) =
default;
2983 mdspan(mdspan&& __other) =
default;
2985 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
2986 requires (
sizeof...(_OIndexTypes) == rank()
2987 ||
sizeof...(_OIndexTypes) == rank_dynamic())
2988 && is_constructible_v<mapping_type, extents_type>
2989 && is_default_constructible_v<accessor_type>
2991 mdspan(data_handle_type __handle, _OIndexTypes... __exts)
2993 _M_mapping(_Extents(
static_cast<index_type
>(
std::move(__exts))...)),
2997 template<
typename _OIndexType,
size_t _Nm>
2998 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
2999 && (_Nm == rank() || _Nm == rank_dynamic())
3000 && is_constructible_v<mapping_type, extents_type>
3001 && is_default_constructible_v<accessor_type>
3002 constexpr explicit(_Nm != rank_dynamic())
3003 mdspan(data_handle_type __handle, span<_OIndexType, _Nm> __exts)
3004 : _M_accessor(), _M_mapping(extents_type(__exts)),
3005 _M_handle(std::move(__handle))
3008 template<
typename _OIndexType,
size_t _Nm>
3009 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3010 && (_Nm == rank() || _Nm == rank_dynamic())
3011 && is_constructible_v<mapping_type, extents_type>
3012 && is_default_constructible_v<accessor_type>
3013 constexpr explicit(_Nm != rank_dynamic())
3014 mdspan(data_handle_type __handle,
const array<_OIndexType, _Nm>& __exts)
3015 : _M_accessor(), _M_mapping(extents_type(__exts)),
3016 _M_handle(std::move(__handle))
3020 mdspan(data_handle_type __handle,
const extents_type& __exts)
3021 requires is_constructible_v<mapping_type, const extents_type&>
3022 && is_default_constructible_v<accessor_type>
3023 : _M_accessor(), _M_mapping(__exts), _M_handle(
std::move(__handle))
3027 mdspan(data_handle_type __handle,
const mapping_type& __mapping)
3028 requires is_default_constructible_v<accessor_type>
3029 : _M_accessor(), _M_mapping(__mapping), _M_handle(
std::move(__handle))
3033 mdspan(data_handle_type __handle,
const mapping_type& __mapping,
3034 const accessor_type& __accessor)
3035 : _M_accessor(__accessor), _M_mapping(__mapping),
3036 _M_handle(std::move(__handle))
3039 template<
typename _OElementType,
typename _OExtents,
typename _OLayout,
3040 typename _OAccessor>
3041 requires is_constructible_v<mapping_type,
3042 const typename _OLayout::template mapping<_OExtents>&>
3043 && is_constructible_v<accessor_type, const _OAccessor&>
3044 constexpr explicit(!is_convertible_v<
3045 const typename _OLayout::template mapping<_OExtents>&, mapping_type>
3046 || !is_convertible_v<const _OAccessor&, accessor_type>)
3047 mdspan(
const mdspan<_OElementType, _OExtents, _OLayout, _OAccessor>&
3049 : _M_accessor(__other.accessor()), _M_mapping(__other.mapping()),
3050 _M_handle(__other.data_handle())
3052 static_assert(is_constructible_v<data_handle_type,
3053 const typename _OAccessor::data_handle_type&>);
3054 static_assert(is_constructible_v<extents_type, _OExtents>);
3058 operator=(
const mdspan& __other) =
default;
3061 operator=(mdspan&& __other) =
default;
3063 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
3064 requires (
sizeof...(_OIndexTypes) == rank())
3066 operator[](_OIndexTypes... __indices)
const
3068 if constexpr (rank() == 0)
3069 return _M_accessor.access(_M_handle, _M_mapping());
3070 else if constexpr (!(is_same_v<_OIndexTypes, index_type> && ...))
3072 __mdspan::__index_type_cast<index_type>(
std::move(__indices))...);
3075 auto __is_multi_index = [&]<
size_t... _Counts>(
index_sequence<_Counts...>)
3076 {
return ((__indices < extents().extent(_Counts)) && ...); };
3078 __glibcxx_assert(__is_multi_index(make_index_sequence<rank()>()));
3079 return _M_accessor.access(_M_handle, _M_mapping(__indices...));
3083 template<
typename _OIndexType>
3084 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3086 operator[](span<_OIndexType, rank()> __indices)
const
3092 __mdspan::__index_type_cast<index_type>(as_const(__indices[_Counts]))...);
3094 return __call(make_index_sequence<rank()>());
3097 template<
typename _OIndexType>
3098 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3100 operator[](
const array<_OIndexType, rank()>& __indices)
const
3101 {
return operator[](span<
const _OIndexType, rank()>(__indices)); }
3103#if __cplusplus > 202302L
3104 template<__mdspan::__val
id_index_type<index_type>... _OIndexTypes>
3105 requires (
sizeof...(_OIndexTypes) == rank())
3107 at(_OIndexTypes... __indices)
const
3109 if constexpr (rank() == 0)
3110 return _M_accessor.access(_M_handle, _M_mapping());
3111 else if constexpr (!(is_integral_v<_OIndexTypes> && ...))
3112 return at(__index_int_t<_OIndexTypes>(
std::move(__indices))...);
3115 auto __check_bound = [&]<
typename _OIntType>(
size_t __dim, _OIntType __index)
3117 if constexpr (is_signed_v<_OIntType>)
3119 std::__throw_out_of_range_fmt(
3120 __N(
"mdspan::at: %zuth index is negative"), __dim);
3122 const auto __ext = extents().extent(__dim);
3123 if (std::cmp_greater_equal(__index, __ext))
3124 std::__throw_out_of_range_fmt(
3125 __N(
"mdspan::at: %zuth index (which is %zu)"
3126 " >= extent(%zu) (which is %zu)"),
3127 __dim,
size_t(__index), __dim,
size_t(__ext));
3129 auto __check_bounds = [&]<
size_t... _Counts>(
index_sequence<_Counts...>)
3130 { (__check_bound(_Counts, __indices), ...); };
3132 __check_bounds(make_index_sequence<rank()>());
3133 auto __index = _M_mapping(
static_cast<index_type
>(__indices)...);
3134 return _M_accessor.access(_M_handle, __index);
3138 template<
typename _OIndexType>
3139 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3141 at(span<_OIndexType, rank()> __indices)
const
3147 __index_int_t<_OIndexType>(as_const(__indices[_Counts]))...);
3149 return __call(make_index_sequence<rank()>());
3152 template<
typename _OIndexType>
3153 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
3155 at(
const array<_OIndexType, rank()>& __indices)
const
3156 {
return at(span<
const _OIndexType, rank()>(__indices)); }
3160 size() const noexcept
3162 __glibcxx_assert(cmp_less_equal(_M_mapping.required_span_size(),
3165 return size_type(__mdspan::__size(extents()));
3170 empty() const noexcept
3171 {
return __mdspan::__empty(extents()); }
3173 friend constexpr void
3174 swap(mdspan& __x, mdspan& __y)
noexcept
3177 swap(__x._M_mapping, __y._M_mapping);
3178 swap(__x._M_accessor, __y._M_accessor);
3179 swap(__x._M_handle, __y._M_handle);
3182 constexpr const extents_type&
3183 extents() const noexcept {
return _M_mapping.extents(); }
3185 constexpr const data_handle_type&
3186 data_handle() const noexcept {
return _M_handle; }
3188 constexpr const mapping_type&
3189 mapping() const noexcept {
return _M_mapping; }
3191 constexpr const accessor_type&
3192 accessor() const noexcept {
return _M_accessor; }
3196 static constexpr bool
3197 is_always_unique() noexcept(noexcept(mapping_type::is_always_unique()))
3198 {
return mapping_type::is_always_unique(); }
3200 static constexpr bool
3201 is_always_exhaustive()
3202 noexcept(noexcept(mapping_type::is_always_exhaustive()))
3203 {
return mapping_type::is_always_exhaustive(); }
3205 static constexpr bool
3207 noexcept(noexcept(mapping_type::is_always_strided()))
3208 {
return mapping_type::is_always_strided(); }
3211 is_unique() const noexcept(noexcept(_M_mapping.is_unique()))
3212 {
return _M_mapping.is_unique(); }
3215 is_exhaustive() const noexcept(noexcept(_M_mapping.is_exhaustive()))
3216 {
return _M_mapping.is_exhaustive(); }
3219 is_strided() const noexcept(noexcept(_M_mapping.is_strided()))
3220 {
return _M_mapping.is_strided(); }
3222 constexpr index_type
3223 stride(rank_type __r)
const {
return _M_mapping.stride(__r); }
3226 template<
typename _OIndexType>
3227 using __index_int_t = std::__conditional_t<
3228 is_integral_v<_OIndexType>, _OIndexType, index_type>;
3230 [[no_unique_address]] accessor_type _M_accessor = accessor_type();
3231 [[no_unique_address]] mapping_type _M_mapping = mapping_type();
3232 [[no_unique_address]] data_handle_type _M_handle = data_handle_type();
3235 template<
typename _CArray>
3236 requires is_array_v<_CArray> && (rank_v<_CArray> == 1)
3238 -> mdspan<remove_all_extents_t<_CArray>,
3239 extents<size_t, extent_v<_CArray, 0>>>;
3241 template<
typename _Po
inter>
3242 requires is_pointer_v<remove_reference_t<_Pointer>>
3244 -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;
3246 template<
typename _ElementType,
typename... _Integrals>
3247 requires (is_convertible_v<_Integrals, size_t> && ...)
3248 && (
sizeof...(_Integrals) > 0)
3249 explicit mdspan(_ElementType*, _Integrals...)
3250 -> mdspan<_ElementType,
3251 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>>;
3253 template<
typename _ElementType,
typename _OIndexType,
size_t _Nm>
3254 mdspan(_ElementType*, span<_OIndexType, _Nm>)
3255 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
3257 template<
typename _ElementType,
typename _OIndexType,
size_t _Nm>
3258 mdspan(_ElementType*,
const array<_OIndexType, _Nm>&)
3259 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
3261 template<
typename _ElementType,
typename _IndexType,
size_t... _ExtentsPack>
3262 mdspan(_ElementType*,
const extents<_IndexType, _ExtentsPack...>&)
3263 -> mdspan<_ElementType, extents<_IndexType, _ExtentsPack...>>;
3265 template<
typename _ElementType,
typename _MappingType>
3266 mdspan(_ElementType*,
const _MappingType&)
3267 -> mdspan<_ElementType,
typename _MappingType::extents_type,
3268 typename _MappingType::layout_type>;
3270 template<
typename _MappingType,
typename _AccessorType>
3271 mdspan(
const typename _AccessorType::data_handle_type&,
const _MappingType&,
3272 const _AccessorType&)
3273 -> mdspan<
typename _AccessorType::element_type,
3274 typename _MappingType::extents_type,
3275 typename _MappingType::layout_type, _AccessorType>;
3277#if __glibcxx_submdspan
3280 template<
typename _IndexType,
typename _Slice>
3282 __canonical_index(_Slice&& __slice)
3284 if constexpr (__detail::__integral_constant_like<_Slice>)
3286 static_assert(__is_representable_integer<_IndexType>(_Slice::value));
3287 static_assert(_Slice::value >= 0);
3288 return std::cw<_IndexType(_Slice::value)>;
3291 return __mdspan::__index_type_cast<_IndexType>(
std::move(__slice));
3294 template<
typename _IndexType,
3295 typename _OffsetType,
typename _SpanType,
typename _StrideType>
3297 __canonical_range_slice(_OffsetType __offset, _SpanType __span,
3298 _StrideType __stride)
3300 if constexpr (is_same_v<_SpanType, constant_wrapper<_IndexType(0)>>
3301 || is_same_v<_StrideType, constant_wrapper<_IndexType(1)>>)
3302 return extent_slice{
3305 .stride = cw<_IndexType(1)>
3307 else if constexpr (__is_constant_wrapper<_StrideType>)
3309 static_assert(_StrideType::value > 0);
3310 if constexpr (__is_constant_wrapper<_SpanType>)
3311 return extent_slice{
3313 .extent = cw<_IndexType(1 + (_SpanType::value - 1) / _StrideType::value)>,
3317 return extent_slice{
3319 .extent = _IndexType(__span > 0 ? 1 + (__span - 1) / _StrideType::value : 0),
3323 else if (__span == 0 || __stride == 1)
3324 return extent_slice{
3326 .extent = _IndexType(__span),
3327 .stride = _IndexType(1)
3331 __glibcxx_assert(__stride > 0);
3332 return extent_slice{
3334 .extent = _IndexType(1 + (__span - 1) / __stride),
3340 template<
typename _IndexType,
typename _Slice>
3342 __slice_cast(_Slice&& __slice)
3344 using _SliceType = remove_cvref_t<_Slice>;
3345 if constexpr (is_convertible_v<_SliceType, full_extent_t>)
3346 return static_cast<full_extent_t
>(
std::move(__slice));
3347 else if constexpr (is_convertible_v<_SliceType, _IndexType>)
3348 return __mdspan::__canonical_index<_IndexType>(
std::move(__slice));
3349 else if constexpr (__is_extent_slice<_SliceType>)
3350 return extent_slice{
3351 .offset = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.offset)),
3352 .extent = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.extent)),
3353 .stride = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.stride))
3355 else if constexpr (__is_range_slice<_SliceType>)
3358 = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.first));
3360 = __mdspan::__canonical_index<_IndexType>(
std::move(__slice.last));
3361 return __mdspan::__canonical_range_slice<_IndexType>(
3363 __mdspan::__canonical_index<_IndexType>(__last - __first),
3364 __mdspan::__canonical_index<_IndexType>(
std::move(__slice.stride)));
3368 auto [__sbegin, __send] =
std::move(__slice);
3370 = __mdspan::__canonical_index<_IndexType>(
std::move(__sbegin));
3372 = __mdspan::__canonical_index<_IndexType>(
std::move(__send));
3374 = __mdspan::__canonical_index<_IndexType>(__cend - __cbegin);
3375 return __mdspan::__canonical_range_slice<_IndexType>(
3376 __cbegin, __cspan, cw<_IndexType(1)>);
3380 template<
typename _IndexType,
size_t _Extent,
typename _OIndexType>
3382 __check_inrange_index(
const extents<_IndexType, _Extent>& __ext,
3383 const _OIndexType& __idx)
3385 if constexpr (__is_constant_wrapper<_OIndexType>
3386 && _Extent != dynamic_extent)
3388 static_assert(_OIndexType::value >= 0);
3389 static_assert(std::cmp_less(_OIndexType::value, _Extent));
3392 __glibcxx_assert(__idx < __ext.extent(0));
3395 template<
typename _IndexType,
size_t _Extent,
typename _OIndexType>
3397 __check_valid_index(
const extents<_IndexType, _Extent>& __ext,
3398 const _OIndexType& __idx)
3400 if constexpr (__is_constant_wrapper<_OIndexType>
3401 && _Extent != dynamic_extent)
3403 static_assert(_OIndexType::value >= 0);
3404 static_assert(std::cmp_less_equal(_OIndexType::value, _Extent));
3407 __glibcxx_assert(__idx <= __ext.extent(0));
3410 template<
typename _IndexType,
size_t _Extent,
typename _Slice>
3412 __check_valid_slice(
const extents<_IndexType, _Extent>& __ext,
3413 const _Slice& __slice)
3415 if constexpr (__is_extent_slice<_Slice>)
3417 __mdspan::__check_valid_index(__ext, __slice.extent);
3422 if constexpr (is_same_v<
typename _Slice::extent_type,
3423 constant_wrapper<_IndexType(0)>>)
3424 __mdspan::__check_valid_index(__ext, __slice.offset);
3425 else if constexpr (is_same_v<
typename _Slice::extent_type,
3426 constant_wrapper<_IndexType(1)>>)
3427 __mdspan::__check_inrange_index(__ext, __slice.offset);
3428 else if constexpr (__is_constant_wrapper<typename _Slice::extent_type>)
3430 __mdspan::__check_inrange_index(__ext, __slice.offset);
3431 if constexpr (__is_constant_wrapper<typename _Slice::stride_type>)
3432 static_assert(_Slice::stride_type::value > 0);
3434 __glibcxx_assert(__slice.stride > 0);
3436 if constexpr (_Extent != dynamic_extent
3437 && __is_constant_wrapper<typename _Slice::offset_type>)
3438 static_assert(std::cmp_greater_equal(
3439 _Extent - _Slice::offset_type::value,
3440 _Slice::extent_type::value));
3441 if constexpr (_Extent != dynamic_extent
3442 && __is_constant_wrapper<typename _Slice::stride_type>)
3443 static_assert(std::cmp_greater(
3445 (_Slice::extent_type::value - 1) * _Slice::stride_type::value));
3447 if constexpr (_Extent != dynamic_extent
3448 && __is_constant_wrapper<typename _Slice::offset_type>
3449 && __is_constant_wrapper<typename _Slice::stride_type>)
3450 static_assert(std::cmp_greater(
3451 _Extent - _Slice::offset_type::value,
3452 (_Slice::extent_type::value - 1) * _Slice::stride_type::value));
3454 __glibcxx_assert(std::cmp_greater(
3455 __ext.extent(0) - __slice.offset,
3456 (_Slice::extent_type::value - 1) * __slice.stride));
3458 else if constexpr (is_same_v<
typename _Slice::stride_type,
3459 constant_wrapper<_IndexType(1)>>)
3461 __mdspan::__check_valid_index(__ext, __slice.offset);
3462 __glibcxx_assert(std::cmp_greater_equal(
3463 __ext.extent(0) - __slice.offset,
3466 else if (__slice.extent == 0)
3467 __mdspan::__check_valid_index(__ext, __slice.offset);
3470 __glibcxx_assert(__slice.offset < __ext.extent(0));
3471 __glibcxx_assert(__slice.extent == 1 || __slice.stride > 0);
3472 __glibcxx_assert(__slice.extent == 1 || std::cmp_greater(
3473 __ext.extent(0) - __slice.offset,
3474 (__slice.extent - 1) * __slice.stride));
3477 else if constexpr (!is_same_v<_Slice, full_extent_t>)
3478 __mdspan::__check_inrange_index(__ext, __slice);
3481 template<
typename _Extents,
typename... _Slices>
3483 __check_valid_slices(
const _Extents& __exts,
const _Slices&... __slices)
3485 constexpr auto __rank = _Extents::rank();
3488 ((__mdspan::__check_valid_slice(__extract_extent<_Is>(__exts),
3489 __slices...[_Is])),...);
3491 __impl(make_index_sequence<__rank>());
3494 template<
typename _Slice>
3495 using __full_extent_t = std::full_extent_t;
3498 void submdspan_mapping() =
delete;
3500 template<
typename _Mapping,
typename... _Slices>
3501 concept __sliceable_mapping =
requires(
const _Mapping __m, _Slices... __slices)
3503 { submdspan_mapping(__m, __slices...) } -> __submdspan_mapping_result;
3506 template<
typename _Mapping,
typename... _Slices>
3508 __submapping(
const _Mapping& __mapping, _Slices... __slices)
3510 __mdspan::__check_valid_slices(__mapping.extents(), __slices...);
3511 return submdspan_mapping(__mapping, __slices...);
3515 template<
typename _IndexType,
size_t... _Extents,
typename... _RawSlices>
3516 requires (
sizeof...(_RawSlices) ==
sizeof...(_Extents))
3518 subextents(
const extents<_IndexType, _Extents...>& __exts,
3519 _RawSlices... __raw_slices)
3521 auto __impl = [&__exts](
auto... __slices)
3523 __mdspan::__check_valid_slices(__exts, __slices...);
3524 return __mdspan::__subextents(__exts, __slices...);
3526 return __impl(__mdspan::__slice_cast<_IndexType>(__raw_slices)...);
3529 template<
typename _IndexType,
size_t... _Extents,
typename... _RawSlices>
3530 requires (
sizeof...(_Extents) ==
sizeof...(_RawSlices))
3532 canonical_slices(
const extents<_IndexType, _Extents...>& __exts,
3533 _RawSlices... __raw_slices)
3535 auto __impl = [&__exts](
auto... __slices)
3537 __mdspan::__check_valid_slices(__exts, __slices...);
3540 return __impl(__mdspan::__slice_cast<_IndexType>(__raw_slices)...);
3543 template<
typename _ElementType,
typename _Extents,
typename _Layout,
3544 typename _Accessor,
typename... _RawSlices>
3545 requires (
sizeof...(_RawSlices) == _Extents::rank()
3546 && __mdspan::__sliceable_mapping<typename _Layout::template mapping<_Extents>,
3547 __mdspan::__full_extent_t<_RawSlices>...>)
3550 const mdspan<_ElementType, _Extents, _Layout, _Accessor>& __md,
3551 _RawSlices... __raw_slices)
3553 using _IndexType =
typename _Extents::index_type;
3554 auto [__mapping, __offset] = __mdspan::__submapping(
3555 __md.mapping(), __mdspan::__slice_cast<_IndexType>(__raw_slices)...);
3557 __md.accessor().offset(__md.data_handle(), __offset),
3559 typename _Accessor::offset_policy(__md.accessor()));
3563_GLIBCXX_END_NAMESPACE_VERSION
constexpr _Tp * assume_aligned(_Tp *__ptr) noexcept
Inform the compiler that a pointer is aligned.
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
constexpr tuple< typename __decay_and_strip< _Elements >::__type... > make_tuple(_Elements &&... __args)
Create a tuple containing copies of the arguments.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.
make_integer_sequence< size_t, _Num > make_index_sequence
Alias template make_index_sequence.
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.