12#ifndef ARCANE_UTILS_MULTIARRAY2_H
13#define ARCANE_UTILS_MULTIARRAY2_H
17#include "arcane/utils/Array.h"
18#include "arcane/utils/MultiArray2View.h"
58template <
typename DataType>
63 using ConstReferenceType =
typename UniqueArray<DataType>::ConstReferenceType;
64 using ThatClass = MultiArray2<DataType>;
68 MultiArray2() =
default;
77 MultiArray2(
const ThatClass& rhs) =
delete;
78 ThatClass& operator=(
const ThatClass& rhs) =
delete;
88 : m_buffer(do_clone ? rhs.m_buffer.clone() : rhs.m_buffer)
89 , m_indexes(do_clone ? rhs.m_indexes.clone() : rhs.m_indexes)
90 , m_sizes(do_clone ? rhs.m_sizes.clone() : rhs.m_sizes)
94 : m_buffer(aview.m_buffer)
95 , m_indexes(aview.m_indexes)
96 , m_sizes(aview.m_sizes)
99 explicit MultiArray2(
const MemoryAllocationOptions& allocation_options)
100 : m_buffer(allocation_options)
101 , m_indexes(allocation_options)
102 , m_sizes(allocation_options)
105 MultiArray2(
const MemoryAllocationOptions& allocation_options, ConstArrayView<Int32> sizes)
106 : MultiArray2(allocation_options)
113 ArrayView<DataType> operator[](
Integer i)
115 return ArrayView<DataType>(m_sizes[i], m_buffer.data() + (m_indexes[i]));
117 ConstArrayView<DataType> operator[](
Integer i)
const
119 return ConstArrayView<DataType>(m_sizes[i], m_buffer.data() + (m_indexes[i]));
141 return m_buffer[m_indexes[i] + j];
145 return m_buffer[m_indexes[i] + j];
149 return m_buffer.
setAt(m_indexes[i] + j, v);
187 return { m_buffer.smallSpan(), m_indexes, m_sizes };
193 return { m_buffer, m_indexes, m_sizes };
199 return { m_buffer.constSmallSpan(), m_indexes, m_sizes };
205 return m_buffer.view();
211 return m_buffer.constView();
217 if (new_sizes.
empty()) {
233 void _resize(ConstArrayView<Int32> ar)
239 for (
Integer i = 0; i < size1; ++i)
246 for (
Integer i = 0; i < size1; ++i)
247 if (m_sizes[i] != ar[i]) {
255 Integer old_size1 = m_indexes.size();
257 SharedArray<DataType> new_buffer(m_buffer.allocationOptions(), total_size);
260 if (old_size1 > size1)
263 for (
Integer i = 0; i < old_size1; ++i) {
265 Integer old_size2 = m_sizes[i];
266 if (old_size2 > size2)
268 ConstArrayView<DataType> cav(_value(i));
269 for (
Integer j = 0; j < old_size2; ++j)
270 new_buffer[index + j] = cav[j];
273 m_buffer = new_buffer;
275 m_indexes.resize(size1);
276 m_sizes.resize(size1);
277 for (
Integer i2 = 0, index2 = 0; i2 < size1; ++i2) {
279 m_indexes[i2] = index2;
287 void _copy(
const MultiArray2<DataType>& rhs,
bool do_clone)
289 m_buffer = do_clone ? rhs.m_buffer.clone() : rhs.m_buffer;
290 m_indexes = do_clone ? rhs.m_indexes.clone() : rhs.m_indexes;
291 m_sizes = do_clone ? rhs.m_sizes.clone() : rhs.m_sizes;
293 void _copy(ConstMultiArray2View<DataType> aview)
295 m_buffer = aview.m_buffer;
296 m_indexes = aview.m_indexes;
297 m_sizes = aview.m_sizes;
303 SharedArray<DataType> m_buffer;
305 SharedArray<Int32> m_indexes;
307 SharedArray<Int32> m_sizes;
317template <
typename DataType>
318class SharedMultiArray2
319:
public MultiArray2<DataType>
323 using ThatClass = SharedMultiArray2<DataType>;
327 SharedMultiArray2() =
default;
329 : MultiArray2<DataType>(sizes)
332 : MultiArray2<DataType>(
view)
334 SharedMultiArray2(
const SharedMultiArray2<DataType>& rhs)
335 : MultiArray2<DataType>(rhs,
false)
341 ThatClass& operator=(
const ThatClass& rhs)
344 this->_copy(rhs,
false);
352 void operator=(
const MultiArray2<DataType>& rhs) =
delete;
357 SharedMultiArray2<DataType>
clone()
const
359 return SharedMultiArray2<DataType>(this->
constView());
372template <
typename DataType>
373class UniqueMultiArray2
374:
public MultiArray2<DataType>
378 using ThatClass = UniqueMultiArray2<DataType>;
382 UniqueMultiArray2() =
default;
384 : MultiArray2<DataType>(sizes)
390 : MultiArray2<DataType>(allocation_options)
394 : MultiArray2<DataType>(allocation_options, sizes)
397 : MultiArray2<DataType>(
view)
400 : MultiArray2<DataType>(rhs,
true)
402 UniqueMultiArray2(
const UniqueMultiArray2<DataType>& rhs)
403 : MultiArray2<DataType>(rhs,
true)
410 this->_copy(rhs,
true);
419 ThatClass& operator=(
const UniqueMultiArray2<DataType>& rhs)
422 this->_copy(rhs,
true);
425 ThatClass& operator=(
const MultiArray2<DataType>& rhs) =
delete;
430 UniqueMultiArray2<DataType>
clone()
const
432 return UniqueMultiArray2<DataType>(this->
constView());
439template <
typename DataType> SharedMultiArray2<DataType>::
440SharedMultiArray2(
const UniqueMultiArray2<DataType>& rhs)
441: MultiArray2<DataType>(rhs, true)
450 this->_copy(rhs,
true);
Integer size() const
Number of elements in the vector.
Modifiable view of an array of type T.
const T * data() const
Access to the root of the array without any protection.
void setAt(Int64 i, ConstReferenceType value)
Sets the element at index i. Always checks for overflows.
Constant view of an array of type T.
constexpr bool empty() const noexcept
true if the array is empty (size()==0)
Constant view on a MultiArray2.
Interface for a memory allocator.
Options to configure allocations.
Modifiable view on a MultiArray2.
Base class for multi-sized 2D arrays.
void clear()
Clears the array elements.
void resize(ConstArrayView< Int32 > new_sizes)
Resizes the array with new sizes new_sizes.
MultiArray2(const MultiArray2< DataType > &rhs, bool do_clone)
Copy constructor. Temporary method to be removed once the copy constructor and copy operator are dele...
void fill(const DataType &v)
Fills the array elements with the value v.
MultiArray2View< DataType > view()
Mutable view of the array.
JaggedSmallSpan< const DataType > constSpan() const
Constant view of the array.
ConstArrayView< Int32 > dim2Sizes() const
Array of the number of elements following the second dimension.
JaggedSmallSpan< const DataType > span() const
Constant view of the array.
JaggedSmallSpan< DataType > span()
Mutable view of the array.
ConstMultiArray2View< DataType > constView() const
Constant view of the array.
Int32 totalNbElement() const
Total number of elements.
Int32 dim1Size() const
Number of elements following the first dimension.
ConstArrayView< DataType > viewAsArray() const
View of the array as a 1D array.
ArrayView< DataType > viewAsArray()
View of the array as a 1D array.
Multi-sized 2D array with reference semantics.
SharedMultiArray2< DataType > clone() const
Clones the array.
Multi-sized 2D array with value semantics.
UniqueMultiArray2< DataType > clone() const
Clones the array.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
std::int32_t Int32
Signed integer type of 32 bits.