Skip to content

Commit

Permalink
In <primitive>ArrayList.newWithNValues, replace loop with Arrays.fill
Browse files Browse the repository at this point in the history
As suggested by @motlin in the link below.
#105
Add more tests for <primitive>ArrayList.newWithNValues.

Signed-off-by: guoci <zguoci@gmail.com>
  • Loading branch information
guoci committed May 8, 2016
1 parent 1546bbd commit 5ebc381
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTE_DRAFT.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ New Functionality
Optimizations
-------------

* In <primitive>ArrayList.newWithNValues, replace loop with Arrays.fill.
* Optimize MutableList.chunk() for RandomAccess lists to use the backing array instead of an iterator.

Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ public class <name>ArrayList extends Abstract<name>Iterable
public static <name>ArrayList newWithNValues(int size, <type> value)
{
<name>ArrayList newList = new <name>ArrayList(size);
for (int i = 0; i \< size; i++)
{
newList.add(value);
}
newList.size = size;
Arrays.fill(newList.items, value);
return newList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public class <name>ArrayListTest extends Abstract<name>ListTestCase
<name>ArrayList newList = <name>ArrayList.newWithNValues(5, <(literal.(type))("42")>);
Verify.assertSize(5, newList);
Assert.assertEquals(<name>ArrayList.newListWith(<["42", "42", "42", "42", "42"]:(literal.(type))(); separator=", ">), newList);

<name>ArrayList newList2 = <name>ArrayList.newWithNValues(0, <(literal.(type))("2")>);
Verify.assertSize(0, newList2);
}

@Test(expected = NegativeArraySizeException.class)
public void newWithNValues_throws_negative_size()
{
<name>ArrayList newList = <name>ArrayList.newWithNValues(-5, <(literal.(type))("42")>);
}

@Test
Expand Down

0 comments on commit 5ebc381

Please sign in to comment.