Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:34

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //                        Kokkos v. 4.0
0009 //       Copyright (2022) National Technology & Engineering
0010 //               Solutions of Sandia, LLC (NTESS).
0011 //
0012 // Under the terms of Contract DE-NA0003525 with NTESS,
0013 // the U.S. Government retains certain rights in this software.
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 // Helper for lightweight test checking that one did pass a layout policy as LayoutPolicy template argument
0057 namespace __mdspan_detail {
0058 template <class _Layout, class _Extents>
0059 concept __has_invalid_mapping = !requires { typename _Layout::template mapping<_Extents>; };
0060 } // namespace __mdspan_detail
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   // [mdspan.mdspan.cons], mdspan constructors, assignment, and destructor
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     // The following precondition is part of the standard, but is unlikely to be triggered.
0162     // The extents constructor checks this and the mapping must be storing the extents, since
0163     // its extents() function returns a const reference to extents_type.
0164     // The only way this can be triggered is if the mapping conversion constructor would for example
0165     // always construct its extents() only from the dynamic extents, instead of from the other extents.
0166     if constexpr (rank() > 0) {
0167       for (size_t __r = 0; __r < rank(); __r++) {
0168         // Not catching this could lead to out of bounds errors later
0169         // e.g. mdspan<int, dextents<char,1>, non_checking_layout> m =
0170         //        mdspan<int, dextents<unsigned, 1>, non_checking_layout>(ptr, 200); leads to an extent of -56 on m
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   // [mdspan.mdspan.members], members
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     // Note the standard layouts would also check this, but user provided ones may not, so we
0191     // check the precondition here
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     // Could leave this as only checked in debug mode: semantically size() is never
0217     // guaranteed to be related to any accessible range
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   // per LWG-4021 "mdspan::is_always_meow() should be noexcept"
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 // This one is necessary because all the constructors take `data_handle_type`s, not
0296 // `_ElementType*`s, and `data_handle_type` is taken from `accessor_type::data_handle_type`, which
0297 // seems to throw off automatic deduction guides.
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 // _LIBCPP_STD_VER >= 23
0314 
0315 _LIBCPP_END_NAMESPACE_STD
0316 
0317 _LIBCPP_POP_MACROS
0318 
0319 #endif // _LIBCPP___CXX03___MDSPAN_MDSPAN_H