Skip to content

Commit 19b5976

Browse files
Merge pull request #1987 from arcaneframework/dev/gg-minor-improvement-for-array
Minor improvements for `Array` classes
2 parents 9fa0654 + 7461d99 commit 19b5976

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

arccore/src/collections/arccore/collections/Array.h

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,11 +1295,17 @@ class Array
12951295
m_ptr[m_md->size].~T();
12961296
}
12971297
//! Elément d'indice \a i. Vérifie toujours les débordements
1298-
ConstReferenceType at(Int64 i) const
1298+
T& at(Int64 i)
12991299
{
13001300
arccoreCheckAt(i,m_md->size);
13011301
return m_ptr[i];
13021302
}
1303+
//! Elément d'indice \a i. Vérifie toujours les débordements
1304+
ConstReferenceType at(Int64 i) const
1305+
{
1306+
arccoreCheckAt(i, m_md->size);
1307+
return m_ptr[i];
1308+
}
13031309
//! Positionne l'élément d'indice \a i. Vérifie toujours les débordements
13041310
void setAt(Int64 i,ConstReferenceType value)
13051311
{
@@ -1593,18 +1599,24 @@ class SharedArray
15931599
inline SharedArray(const UniqueArray<T>& rhs);
15941600

15951601
/*!
1596-
* \brief Créé un tableau de \a asize éléments avec un
1597-
* allocateur spécifique \a allocator.
1598-
*
1599-
* Si ArrayTraits<T>::IsPODType vaut TrueType, les éléments ne sont pas
1600-
* initialisés. Sinon, c'est le constructeur par défaut de T qui est utilisé.
1602+
* \brief Créé un tableau vide avec un allocateur spécifique \a allocator.
16011603
*
16021604
* \warning Using specific allocator for SharedArray is experimental
16031605
*/
16041606
explicit SharedArray(IMemoryAllocator* allocator)
1607+
: SharedArray(MemoryAllocationOptions(allocator))
1608+
{
1609+
}
1610+
1611+
/*!
1612+
* \brief Créé un tableau vide avec un allocateur spécifique \a allocation_options.
1613+
*
1614+
* \warning Using specific allocator for SharedArray is experimental
1615+
*/
1616+
explicit SharedArray(const MemoryAllocationOptions& allocation_options)
16051617
: Array<T>()
16061618
{
1607-
this->_initFromAllocator(allocator,0);
1619+
this->_initFromAllocator(allocation_options,0);
16081620
this->_checkValidSharedArray();
16091621
}
16101622

@@ -1614,22 +1626,28 @@ class SharedArray
16141626
*
16151627
* Si ArrayTraits<T>::IsPODType vaut TrueType, les éléments ne sont pas
16161628
* initialisés. Sinon, c'est le constructeur par défaut de T qui est utilisé.
1617-
*
1618-
* \warning Using specific allocator for SharedArray is experimental
16191629
*/
16201630
SharedArray(IMemoryAllocator* allocator,Int64 asize)
1621-
: Array<T>()
1631+
: SharedArray(MemoryAllocationOptions(allocator), asize)
16221632
{
1623-
this->_initFromAllocator(allocator,asize);
1624-
this->_resize(asize);
1625-
this->_checkValidSharedArray();
16261633
}
16271634

16281635
/*!
1629-
* \brief Créé un tableau avec l'allocateur \a allocator en recopiant les valeurs \a rhs.
1636+
* \brief Créé un tableau de \a asize éléments avec un
1637+
* allocateur spécifique \a allocator.
16301638
*
1631-
* \warning Using specific allocator for SharedArray is experimental
1639+
* Si ArrayTraits<T>::IsPODType vaut TrueType, les éléments ne sont pas
1640+
* initialisés. Sinon, c'est le constructeur par défaut de T qui est utilisé.
16321641
*/
1642+
SharedArray(const MemoryAllocationOptions& allocation_options, Int64 asize)
1643+
: Array<T>()
1644+
{
1645+
this->_initFromAllocator(allocation_options, asize);
1646+
this->_resize(asize);
1647+
this->_checkValidSharedArray();
1648+
}
1649+
1650+
//!Créé un tableau avec l'allocateur \a allocator en recopiant les valeurs \a rhs.
16331651
SharedArray(IMemoryAllocator* allocator,Span<const T> rhs)
16341652
{
16351653
this->_initFromAllocator(allocator,0);

arccore/src/collections/tests/TestArray.cc

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,12 @@ TEST(Array, Misc2)
479479
UniqueArray<Real> v;
480480
v.resize(3);
481481
v[0] = 1.2;
482-
v[1] = -1.3;
482+
v.at(1) = -1.3;
483483
v[2] = 7.6;
484+
ASSERT_EQ(v[0], 1.2);
485+
ASSERT_EQ(v[1], -1.3);
486+
ASSERT_EQ(v.at(2), 7.6);
487+
484488
v.add(4.3);
485489
for (Real x : v) {
486490
std::cout << " Value: " << x << '\n';
@@ -826,7 +830,7 @@ TEST(SharedArray, Allocator)
826830
ASSERT_EQ(a1.size(), 5);
827831

828832
std::cout << "Array a3\n";
829-
SharedArray<Int32> a3(allocator1);
833+
SharedArray<Int32> a3(MemoryAllocationOptions{ allocator1 });
830834
a3.add(4);
831835
a3.add(6);
832836
a3.add(2);
@@ -837,19 +841,24 @@ TEST(SharedArray, Allocator)
837841
ASSERT_EQ(a3.constSpan(), a1.constSpan());
838842

839843
std::cout << "Array a4\n";
840-
SharedArray<Int32> a4(allocator1);
844+
SharedArray<Int32> a4(allocator1, 2);
845+
ASSERT_EQ(a4.size(), 2);
841846
a4.add(4);
842847
a4.add(6);
843848
a4.add(2);
844-
ASSERT_EQ(a4.size(), 3);
849+
ASSERT_EQ(a4.size(), 5);
850+
ASSERT_EQ(a4[2], 4);
851+
ASSERT_EQ(a4[3], 6);
852+
ASSERT_EQ(a4[4], 2);
853+
845854
a4 = a1.span();
846855
ASSERT_EQ(a4.allocator(), allocator1);
847856

848857
a4 = SharedArray<Int32>(&printable_allocator2);
849858

850859
SharedArray<Int32> array[2];
851860
IMemoryAllocator* allocator3 = allocator1;
852-
for( Integer i=0; i<2; ++i ){
861+
for (Integer i = 0; i < 2; ++i) {
853862
array[i] = SharedArray<Int32>(allocator3);
854863
}
855864
ASSERT_EQ(array[0].allocator(), allocator3);
@@ -1008,6 +1017,7 @@ TEST(Array, DebugInfo)
10081017
MemoryAllocationOptions allocate_options2(&m_default_allocator, eMemoryLocationHint::None, 0);
10091018

10101019
String a1_name("Array1");
1020+
String sa1_name("SharedArray1");
10111021
UniqueArray<Int32> a3;
10121022
{
10131023
std::cout << "Array a1\n";
@@ -1019,6 +1029,17 @@ TEST(Array, DebugInfo)
10191029
ASSERT_EQ(a1.data(), nullptr);
10201030
ASSERT_EQ(a1.debugName(), a1_name);
10211031

1032+
std::cout << "SharedArray sa1\n";
1033+
SharedArray<Int32> sa1(allocate_options2);
1034+
sa1.setDebugName(sa1_name);
1035+
ASSERT_EQ(sa1.allocationOptions(), allocate_options2);
1036+
ASSERT_EQ(sa1.size(), 0);
1037+
ASSERT_EQ(sa1.capacity(), 0);
1038+
ASSERT_EQ(sa1.data(), nullptr);
1039+
ASSERT_EQ(sa1.debugName(), sa1_name);
1040+
1041+
ASSERT_EQ(a1.debugName(), a1_name);
1042+
10221043
std::cout << "Array a2\n";
10231044
UniqueArray<Int32> a2(a1);
10241045
ASSERT_SAME_ARRAY_INFOS(a2, a1);

0 commit comments

Comments
 (0)