Arcane  4.2.1.0
Developer documentation
Loading...
Searching...
No Matches
Span2.h
Go to the documentation of this file.
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Span2.h (C) 2000-2026 */
9/* */
10/* View of a 2D array whose dimensions use Int64. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_SPAN2_H
13#define ARCCORE_BASE_SPAN2_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arccore/base/TraceInfo.h"
20
21#include <type_traits>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arcane
27{
28
29namespace detail
30{
31 // To indicate that Span2<T>::view() returns an Array2View
32 // and Span2<const T>::view() returns a ConstArray2View.
33 template <typename T>
35 {
36 public:
37
38 using view_type = Array2View<T>;
39 };
40 template <typename T>
41 class View2TypeT<const T>
42 {
43 public:
44
45 using view_type = ConstArray2View<T>;
46 };
47} // namespace detail
48
49/*---------------------------------------------------------------------------*/
50/*---------------------------------------------------------------------------*/
51
64template <typename T, typename SizeType, SizeType Extent1, SizeType Extent2>
66{
68
69 public:
70
71 using ElementType = T;
72 using element_type = ElementType;
73 using value_type = typename std::remove_cv<ElementType>::type;
74 using index_type = SizeType;
75 using difference_type = SizeType;
76 using size_type = SizeType;
77 using pointer = ElementType*;
78 using const_pointer = typename std::add_const<ElementType*>::type;
79 using reference = ElementType&;
80 using const_reference = const ElementType&;
81 using view_type = typename detail::View2TypeT<ElementType>::view_type;
82
84 template <typename X>
85 using is_same_const_type = std::enable_if_t<std::is_same_v<X, T> || std::is_same_v<std::add_const_t<X>, T>>;
86
87 public:
88
90 ARCCORE_HOST_DEVICE Span2Impl(pointer ptr, SizeType dim1_size, SizeType dim2_size)
91 : m_ptr(ptr)
92 , m_dim1_size(dim1_size)
93 , m_dim2_size(dim2_size)
94 {}
95
96 Span2Impl() = default;
97 // Constructor from a ConstArrayView. This is only allowed
98 // if T is const.
99 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
100 Span2Impl(const ConstArray2View<X>& from)
101 : m_ptr(from.data())
102 , m_dim1_size(from.dim1Size())
103 , m_dim2_size(from.dim2Size())
104 {}
105 // For a Span<const T>, we are allowed to construct from a Span<T>
106 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
107 ARCCORE_HOST_DEVICE Span2Impl(const Span2<X>& from)
108 : m_ptr(from.data())
109 , m_dim1_size(from.dim1Size())
110 , m_dim2_size(from.dim2Size())
111 {}
112
113 public:
114
116 constexpr ARCCORE_HOST_DEVICE SizeType dim1Size() const { return m_dim1_size; }
118 constexpr ARCCORE_HOST_DEVICE SizeType dim2Size() const { return m_dim2_size; }
120 constexpr ARCCORE_HOST_DEVICE SizeType totalNbElement() const { return m_dim1_size * m_dim2_size; }
121
122 public:
123
124 constexpr ARCCORE_HOST_DEVICE SpanImpl<ElementType, SizeType> operator[](SizeType i) const
125 {
126 ARCCORE_CHECK_AT(i, m_dim1_size);
127 return SpanImpl<ElementType, SizeType>(m_ptr + (m_dim2_size * i), m_dim2_size);
128 }
129
130 constexpr ARCCORE_HOST_DEVICE SpanImpl<ElementType, SizeType> operator()(SizeType i) const
131 {
132 ARCCORE_CHECK_AT(i, m_dim1_size);
133 return SpanImpl<ElementType, SizeType>(m_ptr + (m_dim2_size * i), m_dim2_size);
134 }
135
136 constexpr ARCCORE_HOST_DEVICE reference operator()(SizeType i, SizeType j) const
137 {
138 ARCCORE_CHECK_AT2(i, j, m_dim1_size, m_dim2_size);
139 return m_ptr[(m_dim2_size * i) + j];
140 }
141
142#ifdef ARCCORE_HAS_MULTI_SUBSCRIPT
143 constexpr ARCCORE_HOST_DEVICE reference operator[](SizeType i, SizeType j) const
144 {
145 ARCCORE_CHECK_AT2(i, j, m_dim1_size, m_dim2_size);
146 return m_ptr[(m_dim2_size * i) + j];
147 }
148#endif
149
151 constexpr ARCCORE_HOST_DEVICE ElementType item(SizeType i, SizeType j) const
152 {
153 ARCCORE_CHECK_AT2(i, j, m_dim1_size, m_dim2_size);
154 return m_ptr[(m_dim2_size * i) + j];
155 }
156
158 constexpr ARCCORE_HOST_DEVICE ElementType setItem(SizeType i, SizeType j, const ElementType& value)
159 {
160 ARCCORE_CHECK_AT2(i, j, m_dim1_size, m_dim2_size);
161 m_ptr[(m_dim2_size * i) + j] = value;
162 }
163
164 public:
165
169 constexpr view_type smallView()
170 {
171 Integer s1 = arccoreCheckArraySize(m_dim1_size);
172 Integer s2 = arccoreCheckArraySize(m_dim2_size);
173 return view_type(m_ptr, s1, s2);
174 }
175
180 {
181 Integer s1 = arccoreCheckArraySize(m_dim1_size);
182 Integer s2 = arccoreCheckArraySize(m_dim2_size);
183 return ConstArrayView<value_type>(m_ptr, s1, s2);
184 }
185
186 public:
187
189 constexpr ElementType* unguardedBasePointer() { return m_ptr; }
190
192 constexpr ARCCORE_HOST_DEVICE ElementType* data() { return m_ptr; }
193
195 constexpr ARCCORE_HOST_DEVICE const ElementType* data() const { return m_ptr; }
196
197 public:
198
200 template <typename X, SizeType XExtent1, SizeType XExtent2, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
201 friend bool operator==(const ThatClass& lhs, const Span2Impl<X, SizeType, XExtent1, XExtent2>& rhs)
202 {
203 return Arcane::Impl::areEqual2D(rhs, lhs);
204 }
205
206 template <typename X, SizeType XExtent1, SizeType XExtent2, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
207 friend bool operator!=(const ThatClass& lhs, const Span2Impl<X, SizeType, XExtent1, XExtent2>& rhs)
208 {
209 return !Arcane::Impl::areEqual2D(rhs, lhs);
210 }
211
212 template <SizeType XExtent1, SizeType XExtent2>
213 friend bool operator==(const ThatClass& lhs, const Span2Impl<T, SizeType, XExtent1, XExtent2>& rhs)
214 {
215 return Arcane::Impl::areEqual2D(rhs, lhs);
216 }
217
218 template <SizeType XExtent1, SizeType XExtent2>
219 friend bool operator!=(const ThatClass& lhs, const Span2Impl<T, SizeType, XExtent1, XExtent2>& rhs)
220 {
221 return !Arcane::Impl::areEqual2D(rhs, lhs);
222 }
223
224 protected:
225
226 ElementType* m_ptr = nullptr;
227 SizeType m_dim1_size = 0;
228 SizeType m_dim2_size = 0;
229};
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233
242template <class T, Int32 Extent1, Int32 Extent2>
244: public Span2Impl<T, Int32, Extent1, Extent2>
245{
246 friend class Span2<T>;
247
248 public:
249
250 using ThatClass = SmallSpan2<T, Extent1, Extent2>;
252 using size_type = Int32;
253 using value_type = typename BaseClass::value_type;
254 using pointer = typename BaseClass::pointer;
255 using BaseClass::operator();
256 using BaseClass::operator[];
257 using ElementType = typename BaseClass::ElementType;
258
259 private:
260
261 using BaseClass::m_dim1_size;
262 using BaseClass::m_dim2_size;
263 using BaseClass::m_ptr;
264
265 public:
266
268 ARCCORE_HOST_DEVICE SmallSpan2(pointer ptr, Int32 dim1_size, Int32 dim2_size)
269 : BaseClass(ptr, dim1_size, dim2_size)
270 {}
271
272 ARCCORE_HOST_DEVICE SmallSpan2()
273 : BaseClass()
274 {}
275
277 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
278 {}
279 // Constructor from a ConstArrayView. This is only allowed
280 // if T is const.
281 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
282 SmallSpan2(const ConstArray2View<X>& from)
283 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
284 {}
285 // For a Span<const T>, we are allowed to construct from a Span<T>
286 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
287 ARCCORE_HOST_DEVICE SmallSpan2(const SmallSpan2<X>& from)
288 : BaseClass(from.data(), from.dim1Size(), from.dim2Size())
289 {}
290
291 public:
292
293 ARCCORE_HOST_DEVICE SmallSpan<ElementType> operator[](Int32 i) const
294 {
295 ARCCORE_CHECK_AT(i, m_dim1_size);
296 return SmallSpan<ElementType>(m_ptr + (m_dim2_size * i), m_dim2_size);
297 }
298
299 ARCCORE_HOST_DEVICE SmallSpan<ElementType> operator()(Int32 i) const
300 {
301 ARCCORE_CHECK_AT(i, m_dim1_size);
302 return SmallSpan<ElementType>(m_ptr + (m_dim2_size * i), m_dim2_size);
303 }
304};
305
306/*---------------------------------------------------------------------------*/
307/*---------------------------------------------------------------------------*/
308
317template <class T, Int64 Extent1, Int64 Extent2>
318class Span2
319: public Span2Impl<T, Int64, Extent1, Extent2>
320{
321 public:
322
323 using ThatClass = Span2<T, Extent1, Extent2>;
325 using size_type = Int64;
326 using value_type = typename BaseClass::value_type;
327 using pointer = typename BaseClass::pointer;
328 using BaseClass::operator();
329 using BaseClass::operator[];
330 using ElementType = typename BaseClass::ElementType;
331
332 private:
333
334 using BaseClass::m_dim1_size;
335 using BaseClass::m_dim2_size;
336 using BaseClass::m_ptr;
337
338 public:
339
341 ARCCORE_HOST_DEVICE Span2(pointer ptr, Int64 dim1_size, Int64 dim2_size)
342 : BaseClass(ptr, dim1_size, dim2_size)
343 {}
344
345 ARCCORE_HOST_DEVICE Span2()
346 : BaseClass()
347 {}
348
350 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
351 {}
352 // Constructor from a ConstArrayView. This is only allowed
353 // if T is const.
354 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
355 Span2(const ConstArray2View<X>& from)
356 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
357 {}
358
360 Span2(const SmallSpan2<T>& from)
361 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
362 {}
363
364 // For a Span<const T>, we are allowed to construct from a Span<T>
365 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
366 ARCCORE_HOST_DEVICE Span2(const Span2<X>& from)
367 : BaseClass(from)
368 {}
369
370 // For a Span<const T>, we are allowed to construct from a SmallSpan<T>
371 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
372 ARCCORE_HOST_DEVICE Span2(const SmallSpan2<X>& from)
373 : BaseClass(from.data(), from.dim1Size(), from.dim2Size())
374 {}
375
376 public:
377
378 ARCCORE_HOST_DEVICE Span<ElementType> operator[](Int64 i) const
379 {
380 ARCCORE_CHECK_AT(i, m_dim1_size);
381 return Span<ElementType>(m_ptr + (m_dim2_size * i), m_dim2_size);
382 }
383
384 ARCCORE_HOST_DEVICE Span<ElementType> operator()(Int64 i) const
385 {
386 ARCCORE_CHECK_AT(i, m_dim1_size);
387 return Span<ElementType>(m_ptr + (m_dim2_size * i), m_dim2_size);
388 }
389};
390
391/*---------------------------------------------------------------------------*/
392/*---------------------------------------------------------------------------*/
393
394} // namespace Arcane
395
396/*---------------------------------------------------------------------------*/
397/*---------------------------------------------------------------------------*/
398
399#endif
Declarations of types for the 'base' component of Arccore.
Types and functions associated with the classes Array2View and ConstArray2View.
Constant view of an array of type T.
View for a 2D array whose size is an 'Int32'.
Definition Span2.h:245
__host__ __device__ SmallSpan2()
Creates an empty 2D view.
Definition Span2.h:272
__host__ __device__ SmallSpan2(pointer ptr, Int32 dim1_size, Int32 dim2_size)
Creates a 2D view of dimension [dim1_size][dim2_size].
Definition Span2.h:268
SmallSpan2(const Array2View< value_type > &from)
Copy constructor from another view.
Definition Span2.h:276
friend bool operator!=(const ThatClass &lhs, const Span2Impl< T, SizeType, XExtent1, XExtent2 > &rhs)
Inequality operator.
Definition Span2.h:219
constexpr __host__ __device__ ElementType * data()
Definition Span2.h:192
friend bool operator==(const ThatClass &lhs, const Span2Impl< X, SizeType, XExtent1, XExtent2 > &rhs)
Equality operator (valid if T is const but not X).
Definition Span2.h:201
friend bool operator!=(const ThatClass &lhs, const Span2Impl< X, SizeType, XExtent1, XExtent2 > &rhs)
Inequality operator (valid if T is const but not X).
Definition Span2.h:207
friend bool operator==(const ThatClass &lhs, const Span2Impl< T, SizeType, XExtent1, XExtent2 > &rhs)
Equality operator.
Definition Span2.h:213
constexpr __host__ __device__ Int32 dim2Size() const
Definition Span2.h:118
constexpr ElementType * unguardedBasePointer()
Pointer to the allocated memory.
Definition Span2.h:189
std::enable_if_t< std::is_same_v< X, T >||std::is_same_v< std::add_const_t< X >, T > > is_same_const_type
Indicates if an 'X' or 'const X' can be converted to a 'T'.
Definition Span2.h:85
constexpr ConstArrayView< value_type > constSmallView() const
Constant view of this view.
Definition Span2.h:179
__host__ __device__ Span2Impl(pointer ptr, SizeType dim1_size, SizeType dim2_size)
Creates a 2D view of dimension [dim1_size][dim2_size].
Definition Span2.h:90
constexpr view_type smallView()
Constant view of this view.
Definition Span2.h:169
constexpr __host__ __device__ ElementType item(SizeType i, SizeType j) const
Value of the element [i][j].
Definition Span2.h:151
Span2Impl()=default
Creates an empty 2D view.
constexpr __host__ __device__ SizeType totalNbElement() const
Total number of elements.
Definition Span2.h:120
constexpr __host__ __device__ ElementType setItem(SizeType i, SizeType j, const ElementType &value)
Positions the element [i][j] at value.
Definition Span2.h:158
constexpr __host__ __device__ const ElementType * data() const
Pointer to the allocated memory.
Definition Span2.h:195
constexpr __host__ __device__ Int32 dim1Size() const
Definition Span2.h:116
View for a 2D array whose size is an 'Int64'.
Definition Span2.h:320
__host__ __device__ Span2(pointer ptr, Int64 dim1_size, Int64 dim2_size)
Creates a 2D view of dimension [dim1_size][dim2_size].
Definition Span2.h:341
__host__ __device__ Span2()
Creates an empty 2D view.
Definition Span2.h:345
Span2(const SmallSpan2< T > &from)
Copy constructor from a 'SmallSpan'.
Definition Span2.h:360
Span2(const Array2View< value_type > &from)
Copy constructor from another view.
Definition Span2.h:349
View of an array of elements of type T.
Definition Span.h:189
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
constexpr __host__ __device__ Integer arccoreCheckArraySize(unsigned long long size)
Checks that size can be converted into an 'Integer' to serve as an array size. If possible,...
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
std::int32_t Int32
Signed integer type of 32 bits.
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.).
Definition rapidjson.h:416