Skip to content

Commit ccc36af

Browse files
List constructor creates a buffer with capacity exactly the expected number of elements. (#248)
1 parent b871257 commit ccc36af

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGES.txt

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
** Improvements
1717

18+
GH-248: List constructor creates a buffer with capacity exactly the expected number of elements.
19+
(Bruno Roustant)
20+
1821
** Bugs
1922

2023
GH-237: Fix HashMap put/remove returned value for empty key after clear. (Bruno Roustant)

hppc/src/main/templates/com/carrotsearch/hppc/KTypeArrayList.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public KTypeArrayList(int expectedElements) {
9696
public KTypeArrayList(int expectedElements, ArraySizingStrategy resizer) {
9797
assert resizer != null;
9898
this.resizer = resizer;
99-
ensureCapacity(expectedElements);
99+
buffer = Arrays.copyOf(buffer, expectedElements);
100100
}
101101

102102
/**

hppc/src/test/templates/com/carrotsearch/hppc/KTypeArrayListTest.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ public void testEnsureCapacity()
322322
{
323323
TightRandomResizingStrategy resizer = new TightRandomResizingStrategy(0);
324324
KTypeArrayList<KType> list = new KTypeArrayList<>(0, resizer);
325+
assertEquals(list.size(), list.buffer.length);
325326

326327
// Add some elements.
327328
final int max = rarely() ? 0 : randomIntBetween(0, 1000);
@@ -390,6 +391,7 @@ public void testGrowth()
390391

391392
list = new KTypeArrayList<KType>(0,
392393
new BoundedProportionalArraySizingStrategy(5, maxGrowth, 2));
394+
assertEquals(list.size(), list.buffer.length);
393395

394396
for (int i = 0; i < count; i++)
395397
list.add(cast(i));
@@ -492,9 +494,21 @@ public void testClear()
492494
@Test
493495
public void testFrom()
494496
{
495-
KTypeArrayList<KType> variable = KTypeArrayList.from(k1, k2, k3);
496-
assertEquals(3, variable.size());
497-
assertListEquals(variable.toArray(), 1, 2, 3);
497+
list = KTypeArrayList.from(k1, k2, k3);
498+
assertEquals(3, list.size());
499+
assertListEquals(list.toArray(), 1, 2, 3);
500+
assertEquals(list.size(), list.buffer.length);
501+
}
502+
503+
/* */
504+
@Test
505+
public void testCopyContainer()
506+
{
507+
list.add(asArray( 1, 2, 3));
508+
KTypeArrayList<KType> copy = new KTypeArrayList<KType>(list);
509+
assertEquals(3, copy.size());
510+
assertListEquals(copy.toArray(), 1, 2, 3);
511+
assertEquals(copy.size(), copy.buffer.length);
498512
}
499513

500514
/* */

0 commit comments

Comments
 (0)