tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
mdspan.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MDSPAN_MDSPAN_HPP
4#define TETL_MDSPAN_MDSPAN_HPP
5
6#include <etl/_config/all.hpp>
7
13#include <etl/_span/span.hpp>
28#include <etl/_utility/move.hpp>
29
30namespace etl {
31
33template <
34 typename ElementType,
35 typename Extents,
36 typename LayoutPolicy = layout_right,
37 typename AccessorPolicy = default_accessor<ElementType>>
38struct mdspan {
39 using extents_type = Extents;
40 using layout_type = LayoutPolicy;
41 using accessor_type = AccessorPolicy;
42 using mapping_type = typename layout_type::template mapping<extents_type>;
43 using element_type = ElementType;
45 using index_type = typename extents_type::index_type;
46 using size_type = typename extents_type::size_type;
47 using rank_type = typename extents_type::rank_type;
50
51 [[nodiscard]] static constexpr auto rank() noexcept -> rank_type { return extents_type::rank(); }
52
53 [[nodiscard]] static constexpr auto rank_dynamic() noexcept -> rank_type { return extents_type::rank_dynamic(); }
54
55 [[nodiscard]] static constexpr auto static_extent(rank_type r) noexcept -> size_t
56 {
57 return Extents::static_extent(r);
58 }
59
60 [[nodiscard]] constexpr auto extent(rank_type r) const noexcept -> index_type { return extents().extent(r); }
61
62 // Constructor (1)
63 constexpr mdspan()
66 : _ptr()
67 , _map()
68 , _acc()
69 {
70 }
71
72 // Constructor (2)
73 template <typename... OtherIndexTypes>
76 and ((sizeof...(OtherIndexTypes) == rank()) || (sizeof...(OtherIndexTypes) == rank_dynamic()))
78 explicit constexpr mdspan(data_handle_type ptr, OtherIndexTypes... exts)
79 : _ptr(etl::move(ptr))
80 , _map(extents_type(static_cast<index_type>(etl::move(exts))...))
81 {
82 }
83
84 // Constructor (3)
85 template <typename OtherIndexType, size_t N>
88 and (N == rank() or N == rank_dynamic())
90 explicit(N != rank_dynamic()) constexpr mdspan(data_handle_type p, span<OtherIndexType, N> exts)
91 : _ptr(etl::move(p))
93 , _acc()
94 {
95 }
96
97 // Constructor (4)
98 template <typename OtherIndexType, size_t N>
101 and (N == rank() or N == rank_dynamic())
103 explicit(N != rank_dynamic()) constexpr mdspan(data_handle_type p, array<OtherIndexType, N> const& exts)
104 : _ptr(etl::move(p))
106 , _acc()
107 {
108 }
109
110 // Constructor (5)
111 constexpr mdspan(data_handle_type ptr, extents_type const& ext)
113 : _ptr(etl::move(ptr))
114 , _map(ext)
115 {
116 }
117
118 // Constructor (6)
119 constexpr mdspan(data_handle_type ptr, mapping_type const& m)
121 : _ptr(etl::move(ptr))
122 , _map(m)
123 {
124 }
125
126 // Constructor (7)
127 constexpr mdspan(data_handle_type ptr, mapping_type const& m, accessor_type const& a)
128 : _ptr(etl::move(ptr))
129 , _map(m)
130 , _acc(a)
131 {
132 }
133
134 template <typename OtherElement, typename OtherExtents, typename OtherLayout, typename OtherAccessor>
137 explicit(
141 : _ptr(other.data_handle())
142 , _map(other.mapping())
148
149 constexpr mdspan(mdspan const& rhs) = default;
150 constexpr mdspan(mdspan&& rhs) = default; // NOLINT(performance-noexcept-move-constructor)
151
152 template <typename... OtherIndexTypes>
153 requires(
156 and sizeof...(OtherIndexTypes) == rank()
157 )
158 [[nodiscard]] constexpr auto operator()(OtherIndexTypes... indices) const -> reference
159 {
160 auto const idx = static_cast<etl::size_t>(_map(extents_type::index_cast(etl::move(indices))...));
161 return _acc.access(_ptr, idx);
162 }
163
164#if defined(__cpp_multidimensional_subscript)
165 template <typename... OtherIndexTypes>
166 requires(
169 and sizeof...(OtherIndexTypes) == rank()
170 )
171 [[nodiscard]] constexpr auto operator[](OtherIndexTypes... indices) const -> reference
172 {
173 return (*this)(etl::move(indices)...);
174 }
175#endif
176
177 template <typename OtherIndexType>
179 [[nodiscard]] constexpr auto operator[](span<OtherIndexType, rank()> indices) const -> reference
180 {
181 return [&]<size_t... Is>(index_sequence<Is...> /*seq*/) -> reference {
182 return (*this)(indices[Is]...);
184 }
185
186 template <typename OtherIndexType>
188 [[nodiscard]] constexpr auto operator[](array<OtherIndexType, rank()> const& indices) const -> reference
189 {
190 return (*this)[etl::span{indices}];
191 }
192
193 [[nodiscard]] constexpr auto data_handle() const noexcept -> data_handle_type const& { return _ptr; }
194 [[nodiscard]] constexpr auto mapping() const noexcept -> mapping_type const& { return _map; }
195 [[nodiscard]] constexpr auto accessor() const noexcept -> accessor_type const& { return _acc; }
196
197 [[nodiscard]] constexpr auto extents() const noexcept -> extents_type const& { return _map.extents(); }
198 [[nodiscard]] constexpr auto stride(rank_type r) const -> index_type { return _map.stride(r); }
199 [[nodiscard]] constexpr auto empty() const noexcept -> bool { return size() == size_type{}; }
200 [[nodiscard]] constexpr auto size() const noexcept -> size_type
201 {
202 return static_cast<size_type>(extents().fwd_prod_of_extents(rank()));
203 }
204
205 [[nodiscard]] constexpr auto is_unique() const -> bool { return _map.is_unique(); }
206 [[nodiscard]] constexpr auto is_exhaustive() const -> bool { return _map.is_exhaustive(); }
207 [[nodiscard]] constexpr auto is_strided() const -> bool { return _map.is_strided(); }
208
209 [[nodiscard]] static constexpr auto is_always_unique() -> bool { return mapping_type::is_always_unique(); }
210 [[nodiscard]] static constexpr auto is_always_exhaustive() -> bool { return mapping_type::is_always_exhaustive(); }
211 [[nodiscard]] static constexpr auto is_always_strided() -> bool { return mapping_type::is_always_strided(); }
212
213private:
214 TETL_NO_UNIQUE_ADDRESS data_handle_type _ptr; // NOLINT(modernize-use-default-member-init)
215 TETL_NO_UNIQUE_ADDRESS mapping_type _map; // NOLINT(modernize-use-default-member-init)
216 TETL_NO_UNIQUE_ADDRESS accessor_type _acc; // NOLINT(modernize-use-default-member-init)
217};
218
219template <typename CArray>
220 requires(is_array_v<CArray> && rank_v<CArray> == 1)
222
223template <typename Pointer>
226
227template <typename ElementType, typename... Integrals>
228 requires((is_convertible_v<Integrals, size_t> && ...) && sizeof...(Integrals) > 0)
229explicit mdspan(ElementType*, Integrals...) -> mdspan<ElementType, dextents<size_t, sizeof...(Integrals)>>;
230
231template <typename ElementType, typename IndexType, size_t... ExtentsPack>
233 -> mdspan<ElementType, extents<IndexType, ExtentsPack...>>;
234
235template <typename ElementType, typename MappingType>
236mdspan(ElementType*, MappingType const&)
238
239template <typename MappingType, typename AccessorType>
240mdspan(typename AccessorType::data_handle_type const&, MappingType const&, AccessorType const&) -> mdspan<
241 typename AccessorType::element_type,
242 typename MappingType::extents_type,
243 typename MappingType::layout_type,
244 AccessorType>;
245
246template <typename ElementType, typename Extents, typename LayoutPolicy, typename Container>
247struct mdarray;
248
249template <class ElementType, class Extents, class Layout, class Container>
251 typename decltype(declval<mdarray<ElementType, Extents, Layout, Container>>().to_mdspan())::element_type,
252 typename decltype(declval<mdarray<ElementType, Extents, Layout, Container>>().to_mdspan())::extens_type,
253 typename decltype(declval<mdarray<ElementType, Extents, Layout, Container>>().to_mdspan())::layout_type,
254 typename decltype(declval<mdarray<ElementType, Extents, Layout, Container>>().to_mdspan())::accessor_type>;
255
256} // namespace etl
257
258#endif // TETL_MDSPAN_MDSPAN_HPP
#define TETL_NO_UNIQUE_ADDRESS
Definition attributes.hpp:41
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
Definition adjacent_find.hpp:8
constexpr bool is_constructible_v
Definition is_constructible.hpp:24
mdspan(CArray &) -> mdspan< remove_all_extents_t< CArray >, extents< size_t, extent_v< CArray, 0 > > >
auto declval() noexcept -> add_rvalue_reference_t< T >
etl::make_integer_sequence< etl::size_t, Size > make_index_sequence
Definition index_sequence.hpp:15
constexpr size_t rank_v
Definition rank.hpp:25
remove_const_t< remove_volatile_t< T > > remove_cv_t
Definition remove_cv.hpp:22
constexpr bool is_default_constructible_v
Definition is_default_constructible.hpp:26
constexpr bool is_pointer_v
Definition is_pointer.hpp:30
etl::integer_sequence< etl::size_t, Ints... > index_sequence
Definition index_sequence.hpp:12
constexpr bool is_array_v
Definition is_array.hpp:26
constexpr bool is_nothrow_constructible_v
Definition is_nothrow_constructible.hpp:50
typename detail::dextents_impl< IndexType, etl::make_index_sequence< Rank > >::type dextents
Definition extents.hpp:196
constexpr bool is_convertible_v
Definition is_convertible.hpp:46
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
A container that encapsulates fixed size arrays.
Definition array.hpp:48
Definition default_accessor.hpp:12
ElementType & reference
Definition default_accessor.hpp:15
ElementType * data_handle_type
Definition default_accessor.hpp:16
Definition extents.hpp:22
Definition layout.hpp:13
Definition mdarray.hpp:25
Definition mdspan.hpp:38
static constexpr auto is_always_unique() -> bool
Definition mdspan.hpp:209
typename extents_type::rank_type rank_type
Definition mdspan.hpp:47
constexpr mdspan(data_handle_type ptr, mapping_type const &m)
Definition mdspan.hpp:119
static constexpr auto rank() noexcept -> rank_type
Definition mdspan.hpp:51
constexpr auto accessor() const noexcept -> accessor_type const &
Definition mdspan.hpp:195
typename extents_type::index_type index_type
Definition mdspan.hpp:45
constexpr mdspan(data_handle_type ptr, extents_type const &ext)
Definition mdspan.hpp:111
constexpr auto stride(rank_type r) const -> index_type
Definition mdspan.hpp:198
extents_type extents_type
Definition mdspan.hpp:39
default_accessor< element_type > accessor_type
Definition mdspan.hpp:41
OtherAccessor const & other
Definition mdspan.hpp:142
constexpr mdspan()
Definition mdspan.hpp:63
typename accessor_type::data_handle_type data_handle_type
Definition mdspan.hpp:48
typename accessor_type::reference reference
Definition mdspan.hpp:49
layout_type layout_type
Definition mdspan.hpp:40
constexpr auto mapping() const noexcept -> mapping_type const &
Definition mdspan.hpp:194
element_type element_type
Definition mdspan.hpp:43
constexpr mdspan(data_handle_type ptr, mapping_type const &m, accessor_type const &a)
Definition mdspan.hpp:127
explicit(N !=rank_dynamic()) const expr mdspan(data_handle_type p
constexpr auto extent(rank_type r) const noexcept -> index_type
Definition mdspan.hpp:60
span< OtherIndexType, N > exts
Definition mdspan.hpp:92
constexpr auto empty() const noexcept -> bool
Definition mdspan.hpp:199
typename extents_type::size_type size_type
Definition mdspan.hpp:46
constexpr auto data_handle() const noexcept -> data_handle_type const &
Definition mdspan.hpp:193
constexpr auto operator[](span< OtherIndexType, rank()> indices) const -> reference
Definition mdspan.hpp:179
static constexpr auto rank_dynamic() noexcept -> rank_type
Definition mdspan.hpp:53
static constexpr auto is_always_exhaustive() -> bool
Definition mdspan.hpp:210
constexpr auto is_unique() const -> bool
Definition mdspan.hpp:205
constexpr mdspan(mdspan &&rhs)=default
constexpr auto is_strided() const -> bool
Definition mdspan.hpp:207
constexpr auto is_exhaustive() const -> bool
Definition mdspan.hpp:206
static constexpr auto static_extent(rank_type r) noexcept -> size_t
Definition mdspan.hpp:55
typename layout_type::template mapping< extents_type > mapping_type
Definition mdspan.hpp:42
remove_cv_t< element_type > value_type
Definition mdspan.hpp:44
constexpr auto extents() const noexcept -> extents_type const &
Definition mdspan.hpp:197
constexpr auto size() const noexcept -> size_type
Definition mdspan.hpp:200
static constexpr auto is_always_strided() -> bool
Definition mdspan.hpp:211
constexpr mdspan(mdspan const &rhs)=default
If Type is an array type, provides the member constant value equal to the number of dimensions of the...
Definition rank.hpp:16
A non-owning view over a contiguous sequence of objects.
Definition span.hpp:83