Arcane  4.1.12.0
User 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-2025 */
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
52/*!
53 * \ingroup Collection
54 *
55 * \brief View for a 2D array.
56 *
57 * Like any view, an instance of this class is only valid as long as
58 * the container it originated from does not change its number of elements.
59 * The view is non-modifiable if the template argument is of type 'const T'.
60 * This class allows accessing and using an array of elements of type \a T
61 * in the same way as a standard C array. \a SizeType is the type used
62 * to store the number of elements in the array. This can be 'Int32' or 'Int64'.
63 */
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
83 //! Indicates if an 'X' or 'const X' can be converted to a 'T'
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
89 //! Creates a 2D view of dimension [\a dim1_size][\a dim2_size]
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 //! Creates an empty 2D view.
96 ARCCORE_HOST_DEVICE Span2Impl()
97 : m_ptr(nullptr)
98 , m_dim1_size(0)
99 , m_dim2_size(0)
100 {}
101 // Constructor from a ConstArrayView. This is only allowed
102 // if T is const.
103 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
104 Span2Impl(const ConstArray2View<X>& from)
105 : m_ptr(from.data())
106 , m_dim1_size(from.dim1Size())
107 , m_dim2_size(from.dim2Size())
108 {}
109 // For a Span<const T>, we are allowed to construct from a Span<T>
110 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
111 ARCCORE_HOST_DEVICE Span2Impl(const Span2<X>& from)
112 : m_ptr(from.data())
113 , m_dim1_size(from.dim1Size())
114 , m_dim2_size(from.dim2Size())
115 {}
116
117 public:
118
119 //! Number of elements in the first dimension
120 constexpr ARCCORE_HOST_DEVICE SizeType dim1Size() const { return m_dim1_size; }
121 //! Number of elements in the second dimension
122 constexpr ARCCORE_HOST_DEVICE SizeType dim2Size() const { return m_dim2_size; }
123 //! Total number of elements.
124 constexpr ARCCORE_HOST_DEVICE SizeType totalNbElement() const { return m_dim1_size * m_dim2_size; }
125
126 public:
127
128 constexpr ARCCORE_HOST_DEVICE SpanImpl<ElementType, SizeType> operator[](SizeType i) const
129 {
130 ARCCORE_CHECK_AT(i, m_dim1_size);
131 return SpanImpl<ElementType, SizeType>(m_ptr + (m_dim2_size * i), m_dim2_size);
132 }
133
134 constexpr ARCCORE_HOST_DEVICE SpanImpl<ElementType, SizeType> operator()(SizeType i) const
135 {
136 ARCCORE_CHECK_AT(i, m_dim1_size);
137 return SpanImpl<ElementType, SizeType>(m_ptr + (m_dim2_size * i), m_dim2_size);
138 }
139
140 constexpr ARCCORE_HOST_DEVICE reference operator()(SizeType i, SizeType j) const
141 {
142 ARCCORE_CHECK_AT2(i, j, m_dim1_size, m_dim2_size);
143 return m_ptr[(m_dim2_size * i) + j];
144 }
145
146#ifdef ARCCORE_HAS_MULTI_SUBSCRIPT
147 constexpr ARCCORE_HOST_DEVICE reference operator[](SizeType i, SizeType j) const
148 {
149 ARCCORE_CHECK_AT2(i, j, m_dim1_size, m_dim2_size);
150 return m_ptr[(m_dim2_size * i) + j];
151 }
152#endif
153
154 //! Value of the element [\a i][\a j]
155 constexpr ARCCORE_HOST_DEVICE ElementType item(SizeType i, SizeType j) const
156 {
157 ARCCORE_CHECK_AT2(i, j, m_dim1_size, m_dim2_size);
158 return m_ptr[(m_dim2_size * i) + j];
159 }
160
161 //! Positions the element [\a i][\a j] at \a value
162 constexpr ARCCORE_HOST_DEVICE ElementType setItem(SizeType i, SizeType j, const ElementType& value)
163 {
164 ARCCORE_CHECK_AT2(i, j, m_dim1_size, m_dim2_size);
165 m_ptr[(m_dim2_size * i) + j] = value;
166 }
167
168 public:
169
170 /*!
171 * \brief Constant view of this view.
172 */
173 constexpr view_type smallView()
174 {
175 Integer s1 = arccoreCheckArraySize(m_dim1_size);
176 Integer s2 = arccoreCheckArraySize(m_dim2_size);
177 return view_type(m_ptr, s1, s2);
178 }
179
180 /*!
181 * \brief Constant view of this view.
182 */
184 {
185 Integer s1 = arccoreCheckArraySize(m_dim1_size);
186 Integer s2 = arccoreCheckArraySize(m_dim2_size);
187 return ConstArrayView<value_type>(m_ptr, s1, s2);
188 }
189
190 public:
191
192 //! Pointer to the allocated memory.
193 constexpr ElementType* unguardedBasePointer() { return m_ptr; }
194
195 //! Pointer to the allocated memory.
196 constexpr ARCCORE_HOST_DEVICE ElementType* data() { return m_ptr; }
197
198 //! Pointer to the allocated memory.
199 constexpr ARCCORE_HOST_DEVICE const ElementType* data() const { return m_ptr; }
200
201 public:
202
203 //! Equality operator (valid if T is const but not X)
204 template <typename X, SizeType XExtent1, SizeType XExtent2, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
205 friend bool operator==(const ThatClass& lhs, const Span2Impl<X, SizeType, XExtent1, XExtent2>& rhs)
206 {
207 return impl::areEqual2D(rhs, lhs);
208 }
209 //! Inequality operator (valid if T is const but not X)
210 template <typename X, SizeType XExtent1, SizeType XExtent2, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
211 friend bool operator!=(const ThatClass& lhs, const Span2Impl<X, SizeType, XExtent1, XExtent2>& rhs)
212 {
213 return !impl::areEqual2D(rhs, lhs);
214 }
215 //! Equality operator
216 template <SizeType XExtent1, SizeType XExtent2>
217 friend bool operator==(const ThatClass& lhs, const Span2Impl<T, SizeType, XExtent1, XExtent2>& rhs)
218 {
219 return impl::areEqual2D(rhs, lhs);
220 }
221 //! Inequality operator
222 template <SizeType XExtent1, SizeType XExtent2>
223 friend bool operator!=(const ThatClass& lhs, const Span2Impl<T, SizeType, XExtent1, XExtent2>& rhs)
224 {
225 return !impl::areEqual2D(rhs, lhs);
226 }
227
228 protected:
229
230 ElementType* m_ptr;
231 SizeType m_dim1_size;
232 SizeType m_dim2_size;
233};
234
235/*---------------------------------------------------------------------------*/
236/*---------------------------------------------------------------------------*/
237
238/*!
239 * \ingroup Collection
240 *
241 * \brief View for a 2D array whose size is an 'Int32'
242 *
243 * Like any view, an instance of this class is only valid as long as
244 * the container it originated from does not change its number of elements.
245 */
246template <class T, Int32 Extent1, Int32 Extent2>
248: public Span2Impl<T, Int32, Extent1, Extent2>
249{
250 friend class Span2<T>;
251
252 public:
253
254 using ThatClass = SmallSpan2<T, Extent1, Extent2>;
256 using size_type = Int32;
257 using value_type = typename BaseClass::value_type;
258 using pointer = typename BaseClass::pointer;
259 using BaseClass::operator();
260 using BaseClass::operator[];
261 using ElementType = typename BaseClass::ElementType;
262
263 private:
264
265 using BaseClass::m_dim1_size;
266 using BaseClass::m_dim2_size;
267 using BaseClass::m_ptr;
268
269 public:
270
271 //! Creates a 2D view of dimension [\a dim1_size][\a dim2_size]
272 ARCCORE_HOST_DEVICE SmallSpan2(pointer ptr, Int32 dim1_size, Int32 dim2_size)
273 : BaseClass(ptr, dim1_size, dim2_size)
274 {}
275 //! Creates an empty 2D view.
276 ARCCORE_HOST_DEVICE SmallSpan2()
277 : BaseClass()
278 {}
279 //! Copy constructor from another view
281 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
282 {}
283 // Constructor from a ConstArrayView. This is only allowed
284 // if T is const.
285 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
286 SmallSpan2(const ConstArray2View<X>& from)
287 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
288 {}
289 // For a Span<const T>, we are allowed to construct from a Span<T>
290 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
291 ARCCORE_HOST_DEVICE SmallSpan2(const SmallSpan2<X>& from)
292 : BaseClass(from.data(), from.dim1Size(), from.dim2Size())
293 {}
294
295 public:
296
297 ARCCORE_HOST_DEVICE SmallSpan<ElementType> operator[](Int32 i) const
298 {
299 ARCCORE_CHECK_AT(i, m_dim1_size);
300 return SmallSpan<ElementType>(m_ptr + (m_dim2_size * i), m_dim2_size);
301 }
302
303 ARCCORE_HOST_DEVICE SmallSpan<ElementType> operator()(Int32 i) const
304 {
305 ARCCORE_CHECK_AT(i, m_dim1_size);
306 return SmallSpan<ElementType>(m_ptr + (m_dim2_size * i), m_dim2_size);
307 }
308};
309
310/*---------------------------------------------------------------------------*/
311/*---------------------------------------------------------------------------*/
312
313/*!
314 * \ingroup Collection
315 *
316 * \brief View for a 2D array whose size is an 'Int64'
317 *
318 * Like any view, an instance of this class is only valid as long as
319 * the container it originated from does not change its number of elements.
320 */
321template <class T, Int64 Extent1, Int64 Extent2>
322class Span2
323: public Span2Impl<T, Int64, Extent1, Extent2>
324{
325 public:
326
327 using ThatClass = Span2<T, Extent1, Extent2>;
329 using size_type = Int64;
330 using value_type = typename BaseClass::value_type;
331 using pointer = typename BaseClass::pointer;
332 using BaseClass::operator();
333 using BaseClass::operator[];
334 using ElementType = typename BaseClass::ElementType;
335
336 private:
337
338 using BaseClass::m_dim1_size;
339 using BaseClass::m_dim2_size;
340 using BaseClass::m_ptr;
341
342 public:
343
344 //! Creates a 2D view of dimension [\a dim1_size][\a dim2_size]
345 ARCCORE_HOST_DEVICE Span2(pointer ptr, Int64 dim1_size, Int64 dim2_size)
346 : BaseClass(ptr, dim1_size, dim2_size)
347 {}
348 //! Creates an empty 2D view.
349 ARCCORE_HOST_DEVICE Span2()
350 : BaseClass()
351 {}
352 //! Copy constructor from another view
354 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
355 {}
356 // Constructor from a ConstArrayView. This is only allowed
357 // if T is const.
358 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
359 Span2(const ConstArray2View<X>& from)
360 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
361 {}
362
363 //! Copy constructor from a 'SmallSpan'
364 Span2(const SmallSpan2<T>& from)
365 : BaseClass(from.m_ptr, from.dim1Size(), from.dim2Size())
366 {}
367
368 // For a Span<const T>, we are allowed to construct from a Span<T>
369 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
370 ARCCORE_HOST_DEVICE Span2(const Span2<X>& from)
371 : BaseClass(from)
372 {}
373
374 // For a Span<const T>, we are allowed to construct from a SmallSpan<T>
375 template <typename X, typename = std::enable_if_t<std::is_same_v<X, value_type>>>
376 ARCCORE_HOST_DEVICE Span2(const SmallSpan2<X>& from)
377 : BaseClass(from.data(), from.dim1Size(), from.dim2Size())
378 {}
379
380 public:
381
382 ARCCORE_HOST_DEVICE Span<ElementType> operator[](Int64 i) const
383 {
384 ARCCORE_CHECK_AT(i, m_dim1_size);
385 return Span<ElementType>(m_ptr + (m_dim2_size * i), m_dim2_size);
386 }
387
388 ARCCORE_HOST_DEVICE Span<ElementType> operator()(Int64 i) const
389 {
390 ARCCORE_CHECK_AT(i, m_dim1_size);
391 return Span<ElementType>(m_ptr + (m_dim2_size * i), m_dim2_size);
392 }
393};
394
395/*---------------------------------------------------------------------------*/
396/*---------------------------------------------------------------------------*/
397
398} // namespace Arcane
399
400/*---------------------------------------------------------------------------*/
401/*---------------------------------------------------------------------------*/
402
403#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:249
__host__ __device__ SmallSpan2()
Creates an empty 2D view.
Definition Span2.h:276
__host__ __device__ SmallSpan2(pointer ptr, Int32 dim1_size, Int32 dim2_size)
Creates a 2D view of dimension [dim1_size][dim2_size].
Definition Span2.h:272
SmallSpan2(const Array2View< value_type > &from)
Copy constructor from another view.
Definition Span2.h:280
friend bool operator!=(const ThatClass &lhs, const Span2Impl< T, SizeType, XExtent1, XExtent2 > &rhs)
Inequality operator.
Definition Span2.h:223
constexpr __host__ __device__ ElementType * data()
Pointer to the allocated memory.
Definition Span2.h:196
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:205
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:211
friend bool operator==(const ThatClass &lhs, const Span2Impl< T, SizeType, XExtent1, XExtent2 > &rhs)
Equality operator.
Definition Span2.h:217
constexpr __host__ __device__ SizeType dim2Size() const
Number of elements in the second dimension.
Definition Span2.h:122
constexpr ElementType * unguardedBasePointer()
Pointer to the allocated memory.
Definition Span2.h:193
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:183
__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:173
constexpr __host__ __device__ ElementType item(SizeType i, SizeType j) const
Value of the element [i][j].
Definition Span2.h:155
constexpr __host__ __device__ SizeType totalNbElement() const
Total number of elements.
Definition Span2.h:124
__host__ __device__ Span2Impl()
Creates an empty 2D view.
Definition Span2.h:96
constexpr __host__ __device__ ElementType setItem(SizeType i, SizeType j, const ElementType &value)
Positions the element [i][j] at value.
Definition Span2.h:162
constexpr __host__ __device__ const ElementType * data() const
Pointer to the allocated memory.
Definition Span2.h:199
constexpr __host__ __device__ SizeType dim1Size() const
Number of elements in the first dimension.
Definition Span2.h:120
View for a 2D array whose size is an 'Int64'.
Definition Span2.h:324
__host__ __device__ Span2(pointer ptr, Int64 dim1_size, Int64 dim2_size)
Creates a 2D view of dimension [dim1_size][dim2_size].
Definition Span2.h:345
__host__ __device__ Span2()
Creates an empty 2D view.
Definition Span2.h:349
Span2(const SmallSpan2< T > &from)
Copy constructor from a 'SmallSpan'.
Definition Span2.h:364
Span2(const Array2View< value_type > &from)
Copy constructor from another view.
Definition Span2.h:353
View of an array of elements of type T.
Definition Span.h:191
-- 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.