Path: blob/main/contrib/llvm-project/libcxx/include/__mdspan/mdspan.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7// Kokkos v. 4.08// Copyright (2022) National Technology & Engineering9// Solutions of Sandia, LLC (NTESS).10//11// Under the terms of Contract DE-NA0003525 with NTESS,12// the U.S. Government retains certain rights in this software.13//14//===---------------------------------------------------------------------===//1516#ifndef _LIBCPP___MDSPAN_MDSPAN_H17#define _LIBCPP___MDSPAN_MDSPAN_H1819#include <__assert>20#include <__config>21#include <__fwd/mdspan.h>22#include <__mdspan/default_accessor.h>23#include <__mdspan/extents.h>24#include <__type_traits/extent.h>25#include <__type_traits/is_abstract.h>26#include <__type_traits/is_array.h>27#include <__type_traits/is_constructible.h>28#include <__type_traits/is_convertible.h>29#include <__type_traits/is_nothrow_constructible.h>30#include <__type_traits/is_pointer.h>31#include <__type_traits/is_same.h>32#include <__type_traits/rank.h>33#include <__type_traits/remove_all_extents.h>34#include <__type_traits/remove_cv.h>35#include <__type_traits/remove_pointer.h>36#include <__type_traits/remove_reference.h>37#include <__utility/integer_sequence.h>38#include <array>39#include <cinttypes>40#include <cstddef>41#include <limits>42#include <span>4344#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)45# pragma GCC system_header46#endif4748_LIBCPP_PUSH_MACROS49#include <__undef_macros>5051_LIBCPP_BEGIN_NAMESPACE_STD5253#if _LIBCPP_STD_VER >= 235455// Helper for lightweight test checking that one did pass a layout policy as LayoutPolicy template argument56namespace __mdspan_detail {57template <class _Layout, class _Extents>58concept __has_invalid_mapping = !requires { typename _Layout::template mapping<_Extents>; };59} // namespace __mdspan_detail6061template <class _ElementType,62class _Extents,63class _LayoutPolicy = layout_right,64class _AccessorPolicy = default_accessor<_ElementType> >65class mdspan {66private:67static_assert(__mdspan_detail::__is_extents_v<_Extents>,68"mdspan: Extents template parameter must be a specialization of extents.");69static_assert(!is_array_v<_ElementType>, "mdspan: ElementType template parameter may not be an array type");70static_assert(!is_abstract_v<_ElementType>, "mdspan: ElementType template parameter may not be an abstract class");71static_assert(is_same_v<_ElementType, typename _AccessorPolicy::element_type>,72"mdspan: ElementType template parameter must match AccessorPolicy::element_type");73static_assert(!__mdspan_detail::__has_invalid_mapping<_LayoutPolicy, _Extents>,74"mdspan: LayoutPolicy template parameter is invalid. A common mistake is to pass a layout mapping "75"instead of a layout policy");7677public:78using extents_type = _Extents;79using layout_type = _LayoutPolicy;80using accessor_type = _AccessorPolicy;81using mapping_type = typename layout_type::template mapping<extents_type>;82using element_type = _ElementType;83using value_type = remove_cv_t<element_type>;84using index_type = typename extents_type::index_type;85using size_type = typename extents_type::size_type;86using rank_type = typename extents_type::rank_type;87using data_handle_type = typename accessor_type::data_handle_type;88using reference = typename accessor_type::reference;8990_LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank() noexcept { return extents_type::rank(); }91_LIBCPP_HIDE_FROM_ABI static constexpr rank_type rank_dynamic() noexcept { return extents_type::rank_dynamic(); }92_LIBCPP_HIDE_FROM_ABI static constexpr size_t static_extent(rank_type __r) noexcept {93return extents_type::static_extent(__r);94}95_LIBCPP_HIDE_FROM_ABI constexpr index_type extent(rank_type __r) const noexcept {96return __map_.extents().extent(__r);97};9899public:100//--------------------------------------------------------------------------------101// [mdspan.mdspan.cons], mdspan constructors, assignment, and destructor102103_LIBCPP_HIDE_FROM_ABI constexpr mdspan()104requires((extents_type::rank_dynamic() > 0) && is_default_constructible_v<data_handle_type> &&105is_default_constructible_v<mapping_type> && is_default_constructible_v<accessor_type>)106= default;107_LIBCPP_HIDE_FROM_ABI constexpr mdspan(const mdspan&) = default;108_LIBCPP_HIDE_FROM_ABI constexpr mdspan(mdspan&&) = default;109110template <class... _OtherIndexTypes>111requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&112(is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&113((sizeof...(_OtherIndexTypes) == rank()) || (sizeof...(_OtherIndexTypes) == rank_dynamic())) &&114is_constructible_v<mapping_type, extents_type> && is_default_constructible_v<accessor_type>)115_LIBCPP_HIDE_FROM_ABI explicit constexpr mdspan(data_handle_type __p, _OtherIndexTypes... __exts)116: __ptr_(std::move(__p)), __map_(extents_type(static_cast<index_type>(std::move(__exts))...)), __acc_{} {}117118template <class _OtherIndexType, size_t _Size>119requires(is_convertible_v<const _OtherIndexType&, index_type> &&120is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&121((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&122is_default_constructible_v<accessor_type>)123explicit(_Size != rank_dynamic())124_LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const array<_OtherIndexType, _Size>& __exts)125: __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}126127template <class _OtherIndexType, size_t _Size>128requires(is_convertible_v<const _OtherIndexType&, index_type> &&129is_nothrow_constructible_v<index_type, const _OtherIndexType&> &&130((_Size == rank()) || (_Size == rank_dynamic())) && is_constructible_v<mapping_type, extents_type> &&131is_default_constructible_v<accessor_type>)132explicit(_Size != rank_dynamic())133_LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, span<_OtherIndexType, _Size> __exts)134: __ptr_(std::move(__p)), __map_(extents_type(__exts)), __acc_{} {}135136_LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const extents_type& __exts)137requires(is_default_constructible_v<accessor_type> && is_constructible_v<mapping_type, const extents_type&>)138: __ptr_(std::move(__p)), __map_(__exts), __acc_{} {}139140_LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m)141requires(is_default_constructible_v<accessor_type>)142: __ptr_(std::move(__p)), __map_(__m), __acc_{} {}143144_LIBCPP_HIDE_FROM_ABI constexpr mdspan(data_handle_type __p, const mapping_type& __m, const accessor_type& __a)145: __ptr_(std::move(__p)), __map_(__m), __acc_(__a) {}146147template <class _OtherElementType, class _OtherExtents, class _OtherLayoutPolicy, class _OtherAccessor>148requires(is_constructible_v<mapping_type, const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&> &&149is_constructible_v<accessor_type, const _OtherAccessor&>)150explicit(!is_convertible_v<const typename _OtherLayoutPolicy::template mapping<_OtherExtents>&, mapping_type> ||151!is_convertible_v<const _OtherAccessor&, accessor_type>)152_LIBCPP_HIDE_FROM_ABI constexpr mdspan(153const mdspan<_OtherElementType, _OtherExtents, _OtherLayoutPolicy, _OtherAccessor>& __other)154: __ptr_(__other.__ptr_), __map_(__other.__map_), __acc_(__other.__acc_) {155static_assert(is_constructible_v<data_handle_type, const typename _OtherAccessor::data_handle_type&>,156"mdspan: incompatible data_handle_type for mdspan construction");157static_assert(158is_constructible_v<extents_type, _OtherExtents>, "mdspan: incompatible extents for mdspan construction");159160// The following precondition is part of the standard, but is unlikely to be triggered.161// The extents constructor checks this and the mapping must be storing the extents, since162// its extents() function returns a const reference to extents_type.163// The only way this can be triggered is if the mapping conversion constructor would for example164// always construct its extents() only from the dynamic extents, instead of from the other extents.165if constexpr (rank() > 0) {166for (size_t __r = 0; __r < rank(); __r++) {167// Not catching this could lead to out of bounds errors later168// e.g. mdspan<int, dextents<char,1>, non_checking_layout> m =169// mdspan<int, dextents<unsigned, 1>, non_checking_layout>(ptr, 200); leads to an extent of -56 on m170_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(171(static_extent(__r) == dynamic_extent) ||172(static_cast<index_type>(__other.extent(__r)) == static_cast<index_type>(static_extent(__r))),173"mdspan: conversion mismatch of source dynamic extents with static extents");174}175}176}177178_LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(const mdspan&) = default;179_LIBCPP_HIDE_FROM_ABI constexpr mdspan& operator=(mdspan&&) = default;180181//--------------------------------------------------------------------------------182// [mdspan.mdspan.members], members183184template <class... _OtherIndexTypes>185requires((is_convertible_v<_OtherIndexTypes, index_type> && ...) &&186(is_nothrow_constructible_v<index_type, _OtherIndexTypes> && ...) &&187(sizeof...(_OtherIndexTypes) == rank()))188_LIBCPP_HIDE_FROM_ABI constexpr reference operator[](_OtherIndexTypes... __indices) const {189// Note the standard layouts would also check this, but user provided ones may not, so we190// check the precondition here191_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__mdspan_detail::__is_multidimensional_index_in(extents(), __indices...),192"mdspan: operator[] out of bounds access");193return __acc_.access(__ptr_, __map_(static_cast<index_type>(std::move(__indices))...));194}195196template <class _OtherIndexType>197requires(is_convertible_v<const _OtherIndexType&, index_type> &&198is_nothrow_constructible_v<index_type, const _OtherIndexType&>)199_LIBCPP_HIDE_FROM_ABI constexpr reference operator[](const array< _OtherIndexType, rank()>& __indices) const {200return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {201return __map_(__indices[_Idxs]...);202}(make_index_sequence<rank()>()));203}204205template <class _OtherIndexType>206requires(is_convertible_v<const _OtherIndexType&, index_type> &&207is_nothrow_constructible_v<index_type, const _OtherIndexType&>)208_LIBCPP_HIDE_FROM_ABI constexpr reference operator[](span<_OtherIndexType, rank()> __indices) const {209return __acc_.access(__ptr_, [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {210return __map_(__indices[_Idxs]...);211}(make_index_sequence<rank()>()));212}213214_LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept {215// Could leave this as only checked in debug mode: semantically size() is never216// guaranteed to be related to any accessible range217_LIBCPP_ASSERT_UNCATEGORIZED(218false == ([&]<size_t... _Idxs>(index_sequence<_Idxs...>) {219size_type __prod = 1;220return (__builtin_mul_overflow(__prod, extent(_Idxs), &__prod) || ... || false);221}(make_index_sequence<rank()>())),222"mdspan: size() is not representable as size_type");223return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {224return ((static_cast<size_type>(__map_.extents().extent(_Idxs))) * ... * size_type(1));225}(make_index_sequence<rank()>());226}227228[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const noexcept {229return [&]<size_t... _Idxs>(index_sequence<_Idxs...>) {230return (rank() > 0) && ((__map_.extents().extent(_Idxs) == index_type(0)) || ... || false);231}(make_index_sequence<rank()>());232}233234_LIBCPP_HIDE_FROM_ABI friend constexpr void swap(mdspan& __x, mdspan& __y) noexcept {235swap(__x.__ptr_, __y.__ptr_);236swap(__x.__map_, __y.__map_);237swap(__x.__acc_, __y.__acc_);238}239240_LIBCPP_HIDE_FROM_ABI constexpr const extents_type& extents() const noexcept { return __map_.extents(); };241_LIBCPP_HIDE_FROM_ABI constexpr const data_handle_type& data_handle() const noexcept { return __ptr_; };242_LIBCPP_HIDE_FROM_ABI constexpr const mapping_type& mapping() const noexcept { return __map_; };243_LIBCPP_HIDE_FROM_ABI constexpr const accessor_type& accessor() const noexcept { return __acc_; };244245// per LWG-4021 "mdspan::is_always_meow() should be noexcept"246_LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_unique() noexcept { return mapping_type::is_always_unique(); };247_LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_exhaustive() noexcept {248return mapping_type::is_always_exhaustive();249};250_LIBCPP_HIDE_FROM_ABI static constexpr bool is_always_strided() noexcept {251return mapping_type::is_always_strided();252};253254_LIBCPP_HIDE_FROM_ABI constexpr bool is_unique() const { return __map_.is_unique(); };255_LIBCPP_HIDE_FROM_ABI constexpr bool is_exhaustive() const { return __map_.is_exhaustive(); };256_LIBCPP_HIDE_FROM_ABI constexpr bool is_strided() const { return __map_.is_strided(); };257_LIBCPP_HIDE_FROM_ABI constexpr index_type stride(rank_type __r) const { return __map_.stride(__r); };258259private:260_LIBCPP_NO_UNIQUE_ADDRESS data_handle_type __ptr_{};261_LIBCPP_NO_UNIQUE_ADDRESS mapping_type __map_{};262_LIBCPP_NO_UNIQUE_ADDRESS accessor_type __acc_{};263264template <class, class, class, class>265friend class mdspan;266};267268# if _LIBCPP_STD_VER >= 26269template <class _ElementType, class... _OtherIndexTypes>270requires((is_convertible_v<_OtherIndexTypes, size_t> && ...) && (sizeof...(_OtherIndexTypes) > 0))271explicit mdspan(_ElementType*,272_OtherIndexTypes...) -> mdspan<_ElementType, extents<size_t, __maybe_static_ext<_OtherIndexTypes>...>>;273# else274template <class _ElementType, class... _OtherIndexTypes>275requires((is_convertible_v<_OtherIndexTypes, size_t> && ...) && (sizeof...(_OtherIndexTypes) > 0))276explicit mdspan(_ElementType*,277_OtherIndexTypes...) -> mdspan<_ElementType, dextents<size_t, sizeof...(_OtherIndexTypes)>>;278# endif279280template <class _Pointer>281requires(is_pointer_v<remove_reference_t<_Pointer>>)282mdspan(_Pointer&&) -> mdspan<remove_pointer_t<remove_reference_t<_Pointer>>, extents<size_t>>;283284template <class _CArray>285requires(is_array_v<_CArray> && (rank_v<_CArray> == 1))286mdspan(_CArray&) -> mdspan<remove_all_extents_t<_CArray>, extents<size_t, extent_v<_CArray, 0>>>;287288template <class _ElementType, class _OtherIndexType, size_t _Size>289mdspan(_ElementType*, const array<_OtherIndexType, _Size>&) -> mdspan<_ElementType, dextents<size_t, _Size>>;290291template <class _ElementType, class _OtherIndexType, size_t _Size>292mdspan(_ElementType*, span<_OtherIndexType, _Size>) -> mdspan<_ElementType, dextents<size_t, _Size>>;293294// This one is necessary because all the constructors take `data_handle_type`s, not295// `_ElementType*`s, and `data_handle_type` is taken from `accessor_type::data_handle_type`, which296// seems to throw off automatic deduction guides.297template <class _ElementType, class _OtherIndexType, size_t... _ExtentsPack>298mdspan(_ElementType*, const extents<_OtherIndexType, _ExtentsPack...>&)299-> mdspan<_ElementType, extents<_OtherIndexType, _ExtentsPack...>>;300301template <class _ElementType, class _MappingType>302mdspan(_ElementType*, const _MappingType&)303-> mdspan<_ElementType, typename _MappingType::extents_type, typename _MappingType::layout_type>;304305template <class _MappingType, class _AccessorType>306mdspan(const typename _AccessorType::data_handle_type, const _MappingType&, const _AccessorType&)307-> mdspan<typename _AccessorType::element_type,308typename _MappingType::extents_type,309typename _MappingType::layout_type,310_AccessorType>;311312#endif // _LIBCPP_STD_VER >= 23313314_LIBCPP_END_NAMESPACE_STD315316_LIBCPP_POP_MACROS317318#endif // _LIBCPP___MDSPAN_MDSPAN_H319320321