Back to home page

EIC code displayed by LXR

 
 

    


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

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___MDSPAN_MDSPAN_H
0018 #define _LIBCPP___MDSPAN_MDSPAN_H
0019 
0020 #include <__assert>
0021 #include <__config>
0022 #include <__fwd/mdspan.h>
0023 #include <__mdspan/default_accessor.h>
0024 #include <__mdspan/extents.h>
0025 #include <__type_traits/extent.h>
0026 #include <__type_traits/is_abstract.h>
0027 #include <__type_traits/is_array.h>
0028 #include <__type_traits/is_constructible.h>
0029 #include <__type_traits/is_convertible.h>
0030 #include <__type_traits/is_nothrow_constructible.h>
0031 #include <__type_traits/is_pointer.h>
0032 #include <__type_traits/is_same.h>
0033 #include <__type_traits/rank.h>
0034 #include <__type_traits/remove_all_extents.h>
0035 #include <__type_traits/remove_cv.h>
0036 #include <__type_traits/remove_pointer.h>
0037 #include <__type_traits/remove_reference.h>
0038 #include <__utility/integer_sequence.h>
0039 #include <array>
0040 #include <span>
0041 
0042 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0043 #  pragma GCC system_header
0044 #endif
0045 
0046 _LIBCPP_PUSH_MACROS
0047 #include <__undef_macros>
0048 
0049 _LIBCPP_BEGIN_NAMESPACE_STD
0050 
0051 #if _LIBCPP_STD_VER >= 23
0052 
0053 // Helper for lightweight test checking that one did pass a layout policy as LayoutPolicy template argument
0054 namespace __mdspan_detail {
0055 template <class _Layout, class _Extents>
0056 concept __has_invalid_mapping = !requires { typename _Layout::template mapping<_Extents>; };
0057 } // namespace __mdspan_detail
0058 
0059 template <class _ElementType,
0060           class _Extents,
0061           class _LayoutPolicy   = layout_right,
0062           class _AccessorPolicy = default_accessor<_ElementType> >
0063 class mdspan {
0064 private:
0065   static_assert(__mdspan_detail::__is_extents_v<_Extents>,
0066                 "mdspan: Extents template parameter must be a specialization of extents.");
0067   static_assert(!is_array_v<_ElementType>, "mdspan: ElementType template parameter may not be an array type");
0068   static_assert(!is_abstract_v<_ElementType>, "mdspan: ElementType template parameter may not be an abstract class");
0069   static_assert(is_same_v<_ElementType, typename _AccessorPolicy::element_type>,
0070                 "mdspan: ElementType template parameter must match AccessorPolicy::element_type");
0071   static_assert(!__mdspan_detail::__has_invalid_mapping<_LayoutPolicy, _Extents>,
0072                 "mdspan: LayoutPolicy template parameter is invalid. A common mistake is to pass a layout mapping "
0073                 "instead of a layout policy");
0074 
0075 public:
0076   using extents_type     = _Extents;
0077   using layout_type      = _LayoutPolicy;
0078   using accessor_type    = _AccessorPolicy;
0079   using mapping_type     = typename layout_type::template mapping<extents_type>;
0080   using element_type     = _ElementType;
0081   using value_type       = remove_cv_t<element_type>;
0082   using index_type       = typename extents_type::index_type;
0083   using size_type        = typename extents_type::size_type;
0084   using rank_type        = typename extents_type::rank_type;
0085   using data_handle_type = typename accessor_type::data_handle_type;
0086   using reference        = typename accessor_type::reference;
0087 
0088   _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank() noexcept { return extents_type::rank(); }
0089   _LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank_dynamic() noexcept { return extents_type::rank_dynamic(); }
0090   _LIBCPP_HIDE_FROM_ABI static constexpr size_t static_extent(rank_type __r) noexcept {
0091     return extents_type::static_extent(__r);
0092   }
0093   _LIBCPP_HIDE_FROM_ABI constexpr index_type extent(rank_type __r) const noexcept {
0094     return __map_.extents().extent(__r);
0095   };
0096 
0097 public:
0098   //--------------------------------------------------------------------------------
0099   // [mdspan.mdspan.cons], mdspan constructors, assignment, and destructor
0100 
0101   _LIBCPP_HIDE_FROM_ABI constexpr mdspan()
0102     requires((extents_type::rank_dynamic() > 0) && is_default_constructible_v<data_handle_type> &&
0103              is_default_constructible_v<mapping_type> && is_default_constructible_v<accessor_type>)
0104   = default;
0105   _LIBCPP_HIDE_FROM_ABI constexpr mdspan(const mdspan&) = default;
0106   _LIBCPP_HIDE_FROM_ABI constexpr mdspan(mdspan&&)      = default;
0107 
0108   template <class... _OtherIndexTypes>
0109     requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&
0110              (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&
0111              ((sizeof...(_OtherIndexTypes) == rank()) || (sizeof...(_OtherIndexTypes) == rank_dynamic())) &&
0112              is_constructible_v<mapping_type, extents_type> && is_default_constructible_v<accessor_type>)
0113   _LIBCPP_HIDE_FROM_ABI explicit constexpr mdspan(data_handle_type __p, _OtherIndexTypes... __exts)
0114       : __ptr_(std::move(__p)), __map_(extents_type(static_cast<index_type>(std::move(__exts))...)), __acc_{} {}
0115 
0116   template <class _OtherIndexType, size_t _Size>
0117     requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0118              is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&
0119              ((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&
0120              is_default_constructible_v<accessor_type>)
0121   explicit(_Size != rank_dynamic())
0122       _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const array<_OtherIndexType, _Size>& __exts)
0123       : __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}
0124 
0125   template <class _OtherIndexType, size_t _Size>
0126     requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0127              is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&
0128              ((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&
0129              is_default_constructible_v<accessor_type>)
0130   explicit(_Size != rank_dynamic())
0131       _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, span<_OtherIndexType, _Size> __exts)
0132       : __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}
0133 
0134   _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const extents_type& __exts)
0135     requires(is_default_constructible_v<accessor_type> && is_constructible_v<mapping_type, const extents_type&>)
0136       : __ptr_(std::move(__p)), __map_(__exts), __acc_{} {}
0137 
0138   _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m)
0139     requires(is_default_constructible_v<accessor_type>)
0140       : __ptr_(std::move(__p)), __map_(__m), __acc_{} {}
0141 
0142   _LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m, const accessor_type& __a)
0143       : __ptr_(std::move(__p)), __map_(__m), __acc_(__a) {}
0144 
0145   template <class _OtherElementType, class _OtherExtents, class _OtherLayoutPolicy, class _OtherAccessor>
0146     requires(is_constructible_v<mapping_type, const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&> &&
0147              is_constructible_v<accessor_type, const _OtherAccessor&>)
0148   explicit(!is_convertible_v<const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&, mapping_type> ||
0149            !is_convertible_v<const _OtherAccessor&, accessor_type>)
0150       _LIBCPP_HIDE_FROM_ABI constexpr mdspan(
0151           const mdspan<_OtherElementType, _OtherExtents, _OtherLayoutPolicy, _OtherAccessor>& __other)
0152       : __ptr_(__other.__ptr_), __map_(__other.__map_), __acc_(__other.__acc_) {
0153     static_assert(is_constructible_v<data_handle_type, const typename _OtherAccessor::data_handle_type&>,
0154                   "mdspan: incompatible data_handle_type for mdspan construction");
0155     static_assert(
0156         is_constructible_v<extents_type, _OtherExtents>, "mdspan: incompatible extents for mdspan construction");
0157 
0158     // The following precondition is part of the standard, but is unlikely to be triggered.
0159     // The extents constructor checks this and the mapping must be storing the extents, since
0160     // its extents() function returns a const reference to extents_type.
0161     // The only way this can be triggered is if the mapping conversion constructor would for example
0162     // always construct its extents() only from the dynamic extents, instead of from the other extents.
0163     if constexpr (rank() > 0) {
0164       for (size_t __r = 0; __r < rank(); __r++) {
0165         // Not catching this could lead to out of bounds errors later
0166         // e.g. mdspan<int, dextents<char,1>, non_checking_layout> m =
0167         //        mdspan<int, dextents<unsigned, 1>, non_checking_layout>(ptr, 200); leads to an extent of -56 on m
0168         _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
0169             (static_extent(__r) == dynamic_extent) ||
0170                 (static_cast<index_type>(__other.extent(__r)) == static_cast<index_type>(static_extent(__r))),
0171             "mdspan: conversion mismatch of source dynamic extents with static extents");
0172       }
0173     }
0174   }
0175 
0176   _LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(const mdspan&) = default;
0177   _LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(mdspan&&)      = default;
0178 
0179   //--------------------------------------------------------------------------------
0180   // [mdspan.mdspan.members], members
0181 
0182   template <class... _OtherIndexTypes>
0183     requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&
0184              (is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&
0185              (sizeof...(_OtherIndexTypes) == rank()))
0186   _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](_OtherIndexTypes... __indices) const {
0187     // Note the standard layouts would also check this, but user provided ones may not, so we
0188     // check the precondition here
0189     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__mdspan_detail::__is_multidimensional_index_in(extents(), __indices...),
0190                                         "mdspan: operator[] out of bounds access");
0191     return __acc_.access(__ptr_, __map_(static_cast<index_type>(std::move(__indices))...));
0192   }
0193 
0194   template <class _OtherIndexType>
0195     requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0196              is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
0197   _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](const array< _OtherIndexType, rank()>& __indices) const {
0198     return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0199       return __map_(__indices[_Idxs]...);
0200     }(make_index_sequence<rank()>()));
0201   }
0202 
0203   template <class _OtherIndexType>
0204     requires(is_convertible_v<const _OtherIndexType&, index_type> &&
0205              is_nothrow_constructible_v<index_type, const _OtherIndexType&>)
0206   _LIBCPP_HIDE_FROM_ABI constexpr reference operator[](span<_OtherIndexType, rank()> __indices) const {
0207     return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0208       return __map_(__indices[_Idxs]...);
0209     }(make_index_sequence<rank()>()));
0210   }
0211 
0212   _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept {
0213     // Could leave this as only checked in debug mode: semantically size() is never
0214     // guaranteed to be related to any accessible range
0215     _LIBCPP_ASSERT_UNCATEGORIZED(
0216         false == ([&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0217           size_type __prod = 1;
0218           return (__builtin_mul_overflow(__prod, extent(_Idxs), &__prod) || ... || false);
0219         }(make_index_sequence<rank()>())),
0220         "mdspan: size() is not representable as size_type");
0221     return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0222       return ((static_cast<size_type>(__map_.extents().extent(_Idxs))) * ... * size_type(1));
0223     }(make_index_sequence<rank()>());
0224   }
0225 
0226   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept {
0227     return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {
0228       return (rank() > 0) && ((__map_.extents().extent(_Idxs) == index_type(0)) || ... || false);
0229     }(make_index_sequence<rank()>());
0230   }
0231 
0232   _LIBCPP_HIDE_FROM_ABI friend constexpr void swap(mdspan& __x, mdspan& __y) noexcept {
0233     swap(__x.__ptr_, __y.__ptr_);
0234     swap(__x.__map_, __y.__map_);
0235     swap(__x.__acc_, __y.__acc_);
0236   }
0237 
0238   _LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __map_.extents(); };
0239   _LIBCPP_HIDE_FROM_ABI constexpr const data_handle_type& data_handle() const noexcept { return __ptr_; };
0240   _LIBCPP_HIDE_FROM_ABI constexpr const mapping_type& mapping() const noexcept { return __map_; };
0241   _LIBCPP_HIDE_FROM_ABI constexpr const accessor_type& accessor() const noexcept { return __acc_; };
0242 
0243   // per LWG-4021 "mdspan::is_always_meow() should be noexcept"
0244   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return mapping_type::is_always_unique(); };
0245   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept {
0246     return mapping_type::is_always_exhaustive();
0247   };
0248   _LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept {
0249     return mapping_type::is_always_strided();
0250   };
0251 
0252   _LIBCPP_HIDE_FROM_ABI constexpr bool is_unique() const { return __map_.is_unique(); };
0253   _LIBCPP_HIDE_FROM_ABI constexpr bool is_exhaustive() const { return __map_.is_exhaustive(); };
0254   _LIBCPP_HIDE_FROM_ABI constexpr bool is_strided() const { return __map_.is_strided(); };
0255   _LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const { return __map_.stride(__r); };
0256 
0257 private:
0258   _LIBCPP_NO_UNIQUE_ADDRESS data_handle_type __ptr_{};
0259   _LIBCPP_NO_UNIQUE_ADDRESS mapping_type __map_{};
0260   _LIBCPP_NO_UNIQUE_ADDRESS accessor_type __acc_{};
0261 
0262   template <class, class, class, class>
0263   friend class mdspan;
0264 };
0265 
0266 #  if _LIBCPP_STD_VER >= 26
0267 template <class _ElementType, class... _OtherIndexTypes>
0268   requires((is_convertible_v<_OtherIndexTypes, size_t> && ...) && (sizeof...(_OtherIndexTypes) > 0))
0269 explicit mdspan(_ElementType*,
0270                 _OtherIndexTypes...) -> mdspan<_ElementType, extents<size_t, __maybe_static_ext<_OtherIndexTypes>...>>;
0271 #  else
0272 template <class _ElementType, class... _OtherIndexTypes>
0273   requires((is_convertible_v<_OtherIndexTypes, size_t> && ...) && (sizeof...(_OtherIndexTypes) > 0))
0274 explicit mdspan(_ElementType*,
0275                 _OtherIndexTypes...) -> mdspan<_ElementType, dextents<size_t, sizeof...(_OtherIndexTypes)>>;
0276 #  endif
0277 
0278 template <class _Pointer>
0279   requires(is_pointer_v<remove_reference_t<_Pointer>>)
0280 mdspan(_Pointer&&) -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;
0281 
0282 template <class _CArray>
0283   requires(is_array_v<_CArray> && (rank_v<_CArray> == 1))
0284 mdspan(_CArray&) -> mdspan<remove_all_extents_t<_CArray>, extents<size_t, extent_v<_CArray, 0>>>;
0285 
0286 template <class _ElementType, class _OtherIndexType, size_t _Size>
0287 mdspan(_ElementType*, const array<_OtherIndexType, _Size>&) -> mdspan<_ElementType, dextents<size_t, _Size>>;
0288 
0289 template <class _ElementType, class _OtherIndexType, size_t _Size>
0290 mdspan(_ElementType*, span<_OtherIndexType, _Size>) -> mdspan<_ElementType, dextents<size_t, _Size>>;
0291 
0292 // This one is necessary because all the constructors take `data_handle_type`s, not
0293 // `_ElementType*`s, and `data_handle_type` is taken from `accessor_type::data_handle_type`, which
0294 // seems to throw off automatic deduction guides.
0295 template <class _ElementType, class _OtherIndexType, size_t... _ExtentsPack>
0296 mdspan(_ElementType*, const extents<_OtherIndexType, _ExtentsPack...>&)
0297     -> mdspan<_ElementType, extents<_OtherIndexType, _ExtentsPack...>>;
0298 
0299 template <class _ElementType, class _MappingType>
0300 mdspan(_ElementType*, const _MappingType&)
0301     -> mdspan<_ElementType, typename _MappingType::extents_type, typename _MappingType::layout_type>;
0302 
0303 template <class _MappingType, class _AccessorType>
0304 mdspan(const typename _AccessorType::data_handle_type, const _MappingType&, const _AccessorType&)
0305     -> mdspan<typename _AccessorType::element_type,
0306               typename _MappingType::extents_type,
0307               typename _MappingType::layout_type,
0308               _AccessorType>;
0309 
0310 #endif // _LIBCPP_STD_VER >= 23
0311 
0312 _LIBCPP_END_NAMESPACE_STD
0313 
0314 _LIBCPP_POP_MACROS
0315 
0316 #endif // _LIBCPP___MDSPAN_MDSPAN_H