Warning, /include/c++/v1/mdspan is written in an unsupported language. File is not indexed.
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 //===---------------------------------------------------------------------===//
0009
0010 /*
0011
0012 // Overall mdspan synopsis
0013
0014 namespace std {
0015 // [mdspan.extents], class template extents
0016 template<class IndexType, size_t... Extents>
0017 class extents;
0018
0019 // [mdspan.extents.dextents], alias template dextents
0020 template<class IndexType, size_t Rank>
0021 using dextents = see below;
0022
0023 // [mdspan.extents.dims], alias template dims
0024 template<size_t Rank, class IndexType = size_t>
0025 using dims = see below; // since C++26
0026
0027 // [mdspan.layout], layout mapping
0028 struct layout_left;
0029 struct layout_right;
0030 struct layout_stride;
0031
0032 // [mdspan.accessor.default], class template default_accessor
0033 template<class ElementType>
0034 class default_accessor;
0035
0036 // [mdspan.mdspan], class template mdspan
0037 template<class ElementType, class Extents, class LayoutPolicy = layout_right,
0038 class AccessorPolicy = default_accessor<ElementType>>
0039 class mdspan; // not implemented yet
0040 }
0041
0042 // extents synopsis
0043
0044 namespace std {
0045 template<class _IndexType, size_t... _Extents>
0046 class extents {
0047 public:
0048 using index_type = _IndexType;
0049 using size_type = make_unsigned_t<index_type>;
0050 using rank_type = size_t;
0051
0052 // [mdspan.extents.obs], observers of the multidimensional index space
0053 static constexpr rank_type rank() noexcept { return sizeof...(_Extents); }
0054 static constexpr rank_type rank_dynamic() noexcept { return dynamic-index(rank()); }
0055 static constexpr size_t static_extent(rank_type) noexcept;
0056 constexpr index_type extent(rank_type) const noexcept;
0057
0058 // [mdspan.extents.cons], constructors
0059 constexpr extents() noexcept = default;
0060
0061 template<class _OtherIndexType, size_t... _OtherExtents>
0062 constexpr explicit(see below)
0063 extents(const extents<_OtherIndexType, _OtherExtents...>&) noexcept;
0064 template<class... _OtherIndexTypes>
0065 constexpr explicit extents(_OtherIndexTypes...) noexcept;
0066 template<class _OtherIndexType, size_t N>
0067 constexpr explicit(N != rank_dynamic())
0068 extents(span<_OtherIndexType, N>) noexcept;
0069 template<class _OtherIndexType, size_t N>
0070 constexpr explicit(N != rank_dynamic())
0071 extents(const array<_OtherIndexType, N>&) noexcept;
0072
0073 // [mdspan.extents.cmp], comparison operators
0074 template<class _OtherIndexType, size_t... _OtherExtents>
0075 friend constexpr bool operator==(const extents&,
0076 const extents<_OtherIndexType, _OtherExtents...>&) noexcept;
0077
0078 private:
0079 // libcxx note: we do not use an array here, but we need to preserve the as-if behavior
0080 // for example the default constructor must zero initialize dynamic extents
0081 array<index_type, rank_dynamic()> dynamic-extents{}; // exposition only
0082 };
0083
0084 template<class... Integrals>
0085 explicit extents(Integrals...)
0086 -> see below;
0087 }
0088
0089 // layout_left synopsis
0090
0091 namespace std {
0092 template<class Extents>
0093 class layout_left::mapping {
0094 public:
0095 using extents_type = Extents;
0096 using index_type = typename extents_type::index_type;
0097 using size_type = typename extents_type::size_type;
0098 using rank_type = typename extents_type::rank_type;
0099 using layout_type = layout_left;
0100
0101 // [mdspan.layout.right.cons], constructors
0102 constexpr mapping() noexcept = default;
0103 constexpr mapping(const mapping&) noexcept = default;
0104 constexpr mapping(const extents_type&) noexcept;
0105 template<class OtherExtents>
0106 constexpr explicit(!is_convertible_v<OtherExtents, extents_type>)
0107 mapping(const mapping<OtherExtents>&) noexcept;
0108 template<class OtherExtents>
0109 constexpr explicit(!is_convertible_v<OtherExtents, extents_type>)
0110 mapping(const layout_right::mapping<OtherExtents>&) noexcept;
0111 template<class OtherExtents>
0112 constexpr explicit(extents_type::rank() > 0)
0113 mapping(const layout_stride::mapping<OtherExtents>&) noexcept;
0114
0115 constexpr mapping& operator=(const mapping&) noexcept = default;
0116
0117 // [mdspan.layout.right.obs], observers
0118 constexpr const extents_type& extents() const noexcept { return extents_; }
0119
0120 constexpr index_type required_span_size() const noexcept;
0121
0122 template<class... Indices>
0123 constexpr index_type operator()(Indices...) const noexcept;
0124
0125 static constexpr bool is_always_unique() noexcept { return true; }
0126 static constexpr bool is_always_exhaustive() noexcept { return true; }
0127 static constexpr bool is_always_strided() noexcept { return true; }
0128
0129 static constexpr bool is_unique() noexcept { return true; }
0130 static constexpr bool is_exhaustive() noexcept { return true; }
0131 static constexpr bool is_strided() noexcept { return true; }
0132
0133 constexpr index_type stride(rank_type) const noexcept;
0134
0135 template<class OtherExtents>
0136 friend constexpr bool operator==(const mapping&, const mapping<OtherExtents>&) noexcept;
0137
0138 private:
0139 extents_type extents_{}; // exposition only
0140 };
0141 }
0142
0143 // layout_right synopsis
0144
0145 namespace std {
0146 template<class Extents>
0147 class layout_right::mapping {
0148 public:
0149 using extents_type = Extents;
0150 using index_type = typename extents_type::index_type;
0151 using size_type = typename extents_type::size_type;
0152 using rank_type = typename extents_type::rank_type;
0153 using layout_type = layout_right;
0154
0155 // [mdspan.layout.right.cons], constructors
0156 constexpr mapping() noexcept = default;
0157 constexpr mapping(const mapping&) noexcept = default;
0158 constexpr mapping(const extents_type&) noexcept;
0159 template<class OtherExtents>
0160 constexpr explicit(!is_convertible_v<OtherExtents, extents_type>)
0161 mapping(const mapping<OtherExtents>&) noexcept;
0162 template<class OtherExtents>
0163 constexpr explicit(!is_convertible_v<OtherExtents, extents_type>)
0164 mapping(const layout_left::mapping<OtherExtents>&) noexcept;
0165 template<class OtherExtents>
0166 constexpr explicit(extents_type::rank() > 0)
0167 mapping(const layout_stride::mapping<OtherExtents>&) noexcept;
0168
0169 constexpr mapping& operator=(const mapping&) noexcept = default;
0170
0171 // [mdspan.layout.right.obs], observers
0172 constexpr const extents_type& extents() const noexcept { return extents_; }
0173
0174 constexpr index_type required_span_size() const noexcept;
0175
0176 template<class... Indices>
0177 constexpr index_type operator()(Indices...) const noexcept;
0178
0179 static constexpr bool is_always_unique() noexcept { return true; }
0180 static constexpr bool is_always_exhaustive() noexcept { return true; }
0181 static constexpr bool is_always_strided() noexcept { return true; }
0182
0183 static constexpr bool is_unique() noexcept { return true; }
0184 static constexpr bool is_exhaustive() noexcept { return true; }
0185 static constexpr bool is_strided() noexcept { return true; }
0186
0187 constexpr index_type stride(rank_type) const noexcept;
0188
0189 template<class OtherExtents>
0190 friend constexpr bool operator==(const mapping&, const mapping<OtherExtents>&) noexcept;
0191
0192 private:
0193 extents_type extents_{}; // exposition only
0194 };
0195 }
0196
0197 // layout_stride synopsis
0198
0199 namespace std {
0200 template<class Extents>
0201 class layout_stride::mapping {
0202 public:
0203 using extents_type = Extents;
0204 using index_type = typename extents_type::index_type;
0205 using size_type = typename extents_type::size_type;
0206 using rank_type = typename extents_type::rank_type;
0207 using layout_type = layout_stride;
0208
0209 private:
0210 static constexpr rank_type rank_ = extents_type::rank(); // exposition only
0211
0212 public:
0213 // [mdspan.layout.stride.cons], constructors
0214 constexpr mapping() noexcept;
0215 constexpr mapping(const mapping&) noexcept = default;
0216 template<class OtherIndexType>
0217 constexpr mapping(const extents_type&, span<OtherIndexType, rank_>) noexcept;
0218 template<class OtherIndexType>
0219 constexpr mapping(const extents_type&, const array<OtherIndexType, rank_>&) noexcept;
0220
0221 template<class StridedLayoutMapping>
0222 constexpr explicit(see below) mapping(const StridedLayoutMapping&) noexcept;
0223
0224 constexpr mapping& operator=(const mapping&) noexcept = default;
0225
0226 // [mdspan.layout.stride.obs], observers
0227 constexpr const extents_type& extents() const noexcept { return extents_; }
0228 constexpr array<index_type, rank_> strides() const noexcept { return strides_; }
0229
0230 constexpr index_type required_span_size() const noexcept;
0231
0232 template<class... Indices>
0233 constexpr index_type operator()(Indices...) const noexcept;
0234
0235 static constexpr bool is_always_unique() noexcept { return true; }
0236 static constexpr bool is_always_exhaustive() noexcept { return false; }
0237 static constexpr bool is_always_strided() noexcept { return true; }
0238
0239 static constexpr bool is_unique() noexcept { return true; }
0240 constexpr bool is_exhaustive() const noexcept;
0241 static constexpr bool is_strided() noexcept { return true; }
0242
0243 constexpr index_type stride(rank_type i) const noexcept { return strides_[i]; }
0244
0245 template<class OtherMapping>
0246 friend constexpr bool operator==(const mapping&, const OtherMapping&) noexcept;
0247
0248 private:
0249 extents_type extents_{}; // exposition only
0250 array<index_type, rank_> strides_{}; // exposition only
0251 };
0252 }
0253
0254 // default_accessor synopsis
0255
0256 namespace std {
0257 template<class ElementType>
0258 struct default_accessor {
0259 using offset_policy = default_accessor;
0260 using element_type = ElementType;
0261 using reference = ElementType&;
0262 using data_handle_type = ElementType*;
0263
0264 constexpr default_accessor() noexcept = default;
0265 template<class OtherElementType>
0266 constexpr default_accessor(default_accessor<OtherElementType>) noexcept;
0267 constexpr reference access(data_handle_type p, size_t i) const noexcept;
0268 constexpr data_handle_type offset(data_handle_type p, size_t i) const noexcept;
0269 };
0270 }
0271
0272 // mdspan synopsis
0273
0274 namespace std {
0275 template<class ElementType, class Extents, class LayoutPolicy = layout_right,
0276 class AccessorPolicy = default_accessor<ElementType>>
0277 class mdspan {
0278 public:
0279 using extents_type = Extents;
0280 using layout_type = LayoutPolicy;
0281 using accessor_type = AccessorPolicy;
0282 using mapping_type = typename layout_type::template mapping<extents_type>;
0283 using element_type = ElementType;
0284 using value_type = remove_cv_t<element_type>;
0285 using index_type = typename extents_type::index_type;
0286 using size_type = typename extents_type::size_type;
0287 using rank_type = typename extents_type::rank_type;
0288 using data_handle_type = typename accessor_type::data_handle_type;
0289 using reference = typename accessor_type::reference;
0290
0291 static constexpr rank_type rank() noexcept { return extents_type::rank(); }
0292 static constexpr rank_type rank_dynamic() noexcept { return extents_type::rank_dynamic(); }
0293 static constexpr size_t static_extent(rank_type r) noexcept
0294 { return extents_type::static_extent(r); }
0295 constexpr index_type extent(rank_type r) const noexcept { return extents().extent(r); }
0296
0297 // [mdspan.mdspan.cons], constructors
0298 constexpr mdspan();
0299 constexpr mdspan(const mdspan& rhs) = default;
0300 constexpr mdspan(mdspan&& rhs) = default;
0301
0302 template<class... OtherIndexTypes>
0303 constexpr explicit mdspan(data_handle_type ptr, OtherIndexTypes... exts);
0304 template<class OtherIndexType, size_t N>
0305 constexpr explicit(N != rank_dynamic())
0306 mdspan(data_handle_type p, span<OtherIndexType, N> exts);
0307 template<class OtherIndexType, size_t N>
0308 constexpr explicit(N != rank_dynamic())
0309 mdspan(data_handle_type p, const array<OtherIndexType, N>& exts);
0310 constexpr mdspan(data_handle_type p, const extents_type& ext);
0311 constexpr mdspan(data_handle_type p, const mapping_type& m);
0312 constexpr mdspan(data_handle_type p, const mapping_type& m, const accessor_type& a);
0313
0314 template<class OtherElementType, class OtherExtents,
0315 class OtherLayoutPolicy, class OtherAccessorPolicy>
0316 constexpr explicit(see below)
0317 mdspan(const mdspan<OtherElementType, OtherExtents,
0318 OtherLayoutPolicy, OtherAccessorPolicy>& other);
0319
0320 constexpr mdspan& operator=(const mdspan& rhs) = default;
0321 constexpr mdspan& operator=(mdspan&& rhs) = default;
0322
0323 // [mdspan.mdspan.members], members
0324 template<class... OtherIndexTypes>
0325 constexpr reference operator[](OtherIndexTypes... indices) const;
0326 template<class OtherIndexType>
0327 constexpr reference operator[](span<OtherIndexType, rank()> indices) const;
0328 template<class OtherIndexType>
0329 constexpr reference operator[](const array<OtherIndexType, rank()>& indices) const;
0330
0331 constexpr size_type size() const noexcept;
0332 [[nodiscard]] constexpr bool empty() const noexcept;
0333
0334 friend constexpr void swap(mdspan& x, mdspan& y) noexcept;
0335
0336 constexpr const extents_type& extents() const noexcept { return map_.extents(); }
0337 constexpr const data_handle_type& data_handle() const noexcept { return ptr_; }
0338 constexpr const mapping_type& mapping() const noexcept { return map_; }
0339 constexpr const accessor_type& accessor() const noexcept { return acc_; }
0340
0341 // per LWG-4021 "mdspan::is_always_meow() should be noexcept"
0342 static constexpr bool is_always_unique() noexcept
0343 { return mapping_type::is_always_unique(); }
0344 static constexpr bool is_always_exhaustive() noexcept
0345 { return mapping_type::is_always_exhaustive(); }
0346 static constexpr bool is_always_strided() noexcept
0347 { return mapping_type::is_always_strided(); }
0348
0349 constexpr bool is_unique() const
0350 { return map_.is_unique(); }
0351 constexpr bool is_exhaustive() const
0352 { return map_.is_exhaustive(); }
0353 constexpr bool is_strided() const
0354 { return map_.is_strided(); }
0355 constexpr index_type stride(rank_type r) const
0356 { return map_.stride(r); }
0357
0358 private:
0359 accessor_type acc_; // exposition only
0360 mapping_type map_; // exposition only
0361 data_handle_type ptr_; // exposition only
0362 };
0363
0364 template<class CArray>
0365 requires(is_array_v<CArray> && rank_v<CArray> == 1)
0366 mdspan(CArray&)
0367 -> mdspan<remove_all_extents_t<CArray>, extents<size_t, extent_v<CArray, 0>>>;
0368
0369 template<class Pointer>
0370 requires(is_pointer_v<remove_reference_t<Pointer>>)
0371 mdspan(Pointer&&)
0372 -> mdspan<remove_pointer_t<remove_reference_t<Pointer>>, extents<size_t>>;
0373
0374 template<class ElementType, class... Integrals>
0375 requires((is_convertible_v<Integrals, size_t> && ...) && sizeof...(Integrals) > 0)
0376 explicit mdspan(ElementType*, Integrals...)
0377 -> mdspan<ElementType, dextents<size_t, sizeof...(Integrals)>>; // until C++26
0378 template<class ElementType, class... Integrals>
0379 requires((is_convertible_v<Integrals, size_t> && ...) && sizeof...(Integrals) > 0)
0380 explicit mdspan(ElementType*, Integrals...)
0381 -> mdspan<ElementType, extents<size_t, maybe-static-ext<Integrals>...>>; // since C++26
0382
0383 template<class ElementType, class OtherIndexType, size_t N>
0384 mdspan(ElementType*, span<OtherIndexType, N>)
0385 -> mdspan<ElementType, dextents<size_t, N>>;
0386
0387 template<class ElementType, class OtherIndexType, size_t N>
0388 mdspan(ElementType*, const array<OtherIndexType, N>&)
0389 -> mdspan<ElementType, dextents<size_t, N>>;
0390
0391 template<class ElementType, class IndexType, size_t... ExtentsPack>
0392 mdspan(ElementType*, const extents<IndexType, ExtentsPack...>&)
0393 -> mdspan<ElementType, extents<IndexType, ExtentsPack...>>;
0394
0395 template<class ElementType, class MappingType>
0396 mdspan(ElementType*, const MappingType&)
0397 -> mdspan<ElementType, typename MappingType::extents_type,
0398 typename MappingType::layout_type>;
0399
0400 template<class MappingType, class AccessorType>
0401 mdspan(const typename AccessorType::data_handle_type&, const MappingType&,
0402 const AccessorType&)
0403 -> mdspan<typename AccessorType::element_type, typename MappingType::extents_type,
0404 typename MappingType::layout_type, AccessorType>;
0405 }
0406 */
0407
0408 #ifndef _LIBCPP_MDSPAN
0409 #define _LIBCPP_MDSPAN
0410
0411 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0412 # include <__cxx03/mdspan>
0413 #else
0414 # include <__config>
0415
0416 # if _LIBCPP_STD_VER >= 23
0417 # include <__fwd/mdspan.h>
0418 # include <__mdspan/default_accessor.h>
0419 # include <__mdspan/extents.h>
0420 # include <__mdspan/layout_left.h>
0421 # include <__mdspan/layout_right.h>
0422 # include <__mdspan/layout_stride.h>
0423 # include <__mdspan/mdspan.h>
0424 # endif
0425
0426 # include <version>
0427
0428 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0429 # pragma GCC system_header
0430 # endif
0431 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0432
0433 #endif // _LIBCPP_MDSPAN