Arcane  4.1.15.0
User documentation
Loading...
Searching...
No Matches
RealArrayVariant.h
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/* RealArrayVariant.h (C) 2000-2024 */
9/* */
10/* Variant that can contain the types ConstArrayView, Real2, and Real3. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#ifndef ARCANE_DATATYPE_REALARRAYVARIANT_H
14#define ARCANE_DATATYPE_REALARRAYVARIANT_H
15/*---------------------------------------------------------------------------*/
16/*---------------------------------------------------------------------------*/
17
18#include "arcane/utils/Array.h"
19#include "arcane/utils/ArrayView.h"
20#include "arcane/utils/Real2.h"
21#include "arcane/utils/Real3.h"
22#if defined(ARCANE_HAS_ACCELERATOR_API)
23#include "arcane/utils/NumArray.h"
24#endif
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane
30{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35/*!
36 * \internal
37 * \brief Variant that can contain the types ConstArrayView, Real2, and Real3.
38 */
39class RealArrayVariant
40{
41 public:
42
43 static const Integer MAX_SIZE = 9;
44
45 RealArrayVariant() = default;
46 RealArrayVariant(UniqueArray<Real> v)
47 : RealArrayVariant(v.constView())
48 {}
49 RealArrayVariant(ConstArrayView<Real> v)
50 {
51 _setValue(v.data(), v.size());
52 }
53#if defined(ARCANE_HAS_ACCELERATOR_API)
54 template <typename LayoutType>
55 RealArrayVariant(const NumArray<Real, MDDim1, LayoutType>& v)
56 : RealArrayVariant(v.mdspan())
57 {}
58 template <typename LayoutType>
59 RealArrayVariant(MDSpan<Real, MDDim1, LayoutType> v)
60 {
61 _setValue(v.to1DSpan().data(), v.extent0());
62 }
63 template <typename LayoutType>
65 {
66 _setValue(v.to1DSpan().data(), v.extent0());
67 }
68#endif
69 RealArrayVariant(Real2 r)
70 {
71 _setValue(reinterpret_cast<Real*>(&r), 2);
72 }
73 RealArrayVariant(Real3 r)
74 {
75 _setValue(reinterpret_cast<Real*>(&r), 3);
76 }
77
78 RealArrayVariant& operator=(const RealArrayVariant& rhs) = default;
79 RealArrayVariant& operator=(ConstArrayView<Real> v)
80 {
81 _setValue(v.data(), v.size());
82 return (*this);
83 }
84 RealArrayVariant& operator=(Real2 r)
85 {
86 _setValue(reinterpret_cast<Real*>(&r), 2);
87 return (*this);
88 }
89 RealArrayVariant& operator=(Real3 r)
90 {
91 _setValue(reinterpret_cast<Real*>(&r), 3);
92 return (*this);
93 }
94
95 Real& operator[](Integer index)
96 {
97 ARCANE_ASSERT(index < m_nb_value, ("Index out of range"));
98 return m_value[index];
99 }
100 Real operator[](Integer index) const
101 {
102 ARCANE_ASSERT(index < m_nb_value, ("Index out of range"));
103 return m_value[index];
104 }
105 Real& operator()(Integer index)
106 {
107 ARCANE_ASSERT(index < m_nb_value, ("Index out of range"));
108 return m_value[index];
109 }
110 Real operator()(Integer index) const
111 {
112 ARCANE_ASSERT(index < m_nb_value, ("Index out of range"));
113 return m_value[index];
114 }
115
116 Int32 size() const { return m_nb_value; }
117 Real* data() { return m_value; }
118 const Real* data() const { return m_value; }
119 operator ConstArrayView<Real>() const { return ConstArrayView<Real>(m_nb_value, m_value); }
120 operator Real2() const { return Real2(m_value[0], m_value[1]); }
121 operator Real3() const { return Real3(m_value[0], m_value[1], m_value[2]); }
122
123#if defined(ARCANE_HAS_ACCELERATOR_API)
124 operator NumArray<Real, MDDim1>() const
125 {
126 NumArray<Real, MDDim1> v(m_nb_value);
127 for (Integer i = 0, n = m_nb_value; i < n; ++i)
128 v[i] = m_value[i];
129 return v;
130 }
131#endif
132
133 private:
134
135 Real m_value[MAX_SIZE];
136 Int32 m_nb_value = 0;
137
138 private:
139
140 void _setValue(const Real* v, Int32 nb_value)
141 {
142 m_nb_value = nb_value;
143 ARCANE_ASSERT(nb_value <= MAX_SIZE, ("Size is too large"));
144 for (Integer i = 0; i < nb_value; ++i)
145 m_value[i] = v[i];
146 }
147};
148
149/*---------------------------------------------------------------------------*/
150/*---------------------------------------------------------------------------*/
151
152} // namespace Arcane
153
154/*---------------------------------------------------------------------------*/
155/*---------------------------------------------------------------------------*/
156
157#endif
ConstArrayView< T > constView() const
Constant view of this array.
Constant view of an array of type T.
constexpr const_pointer data() const noexcept
Pointer to the allocated memory.
constexpr Integer size() const noexcept
Number of elements in the array.
Base class for multidimensional views.
constexpr ExtentIndexType extent0() const
Value of the first dimension.
Multi-dimensional arrays for numerical types accessible on accelerators.
Class managing a 2-dimensional real vector.
Definition Real2.h:122
Class managing a 3-dimensional real vector.
Definition Real3.h:132
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
Definition Span.h:539
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
std::int32_t Int32
Signed integer type of 32 bits.