Skip to content

Commit

Permalink
List constructor creates a buffer with capacity exactly the expected …
Browse files Browse the repository at this point in the history
…number of elements. (#248)
  • Loading branch information
bruno-roustant authored May 24, 2024
1 parent b871257 commit ccc36af
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

** Improvements

GH-248: List constructor creates a buffer with capacity exactly the expected number of elements.
(Bruno Roustant)

** Bugs

GH-237: Fix HashMap put/remove returned value for empty key after clear. (Bruno Roustant)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public KTypeArrayList(int expectedElements) {
public KTypeArrayList(int expectedElements, ArraySizingStrategy resizer) {
assert resizer != null;
this.resizer = resizer;
ensureCapacity(expectedElements);
buffer = Arrays.copyOf(buffer, expectedElements);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ public void testEnsureCapacity()
{
TightRandomResizingStrategy resizer = new TightRandomResizingStrategy(0);
KTypeArrayList<KType> list = new KTypeArrayList<>(0, resizer);
assertEquals(list.size(), list.buffer.length);

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

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

for (int i = 0; i < count; i++)
list.add(cast(i));
Expand Down Expand Up @@ -492,9 +494,21 @@ public void testClear()
@Test
public void testFrom()
{
KTypeArrayList<KType> variable = KTypeArrayList.from(k1, k2, k3);
assertEquals(3, variable.size());
assertListEquals(variable.toArray(), 1, 2, 3);
list = KTypeArrayList.from(k1, k2, k3);
assertEquals(3, list.size());
assertListEquals(list.toArray(), 1, 2, 3);
assertEquals(list.size(), list.buffer.length);
}

/* */
@Test
public void testCopyContainer()
{
list.add(asArray( 1, 2, 3));
KTypeArrayList<KType> copy = new KTypeArrayList<KType>(list);
assertEquals(3, copy.size());
assertListEquals(copy.toArray(), 1, 2, 3);
assertEquals(copy.size(), copy.buffer.length);
}

/* */
Expand Down

0 comments on commit ccc36af

Please sign in to comment.