Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Array2Data.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/* Array2Data.h (C) 2000-2025 */
9/* */
10/* Data of type 'Array2'. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_IMPL_INTERNAL_ARRAY2DATA_H
13#define ARCANE_IMPL_INTERNAL_ARRAY2DATA_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19#include "arcane/utils/ArrayShape.h"
20#include "arcane/utils/String.h"
21#include "arcane/utils/IDataCompressor.h"
22#include "arcane/utils/Array2.h"
23#include "arcane/utils/MemoryView.h"
24#include "arcane/utils/Ref.h"
25#include "arcane/utils/FatalErrorException.h"
26
27#include "arcane/core/IData.h"
28#include "arcane/core/IDataVisitor.h"
29#include "arcane/core/internal/IDataInternal.h"
30#include "arcane/core/datatype/DataAllocationInfo.h"
31#include "arcane/core/datatype/DataTypeTraits.h"
32#include "arcane/core/datatype/DataStorageTypeInfo.h"
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37namespace Arcane
38{
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
46template <class DataType>
47class Array2DataT
49, public IArray2DataT<DataType>
50{
52 class Impl;
53 friend class Impl;
54
55 public:
56
57 typedef Array2DataT<DataType> ThatClass;
58 typedef IArray2DataT<DataType> DataInterfaceType;
59
60 public:
61
62 explicit Array2DataT(ITraceMng* trace);
63 explicit Array2DataT(const DataStorageBuildInfo& dsbi);
64 Array2DataT(const Array2DataT<DataType>& rhs);
65 ~Array2DataT() override;
66
67 public:
68
69 Integer dimension() const override { return 2; }
70 Integer multiTag() const override { return 0; }
71 eDataType dataType() const override { return DataTypeTraitsT<DataType>::type(); }
72 void serialize(ISerializer* sbuf, IDataOperation* operation) override;
73 void serialize(ISerializer* sbuf, Int32ConstArrayView ids, IDataOperation* operation) override;
74 Array2<DataType>& value() override { return m_value; }
75 const Array2<DataType>& value() const override { return m_value; }
76 Array2View<DataType> view() override { return m_value; }
77 ConstArray2View<DataType> view() const override { return m_value; }
78 void resize(Integer new_size) override;
79 IData* clone() override { return _cloneTrue(); }
80 IData* cloneEmpty() override { return _cloneTrueEmpty(); };
81 Ref<IData> cloneRef() override { return makeRef(cloneTrue()); }
83 DataStorageTypeInfo storageTypeInfo() const override;
84 DataInterfaceType* cloneTrue() override { return _cloneTrue(); }
85 DataInterfaceType* cloneTrueEmpty() override { return _cloneTrueEmpty(); }
87 {
88 auto* d = _cloneTrue();
89 return makeRef(d);
90 }
92 {
93 auto* d = _cloneTrueEmpty();
94 return makeRef(d);
95 }
96 void fillDefault() override;
97 void setName(const String& name) override;
98 Ref<ISerializedData> createSerializedDataRef(bool use_basic_type) const override;
100 void assignSerializedData(const ISerializedData* sdata) override;
101 void copy(const IData* data) override;
102 void swapValues(IData* data) override;
103 void computeHash(IHashAlgorithm* algo, ByteArray& output) const override;
104 void computeHash(DataHashInfo& hash_algo) const;
105 ArrayShape shape() const override { return m_shape; }
106 void setShape(const ArrayShape& new_shape) override { m_shape = new_shape; }
107 void setAllocationInfo(const DataAllocationInfo& v) override;
108 DataAllocationInfo allocationInfo() const override { return m_allocation_info; }
109
110 void visit(IArray2DataVisitor* visitor)
111 {
112 visitor->applyVisitor(this);
113 }
114 void visit(IDataVisitor* visitor) override
115 {
116 visitor->applyDataVisitor(this);
117 }
119 {
120 ARCANE_THROW(NotSupportedException, "Can not visit scalar data with array2 data");
121 }
123 {
124 ARCANE_THROW(NotSupportedException, "Can not visit array data with array2 data");
125 }
126 void visitArray2(IArray2DataVisitor* visitor) override
127 {
128 visitor->applyVisitor(this);
129 }
130
131 public:
132
133 void swapValuesDirect(ThatClass* true_data);
134 void changeAllocator(const MemoryAllocationOptions& alloc_info);
135
136 public:
137
138 IArray2DataInternalT<DataType>* _internal() override { return m_internal; }
139 IDataInternal* _commonInternal() override { return m_internal; }
140
141 public:
142
143 static DataStorageTypeInfo staticStorageTypeInfo();
144
145 private:
146
148 ITraceMng* m_trace;
150 ArrayShape m_shape;
151 DataAllocationInfo m_allocation_info;
152
153 private:
154
155 IArray2DataT<DataType>* _cloneTrue() const { return new ThatClass(*this); }
156 IArray2DataT<DataType>* _cloneTrueEmpty() const { return new ThatClass(m_trace); }
157};
158
159/*---------------------------------------------------------------------------*/
160/*---------------------------------------------------------------------------*/
161
162template <typename DataType>
163class Array2DataT<DataType>::Impl
164: public IArray2DataInternalT<DataType>
166{
167 public:
168
169 explicit Impl(Array2DataT<DataType>* p)
170 : m_p(p)
171 {}
172
173 public:
174
175 void reserve(Integer new_capacity) override { m_p->m_value.reserve(new_capacity); }
176 void resizeOnlyDim1(Int32 new_dim1_size) override
177 {
178 m_p->m_value.resize(new_dim1_size, m_p->m_value.dim2Size());
179 }
180 void resize(Int32 new_dim1_size, Int32 new_dim2_size) override
181 {
182 if (new_dim1_size < 0)
183 ARCANE_FATAL("Bad value '{0}' for dim1_size", new_dim1_size);
184 if (new_dim2_size < 0)
185 ARCANE_FATAL("Bad value '{0}' for dim2_size", new_dim2_size);
186 // This method is called if the second dimension is modified.
187 // In this case, it invalidates the old shape value.
188 bool need_reshape = false;
189 if (new_dim2_size != m_p->m_value.dim2Size())
190 need_reshape = true;
191 m_p->m_value.resize(new_dim1_size, new_dim2_size);
192 if (need_reshape) {
193 m_p->m_shape.setNbDimension(1);
194 m_p->m_shape.setDimension(0, new_dim2_size);
195 }
196 }
197 Array2<DataType>& _internalDeprecatedValue() override { return m_p->m_value; }
198 void shrink() const override { m_p->m_value.shrink(); }
200 {
201 IDataCompressor* compressor = buf.m_compressor;
202 if (!compressor)
203 return false;
204 Span<const DataType> values = m_p->m_value.to1DSpan();
205 Span<const std::byte> bytes = asBytes(values);
206 compressor->compress(bytes, buf.m_buffer);
207 buf.m_original_dim1_size = m_p->m_value.dim1Size();
208 buf.m_original_dim2_size = m_p->m_value.dim2Size();
209 m_p->m_value.clear();
210 m_p->m_value.shrink();
211 return true;
212 }
214 {
215 IDataCompressor* compressor = buf.m_compressor;
216 if (!compressor)
217 return false;
218 m_p->m_value.resize(buf.m_original_dim1_size, buf.m_original_dim2_size);
219 Span<DataType> values = m_p->m_value.to1DSpan();
220 compressor->decompress(buf.m_buffer, asWritableBytes(values));
221 return true;
222 }
223
225 {
226 Array2View<DataType> value = m_p->view();
227 Int32 dim1_size = value.dim1Size();
228 Int32 dim2_size = value.dim2Size();
229 DataStorageTypeInfo storage_info = m_p->storageTypeInfo();
230 Int32 nb_basic_element = storage_info.nbBasicElement();
231 Int32 datatype_size = basicDataTypeSize(storage_info.basicDataType()) * nb_basic_element;
232 return makeMutableMemoryView(value.data(), datatype_size * dim2_size, dim1_size);
233 }
234 Int32 extent0() const override
235 {
236 return m_p->view().dim1Size();
237 }
238 INumericDataInternal* numericData() override { return this; }
239 void changeAllocator(const MemoryAllocationOptions& v) override { m_p->changeAllocator(v); }
240 void computeHash(DataHashInfo& hash_info) override
241 {
242 m_p->computeHash(hash_info);
243 }
244 IMemoryAllocator* memoryAllocator() const override { return m_p->m_value.allocator(); }
245
246 private:
247
248 Array2DataT<DataType>* m_p = nullptr;
249};
250
251/*---------------------------------------------------------------------------*/
252/*---------------------------------------------------------------------------*/
253
254} // namespace Arcane
255
256/*---------------------------------------------------------------------------*/
257/*---------------------------------------------------------------------------*/
258
259#endif
#define ARCANE_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
#define ARCCORE_DEFINE_REFERENCE_COUNTED_INCLASS_METHODS()
Macro to define methods managing counters of references.
Int32 extent0() const override
Number of elements in the first dimension.
Definition Array2Data.h:234
void resizeOnlyDim1(Int32 new_dim1_size) override
Resizes the container only in dimension 1.
Definition Array2Data.h:176
Array2< DataType > & _internalDeprecatedValue() override
Container associated with the data.
Definition Array2Data.h:197
MutableMemoryView memoryView() override
Memory view of the data.
Definition Array2Data.h:224
IMemoryAllocator * memoryAllocator() const override
Allocator used for the data.
Definition Array2Data.h:244
bool decompressAndFill(DataCompressionBuffer &buf) override
Decompresses the data and fills the data values.
Definition Array2Data.h:213
void resize(Int32 new_dim1_size, Int32 new_dim2_size) override
Resizes the container.
Definition Array2Data.h:180
void changeAllocator(const MemoryAllocationOptions &v) override
Changes the variable's allocator.
Definition Array2Data.h:239
bool compressAndClear(DataCompressionBuffer &buf) override
Compresses the data and frees the associated memory.
Definition Array2Data.h:199
void computeHash(DataHashInfo &hash_info) override
Calculates the hash of the data.
Definition Array2Data.h:240
void reserve(Integer new_capacity) override
Reserves memory for new_capacity elements.
Definition Array2Data.h:175
INumericDataInternal * numericData() override
Generic interface for numeric data (nullptr if the data is not numeric).
Definition Array2Data.h:238
void shrink() const override
Frees additional allocated memory.
Definition Array2Data.h:198
ConstArray2View< DataType > view() const override
Constant view on the data.
Definition Array2Data.h:77
void fillDefault() override
Fills the data with its default value.
void assignSerializedData(const ISerializedData *sdata) override
Assign the serialized values sdata to the data.
void resize(Integer new_size) override
Resize the data.
IData * clone() override
Clone the data. The created instance must be destroyed by the 'delete' operator.
Definition Array2Data.h:79
const Array2< DataType > & value() const override
Data value.
Definition Array2Data.h:75
void visitArray2(IArray2DataVisitor *visitor) override
Apply the visitor to the data.
Definition Array2Data.h:126
UniqueArray2< DataType > m_value
Data.
Definition Array2Data.h:147
DataAllocationInfo allocationInfo() const override
Allocation information.
Definition Array2Data.h:108
IData * cloneEmpty() override
Clone the data but without elements. The created instance must be destroyed by the 'delete' operator.
Definition Array2Data.h:80
Ref< DataInterfaceType > cloneTrueRef() override
Clone the data.
Definition Array2Data.h:86
IDataInternal * _commonInternal() override
Definition Array2Data.h:139
void computeHash(IHashAlgorithm *algo, ByteArray &output) const override
Compute a hash key on this data.
Ref< IData > cloneRef() override
Clone the data.
Definition Array2Data.h:81
void serialize(ISerializer *sbuf, IDataOperation *operation) override
Serializes the data by applying the operation.
Ref< IData > cloneEmptyRef() override
Clone the data but without elements.
Definition Array2Data.h:82
Ref< ISerializedData > createSerializedDataRef(bool use_basic_type) const override
Serialize the data.
DataInterfaceType * cloneTrueEmpty() override
Clone the data but without elements.
Definition Array2Data.h:85
void allocateBufferForSerializedData(ISerializedData *sdata) override
Allocate memory to read the serialized values sdata.
DataStorageTypeInfo storageTypeInfo() const override
Information about the data container type.
IArray2DataInternalT< DataType > * _internal() override
Definition Array2Data.h:138
Array2< DataType > & value() override
Data value.
Definition Array2Data.h:74
Integer dimension() const override
Dimension. 0 for a scalar, 1 for a mono-dim array, 2 for a bi-dim array.
Definition Array2Data.h:69
DataInterfaceType * cloneTrue() override
Clone the data.
Definition Array2Data.h:84
Ref< DataInterfaceType > cloneTrueEmptyRef() override
Clone the data but without elements.
Definition Array2Data.h:91
void swapValues(IData *data) override
Swap the values of data with those of the instance.
Array2View< DataType > view() override
View on the data.
Definition Array2Data.h:76
void visit(IDataVisitor *visitor) override
Applies the visitor to the data.
Definition Array2Data.h:114
Integer multiTag() const override
Multi-tag. 0 if not multiple, 1 if multiple, 2 if multiple for MultiArray variables (obsolete).
Definition Array2Data.h:70
void visitScalar(IScalarDataVisitor *) override
Apply the visitor to the data.
Definition Array2Data.h:118
void setAllocationInfo(const DataAllocationInfo &v) override
Sets the allocation information.
void setShape(const ArrayShape &new_shape) override
Sets the array shape.
Definition Array2Data.h:106
eDataType dataType() const override
Data type.
Definition Array2Data.h:71
ArrayShape shape() const override
Array shape for a 1D or 2D data item.
Definition Array2Data.h:105
void visitArray(IArrayDataVisitor *) override
Apply the visitor to the data.
Definition Array2Data.h:122
void copy(const IData *data) override
Copy the data data into the current instance.
void setName(const String &name) override
Sets the name of the data (internal).
Class representing a classic 2D array.
Array shape.
Definition ArrayShape.h:42
Information on data allocation.
Class to manage data compression/decompression.
Information for calculating data hash.
Information to construct an instance of 'IData'.
Type information for a data container.
Interface for a two-dimensional array data of type T.
Interface of a bi-dimensional array data item of type T.
Definition IData.h:374
Interface of the visitor pattern for a 2D array data item.
Interface of the visitor pattern for an array data item.
Interface of a service for compressing/decompressing data.
virtual void decompress(Span< const std::byte > compressed_values, Span< std::byte > values)=0
Decompresses the data compressed_values and stores it in values.
virtual void compress(Span< const std::byte > values, Array< std::byte > &compressed_values)=0
Compresses the data values and stores it in compressed_values.
Internal part of IData.
Interface of an operation on a data.
Interface of the visitor pattern for a data item.
Interface of a data item.
Definition IData.h:34
Interface of a hashing algorithm.
Interface for an 'IData' of a numeric type.
Interface of the visitor pattern for a scalar data item.
Interface of a serialized data.
Mutable view on a contiguous memory region containing fixed-size elements.
Reference to an instance.
Thread-safe implementation of a reference counter.
View of an array of elements of type T.
Definition Span.h:635
2D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
MutableMemoryView makeMutableMemoryView(void *ptr, Int32 datatype_size, Int64 nb_element)
Creates a mutable memory view.
Definition MemoryView.cc:26
Int32 Integer
Type representing an integer.
Array< Byte > ByteArray
Dynamic one-dimensional array of characters.
Definition UtilsTypes.h:121
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
Definition UtilsTypes.h:482
Impl::SpanTypeFromSize< conststd::byte, SizeType >::SpanType asBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of non-modifiable bytes.
Definition Span.h:1032
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
Impl::SpanTypeFromSize< std::byte, SizeType >::SpanType asWritableBytes(const SpanImpl< DataType, SizeType, Extent > &s)
Converts the view into an array of modifiable bytes.
Definition Span.h:1068
eDataType
Data type.
Definition DataTypes.h:41
Integer basicDataTypeSize(eBasicDataType type)
Size of data type type.
std::int32_t Int32
Signed integer type of 32 bits.