3// Copyright The GNU Toolchain Authors.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
26 * This is a Standard C++ Library header.
29#ifndef _GLIBCXX_MDSPAN
30#define _GLIBCXX_MDSPAN 1
33#pragma GCC system_header
41#if __cplusplus > 202302L
42#include <bits/align.h>
45#define __glibcxx_want_mdspan
46#define __glibcxx_want_aligned_accessor
47#include <bits/version.h>
49#ifdef __glibcxx_mdspan
51namespace std _GLIBCXX_VISIBILITY(default)
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
57 __all_static(std::span<const size_t> __extents)
59 for(auto __ext : __extents)
60 if (__ext == dynamic_extent)
66 __all_dynamic(std::span<const size_t> __extents)
68 for(auto __ext : __extents)
69 if (__ext != dynamic_extent)
74 template<array _Extents>
78 static constexpr size_t _S_rank = _Extents.size();
80 // For __r in [0, _S_rank], _S_dynamic_index(__r) is the number
81 // of dynamic extents up to (and not including) __r.
83 // If __r is the index of a dynamic extent, then
84 // _S_dynamic_index[__r] is the index of that extent in
86 static constexpr size_t
87 _S_dynamic_index(size_t __r) noexcept
88 { return _S_dynamic_index_data[__r]; }
90 static constexpr auto _S_dynamic_index_data = [] consteval
92 array<size_t, _S_rank+1> __ret;
94 for (size_t __i = 0; __i < _S_rank; ++__i)
97 __dyn += (_Extents[__i] == dynamic_extent);
99 __ret[_S_rank] = __dyn;
103 static constexpr size_t _S_rank_dynamic = _S_dynamic_index(_S_rank);
105 // For __r in [0, _S_rank_dynamic), _S_dynamic_index_inv(__r) is the
106 // index of the __r-th dynamic extent in _Extents.
107 static constexpr size_t
108 _S_dynamic_index_inv(size_t __r) noexcept
109 { return _S_dynamic_index_inv_data[__r]; }
111 static constexpr auto _S_dynamic_index_inv_data = [] consteval
113 array<size_t, _S_rank_dynamic> __ret;
114 for (size_t __i = 0, __r = 0; __i < _S_rank; ++__i)
115 if (_Extents[__i] == dynamic_extent)
120 static constexpr size_t
121 _S_static_extent(size_t __r) noexcept
122 { return _Extents[__r]; }
125 template<array _Extents>
126 requires (__all_dynamic<_Extents>())
127 class _StaticExtents<_Extents>
130 static constexpr size_t _S_rank = _Extents.size();
132 static constexpr size_t
133 _S_dynamic_index(size_t __r) noexcept
136 static constexpr size_t _S_rank_dynamic = _S_rank;
138 static constexpr size_t
139 _S_dynamic_index_inv(size_t __k) noexcept
142 static constexpr size_t
143 _S_static_extent(size_t) noexcept
144 { return dynamic_extent; }
147 template<typename _IndexType, array _Extents>
148 class _ExtentsStorage : public _StaticExtents<_Extents>
151 using _Base = _StaticExtents<_Extents>;
154 using _Base::_S_rank;
155 using _Base::_S_rank_dynamic;
156 using _Base::_S_dynamic_index;
157 using _Base::_S_dynamic_index_inv;
158 using _Base::_S_static_extent;
160 static constexpr bool
161 _S_is_dynamic(size_t __r) noexcept
163 if constexpr (__all_static(_Extents))
165 else if constexpr (__all_dynamic(_Extents))
168 return _Extents[__r] == dynamic_extent;
171 template<typename _OIndexType>
172 static constexpr _IndexType
173 _S_int_cast(const _OIndexType& __other) noexcept
174 { return _IndexType(__other); }
177 _M_extent(size_t __r) const noexcept
179 if (_S_is_dynamic(__r))
180 return _M_dyn_exts[_S_dynamic_index(__r)];
182 return _S_static_extent(__r);
185 template<size_t _OtherRank, typename _GetOtherExtent>
186 static constexpr bool
187 _S_is_compatible_extents(_GetOtherExtent __get_extent) noexcept
189 if constexpr (_OtherRank == _S_rank)
190 for (size_t __i = 0; __i < _S_rank; ++__i)
191 if (!_S_is_dynamic(__i)
192 && !cmp_equal(_Extents[__i], _S_int_cast(__get_extent(__i))))
197 template<size_t _OtherRank, typename _GetOtherExtent>
199 _M_init_dynamic_extents(_GetOtherExtent __get_extent) noexcept
201 __glibcxx_assert(_S_is_compatible_extents<_OtherRank>(__get_extent));
202 for (size_t __i = 0; __i < _S_rank_dynamic; ++__i)
205 if constexpr (_OtherRank != _S_rank_dynamic)
206 __di = _S_dynamic_index_inv(__i);
207 _M_dyn_exts[__i] = _S_int_cast(__get_extent(__di));
212 _ExtentsStorage() noexcept = default;
214 template<typename _OIndexType, array _OExtents>
216 _ExtentsStorage(const _ExtentsStorage<_OIndexType, _OExtents>&
219 _M_init_dynamic_extents<_S_rank>([&__other](size_t __i)
220 { return __other._M_extent(__i); });
223 template<typename _OIndexType, size_t _Nm>
225 _ExtentsStorage(span<const _OIndexType, _Nm> __exts) noexcept
227 _M_init_dynamic_extents<_Nm>(
228 [&__exts](size_t __i) -> const _OIndexType&
229 { return __exts[__i]; });
232 static constexpr const array<size_t, _S_rank>&
233 _S_static_extents() noexcept
236 constexpr span<const _IndexType>
237 _M_dynamic_extents(size_t __begin, size_t __end) const noexcept
238 requires (_Extents.size() > 0)
240 return {_M_dyn_exts + _S_dynamic_index(__begin),
241 _M_dyn_exts + _S_dynamic_index(__end)};
245 using _Storage = __array_traits<_IndexType, _S_rank_dynamic>::_Type;
246 [[no_unique_address]] _Storage _M_dyn_exts{};
249 template<typename _OIndexType, typename _SIndexType>
250 concept __valid_index_type =
251 is_convertible_v<_OIndexType, _SIndexType> &&
252 is_nothrow_constructible_v<_SIndexType, _OIndexType>;
254 template<size_t _Extent, typename _IndexType>
256 __valid_static_extent = _Extent == dynamic_extent
257 || _Extent <= __gnu_cxx::__int_traits<_IndexType>::__max;
259 template<typename _Extents>
260 constexpr const array<size_t, _Extents::rank()>&
261 __static_extents() noexcept
262 { return _Extents::_Storage::_S_static_extents(); }
264 // Pre-compute: \prod_{i = 0}^r _Extents[i], for r = 0,..., n (exclusive)
265 template<array _Extents>
266 constexpr auto __fwd_partial_prods = [] consteval
268 constexpr size_t __rank = _Extents.size();
269 std::array<size_t, __rank> __ret;
271 for (size_t __r = 0; __r < __rank; ++__r)
274 if (size_t __ext = _Extents[__r]; __ext != dynamic_extent)
280 // Pre-compute: \prod_{i = r+1}^{n-1} _Extents[i]
281 template<array _Extents>
282 constexpr auto __rev_partial_prods = [] consteval
284 constexpr size_t __rank = _Extents.size();
285 std::array<size_t, __rank> __ret;
287 for (size_t __r = __rank; __r > 0; --__r)
289 __ret[__r - 1] = __prod;
290 if (size_t __ext = _Extents[__r - 1]; __ext != dynamic_extent)
296 template<typename _Extents>
297 constexpr span<const typename _Extents::index_type>
298 __dynamic_extents(const _Extents& __exts, size_t __begin = 0,
299 size_t __end = _Extents::rank()) noexcept
300 { return __exts._M_exts._M_dynamic_extents(__begin, __end); }
303 template<typename _IndexType, size_t... _Extents>
306 static_assert(__is_standard_integer<_IndexType>::value,
307 "IndexType must be a signed or unsigned integer type");
309 (__mdspan::__valid_static_extent<_Extents, _IndexType> && ...),
310 "Extents must either be dynamic or representable as IndexType");
312 using index_type = _IndexType;
313 using size_type = make_unsigned_t<index_type>;
314 using rank_type = size_t;
316 static constexpr rank_type
317 rank() noexcept { return _Storage::_S_rank; }
319 static constexpr rank_type
320 rank_dynamic() noexcept { return _Storage::_S_rank_dynamic; }
322 static constexpr size_t
323 static_extent(rank_type __r) noexcept
325 __glibcxx_assert(__r < rank());
326 if constexpr (rank() == 0)
329 return _Storage::_S_static_extent(__r);
333 extent(rank_type __r) const noexcept
335 __glibcxx_assert(__r < rank());
336 if constexpr (rank() == 0)
339 return _M_exts._M_extent(__r);
343 extents() noexcept = default;
346 static consteval bool
347 _S_is_less_dynamic(size_t __ext, size_t __oext)
348 { return (__ext != dynamic_extent) && (__oext == dynamic_extent); }
350 template<typename _OIndexType, size_t... _OExtents>
351 static consteval bool
354 return (_S_is_less_dynamic(_Extents, _OExtents) || ...)
355 || (__gnu_cxx::__int_traits<index_type>::__max
356 < __gnu_cxx::__int_traits<_OIndexType>::__max);
359 template<size_t... _OExtents>
360 static consteval bool
361 _S_is_compatible_extents()
363 if constexpr (sizeof...(_OExtents) != rank())
366 return ((_OExtents == dynamic_extent || _Extents == dynamic_extent
367 || _OExtents == _Extents) && ...);
371 template<typename _OIndexType, size_t... _OExtents>
372 requires (_S_is_compatible_extents<_OExtents...>())
373 constexpr explicit(_S_ctor_explicit<_OIndexType, _OExtents...>())
374 extents(const extents<_OIndexType, _OExtents...>& __other) noexcept
375 : _M_exts(__other._M_exts)
378 template<__mdspan::__valid_index_type<index_type>... _OIndexTypes>
379 requires (sizeof...(_OIndexTypes) == rank()
380 || sizeof...(_OIndexTypes) == rank_dynamic())
381 constexpr explicit extents(_OIndexTypes... __exts) noexcept
382 : _M_exts(span<const _IndexType, sizeof...(_OIndexTypes)>(
383 initializer_list{static_cast<_IndexType>(std::move(__exts))...}))
386 template<typename _OIndexType, size_t _Nm>
387 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
388 && (_Nm == rank() || _Nm == rank_dynamic())
389 constexpr explicit(_Nm != rank_dynamic())
390 extents(span<_OIndexType, _Nm> __exts) noexcept
391 : _M_exts(span<const _OIndexType, _Nm>(__exts))
394 template<typename _OIndexType, size_t _Nm>
395 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
396 && (_Nm == rank() || _Nm == rank_dynamic())
397 constexpr explicit(_Nm != rank_dynamic())
398 extents(const array<_OIndexType, _Nm>& __exts) noexcept
399 : _M_exts(span<const _OIndexType, _Nm>(__exts))
402 template<typename _OIndexType, size_t... _OExtents>
403 friend constexpr bool
404 operator==(const extents& __self,
405 const extents<_OIndexType, _OExtents...>& __other) noexcept
407 if constexpr (!_S_is_compatible_extents<_OExtents...>())
411 auto __impl = [&__self, &__other]<size_t... _Counts>(
412 index_sequence<_Counts...>)
413 { return (cmp_equal(__self.extent(_Counts),
414 __other.extent(_Counts)) && ...); };
415 return __impl(make_index_sequence<__self.rank()>());
420 friend const array<size_t, rank()>&
421 __mdspan::__static_extents<extents>();
423 friend span<const index_type>
424 __mdspan::__dynamic_extents<extents>(const extents&, size_t, size_t);
426 using _Storage = __mdspan::_ExtentsStorage<
427 _IndexType, array<size_t, sizeof...(_Extents)>{_Extents...}>;
428 [[no_unique_address]] _Storage _M_exts;
430 template<typename _OIndexType, size_t... _OExtents>
431 friend class extents;
436 template<typename _Tp, size_t _Nm>
438 __contains_zero(span<_Tp, _Nm> __exts) noexcept
440 for (size_t __i = 0; __i < __exts.size(); ++__i)
441 if (__exts[__i] == 0)
446 template<typename _Tp, size_t _Nm>
448 __contains_zero(const array<_Tp, _Nm>& __exts) noexcept
449 { return __contains_zero(span<const _Tp>(__exts)); }
451 template<typename _Extents>
453 __empty(const _Extents& __exts) noexcept
455 if constexpr (__contains_zero(__static_extents<_Extents>()))
457 else if constexpr (_Extents::rank_dynamic() > 0)
458 return __contains_zero(__dynamic_extents(__exts));
463 template<typename _Extents>
464 constexpr typename _Extents::index_type
465 __extents_prod(const _Extents& __exts, size_t __sta_prod, size_t __begin,
466 size_t __end) noexcept
471 size_t __ret = __sta_prod;
472 if constexpr (_Extents::rank_dynamic() > 0)
473 for (auto __factor : __dynamic_extents(__exts, __begin, __end))
474 __ret *= size_t(__factor);
475 return static_cast<typename _Extents::index_type>(__ret);
478 // Preconditions: _r < _Extents::rank()
479 template<typename _Extents>
480 constexpr typename _Extents::index_type
481 __fwd_prod(const _Extents& __exts, size_t __r) noexcept
483 constexpr size_t __rank = _Extents::rank();
484 constexpr auto& __sta_exts = __static_extents<_Extents>();
485 if constexpr (__rank == 1)
487 else if constexpr (__rank == 2)
488 return __r == 0 ? 1 : __exts.extent(0);
489 else if constexpr (__all_dynamic(std::span(__sta_exts).first(__rank-1)))
490 return __extents_prod(__exts, 1, 0, __r);
493 size_t __sta_prod = __fwd_partial_prods<__sta_exts>[__r];
494 return __extents_prod(__exts, __sta_prod, 0, __r);
498 // Preconditions: _r < _Extents::rank()
499 template<typename _Extents>
500 constexpr typename _Extents::index_type
501 __rev_prod(const _Extents& __exts, size_t __r) noexcept
503 constexpr size_t __rank = _Extents::rank();
504 constexpr auto& __sta_exts = __static_extents<_Extents>();
505 if constexpr (__rank == 1)
507 else if constexpr (__rank == 2)
508 return __r == 0 ? __exts.extent(1) : 1;
509 else if constexpr (__all_dynamic(std::span(__sta_exts).last(__rank-1)))
510 return __extents_prod(__exts, 1, __r + 1, __rank);
513 size_t __sta_prod = __rev_partial_prods<__sta_exts>[__r];
514 return __extents_prod(__exts, __sta_prod, __r + 1, __rank);
518 template<typename _Extents>
519 constexpr typename _Extents::index_type
520 __size(const _Extents& __exts) noexcept
522 constexpr size_t __sta_prod = [] {
523 span<const size_t> __sta_exts = __static_extents<_Extents>();
525 for(auto __ext : __sta_exts)
526 if (__ext != dynamic_extent)
530 return __extents_prod(__exts, __sta_prod, 0, _Extents::rank());
533 template<typename _IndexType, size_t... _Counts>
534 auto __build_dextents_type(integer_sequence<size_t, _Counts...>)
535 -> extents<_IndexType, ((void) _Counts, dynamic_extent)...>;
538 template<typename _IndexType, size_t _Rank>
539 using dextents = decltype(__mdspan::__build_dextents_type<_IndexType>(
540 make_index_sequence<_Rank>()));
542#if __glibcxx_mdspan >= 202406L
543 template<size_t _Rank, typename _IndexType = size_t>
544 using dims = dextents<_IndexType, _Rank>;
547 template<typename... _Integrals>
548 requires (is_convertible_v<_Integrals, size_t> && ...)
549 explicit extents(_Integrals...) ->
550 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>;
554 template<typename _Extents>
560 template<typename _Extents>
566 template<typename _Extents>
572 template<typename _Tp>
573 constexpr bool __is_extents = false;
575 template<typename _IndexType, size_t... _Extents>
576 constexpr bool __is_extents<extents<_IndexType, _Extents...>> = true;
578 template<typename _Extents, typename... _Indices>
579 constexpr typename _Extents::index_type
580 __linear_index_left(const _Extents& __exts, _Indices... __indices)
583 using _IndexType = typename _Extents::index_type;
584 _IndexType __res = 0;
585 if constexpr (sizeof...(__indices) > 0)
587 _IndexType __mult = 1;
588 auto __update = [&, __pos = 0u](_IndexType __idx) mutable
590 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx, __exts.extent(__pos)));
591 __res += __idx * __mult;
592 __mult *= __exts.extent(__pos);
595 (__update(__indices), ...);
600 template<typename _IndexType>
602 __static_quotient(std::span<const size_t> __sta_exts,
603 _IndexType __nom = __gnu_cxx::__int_traits<_IndexType>::__max)
605 for (auto __factor : __sta_exts)
607 if (__factor != dynamic_extent)
608 __nom /= _IndexType(__factor);
615 template<typename _Extents,
616 typename _IndexType = typename _Extents::index_type>
617 requires __is_extents<_Extents>
619 __static_quotient(_IndexType __nom
620 = __gnu_cxx::__int_traits<_IndexType>::__max)
622 std::span<const size_t> __sta_exts = __static_extents<_Extents>();
623 return __static_quotient<_IndexType>(__sta_exts, __nom);
626 template<typename _Extents>
628 __is_representable_extents(const _Extents& __exts) noexcept
630 using _IndexType = _Extents::index_type;
632 if constexpr (__contains_zero(__static_extents<_Extents>()))
636 constexpr auto __sta_quo = __static_quotient<_Extents>();
637 if constexpr (_Extents::rank_dynamic() == 0)
638 return __sta_quo != 0;
641 auto __dyn_exts = __dynamic_extents(__exts);
642 if (__contains_zero(__dyn_exts))
645 if constexpr (__sta_quo == 0)
649 auto __dyn_quo = _IndexType(__sta_quo);
650 for (auto __factor : __dyn_exts)
652 __dyn_quo /= __factor;
662 template<typename _Extents, typename _IndexType>
663 concept __representable_size = _Extents::rank_dynamic() != 0
664 || __contains_zero(__static_extents<_Extents>())
665 || (__static_quotient<_Extents, _IndexType>() != 0);
667 template<typename _Layout, typename _Mapping>
668 concept __mapping_of =
669 is_same_v<typename _Layout::template mapping<typename _Mapping::extents_type>,
672 template<typename _Mapping>
673 concept __standardized_mapping = __mapping_of<layout_left, _Mapping>
674 || __mapping_of<layout_right, _Mapping>
675 || __mapping_of<layout_stride, _Mapping>;
677 // A tag type to create internal ctors.
678 class __internal_ctor
682 template<typename _Extents>
683 class layout_left::mapping
686 using extents_type = _Extents;
687 using index_type = typename extents_type::index_type;
688 using size_type = typename extents_type::size_type;
689 using rank_type = typename extents_type::rank_type;
690 using layout_type = layout_left;
692 static_assert(__mdspan::__representable_size<extents_type, index_type>,
693 "The size of extents_type must be representable as index_type");
696 mapping() noexcept = default;
699 mapping(const mapping&) noexcept = default;
702 mapping(const extents_type& __extents) noexcept
703 : _M_extents(__extents)
704 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
706 template<typename _OExtents>
707 requires is_constructible_v<extents_type, _OExtents>
708 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
709 mapping(const mapping<_OExtents>& __other) noexcept
710 : mapping(__other.extents(), __mdspan::__internal_ctor{})
713 template<typename _OExtents>
714 requires (extents_type::rank() <= 1)
715 && is_constructible_v<extents_type, _OExtents>
716 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
717 mapping(const layout_right::mapping<_OExtents>& __other) noexcept
718 : mapping(__other.extents(), __mdspan::__internal_ctor{})
721 // noexcept for consistency with other layouts.
722 template<typename _OExtents>
723 requires is_constructible_v<extents_type, _OExtents>
724 constexpr explicit(extents_type::rank() > 0)
725 mapping(const layout_stride::mapping<_OExtents>& __other) noexcept
726 : mapping(__other.extents(), __mdspan::__internal_ctor{})
727 { __glibcxx_assert(*this == __other); }
730 operator=(const mapping&) noexcept = default;
732 constexpr const extents_type&
733 extents() const noexcept { return _M_extents; }
736 required_span_size() const noexcept
737 { return __mdspan::__size(_M_extents); }
739 // _GLIBCXX_RESOLVE_LIB_DEFECTS
740 // 4314. Missing move in mdspan layout mapping::operator()
741 template<__mdspan::__valid_index_type<index_type>... _Indices>
742 requires (sizeof...(_Indices) == extents_type::rank())
744 operator()(_Indices... __indices) const noexcept
746 return __mdspan::__linear_index_left(_M_extents,
747 static_cast<index_type>(std::move(__indices))...);
750 static constexpr bool
751 is_always_unique() noexcept { return true; }
753 static constexpr bool
754 is_always_exhaustive() noexcept { return true; }
756 static constexpr bool
757 is_always_strided() noexcept { return true; }
759 static constexpr bool
760 is_unique() noexcept { return true; }
762 static constexpr bool
763 is_exhaustive() noexcept { return true; }
765 static constexpr bool
766 is_strided() noexcept { return true; }
769 stride(rank_type __i) const noexcept
770 requires (extents_type::rank() > 0)
772 __glibcxx_assert(__i < extents_type::rank());
773 return __mdspan::__fwd_prod(_M_extents, __i);
776 template<typename _OExtents>
777 requires (extents_type::rank() == _OExtents::rank())
778 friend constexpr bool
779 operator==(const mapping& __self, const mapping<_OExtents>& __other)
781 { return __self.extents() == __other.extents(); }
784 template<typename _OExtents>
786 mapping(const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
787 : _M_extents(__oexts)
789 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
790 "The size of OtherExtents must be representable as index_type");
791 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
794 [[no_unique_address]] extents_type _M_extents{};
799 template<typename _Extents, typename... _Indices>
800 constexpr typename _Extents::index_type
801 __linear_index_right(const _Extents& __exts, _Indices... __indices)
804 using _IndexType = typename _Extents::index_type;
805 array<_IndexType, sizeof...(__indices)> __ind_arr{__indices...};
806 _IndexType __res = 0;
807 if constexpr (sizeof...(__indices) > 0)
809 _IndexType __mult = 1;
810 auto __update = [&, __pos = __exts.rank()](_IndexType) mutable
813 _GLIBCXX_DEBUG_ASSERT(cmp_less(__ind_arr[__pos],
814 __exts.extent(__pos)));
815 __res += __ind_arr[__pos] * __mult;
816 __mult *= __exts.extent(__pos);
818 (__update(__indices), ...);
824 template<typename _Extents>
825 class layout_right::mapping
828 using extents_type = _Extents;
829 using index_type = typename extents_type::index_type;
830 using size_type = typename extents_type::size_type;
831 using rank_type = typename extents_type::rank_type;
832 using layout_type = layout_right;
834 static_assert(__mdspan::__representable_size<extents_type, index_type>,
835 "The size of extents_type must be representable as index_type");
838 mapping() noexcept = default;
841 mapping(const mapping&) noexcept = default;
844 mapping(const extents_type& __extents) noexcept
845 : _M_extents(__extents)
846 { __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents)); }
848 template<typename _OExtents>
849 requires is_constructible_v<extents_type, _OExtents>
850 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
851 mapping(const mapping<_OExtents>& __other) noexcept
852 : mapping(__other.extents(), __mdspan::__internal_ctor{})
855 template<typename _OExtents>
856 requires (extents_type::rank() <= 1)
857 && is_constructible_v<extents_type, _OExtents>
858 constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
859 mapping(const layout_left::mapping<_OExtents>& __other) noexcept
860 : mapping(__other.extents(), __mdspan::__internal_ctor{})
863 template<typename _OExtents>
864 requires is_constructible_v<extents_type, _OExtents>
865 constexpr explicit(extents_type::rank() > 0)
866 mapping(const layout_stride::mapping<_OExtents>& __other) noexcept
867 : mapping(__other.extents(), __mdspan::__internal_ctor{})
868 { __glibcxx_assert(*this == __other); }
871 operator=(const mapping&) noexcept = default;
873 constexpr const extents_type&
874 extents() const noexcept { return _M_extents; }
877 required_span_size() const noexcept
878 { return __mdspan::__size(_M_extents); }
880 // _GLIBCXX_RESOLVE_LIB_DEFECTS
881 // 4314. Missing move in mdspan layout mapping::operator()
882 template<__mdspan::__valid_index_type<index_type>... _Indices>
883 requires (sizeof...(_Indices) == extents_type::rank())
885 operator()(_Indices... __indices) const noexcept
887 return __mdspan::__linear_index_right(
888 _M_extents, static_cast<index_type>(std::move(__indices))...);
891 static constexpr bool
892 is_always_unique() noexcept
895 static constexpr bool
896 is_always_exhaustive() noexcept
899 static constexpr bool
900 is_always_strided() noexcept
903 static constexpr bool
907 static constexpr bool
908 is_exhaustive() noexcept
911 static constexpr bool
912 is_strided() noexcept
916 stride(rank_type __i) const noexcept
917 requires (extents_type::rank() > 0)
919 __glibcxx_assert(__i < extents_type::rank());
920 return __mdspan::__rev_prod(_M_extents, __i);
923 template<typename _OExtents>
924 requires (extents_type::rank() == _OExtents::rank())
925 friend constexpr bool
926 operator==(const mapping& __self, const mapping<_OExtents>& __other)
928 { return __self.extents() == __other.extents(); }
931 template<typename _OExtents>
933 mapping(const _OExtents& __oexts, __mdspan::__internal_ctor) noexcept
934 : _M_extents(__oexts)
936 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
937 "The size of OtherExtents must be representable as index_type");
938 __glibcxx_assert(__mdspan::__is_representable_extents(_M_extents));
941 [[no_unique_address]] extents_type _M_extents{};
946 template<typename _Mp>
947 concept __mapping_alike = requires
949 requires __is_extents<typename _Mp::extents_type>;
950 { _Mp::is_always_strided() } -> same_as<bool>;
951 { _Mp::is_always_exhaustive() } -> same_as<bool>;
952 { _Mp::is_always_unique() } -> same_as<bool>;
953 bool_constant<_Mp::is_always_strided()>::value;
954 bool_constant<_Mp::is_always_exhaustive()>::value;
955 bool_constant<_Mp::is_always_unique()>::value;
958 template<typename _Mapping>
959 constexpr typename _Mapping::index_type
960 __offset(const _Mapping& __m) noexcept
962 using _IndexType = typename _Mapping::index_type;
963 constexpr auto __rank = _Mapping::extents_type::rank();
965 if constexpr (__standardized_mapping<_Mapping>)
967 else if (__empty(__m.extents()))
971 auto __impl = [&__m]<size_t... _Counts>(index_sequence<_Counts...>)
972 { return __m(((void) _Counts, _IndexType(0))...); };
973 return __impl(make_index_sequence<__rank>());
977 template<typename _Mapping, typename... _Indices>
978 constexpr typename _Mapping::index_type
979 __linear_index_strides(const _Mapping& __m, _Indices... __indices)
982 using _IndexType = typename _Mapping::index_type;
983 _IndexType __res = 0;
984 if constexpr (sizeof...(__indices) > 0)
986 auto __update = [&, __pos = 0u](_IndexType __idx) mutable
988 _GLIBCXX_DEBUG_ASSERT(cmp_less(__idx,
989 __m.extents().extent(__pos)));
990 __res += __idx * __m.stride(__pos++);
992 (__update(__indices), ...);
998 template<typename _Extents>
999 class layout_stride::mapping
1002 using extents_type = _Extents;
1003 using index_type = typename extents_type::index_type;
1004 using size_type = typename extents_type::size_type;
1005 using rank_type = typename extents_type::rank_type;
1006 using layout_type = layout_stride;
1008 static_assert(__mdspan::__representable_size<extents_type, index_type>,
1009 "The size of extents_type must be representable as index_type");
1014 // The precondition is either statically asserted, or automatically
1015 // satisfied because dynamic extents are zero-initialized.
1016 size_t __stride = 1;
1017 for (size_t __i = extents_type::rank(); __i > 0; --__i)
1019 _M_strides[__i - 1] = index_type(__stride);
1020 __stride *= size_t(_M_extents.extent(__i - 1));
1025 mapping(const mapping&) noexcept = default;
1027 template<typename _OIndexType>
1028 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1030 mapping(const extents_type& __exts,
1031 span<_OIndexType, extents_type::rank()> __strides) noexcept
1032 : _M_extents(__exts)
1034 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1035 _M_strides[__i] = index_type(as_const(__strides[__i]));
1038 template<typename _OIndexType>
1039 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1041 mapping(const extents_type& __exts,
1042 const array<_OIndexType, extents_type::rank()>& __strides)
1045 span<const _OIndexType, extents_type::rank()>(__strides))
1048 template<__mdspan::__mapping_alike _StridedMapping>
1049 requires (is_constructible_v<extents_type,
1050 typename _StridedMapping::extents_type>
1051 && _StridedMapping::is_always_unique()
1052 && _StridedMapping::is_always_strided())
1053 constexpr explicit(!(
1054 is_convertible_v<typename _StridedMapping::extents_type, extents_type>
1055 && __mdspan::__standardized_mapping<_StridedMapping>))
1056 mapping(const _StridedMapping& __other) noexcept
1057 : _M_extents(__other.extents())
1059 using _OIndexType = _StridedMapping::index_type;
1060 using _OExtents = _StridedMapping::extents_type;
1062 __glibcxx_assert(__mdspan::__offset(__other) == 0);
1063 static_assert(__mdspan::__representable_size<_OExtents, index_type>,
1064 "The size of StridedMapping::extents_type must be representable as"
1066 if constexpr (cmp_greater(__gnu_cxx::__int_traits<_OIndexType>::__max,
1067 __gnu_cxx::__int_traits<index_type>::__max))
1068 __glibcxx_assert(!cmp_less(
1069 __gnu_cxx::__int_traits<index_type>::__max,
1070 __other.required_span_size())
1071 && "other.required_span_size() must be representable"
1073 if constexpr (extents_type::rank() > 0)
1074 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1075 _M_strides[__i] = index_type(__other.stride(__i));
1079 operator=(const mapping&) noexcept = default;
1081 constexpr const extents_type&
1082 extents() const noexcept { return _M_extents; }
1084 constexpr array<index_type, extents_type::rank()>
1085 strides() const noexcept
1087 array<index_type, extents_type::rank()> __ret;
1088 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1089 __ret[__i] = _M_strides[__i];
1093 constexpr index_type
1094 required_span_size() const noexcept
1096 if (__mdspan::__empty(_M_extents))
1099 index_type __ret = 1;
1100 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1101 __ret += (_M_extents.extent(__i) - 1) * _M_strides[__i];
1105 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1106 // 4314. Missing move in mdspan layout mapping::operator()
1107 template<__mdspan::__valid_index_type<index_type>... _Indices>
1108 requires (sizeof...(_Indices) == extents_type::rank())
1109 constexpr index_type
1110 operator()(_Indices... __indices) const noexcept
1112 return __mdspan::__linear_index_strides(*this,
1113 static_cast<index_type>(std::move(__indices))...);
1116 static constexpr bool
1117 is_always_unique() noexcept { return true; }
1119 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1120 // 4266. layout_stride::mapping should treat empty mappings as exhaustive
1121 static constexpr bool
1122 is_always_exhaustive() noexcept
1124 return (_Extents::rank() == 0) || __mdspan::__contains_zero(
1125 __mdspan::__static_extents<extents_type>());
1128 static constexpr bool
1129 is_always_strided() noexcept { return true; }
1131 static constexpr bool
1132 is_unique() noexcept { return true; }
1134 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1135 // 4266. layout_stride::mapping should treat empty mappings as exhaustive
1137 is_exhaustive() const noexcept
1139 if constexpr (!is_always_exhaustive())
1141 auto __size = __mdspan::__size(_M_extents);
1143 return __size == required_span_size();
1148 static constexpr bool
1149 is_strided() noexcept { return true; }
1151 constexpr index_type
1152 stride(rank_type __r) const noexcept { return _M_strides[__r]; }
1154 template<__mdspan::__mapping_alike _OMapping>
1155 requires ((extents_type::rank() == _OMapping::extents_type::rank())
1156 && _OMapping::is_always_strided())
1157 friend constexpr bool
1158 operator==(const mapping& __self, const _OMapping& __other) noexcept
1160 if (__self.extents() != __other.extents())
1162 if constexpr (extents_type::rank() > 0)
1163 for (size_t __i = 0; __i < extents_type::rank(); ++__i)
1164 if (!cmp_equal(__self.stride(__i), __other.stride(__i)))
1166 return __mdspan::__offset(__other) == 0;
1170 using _Strides = typename __array_traits<index_type,
1171 extents_type::rank()>::_Type;
1172 [[no_unique_address]] extents_type _M_extents;
1173 [[no_unique_address]] _Strides _M_strides;
1176 template<typename _ElementType>
1177 struct default_accessor
1179 static_assert(!is_array_v<_ElementType>,
1180 "ElementType must not be an array type");
1181 static_assert(!is_abstract_v<_ElementType>,
1182 "ElementType must not be an abstract class type");
1184 using offset_policy = default_accessor;
1185 using element_type = _ElementType;
1186 using reference = element_type&;
1187 using data_handle_type = element_type*;
1190 default_accessor() noexcept = default;
1192 template<typename _OElementType>
1193 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
1195 default_accessor(default_accessor<_OElementType>) noexcept
1199 access(data_handle_type __p, size_t __i) const noexcept
1200 { return __p[__i]; }
1202 constexpr data_handle_type
1203 offset(data_handle_type __p, size_t __i) const noexcept
1204 { return __p + __i; }
1207#ifdef __glibcxx_aligned_accessor
1208 template<typename _ElementType, size_t _ByteAlignment>
1209 struct aligned_accessor
1211 static_assert(has_single_bit(_ByteAlignment),
1212 "ByteAlignment must be a power of two");
1213 static_assert(_ByteAlignment >= alignof(_ElementType));
1215 using offset_policy = default_accessor<_ElementType>;
1216 using element_type = _ElementType;
1217 using reference = element_type&;
1218 using data_handle_type = element_type*;
1220 static constexpr size_t byte_alignment = _ByteAlignment;
1223 aligned_accessor() noexcept = default;
1225 template<typename _OElementType, size_t _OByteAlignment>
1226 requires (_OByteAlignment >= byte_alignment)
1227 && is_convertible_v<_OElementType(*)[], element_type(*)[]>
1229 aligned_accessor(aligned_accessor<_OElementType, _OByteAlignment>)
1233 template<typename _OElementType>
1234 requires is_convertible_v<_OElementType(*)[], element_type(*)[]>
1236 aligned_accessor(default_accessor<_OElementType>) noexcept
1239 template<typename _OElementType>
1240 requires is_convertible_v<element_type(*)[], _OElementType(*)[]>
1242 operator default_accessor<_OElementType>() const noexcept
1246 access(data_handle_type __p, size_t __i) const noexcept
1247 { return std::assume_aligned<byte_alignment>(__p)[__i]; }
1249 constexpr typename offset_policy::data_handle_type
1250 offset(data_handle_type __p, size_t __i) const noexcept
1251 { return std::assume_aligned<byte_alignment>(__p) + __i; }
1257 template<typename _Extents, typename _IndexType, size_t _Nm>
1259 __is_multi_index(const _Extents& __exts, span<_IndexType, _Nm> __indices)
1261 static_assert(__exts.rank() == _Nm);
1262 for (size_t __i = 0; __i < __exts.rank(); ++__i)
1263 if (__indices[__i] >= __exts.extent(__i))
1269 template<typename _ElementType, typename _Extents,
1270 typename _LayoutPolicy = layout_right,
1271 typename _AccessorPolicy = default_accessor<_ElementType>>
1274 static_assert(!is_array_v<_ElementType>,
1275 "ElementType must not be an array type");
1276 static_assert(!is_abstract_v<_ElementType>,
1277 "ElementType must not be an abstract class type");
1278 static_assert(__mdspan::__is_extents<_Extents>,
1279 "Extents must be a specialization of std::extents");
1280 static_assert(is_same_v<_ElementType,
1281 typename _AccessorPolicy::element_type>);
1284 using extents_type = _Extents;
1285 using layout_type = _LayoutPolicy;
1286 using accessor_type = _AccessorPolicy;
1287 using mapping_type = typename layout_type::template mapping<extents_type>;
1288 using element_type = _ElementType;
1289 using value_type = remove_cv_t<element_type>;
1290 using index_type = typename extents_type::index_type;
1291 using size_type = typename extents_type::size_type;
1292 using rank_type = typename extents_type::rank_type;
1293 using data_handle_type = typename accessor_type::data_handle_type;
1294 using reference = typename accessor_type::reference;
1296 static constexpr rank_type
1297 rank() noexcept { return extents_type::rank(); }
1299 static constexpr rank_type
1300 rank_dynamic() noexcept { return extents_type::rank_dynamic(); }
1302 static constexpr size_t
1303 static_extent(rank_type __r) noexcept
1304 { return extents_type::static_extent(__r); }
1306 constexpr index_type
1307 extent(rank_type __r) const noexcept { return extents().extent(__r); }
1311 requires (rank_dynamic() > 0)
1312 && is_default_constructible_v<data_handle_type>
1313 && is_default_constructible_v<mapping_type>
1314 && is_default_constructible_v<accessor_type> = default;
1317 mdspan(const mdspan& __other) = default;
1320 mdspan(mdspan&& __other) = default;
1322 template<__mdspan::__valid_index_type<index_type>... _OIndexTypes>
1323 requires (sizeof...(_OIndexTypes) == rank()
1324 || sizeof...(_OIndexTypes) == rank_dynamic())
1325 && is_constructible_v<mapping_type, extents_type>
1326 && is_default_constructible_v<accessor_type>
1328 mdspan(data_handle_type __handle, _OIndexTypes... __exts)
1330 _M_mapping(_Extents(static_cast<index_type>(std::move(__exts))...)),
1331 _M_handle(std::move(__handle))
1334 template<typename _OIndexType, size_t _Nm>
1335 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1336 && (_Nm == rank() || _Nm == rank_dynamic())
1337 && is_constructible_v<mapping_type, extents_type>
1338 && is_default_constructible_v<accessor_type>
1339 constexpr explicit(_Nm != rank_dynamic())
1340 mdspan(data_handle_type __handle, span<_OIndexType, _Nm> __exts)
1341 : _M_accessor(), _M_mapping(extents_type(__exts)),
1342 _M_handle(std::move(__handle))
1345 template<typename _OIndexType, size_t _Nm>
1346 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1347 && (_Nm == rank() || _Nm == rank_dynamic())
1348 && is_constructible_v<mapping_type, extents_type>
1349 && is_default_constructible_v<accessor_type>
1350 constexpr explicit(_Nm != rank_dynamic())
1351 mdspan(data_handle_type __handle, const array<_OIndexType, _Nm>& __exts)
1352 : _M_accessor(), _M_mapping(extents_type(__exts)),
1353 _M_handle(std::move(__handle))
1357 mdspan(data_handle_type __handle, const extents_type& __exts)
1358 requires is_constructible_v<mapping_type, const extents_type&>
1359 && is_default_constructible_v<accessor_type>
1360 : _M_accessor(), _M_mapping(__exts), _M_handle(std::move(__handle))
1364 mdspan(data_handle_type __handle, const mapping_type& __mapping)
1365 requires is_default_constructible_v<accessor_type>
1366 : _M_accessor(), _M_mapping(__mapping), _M_handle(std::move(__handle))
1370 mdspan(data_handle_type __handle, const mapping_type& __mapping,
1371 const accessor_type& __accessor)
1372 : _M_accessor(__accessor), _M_mapping(__mapping),
1373 _M_handle(std::move(__handle))
1376 template<typename _OElementType, typename _OExtents, typename _OLayout,
1377 typename _OAccessor>
1378 requires is_constructible_v<mapping_type,
1379 const typename _OLayout::template mapping<_OExtents>&>
1380 && is_constructible_v<accessor_type, const _OAccessor&>
1381 constexpr explicit(!is_convertible_v<
1382 const typename _OLayout::template mapping<_OExtents>&, mapping_type>
1383 || !is_convertible_v<const _OAccessor&, accessor_type>)
1384 mdspan(const mdspan<_OElementType, _OExtents, _OLayout, _OAccessor>&
1386 : _M_accessor(__other.accessor()), _M_mapping(__other.mapping()),
1387 _M_handle(__other.data_handle())
1389 static_assert(is_constructible_v<data_handle_type,
1390 const typename _OAccessor::data_handle_type&>);
1391 static_assert(is_constructible_v<extents_type, _OExtents>);
1395 operator=(const mdspan& __other) = default;
1398 operator=(mdspan&& __other) = default;
1400 template<__mdspan::__valid_index_type<index_type>... _OIndexTypes>
1401 requires (sizeof...(_OIndexTypes) == rank())
1403 operator[](_OIndexTypes... __indices) const
1405 auto __checked_call = [this](auto... __idxs) -> index_type
1407 if constexpr (sizeof...(__idxs) > 0)
1408 __glibcxx_assert(__mdspan::__is_multi_index(extents(),
1409 span<const index_type, sizeof...(__idxs)>({__idxs...})));
1410 return _M_mapping(__idxs...);
1413 auto __index = __checked_call(
1414 static_cast<index_type>(std::move(__indices))...);
1415 return _M_accessor.access(_M_handle, __index);
1418 template<typename _OIndexType>
1419 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1421 operator[](span<_OIndexType, rank()> __indices) const
1423 auto __call = [&]<size_t... _Counts>(index_sequence<_Counts...>)
1425 { return (*this)[index_type(as_const(__indices[_Counts]))...]; };
1426 return __call(make_index_sequence<rank()>());
1429 template<typename _OIndexType>
1430 requires __mdspan::__valid_index_type<const _OIndexType&, index_type>
1432 operator[](const array<_OIndexType, rank()>& __indices) const
1433 { return (*this)[span<const _OIndexType, rank()>(__indices)]; }
1436 size() const noexcept
1438 __glibcxx_assert(cmp_less_equal(_M_mapping.required_span_size(),
1439 __gnu_cxx::__int_traits<size_t>
1441 return size_type(__mdspan::__size(extents()));
1446 empty() const noexcept
1447 { return __mdspan::__empty(extents()); }
1449 friend constexpr void
1450 swap(mdspan& __x, mdspan& __y) noexcept
1453 swap(__x._M_mapping, __y._M_mapping);
1454 swap(__x._M_accessor, __y._M_accessor);
1455 swap(__x._M_handle, __y._M_handle);
1458 constexpr const extents_type&
1459 extents() const noexcept { return _M_mapping.extents(); }
1461 constexpr const data_handle_type&
1462 data_handle() const noexcept { return _M_handle; }
1464 constexpr const mapping_type&
1465 mapping() const noexcept { return _M_mapping; }
1467 constexpr const accessor_type&
1468 accessor() const noexcept { return _M_accessor; }
1470 // Strengthened noexcept for all `is_*` methods.
1472 static constexpr bool
1473 is_always_unique() noexcept(noexcept(mapping_type::is_always_unique()))
1474 { return mapping_type::is_always_unique(); }
1476 static constexpr bool
1477 is_always_exhaustive()
1478 noexcept(noexcept(mapping_type::is_always_exhaustive()))
1479 { return mapping_type::is_always_exhaustive(); }
1481 static constexpr bool
1483 noexcept(noexcept(mapping_type::is_always_strided()))
1484 { return mapping_type::is_always_strided(); }
1487 is_unique() const noexcept(noexcept(_M_mapping.is_unique()))
1488 { return _M_mapping.is_unique(); }
1491 is_exhaustive() const noexcept(noexcept(_M_mapping.is_exhaustive()))
1492 { return _M_mapping.is_exhaustive(); }
1495 is_strided() const noexcept(noexcept(_M_mapping.is_strided()))
1496 { return _M_mapping.is_strided(); }
1498 constexpr index_type
1499 stride(rank_type __r) const { return _M_mapping.stride(__r); }
1502 [[no_unique_address]] accessor_type _M_accessor = accessor_type();
1503 [[no_unique_address]] mapping_type _M_mapping = mapping_type();
1504 [[no_unique_address]] data_handle_type _M_handle = data_handle_type();
1507 template<typename _CArray>
1508 requires is_array_v<_CArray> && (rank_v<_CArray> == 1)
1510 -> mdspan<remove_all_extents_t<_CArray>,
1511 extents<size_t, extent_v<_CArray, 0>>>;
1513 template<typename _Pointer>
1514 requires is_pointer_v<remove_reference_t<_Pointer>>
1516 -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;
1518 template<typename _ElementType, typename... _Integrals>
1519 requires (is_convertible_v<_Integrals, size_t> && ...)
1520 && (sizeof...(_Integrals) > 0)
1521 explicit mdspan(_ElementType*, _Integrals...)
1522 -> mdspan<_ElementType,
1523 extents<size_t, __detail::__maybe_static_ext<_Integrals>...>>;
1525 template<typename _ElementType, typename _OIndexType, size_t _Nm>
1526 mdspan(_ElementType*, span<_OIndexType, _Nm>)
1527 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
1529 template<typename _ElementType, typename _OIndexType, size_t _Nm>
1530 mdspan(_ElementType*, const array<_OIndexType, _Nm>&)
1531 -> mdspan<_ElementType, dextents<size_t, _Nm>>;
1533 template<typename _ElementType, typename _IndexType, size_t... _ExtentsPack>
1534 mdspan(_ElementType*, const extents<_IndexType, _ExtentsPack...>&)
1535 -> mdspan<_ElementType, extents<_IndexType, _ExtentsPack...>>;
1537 template<typename _ElementType, typename _MappingType>
1538 mdspan(_ElementType*, const _MappingType&)
1539 -> mdspan<_ElementType, typename _MappingType::extents_type,
1540 typename _MappingType::layout_type>;
1542 template<typename _MappingType, typename _AccessorType>
1543 mdspan(const typename _AccessorType::data_handle_type&, const _MappingType&,
1544 const _AccessorType&)
1545 -> mdspan<typename _AccessorType::element_type,
1546 typename _MappingType::extents_type,
1547 typename _MappingType::layout_type, _AccessorType>;
1549_GLIBCXX_END_NAMESPACE_VERSION