Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
StringArrayData.cc
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/* StringArrayData.cc (C) 2000-2023 */
9/* */
10/* Data of type 'UniqueArray<String>'. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
15
16#include "arcane/utils/NotSupportedException.h"
17#include "arcane/utils/NotImplementedException.h"
18#include "arcane/utils/ArgumentException.h"
19#include "arcane/utils/TraceInfo.h"
20#include "arcane/utils/ITraceMng.h"
21#include "arcane/utils/IHashAlgorithm.h"
22#include "arcane/utils/Array.h"
23#include "arcane/utils/ArrayShape.h"
24
25#include "arcane/core/datatype/DataAllocationInfo.h"
26#include "arcane/core/datatype/DataStorageTypeInfo.h"
27#include "arcane/core/datatype/DataStorageBuildInfo.h"
28#include "arcane/core/datatype/DataTypeTraits.h"
29
30#include "arcane/impl/SerializedData.h"
31#include "arcane/impl/DataStorageFactory.h"
32
33#include "arcane/core/ISerializer.h"
34#include "arcane/core/IDataVisitor.h"
35
36#include "arcane/core/internal/IDataInternal.h"
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
41namespace Arcane
42{
43
44/*---------------------------------------------------------------------------*/
45/*---------------------------------------------------------------------------*/
46
50class StringArrayData
52, public IArrayDataT<String>
53{
55 class Impl;
56 friend class Impl;
57
58 public:
59
60 typedef String DataType;
61 typedef StringArrayData ThatClass;
62 typedef IArrayDataT<String> DataInterfaceType;
63
64 public:
65
66 explicit StringArrayData(ITraceMng* trace);
67 explicit StringArrayData(const DataStorageBuildInfo& dsbi);
68 StringArrayData(const StringArrayData& rhs);
69 ~StringArrayData() override;
70
71 public:
72
73 Integer dimension() const override { return 1; }
74 Integer multiTag() const override { return 0; }
75 eDataType dataType() const override { return DataTypeTraitsT<DataType>::type(); }
76 void serialize(ISerializer* sbuf, IDataOperation* operation) override;
77 void serialize(ISerializer* sbuf, Int32ConstArrayView ids, IDataOperation* operation) override;
78 Array<DataType>& value() override { return m_value; }
79 const Array<DataType>& value() const override { return m_value; }
80 ConstArrayView<DataType> view() const override { return m_value; }
81 ArrayView<DataType> view() override { return m_value; }
82 void resize(Integer new_size) override { m_value.resize(new_size); }
83 IData* clone() override { return cloneTrue(); }
84 IData* cloneEmpty() override { return cloneTrueEmpty(); };
85 Ref<IData> cloneRef() override { return makeRef(cloneTrue()); }
87 DataInterfaceType* cloneTrue() override { return _cloneTrue(); }
88 DataInterfaceType* cloneTrueEmpty() override { return _cloneTrueEmpty(); }
90 {
91 auto* d = _cloneTrue();
92 return makeRef(d);
93 }
95 {
96 auto* d = _cloneTrueEmpty();
97 return makeRef(d);
98 }
99 DataStorageTypeInfo storageTypeInfo() const override;
100 void fillDefault() override { m_value.fill(String()); }
101 void setName(const String& name) override;
102 Ref<ISerializedData> createSerializedDataRef(bool use_basic_type) const override;
104 void assignSerializedData(const ISerializedData* sdata) override;
105 void copy(const IData* data) override;
106 void swapValues(IData* data) override;
107 void computeHash(IHashAlgorithm* algo, ByteArray& output) const override;
108 void computeHash(DataHashInfo& hash_info) const;
109 ArrayShape shape() const override { return {}; }
110 void setShape(const ArrayShape&) override {}
111 void setAllocationInfo(const DataAllocationInfo& v) override { m_allocation_info = v; }
112 DataAllocationInfo allocationInfo() const override { return m_allocation_info; }
113 void visit(IArrayDataVisitor* visitor) override
114 {
115 visitor->applyVisitor(this);
116 }
117 void visit(IDataVisitor* visitor) override
118 {
119 visitor->applyDataVisitor((IArrayData*)this);
120 }
121 void visitScalar(IScalarDataVisitor* visitor) override;
122 void visitArray(IArrayDataVisitor* visitor) override;
123 void visitArray2(IArray2DataVisitor* visitor) override;
124
125 public:
126
127 IArrayDataInternalT<DataType>* _internal() override { return m_internal; }
128 IDataInternal* _commonInternal() override { return m_internal; }
129
130 public:
131
132 static DataStorageTypeInfo staticStorageTypeInfo();
133
134 private:
135
137 ITraceMng* m_trace;
138 IArrayDataInternalT<String>* m_internal;
139 DataAllocationInfo m_allocation_info;
140
141 private:
142
143 ThatClass* _cloneTrue() const { return new ThatClass(*this); }
144 ThatClass* _cloneTrueEmpty() const { return new ThatClass(m_trace); }
145};
146
147/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149
150// TODO: To be merged with the common implementation in ArrayData.
152: public IArrayDataInternalT<String>
153{
154 public:
155
156 using String = DataType;
157
158 explicit Impl(StringArrayData* p)
159 : m_p(p)
160 {}
161
162 public:
163
164 void reserve(Integer new_capacity) override { m_p->m_value.reserve(new_capacity); }
165 Array<DataType>& _internalDeprecatedValue() override { return m_p->m_value; }
166 Integer capacity() const override { return m_p->m_value.capacity(); }
167 void shrink() const override { m_p->m_value.shrink(); }
168 void resize(Integer new_size) override { m_p->m_value.resize(new_size); }
169 void dispose() override { m_p->m_value.dispose(); }
170 void computeHash(DataHashInfo& hash_info) override
171 {
172 m_p->computeHash(hash_info);
173 }
174
175 private:
176
177 StringArrayData* m_p;
178};
179
180/*---------------------------------------------------------------------------*/
181/*---------------------------------------------------------------------------*/
182
183StringArrayData::
184StringArrayData(const DataStorageBuildInfo& dsbi)
185: m_trace(dsbi.traceMng())
186, m_internal(new Impl(this))
187{
188}
189
190StringArrayData::
191StringArrayData(ITraceMng* trace)
192: m_trace(trace)
193, m_internal(new Impl(this))
194{}
195
196StringArrayData::
197StringArrayData(const StringArrayData& rhs)
198: m_value(rhs.m_value)
199, m_trace(rhs.m_trace)
200, m_internal(new Impl(this))
201, m_allocation_info(rhs.m_allocation_info)
202{}
203
204StringArrayData::
205~StringArrayData()
206{
207 delete m_internal;
208}
209
210/*---------------------------------------------------------------------------*/
211/*---------------------------------------------------------------------------*/
212
213/*---------------------------------------------------------------------------*/
214/*---------------------------------------------------------------------------*/
215
216DataStorageTypeInfo StringArrayData::
217staticStorageTypeInfo()
218{
220 Int32 nb_basic_type = 0;
221 Int32 dimension = 2;
222 Int32 multi_tag = 1;
223 return DataStorageTypeInfo(bdt, nb_basic_type, dimension, multi_tag);
224}
225
226/*---------------------------------------------------------------------------*/
227/*---------------------------------------------------------------------------*/
228
230storageTypeInfo() const
231{
232 return staticStorageTypeInfo();
233}
234
235/*---------------------------------------------------------------------------*/
236/*---------------------------------------------------------------------------*/
237
239createSerializedDataRef(bool use_basic_type) const
240{
241 ARCANE_UNUSED(use_basic_type);
242
243 // Positions the dimensions and calculates the necessary size to serialize
244 // the values
245 Int64 needed_memory = 0;
246 Int64 nb_element = m_value.largeSize();
247 Int64UniqueArray dimensions(nb_element);
248 for (Integer i = 0; i < nb_element; ++i) {
249 Span<const Byte> str(m_value[i].bytes());
250 Int64 len = str.size();
251 needed_memory += len;
252 dimensions[i] = len;
253 }
254 Int64 nb_base_element = needed_memory;
255 auto sd = arcaneCreateSerializedDataRef(DT_Byte, needed_memory, 2, nb_element,
256 nb_base_element, true, dimensions);
257 sd->allocateMemory(needed_memory);
258
259 // Copies the values into the allocated array
260 Span<Byte> svalues = sd->writableBytes();
261 {
262 Int64 index = 0;
263 for (Integer i = 0; i < nb_element; ++i) {
264 Span<const Byte> str(m_value[i].bytes());
265 Int64 len = str.size();
266 // TODO: use a copy method directly.
267 for (Int64 z = 0; z < len; ++z)
268 svalues[index + z] = str[z];
269 index += len;
270 }
271 }
272 return sd;
273}
274
275/*---------------------------------------------------------------------------*/
276/*---------------------------------------------------------------------------*/
277
280{
281 if (sdata->baseDataType() != DT_Byte)
282 throw ArgumentException(A_FUNCINFO, "Bad serialized type");
283
284 sdata->allocateMemory(sdata->memorySize());
285 //m_trace->info() << " ALLOC ARRAY STRING ptr=" << (void*)byte_values.begin();
286}
287
288/*---------------------------------------------------------------------------*/
289/*---------------------------------------------------------------------------*/
290
293{
294 if (sdata->baseDataType() != DT_Byte)
295 throw ArgumentException(A_FUNCINFO, "Bad serialized type");
296
297 Span<const Byte> byte_values = sdata->constBytes();
298 //m_trace->info() << " ASSIGN ARRAY STRING ptr=" << (void*)byte_values.begin();
299 Int64ConstArrayView dimensions = sdata->extents();
300 Integer nb_element = dimensions.size();
301 m_value.resize(nb_element);
302 Int64 index = 0;
303 for (Integer i = 0; i < nb_element; ++i) {
304 Int64 len = dimensions[i];
305 Span<const Byte> v(&byte_values[index], len);
306 m_value[i] = String(v);
307 index += len;
308 //m_trace->info() << " READ STRING i=" << i << " v=" << m_value[i] << " len=" << len << " index=" << index;
309 }
310}
311
312/*---------------------------------------------------------------------------*/
313/*---------------------------------------------------------------------------*/
314
316serialize(ISerializer* sbuf, IDataOperation* operation)
317{
318 // TODO: test this method.
319 ARCANE_UNUSED(operation);
320
321 ISerializer::eMode mode = sbuf->mode();
322 if (mode == ISerializer::ModeReserve) {
323 Integer size = m_value.size();
324 sbuf->reserveInteger(1);
325 for (Integer z = 0; z < size; ++z)
326 sbuf->reserve(m_value[z]);
327 }
328 else if (mode == ISerializer::ModePut) {
329 Integer size = m_value.size();
330 sbuf->putInteger(size);
331 for (Integer z = 0; z < size; ++z)
332 sbuf->put(m_value[z]);
333 }
334 else if (mode == ISerializer::ModeGet) {
335 switch (sbuf->readMode()) {
337 Integer size = sbuf->getInteger();
338 m_value.resize(size);
339 for (Integer z = 0; z < size; ++z)
340 sbuf->get(m_value[z]);
341 } break;
343 ARCANE_THROW(NotSupportedException, "option 'ReadAdd'");
344 }
345 }
346 else
347 ARCANE_THROW(NotSupportedException, "Invalid mode");
348}
349
350/*---------------------------------------------------------------------------*/
351/*---------------------------------------------------------------------------*/
352
355{
356 ARCANE_UNUSED(sbuf);
357 ARCANE_UNUSED(ids);
358 ARCANE_UNUSED(operation);
359 throw NotImplementedException(A_FUNCINFO);
360}
361
362/*---------------------------------------------------------------------------*/
363/*---------------------------------------------------------------------------*/
364
366setName(const String& name)
367{
368 ARCANE_UNUSED(name);
369}
370
371/*---------------------------------------------------------------------------*/
372/*---------------------------------------------------------------------------*/
373
375computeHash(IHashAlgorithm* algo, ByteArray& output) const
376{
377 // For now, serialization must be used.
378 // TODO remove unnecessary serialization.
380 s->computeHash(algo, output);
381}
382
383/*---------------------------------------------------------------------------*/
384/*---------------------------------------------------------------------------*/
385
387computeHash(DataHashInfo& hash_info) const
388{
389 hash_info.setVersion(2);
390 IHashAlgorithmContext* context = hash_info.context();
391 for (const String& x : m_value)
392 context->updateHash(asBytes(x.bytes()));
393}
394
395/*---------------------------------------------------------------------------*/
396/*---------------------------------------------------------------------------*/
397
399copy(const IData* data)
400{
401 const auto* true_data = dynamic_cast<const DataInterfaceType*>(data);
402 if (!true_data)
403 ARCANE_THROW(ArgumentException, "Can not cast 'IData' to 'StringArrayData'");
404 m_value.copy(true_data->view());
405}
406
407/*---------------------------------------------------------------------------*/
408/*---------------------------------------------------------------------------*/
409
411swapValues(IData* data)
412{
413 auto* true_data = dynamic_cast<ThatClass*>(data);
414 if (!true_data)
415 ARCANE_THROW(ArgumentException, "Can not cast 'IData' to 'StringArrayData'");
416 m_value.swap(true_data->m_value);
417}
418
419/*---------------------------------------------------------------------------*/
420/*---------------------------------------------------------------------------*/
421
424{
425 ARCANE_THROW(NotSupportedException, "Can not visit scalar data with array data");
426}
427
428/*---------------------------------------------------------------------------*/
429/*---------------------------------------------------------------------------*/
430
433{
434 visitor->applyVisitor(this);
435}
436
437/*---------------------------------------------------------------------------*/
438/*---------------------------------------------------------------------------*/
439
442{
443 ARCANE_THROW(NotSupportedException, "Can not visit array2 data with array data");
444}
445
446/*---------------------------------------------------------------------------*/
447/*---------------------------------------------------------------------------*/
448
449extern "C++" void
450registerStringArrayDataFactory(IDataFactoryMng* dfm)
451{
453}
454
455/*---------------------------------------------------------------------------*/
456/*---------------------------------------------------------------------------*/
457
458} // End namespace Arcane
459
460/*---------------------------------------------------------------------------*/
461/*---------------------------------------------------------------------------*/
#define ARCANE_THROW(exception_class,...)
Macro for throwing an exception with formatting.
#define ARCCORE_DEFINE_REFERENCE_COUNTED_INCLASS_METHODS()
Macro to define methods managing counters of references.
Array shape.
Definition ArrayShape.h:42
Modifiable view of an array of type T.
Base class for 1D data vectors.
Constant view of an array of type T.
constexpr Integer size() const noexcept
Number of elements in the array.
Information on data allocation.
Information for calculating data hash.
Information to construct an instance of 'IData'.
static void registerDataFactory(IDataFactoryMng *dfm)
Registers a factory for the data DataType in dfm.
Type information for a data container.
Interface of the visitor pattern for a 2D array data item.
Interface for an array data of type T.
Interface of a 1D array data item of type T.
Definition IData.h:300
Interface of the visitor pattern for an array data item.
Interface of a 1D array data item.
Definition IData.h:282
Interface of the data factory manager.
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
Context for calculating a hash incrementally.
virtual void updateHash(Span< const std::byte > input)=0
Adds the array input to the calculated hash.
Interface of a hashing algorithm.
Interface of the visitor pattern for a scalar data item.
Interface of a serialized data.
virtual eDataType baseDataType() const =0
Data type.
virtual Int64 memorySize() const =0
Indicates the number of bytes that must be allocated to store or read the data.
virtual void allocateMemory(Int64 size)=0
Allocates an array to hold the serialized elements.
virtual Int64ConstArrayView extents() const =0
Array containing the number of elements for each dimension.
virtual Span< const Byte > constBytes() const =0
Serialized values.
virtual void reserve(eBasicDataType dt, Int64 n)=0
Reserves memory for n objects of type dt.
virtual Integer getInteger()=0
Retrieve a size.
virtual void putInteger(Integer value)=0
Add the integer value.
virtual void put(Span< const Real > values)=0
Add the array values.
virtual eReadMode readMode() const =0
Read mode.
virtual eMode mode() const =0
Current operating mode.
@ ReadReplace
Replace current elements with those read.
@ ReadAdd
Add those read to the current elements.
virtual void get(ArrayView< Real > values)=0
Retrieve the array values.
Reference to an instance.
Thread-safe implementation of a reference counter.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
Definition Span.h:327
View of an array of elements of type T.
Definition Span.h:635
Integer capacity() const override
Capacity allocated by the container.
void dispose() override
Clears the container and frees allocated memory.
void resize(Integer new_size) override
Resizes the container.
void reserve(Integer new_capacity) override
Reserves memory for new_capacity elements.
void computeHash(DataHashInfo &hash_info) override
Calculates the hash of the data.
void shrink() const override
Frees additional allocated memory.
Array< DataType > & _internalDeprecatedValue() override
Container associated with the data.
Ref< IData > cloneEmptyRef() override
Clone the data but without elements.
void copy(const IData *data) override
Copy the data data into the current instance.
ArrayView< DataType > view() override
View on the data.
void setName(const String &name) override
Sets the name of the data (internal).
Ref< ISerializedData > createSerializedDataRef(bool use_basic_type) const override
Serialize the data.
void resize(Integer new_size) override
Resize the data.
Ref< IData > cloneRef() override
Clone the data.
void setShape(const ArrayShape &) override
Sets the array shape.
Integer multiTag() const override
Multi-tag. 0 if not multiple, 1 if multiple, 2 if multiple for MultiArray variables (obsolete).
void visitArray(IArrayDataVisitor *visitor) override
Apply the visitor to the data.
IArrayDataInternalT< DataType > * _internal() override
void assignSerializedData(const ISerializedData *sdata) override
Assign the serialized values sdata to the data.
Ref< DataInterfaceType > cloneTrueRef() override
Clone the data.
eDataType dataType() const override
Data type.
DataStorageTypeInfo storageTypeInfo() const override
Information about the data container type.
void visitScalar(IScalarDataVisitor *visitor) override
Apply the visitor to the data.
Integer dimension() const override
Dimension. 0 for a scalar, 1 for a mono-dim array, 2 for a bi-dim array.
ArrayShape shape() const override
Array shape for a 1D or 2D data item.
Array< DataType > & value() override
Data value.
void computeHash(IHashAlgorithm *algo, ByteArray &output) const override
Compute a hash key on this data.
void setAllocationInfo(const DataAllocationInfo &v) override
Sets the allocation information.
void visit(IArrayDataVisitor *visitor) override
Applies the visitor to the data.
IData * cloneEmpty() override
Clone the data but without elements. The created instance must be destroyed by the 'delete' operator.
void swapValues(IData *data) override
Swap the values of data with those of the instance.
void visit(IDataVisitor *visitor) override
Applies the visitor to the data.
UniqueArray< DataType > m_value
Data.
void serialize(ISerializer *sbuf, IDataOperation *operation) override
Serializes the data by applying the operation.
DataAllocationInfo allocationInfo() const override
Allocation information.
DataInterfaceType * cloneTrueEmpty() override
Clone the data but without elements.
DataInterfaceType * cloneTrue() override
Clone the data.
void fillDefault() override
Fills the data with its default value.
Ref< DataInterfaceType > cloneTrueEmptyRef() override
Clone the data but without elements.
void allocateBufferForSerializedData(ISerializedData *sdata) override
Allocate memory to read the serialized values sdata.
IDataInternal * _commonInternal() override
void visitArray2(IArray2DataVisitor *visitor) override
Apply the visitor to the data.
IData * clone() override
Clone the data. The created instance must be destroyed by the 'delete' operator.
const Array< DataType > & value() const override
Constant data value.
ConstArrayView< DataType > view() const override
Constant view on the data.
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Ref< ISerializedData > arcaneCreateSerializedDataRef(eDataType data_type, Int64 memory_size, Integer nb_dim, Int64 nb_element, Int64 nb_base_element, bool is_multi_size, Int64ConstArrayView dimensions)
Creates serialized data.
UniqueArray< Int64 > Int64UniqueArray
Dynamic 1D array of 64-bit integers.
Definition UtilsTypes.h:339
std::int64_t Int64
Signed integer type of 64 bits.
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
eBasicDataType
Type of a basic data item.
ConstArrayView< Int64 > Int64ConstArrayView
C equivalent of a 1D array of 64-bit integers.
Definition UtilsTypes.h:480
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.
eDataType
Data type.
Definition DataTypes.h:41
@ DT_Byte
Byte data type.
Definition DataTypes.h:42
std::int32_t Int32
Signed integer type of 32 bits.