File indexing completed on 2026-05-03 08:13:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef _LIBCPP___MDSPAN_EXTENTS_H
0018 #define _LIBCPP___MDSPAN_EXTENTS_H
0019
0020 #include <__assert>
0021 #include <__config>
0022
0023 #include <__concepts/arithmetic.h>
0024 #include <__cstddef/byte.h>
0025 #include <__type_traits/common_type.h>
0026 #include <__type_traits/is_convertible.h>
0027 #include <__type_traits/is_nothrow_constructible.h>
0028 #include <__type_traits/is_same.h>
0029 #include <__type_traits/make_unsigned.h>
0030 #include <__utility/integer_sequence.h>
0031 #include <__utility/unreachable.h>
0032 #include <array>
0033 #include <concepts>
0034 #include <limits>
0035 #include <span>
0036
0037 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0038 # pragma GCC system_header
0039 #endif
0040
0041 _LIBCPP_PUSH_MACROS
0042 #include <__undef_macros>
0043
0044 _LIBCPP_BEGIN_NAMESPACE_STD
0045
0046 #if _LIBCPP_STD_VER >= 23
0047
0048 namespace __mdspan_detail {
0049
0050
0051
0052
0053
0054 template <class _Tp, _Tp... _Values>
0055 struct __static_array {
0056 static constexpr array<_Tp, sizeof...(_Values)> __array = {_Values...};
0057
0058 public:
0059 _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size() { return sizeof...(_Values); }
0060 _LIBCPP_HIDE_FROM_ABI static constexpr _Tp __get(size_t __index) noexcept { return __array[__index]; }
0061
0062 template <size_t _Index>
0063 _LIBCPP_HIDE_FROM_ABI static constexpr _Tp __get() {
0064 return __get(_Index);
0065 }
0066 };
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077 template <class _Tp, size_t _Size>
0078 struct __possibly_empty_array {
0079 _Tp __vals_[_Size];
0080 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator[](size_t __index) { return __vals_[__index]; }
0081 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](size_t __index) const { return __vals_[__index]; }
0082 };
0083
0084 template <class _Tp>
0085 struct __possibly_empty_array<_Tp, 0> {
0086 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator[](size_t) { __libcpp_unreachable(); }
0087 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](size_t) const { __libcpp_unreachable(); }
0088 };
0089
0090
0091
0092
0093
0094
0095
0096 template <size_t... _Values>
0097 struct __static_partial_sums {
0098 _LIBCPP_HIDE_FROM_ABI static constexpr array<size_t, sizeof...(_Values)> __static_partial_sums_impl() {
0099 array<size_t, sizeof...(_Values)> __values{_Values...};
0100 array<size_t, sizeof...(_Values)> __partial_sums{{}};
0101 size_t __running_sum = 0;
0102 for (int __i = 0; __i != sizeof...(_Values); ++__i) {
0103 __partial_sums[__i] = __running_sum;
0104 __running_sum += __values[__i];
0105 }
0106 return __partial_sums;
0107 }
0108 static constexpr array<size_t, sizeof...(_Values)> __result{__static_partial_sums_impl()};
0109
0110 _LIBCPP_HIDE_FROM_ABI static constexpr size_t __get(size_t __index) { return __result[__index]; }
0111 };
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121 template <class _TDynamic, class _TStatic, _TStatic _DynTag, _TStatic... _Values>
0122 struct __maybe_static_array {
0123 static_assert(is_convertible<_TStatic, _TDynamic>::value,
0124 "__maybe_static_array: _TStatic must be convertible to _TDynamic");
0125 static_assert(is_convertible<_TDynamic, _TStatic>::value,
0126 "__maybe_static_array: _TDynamic must be convertible to _TStatic");
0127
0128 private:
0129
0130 static constexpr size_t __size_ = sizeof...(_Values);
0131 static constexpr size_t __size_dynamic_ = ((_Values == _DynTag) + ... + 0);
0132 using _StaticValues _LIBCPP_NODEBUG = __static_array<_TStatic, _Values...>;
0133 using _DynamicValues _LIBCPP_NODEBUG = __possibly_empty_array<_TDynamic, __size_dynamic_>;
0134
0135
0136 _LIBCPP_NO_UNIQUE_ADDRESS _DynamicValues __dyn_vals_;
0137
0138
0139 using _DynamicIdxMap _LIBCPP_NODEBUG = __static_partial_sums<static_cast<size_t>(_Values == _DynTag)...>;
0140
0141 template <size_t... _Indices>
0142 _LIBCPP_HIDE_FROM_ABI static constexpr _DynamicValues __zeros(index_sequence<_Indices...>) noexcept {
0143 return _DynamicValues{((void)_Indices, 0)...};
0144 }
0145
0146 public:
0147 _LIBCPP_HIDE_FROM_ABI constexpr __maybe_static_array() noexcept
0148 : __dyn_vals_{__zeros(make_index_sequence<__size_dynamic_>())} {}
0149
0150
0151 template <class... _DynVals>
0152 requires(sizeof...(_DynVals) == __size_dynamic_)
0153 _LIBCPP_HIDE_FROM_ABI constexpr __maybe_static_array(_DynVals... __vals)
0154 : __dyn_vals_{static_cast<_TDynamic>(__vals)...} {}
0155
0156 template <class _Tp, size_t _Size >
0157 requires(_Size == __size_dynamic_)
0158 _LIBCPP_HIDE_FROM_ABI constexpr __maybe_static_array([[maybe_unused]] const span<_Tp, _Size>& __vals) {
0159 if constexpr (_Size > 0) {
0160 for (size_t __i = 0; __i < _Size; __i++)
0161 __dyn_vals_[__i] = static_cast<_TDynamic>(__vals[__i]);
0162 }
0163 }
0164
0165
0166 template <class... _DynVals>
0167 requires(sizeof...(_DynVals) != __size_dynamic_)
0168 _LIBCPP_HIDE_FROM_ABI constexpr __maybe_static_array(_DynVals... __vals) {
0169 static_assert(sizeof...(_DynVals) == __size_, "Invalid number of values.");
0170 _TDynamic __values[__size_] = {static_cast<_TDynamic>(__vals)...};
0171 for (size_t __i = 0; __i < __size_; __i++) {
0172 _TStatic __static_val = _StaticValues::__get(__i);
0173 if (__static_val == _DynTag) {
0174 __dyn_vals_[_DynamicIdxMap::__get(__i)] = __values[__i];
0175 } else
0176
0177
0178
0179
0180 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0181 __values[__i] == static_cast<_TDynamic>(__static_val),
0182 "extents construction: mismatch of provided arguments with static extents.");
0183 }
0184 }
0185
0186 template <class _Tp, size_t _Size>
0187 requires(_Size != __size_dynamic_)
0188 _LIBCPP_HIDE_FROM_ABI constexpr __maybe_static_array(const span<_Tp, _Size>& __vals) {
0189 static_assert(_Size == __size_ || __size_ == dynamic_extent);
0190 for (size_t __i = 0; __i < __size_; __i++) {
0191 _TStatic __static_val = _StaticValues::__get(__i);
0192 if (__static_val == _DynTag) {
0193 __dyn_vals_[_DynamicIdxMap::__get(__i)] = static_cast<_TDynamic>(__vals[__i]);
0194 } else
0195
0196
0197
0198
0199 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0200 static_cast<_TDynamic>(__vals[__i]) == static_cast<_TDynamic>(__static_val),
0201 "extents construction: mismatch of provided arguments with static extents.");
0202 }
0203 }
0204
0205
0206 _LIBCPP_HIDE_FROM_ABI static constexpr _TStatic __static_value(size_t __i) noexcept {
0207 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < __size_, "extents access: index must be less than rank");
0208 return _StaticValues::__get(__i);
0209 }
0210
0211 _LIBCPP_HIDE_FROM_ABI constexpr _TDynamic __value(size_t __i) const {
0212 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < __size_, "extents access: index must be less than rank");
0213 _TStatic __static_val = _StaticValues::__get(__i);
0214 return __static_val == _DynTag ? __dyn_vals_[_DynamicIdxMap::__get(__i)] : static_cast<_TDynamic>(__static_val);
0215 }
0216 _LIBCPP_HIDE_FROM_ABI constexpr _TDynamic operator[](size_t __i) const {
0217 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < __size_, "extents access: index must be less than rank");
0218 return __value(__i);
0219 }
0220
0221
0222 _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size() { return __size_; }
0223 _LIBCPP_HIDE_FROM_ABI static constexpr size_t __size_dynamic() { return __size_dynamic_; }
0224 };
0225
0226
0227
0228
0229 template <integral _To, class _From>
0230 requires(integral<_From>)
0231 _LIBCPP_HIDE_FROM_ABI constexpr bool __is_representable_as(_From __value) {
0232 using _To_u = make_unsigned_t<_To>;
0233 using _From_u = make_unsigned_t<_From>;
0234 if constexpr (is_signed_v<_From>) {
0235 if (__value < 0)
0236 return false;
0237 }
0238 if constexpr (static_cast<_To_u>(numeric_limits<_To>::max()) >= static_cast<_From_u>(numeric_limits<_From>::max())) {
0239 return true;
0240 } else {
0241 return static_cast<_To_u>(numeric_limits<_To>::max()) >= static_cast<_From_u>(__value);
0242 }
0243 }
0244
0245 template <integral _To, class _From>
0246 requires(!integral<_From>)
0247 _LIBCPP_HIDE_FROM_ABI constexpr bool __is_representable_as(_From __value) {
0248 if constexpr (is_signed_v<_To>) {
0249 if (static_cast<_To>(__value) < 0)
0250 return false;
0251 }
0252 return true;
0253 }
0254
0255 template <integral _To, class... _From>
0256 _LIBCPP_HIDE_FROM_ABI constexpr bool __are_representable_as(_From... __values) {
0257 return (__mdspan_detail::__is_representable_as<_To>(__values) && ... && true);
0258 }
0259
0260 template <integral _To, class _From, size_t _Size>
0261 _LIBCPP_HIDE_FROM_ABI constexpr bool __are_representable_as(span<_From, _Size> __values) {
0262 for (size_t __i = 0; __i < _Size; __i++)
0263 if (!__mdspan_detail::__is_representable_as<_To>(__values[__i]))
0264 return false;
0265 return true;
0266 }
0267
0268 }
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278 template <class _IndexType, size_t... _Extents>
0279 class extents {
0280 public:
0281
0282 using index_type = _IndexType;
0283 using size_type = make_unsigned_t<index_type>;
0284 using rank_type = size_t;
0285
0286 static_assert(__libcpp_integer<index_type>, "extents::index_type must be a signed or unsigned integer type");
0287 static_assert(((__mdspan_detail::__is_representable_as<index_type>(_Extents) || (_Extents == dynamic_extent)) && ...),
0288 "extents ctor: arguments must be representable as index_type and nonnegative");
0289
0290 private:
0291 static constexpr rank_type __rank_ = sizeof...(_Extents);
0292 static constexpr rank_type __rank_dynamic_ = ((_Extents == dynamic_extent) + ... + 0);
0293
0294
0295 using _Values _LIBCPP_NODEBUG =
0296 __mdspan_detail::__maybe_static_array<_IndexType, size_t, dynamic_extent, _Extents...>;
0297 [[no_unique_address]] _Values __vals_;
0298
0299 public:
0300
0301 _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank() noexcept { return __rank_; }
0302 _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank_dynamic() noexcept { return __rank_dynamic_; }
0303
0304 _LIBCPP_HIDE_FROM_ABI constexpr index_type extent(rank_type __r) const noexcept { return __vals_.__value(__r); }
0305 _LIBCPP_HIDE_FROM_ABI static constexpr size_t static_extent(rank_type __r) noexcept {
0306 return _Values::__static_value(__r);
0307 }
0308
0309
0310 _LIBCPP_HIDE_FROM_ABI constexpr extents() noexcept = default;
0311
0312
0313
0314 template <class... _OtherIndexTypes>
0315 requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&
0316 (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&
0317 (sizeof...(_OtherIndexTypes) == __rank_ || sizeof...(_OtherIndexTypes) == __rank_dynamic_))
0318 _LIBCPP_HIDE_FROM_ABI constexpr explicit extents(_OtherIndexTypes... __dynvals) noexcept
0319 : __vals_(static_cast<index_type>(__dynvals)...) {
0320
0321
0322 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__mdspan_detail::__are_representable_as<index_type>(__dynvals...),
0323 "extents ctor: arguments must be representable as index_type and nonnegative");
0324 }
0325
0326 template <class _OtherIndexType, size_t _Size>
0327 requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0328 is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&
0329 (_Size == __rank_ || _Size == __rank_dynamic_))
0330 explicit(_Size != __rank_dynamic_)
0331 _LIBCPP_HIDE_FROM_ABI constexpr extents(const array<_OtherIndexType, _Size>& __exts) noexcept
0332 : __vals_(span(__exts)) {
0333
0334
0335 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__mdspan_detail::__are_representable_as<index_type>(span(__exts)),
0336 "extents ctor: arguments must be representable as index_type and nonnegative");
0337 }
0338
0339 template <class _OtherIndexType, size_t _Size>
0340 requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0341 is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&
0342 (_Size == __rank_ || _Size == __rank_dynamic_))
0343 explicit(_Size != __rank_dynamic_)
0344 _LIBCPP_HIDE_FROM_ABI constexpr extents(const span<_OtherIndexType, _Size>& __exts) noexcept
0345 : __vals_(__exts) {
0346
0347
0348
0349 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__mdspan_detail::__are_representable_as<index_type>(__exts),
0350 "extents ctor: arguments must be representable as index_type and nonnegative");
0351 }
0352
0353 private:
0354
0355 template <size_t _DynCount, size_t _Idx, class _OtherExtents, class... _DynamicValues>
0356 requires(_Idx < __rank_)
0357 _LIBCPP_HIDE_FROM_ABI constexpr _Values __construct_vals_from_extents(
0358 integral_constant<size_t, _DynCount>,
0359 integral_constant<size_t, _Idx>,
0360 const _OtherExtents& __exts,
0361 _DynamicValues... __dynamic_values) noexcept {
0362 if constexpr (static_extent(_Idx) == dynamic_extent)
0363 return __construct_vals_from_extents(
0364 integral_constant<size_t, _DynCount + 1>(),
0365 integral_constant<size_t, _Idx + 1>(),
0366 __exts,
0367 __dynamic_values...,
0368 __exts.extent(_Idx));
0369 else
0370 return __construct_vals_from_extents(
0371 integral_constant<size_t, _DynCount>(), integral_constant<size_t, _Idx + 1>(), __exts, __dynamic_values...);
0372 }
0373
0374 template <size_t _DynCount, size_t _Idx, class _OtherExtents, class... _DynamicValues>
0375 requires((_Idx == __rank_) && (_DynCount == __rank_dynamic_))
0376 _LIBCPP_HIDE_FROM_ABI constexpr _Values __construct_vals_from_extents(
0377 integral_constant<size_t, _DynCount>,
0378 integral_constant<size_t, _Idx>,
0379 const _OtherExtents&,
0380 _DynamicValues... __dynamic_values) noexcept {
0381 return _Values{static_cast<index_type>(__dynamic_values)...};
0382 }
0383
0384 public:
0385
0386 template <class _OtherIndexType, size_t... _OtherExtents>
0387 requires((sizeof...(_OtherExtents) == sizeof...(_Extents)) &&
0388 ((_OtherExtents == dynamic_extent || _Extents == dynamic_extent || _OtherExtents == _Extents) && ...))
0389 explicit((((_Extents != dynamic_extent) && (_OtherExtents == dynamic_extent)) || ...) ||
0390 (static_cast<make_unsigned_t<index_type>>(numeric_limits<index_type>::max()) <
0391 static_cast<make_unsigned_t<_OtherIndexType>>(numeric_limits<_OtherIndexType>::max())))
0392 _LIBCPP_HIDE_FROM_ABI constexpr extents(const extents<_OtherIndexType, _OtherExtents...>& __other) noexcept
0393 : __vals_(
0394 __construct_vals_from_extents(integral_constant<size_t, 0>(), integral_constant<size_t, 0>(), __other)) {
0395 if constexpr (rank() > 0) {
0396 for (size_t __r = 0; __r < rank(); __r++) {
0397 if constexpr (static_cast<make_unsigned_t<index_type>>(numeric_limits<index_type>::max()) <
0398 static_cast<make_unsigned_t<_OtherIndexType>>(numeric_limits<_OtherIndexType>::max())) {
0399
0400
0401 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0402 __mdspan_detail::__is_representable_as<index_type>(__other.extent(__r)),
0403 "extents ctor: arguments must be representable as index_type and nonnegative");
0404 }
0405
0406
0407
0408 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0409 (_Values::__static_value(__r) == dynamic_extent) ||
0410 (static_cast<index_type>(__other.extent(__r)) == static_cast<index_type>(_Values::__static_value(__r))),
0411 "extents construction: mismatch of provided arguments with static extents.");
0412 }
0413 }
0414 }
0415
0416
0417 template <class _OtherIndexType, size_t... _OtherExtents>
0418 _LIBCPP_HIDE_FROM_ABI friend constexpr bool
0419 operator==(const extents& __lhs, const extents<_OtherIndexType, _OtherExtents...>& __rhs) noexcept {
0420 if constexpr (rank() != sizeof...(_OtherExtents)) {
0421 return false;
0422 } else {
0423 for (rank_type __r = 0; __r < __rank_; __r++) {
0424
0425 using _CommonType = common_type_t<index_type, _OtherIndexType>;
0426 if (static_cast<_CommonType>(__lhs.extent(__r)) != static_cast<_CommonType>(__rhs.extent(__r))) {
0427 return false;
0428 }
0429 }
0430 }
0431 return true;
0432 }
0433 };
0434
0435
0436 namespace __mdspan_detail {
0437
0438 template <class _IndexType, size_t _Rank, class _Extents = extents<_IndexType>>
0439 struct __make_dextents;
0440
0441 template <class _IndexType, size_t _Rank, size_t... _ExtentsPack>
0442 struct __make_dextents< _IndexType, _Rank, extents<_IndexType, _ExtentsPack...>> {
0443 using type =
0444 typename __make_dextents< _IndexType, _Rank - 1, extents<_IndexType, dynamic_extent, _ExtentsPack...>>::type;
0445 };
0446
0447 template <class _IndexType, size_t... _ExtentsPack>
0448 struct __make_dextents< _IndexType, 0, extents<_IndexType, _ExtentsPack...>> {
0449 using type = extents<_IndexType, _ExtentsPack...>;
0450 };
0451
0452 }
0453
0454
0455 template <class _IndexType, size_t _Rank>
0456 using dextents = typename __mdspan_detail::__make_dextents<_IndexType, _Rank>::type;
0457
0458 # if _LIBCPP_STD_VER >= 26
0459
0460 template <size_t _Rank, class _IndexType = size_t>
0461 using dims = dextents<_IndexType, _Rank>;
0462 # endif
0463
0464
0465 # if _LIBCPP_STD_VER >= 26
0466 template <class... _IndexTypes>
0467 requires(is_convertible_v<_IndexTypes, size_t> && ...)
0468 explicit extents(_IndexTypes...) -> extents<size_t, __maybe_static_ext<_IndexTypes>...>;
0469 # else
0470 template <class... _IndexTypes>
0471 requires(is_convertible_v<_IndexTypes, size_t> && ...)
0472 explicit extents(_IndexTypes...) -> extents<size_t, size_t(((void)sizeof(_IndexTypes), dynamic_extent))...>;
0473 # endif
0474
0475 namespace __mdspan_detail {
0476
0477
0478 template <class _Tp>
0479 struct __is_extents : false_type {};
0480
0481 template <class _IndexType, size_t... _ExtentsPack>
0482 struct __is_extents<extents<_IndexType, _ExtentsPack...>> : true_type {};
0483
0484 template <class _Tp>
0485 inline constexpr bool __is_extents_v = __is_extents<_Tp>::value;
0486
0487
0488
0489
0490
0491
0492 template <integral _IndexType, class _From>
0493 requires(integral<_From>)
0494 _LIBCPP_HIDE_FROM_ABI constexpr bool __is_index_in_extent(_IndexType __extent, _From __value) {
0495 if constexpr (is_signed_v<_From>) {
0496 if (__value < 0)
0497 return false;
0498 }
0499 using _Tp = common_type_t<_IndexType, _From>;
0500 return static_cast<_Tp>(__value) < static_cast<_Tp>(__extent);
0501 }
0502
0503 template <integral _IndexType, class _From>
0504 requires(!integral<_From>)
0505 _LIBCPP_HIDE_FROM_ABI constexpr bool __is_index_in_extent(_IndexType __extent, _From __value) {
0506 if constexpr (is_signed_v<_IndexType>) {
0507 if (static_cast<_IndexType>(__value) < 0)
0508 return false;
0509 }
0510 return static_cast<_IndexType>(__value) < __extent;
0511 }
0512
0513 template <size_t... _Idxs, class _Extents, class... _From>
0514 _LIBCPP_HIDE_FROM_ABI constexpr bool
0515 __is_multidimensional_index_in_impl(index_sequence<_Idxs...>, const _Extents& __ext, _From... __values) {
0516 return (__mdspan_detail::__is_index_in_extent(__ext.extent(_Idxs), __values) && ...);
0517 }
0518
0519 template <class _Extents, class... _From>
0520 _LIBCPP_HIDE_FROM_ABI constexpr bool __is_multidimensional_index_in(const _Extents& __ext, _From... __values) {
0521 return __mdspan_detail::__is_multidimensional_index_in_impl(
0522 make_index_sequence<_Extents::rank()>(), __ext, __values...);
0523 }
0524
0525 }
0526
0527 #endif
0528
0529 _LIBCPP_END_NAMESPACE_STD
0530
0531 _LIBCPP_POP_MACROS
0532
0533 #endif