File indexing completed on 2026-05-03 08:13:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef _LIBCPP___CXX03___MDSPAN_MDSPAN_H
0018 #define _LIBCPP___CXX03___MDSPAN_MDSPAN_H
0019
0020 #include <__cxx03/__assert>
0021 #include <__cxx03/__config>
0022 #include <__cxx03/__fwd/mdspan.h>
0023 #include <__cxx03/__mdspan/default_accessor.h>
0024 #include <__cxx03/__mdspan/extents.h>
0025 #include <__cxx03/__type_traits/extent.h>
0026 #include <__cxx03/__type_traits/is_abstract.h>
0027 #include <__cxx03/__type_traits/is_array.h>
0028 #include <__cxx03/__type_traits/is_constructible.h>
0029 #include <__cxx03/__type_traits/is_convertible.h>
0030 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
0031 #include <__cxx03/__type_traits/is_pointer.h>
0032 #include <__cxx03/__type_traits/is_same.h>
0033 #include <__cxx03/__type_traits/rank.h>
0034 #include <__cxx03/__type_traits/remove_all_extents.h>
0035 #include <__cxx03/__type_traits/remove_cv.h>
0036 #include <__cxx03/__type_traits/remove_pointer.h>
0037 #include <__cxx03/__type_traits/remove_reference.h>
0038 #include <__cxx03/__utility/integer_sequence.h>
0039 #include <__cxx03/array>
0040 #include <__cxx03/cinttypes>
0041 #include <__cxx03/cstddef>
0042 #include <__cxx03/limits>
0043 #include <__cxx03/span>
0044
0045 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0046 # pragma GCC system_header
0047 #endif
0048
0049 _LIBCPP_PUSH_MACROS
0050 #include <__cxx03/__undef_macros>
0051
0052 _LIBCPP_BEGIN_NAMESPACE_STD
0053
0054 #if _LIBCPP_STD_VER >= 23
0055
0056
0057 namespace __mdspan_detail {
0058 template <class _Layout, class _Extents>
0059 concept __has_invalid_mapping = !requires { typename _Layout::template mapping<_Extents>; };
0060 }
0061
0062 template <class _ElementType,
0063 class _Extents,
0064 class _LayoutPolicy = layout_right,
0065 class _AccessorPolicy = default_accessor<_ElementType> >
0066 class mdspan {
0067 private:
0068 static_assert(__mdspan_detail::__is_extents_v<_Extents>,
0069 "mdspan: Extents template parameter must be a specialization of extents.");
0070 static_assert(!is_array_v<_ElementType>, "mdspan: ElementType template parameter may not be an array type");
0071 static_assert(!is_abstract_v<_ElementType>, "mdspan: ElementType template parameter may not be an abstract class");
0072 static_assert(is_same_v<_ElementType, typename _AccessorPolicy::element_type>,
0073 "mdspan: ElementType template parameter must match AccessorPolicy::element_type");
0074 static_assert(!__mdspan_detail::__has_invalid_mapping<_LayoutPolicy, _Extents>,
0075 "mdspan: LayoutPolicy template parameter is invalid. A common mistake is to pass a layout mapping "
0076 "instead of a layout policy");
0077
0078 public:
0079 using extents_type = _Extents;
0080 using layout_type = _LayoutPolicy;
0081 using accessor_type = _AccessorPolicy;
0082 using mapping_type = typename layout_type::template mapping<extents_type>;
0083 using element_type = _ElementType;
0084 using value_type = remove_cv_t<element_type>;
0085 using index_type = typename extents_type::index_type;
0086 using size_type = typename extents_type::size_type;
0087 using rank_type = typename extents_type::rank_type;
0088 using data_handle_type = typename accessor_type::data_handle_type;
0089 using reference = typename accessor_type::reference;
0090
0091 _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank() noexcept { return extents_type::rank(); }
0092 _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank_dynamic() noexcept { return extents_type::rank_dynamic(); }
0093 _LIBCPP_HIDE_FROM_ABI static constexpr size_t static_extent(rank_type __r) noexcept {
0094 return extents_type::static_extent(__r);
0095 }
0096 _LIBCPP_HIDE_FROM_ABI constexpr index_type extent(rank_type __r) const noexcept {
0097 return __map_.extents().extent(__r);
0098 };
0099
0100 public:
0101
0102
0103
0104 _LIBCPP_HIDE_FROM_ABI constexpr mdspan()
0105 requires((extents_type::rank_dynamic() > 0) && is_default_constructible_v<data_handle_type> &&
0106 is_default_constructible_v<mapping_type> && is_default_constructible_v<accessor_type>)
0107 = default;
0108 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(const mdspan&) = default;
0109 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(mdspan&&) = default;
0110
0111 template <class... _OtherIndexTypes>
0112 requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&
0113 (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&
0114 ((sizeof...(_OtherIndexTypes) == rank()) || (sizeof...(_OtherIndexTypes) == rank_dynamic())) &&
0115 is_constructible_v<mapping_type, extents_type> && is_default_constructible_v<accessor_type>)
0116 _LIBCPP_HIDE_FROM_ABI explicit constexpr mdspan(data_handle_type __p, _OtherIndexTypes... __exts)
0117 : __ptr_(std::move(__p)), __map_(extents_type(static_cast<index_type>(std::move(__exts))...)), __acc_{} {}
0118
0119 template <class _OtherIndexType, size_t _Size>
0120 requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0121 is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&
0122 ((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&
0123 is_default_constructible_v<accessor_type>)
0124 explicit(_Size != rank_dynamic())
0125 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const array<_OtherIndexType, _Size>& __exts)
0126 : __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}
0127
0128 template <class _OtherIndexType, size_t _Size>
0129 requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0130 is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&
0131 ((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&
0132 is_default_constructible_v<accessor_type>)
0133 explicit(_Size != rank_dynamic())
0134 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, span<_OtherIndexType, _Size> __exts)
0135 : __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}
0136
0137 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const extents_type& __exts)
0138 requires(is_default_constructible_v<accessor_type> && is_constructible_v<mapping_type, const extents_type&>)
0139 : __ptr_(std::move(__p)), __map_(__exts), __acc_{} {}
0140
0141 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m)
0142 requires(is_default_constructible_v<accessor_type>)
0143 : __ptr_(std::move(__p)), __map_(__m), __acc_{} {}
0144
0145 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m, const accessor_type& __a)
0146 : __ptr_(std::move(__p)), __map_(__m), __acc_(__a) {}
0147
0148 template <class _OtherElementType, class _OtherExtents, class _OtherLayoutPolicy, class _OtherAccessor>
0149 requires(is_constructible_v<mapping_type, const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&> &&
0150 is_constructible_v<accessor_type, const _OtherAccessor&>)
0151 explicit(!is_convertible_v<const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&, mapping_type> ||
0152 !is_convertible_v<const _OtherAccessor&, accessor_type>)
0153 _LIBCPP_HIDE_FROM_ABI constexpr mdspan(
0154 const mdspan<_OtherElementType, _OtherExtents, _OtherLayoutPolicy, _OtherAccessor>& __other)
0155 : __ptr_(__other.__ptr_), __map_(__other.__map_), __acc_(__other.__acc_) {
0156 static_assert(is_constructible_v<data_handle_type, const typename _OtherAccessor::data_handle_type&>,
0157 "mdspan: incompatible data_handle_type for mdspan construction");
0158 static_assert(
0159 is_constructible_v<extents_type, _OtherExtents>, "mdspan: incompatible extents for mdspan construction");
0160
0161
0162
0163
0164
0165
0166 if constexpr (rank() > 0) {
0167 for (size_t __r = 0; __r < rank(); __r++) {
0168
0169
0170
0171 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0172 (static_extent(__r) == dynamic_extent) ||
0173 (static_cast<index_type>(__other.extent(__r)) == static_cast<index_type>(static_extent(__r))),
0174 "mdspan: conversion mismatch of source dynamic extents with static extents");
0175 }
0176 }
0177 }
0178
0179 _LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(const mdspan&) = default;
0180 _LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(mdspan&&) = default;
0181
0182
0183
0184
0185 template <class... _OtherIndexTypes>
0186 requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&
0187 (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&
0188 (sizeof...(_OtherIndexTypes) == rank()))
0189 _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](_OtherIndexTypes... __indices) const {
0190
0191
0192 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__mdspan_detail::__is_multidimensional_index_in(extents(), __indices...),
0193 "mdspan: operator[] out of bounds access");
0194 return __acc_.access(__ptr_, __map_(static_cast<index_type>(std::move(__indices))...));
0195 }
0196
0197 template <class _OtherIndexType>
0198 requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0199 is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
0200 _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](const array< _OtherIndexType, rank()>& __indices) const {
0201 return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0202 return __map_(__indices[_Idxs]...);
0203 }(make_index_sequence<rank()>()));
0204 }
0205
0206 template <class _OtherIndexType>
0207 requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0208 is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
0209 _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](span<_OtherIndexType, rank()> __indices) const {
0210 return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0211 return __map_(__indices[_Idxs]...);
0212 }(make_index_sequence<rank()>()));
0213 }
0214
0215 _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept {
0216
0217
0218 _LIBCPP_ASSERT_UNCATEGORIZED(
0219 false == ([&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0220 size_type __prod = 1;
0221 return (__builtin_mul_overflow(__prod, extent(_Idxs), &__prod) || ... || false);
0222 }(make_index_sequence<rank()>())),
0223 "mdspan: size() is not representable as size_type");
0224 return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0225 return ((static_cast<size_type>(__map_.extents().extent(_Idxs))) * ... * size_type(1));
0226 }(make_index_sequence<rank()>());
0227 }
0228
0229 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept {
0230 return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0231 return (rank() > 0) && ((__map_.extents().extent(_Idxs) == index_type(0)) || ... || false);
0232 }(make_index_sequence<rank()>());
0233 }
0234
0235 _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(mdspan& __x, mdspan& __y) noexcept {
0236 swap(__x.__ptr_, __y.__ptr_);
0237 swap(__x.__map_, __y.__map_);
0238 swap(__x.__acc_, __y.__acc_);
0239 }
0240
0241 _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __map_.extents(); };
0242 _LIBCPP_HIDE_FROM_ABI constexpr const data_handle_type& data_handle() const noexcept { return __ptr_; };
0243 _LIBCPP_HIDE_FROM_ABI constexpr const mapping_type& mapping() const noexcept { return __map_; };
0244 _LIBCPP_HIDE_FROM_ABI constexpr const accessor_type& accessor() const noexcept { return __acc_; };
0245
0246
0247 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return mapping_type::is_always_unique(); };
0248 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept {
0249 return mapping_type::is_always_exhaustive();
0250 };
0251 _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept {
0252 return mapping_type::is_always_strided();
0253 };
0254
0255 _LIBCPP_HIDE_FROM_ABI constexpr bool is_unique() const { return __map_.is_unique(); };
0256 _LIBCPP_HIDE_FROM_ABI constexpr bool is_exhaustive() const { return __map_.is_exhaustive(); };
0257 _LIBCPP_HIDE_FROM_ABI constexpr bool is_strided() const { return __map_.is_strided(); };
0258 _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const { return __map_.stride(__r); };
0259
0260 private:
0261 _LIBCPP_NO_UNIQUE_ADDRESS data_handle_type __ptr_{};
0262 _LIBCPP_NO_UNIQUE_ADDRESS mapping_type __map_{};
0263 _LIBCPP_NO_UNIQUE_ADDRESS accessor_type __acc_{};
0264
0265 template <class, class, class, class>
0266 friend class mdspan;
0267 };
0268
0269 # if _LIBCPP_STD_VER >= 26
0270 template <class _ElementType, class... _OtherIndexTypes>
0271 requires((is_convertible_v<_OtherIndexTypes, size_t> && ...) && (sizeof...(_OtherIndexTypes) > 0))
0272 explicit mdspan(_ElementType*,
0273 _OtherIndexTypes...) -> mdspan<_ElementType, extents<size_t, __maybe_static_ext<_OtherIndexTypes>...>>;
0274 # else
0275 template <class _ElementType, class... _OtherIndexTypes>
0276 requires((is_convertible_v<_OtherIndexTypes, size_t> && ...) && (sizeof...(_OtherIndexTypes) > 0))
0277 explicit mdspan(_ElementType*,
0278 _OtherIndexTypes...) -> mdspan<_ElementType, dextents<size_t, sizeof...(_OtherIndexTypes)>>;
0279 # endif
0280
0281 template <class _Pointer>
0282 requires(is_pointer_v<remove_reference_t<_Pointer>>)
0283 mdspan(_Pointer&&) -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;
0284
0285 template <class _CArray>
0286 requires(is_array_v<_CArray> && (rank_v<_CArray> == 1))
0287 mdspan(_CArray&) -> mdspan<remove_all_extents_t<_CArray>, extents<size_t, extent_v<_CArray, 0>>>;
0288
0289 template <class _ElementType, class _OtherIndexType, size_t _Size>
0290 mdspan(_ElementType*, const array<_OtherIndexType, _Size>&) -> mdspan<_ElementType, dextents<size_t, _Size>>;
0291
0292 template <class _ElementType, class _OtherIndexType, size_t _Size>
0293 mdspan(_ElementType*, span<_OtherIndexType, _Size>) -> mdspan<_ElementType, dextents<size_t, _Size>>;
0294
0295
0296
0297
0298 template <class _ElementType, class _OtherIndexType, size_t... _ExtentsPack>
0299 mdspan(_ElementType*, const extents<_OtherIndexType, _ExtentsPack...>&)
0300 -> mdspan<_ElementType, extents<_OtherIndexType, _ExtentsPack...>>;
0301
0302 template <class _ElementType, class _MappingType>
0303 mdspan(_ElementType*, const _MappingType&)
0304 -> mdspan<_ElementType, typename _MappingType::extents_type, typename _MappingType::layout_type>;
0305
0306 template <class _MappingType, class _AccessorType>
0307 mdspan(const typename _AccessorType::data_handle_type, const _MappingType&, const _AccessorType&)
0308 -> mdspan<typename _AccessorType::element_type,
0309 typename _MappingType::extents_type,
0310 typename _MappingType::layout_type,
0311 _AccessorType>;
0312
0313 #endif
0314
0315 _LIBCPP_END_NAMESPACE_STD
0316
0317 _LIBCPP_POP_MACROS
0318
0319 #endif