add special implementation for add

The special implementation does not need to allocate an array for the
varargs of addAll
This commit is contained in:
2017-09-28 18:22:16 +02:00
parent 4da0726185
commit 4a762f39b9
2 changed files with 11 additions and 8 deletions

View File

@@ -112,9 +112,10 @@ public class IntList {
* the value to add * the value to add
*/ */
public void add(final int value) { public void add(final int value) {
// TODO add a special implementation that does not ensureCapacity(1);
// need to allocate an array for the varargs of addAll
addAll(value); data[index] = value;
index++;
} }
/** /**

View File

@@ -39,11 +39,9 @@ public class IntListTest {
@Test @Test
public void testAdd() { public void testAdd() {
final IntList list = new IntList(); // setting initial size to one, so that the first add does not need to resize,
// but the second add must
list.addAll(); final IntList list = new IntList(1);
Assert.assertTrue(list.isEmpty());
Assert.assertEquals(0, list.size());
list.add(1); list.add(1);
@@ -179,6 +177,10 @@ public class IntListTest {
public void testAddArray() { public void testAddArray() {
final IntList list = new IntList(); final IntList list = new IntList();
list.addAll();
Assert.assertTrue(list.isEmpty());
Assert.assertEquals(0, list.size());
final int size = 100; final int size = 100;
final int[] ints = ThreadLocalRandom.current().ints(size).toArray(); final int[] ints = ThreadLocalRandom.current().ints(size).toArray();