Add IntList a list implementation for primitive integers.

This commit is contained in:
2017-02-03 20:31:58 +01:00
parent d3315a80d5
commit 145c2152d0
12 changed files with 1079 additions and 0 deletions

5
primitiveCollections/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
/bin/
/build/
/.settings/
/.classpath
/.project

View File

@@ -0,0 +1,8 @@
import java.text.SimpleDateFormat;
group='org.lucares'
version = '0.1.'+new SimpleDateFormat("YYYYMMddHHmmss").format(new Date());
dependencies {
}

View File

@@ -0,0 +1,313 @@
package org.lucares.collections;
import java.util.List;
/**
* A list for primitive ints.
* <p>
* This class does not (yet) implements all methods a java.util {@link List}
* would have.
*/
public class IntList {
private static final int DEFAULT_CAPACITY = 10;
private int[] data;
private int index = 0;
/**
* Create a new {@link IntList} with initial capacity 10.
*/
public IntList() {
this(DEFAULT_CAPACITY);
}
/**
* Create a new {@link IntList}.
*
* @param initialCapacity
* initial capacity
* @throws IllegalArgumentException
* if initial capacity is negative
*/
public IntList(final int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("initial capacity must not be negative");
}
data = new int[initialCapacity];
}
/**
* Create a new {@link IntList} with a copy of the elements of
* {@code intList}.
*
* @param intList
* the list to copy
*/
public IntList(final IntList intList) {
data = new int[intList.getCapacity()];
System.arraycopy(intList.data, 0, data, 0, intList.size());
index = intList.size();
}
/**
* Returns {@code true} if this list contains no elements.
*
* @return {@code true} if this list contains no elements
*/
public boolean isEmpty() {
return index == 0;
}
/**
* Returns the number of elements in this list.
*
* @return the number of elements in this list
*/
public int size() {
return index;
}
/**
* Returns the number of elements this list can hold before a resize is
* necessary.
*
* @return the number of elements this list can hold before a resize is
* necessary
*/
public int getCapacity() {
return data.length;
}
/**
* Adds {@code value} to the list.
*
* @param value
* the value to add
*/
public void add(final int value) {
addAll(value);
}
/**
* Inserts {@code values} at position {@code pos} into the list.
*
* @param pos
* the position to insert the elements
* @param values
* the elements to insert
* @throws IndexOutOfBoundsException
* if pos is out of bounds {@code pos < 0 || pos > size()}
* @throws NullPointerException
* if {@code values} is {@code null}
*/
public void insert(final int pos, final int... values) {
if (pos < 0) {
throw new IndexOutOfBoundsException("pos must not be negative, but was: " + pos);
}
if (pos > index) {
throw new IndexOutOfBoundsException("pos must not smaller than size(), but was: " + pos);
}
if (values == null) {
throw new NullPointerException("values parameter must not be null");
}
ensureCapacity(values.length);
final int[] newData = new int[data.length];
System.arraycopy(data, 0, newData, 0, pos);
System.arraycopy(values, 0, newData, pos, values.length);
System.arraycopy(data, pos, newData, pos + values.length, index - pos);
data = newData;
index += values.length;
}
/**
* Set the value {@code value} at position {@code pos}.
*
* @param pos
* the position to overwrite
* @param value
* the new value
* @throws IndexOutOfBoundsException
* if pos is out of bounds {@code pos < 0 || pos >= size()}
*/
public void set(final int pos, final int value) {
if (pos < 0) {
throw new IndexOutOfBoundsException("pos must not be negative, but was: " + pos);
}
if (pos >= index) {
throw new IndexOutOfBoundsException("pos must not smaller than size(), but was: " + pos);
}
data[pos] = value;
}
/**
* Add {@code values} to the list.
*
* @param values
* the values to add
* @throws NullPointerException
* if {@code values} is {@code null}
*/
public void addAll(final int... values) {
ensureCapacity(values.length);
System.arraycopy(values, 0, data, index, values.length);
index += values.length;
}
/**
* Removes elements from the list.
*
* @param from
* index of the first element to remove
* @param length
* number of elements to remove
* @throws IndexOutOfBoundsException
* if {@code from} or {@code length} is negative, or if the
* range defined by {@code from} and {@code length} is out of
* bounds
*/
public void remove(final int from, final int length) {
if (from < 0) {
throw new IndexOutOfBoundsException("from must not be negative, but was: " + from);
}
if (length < 0) {
throw new IndexOutOfBoundsException("length must not be negative, but was: " + length);
}
if (from + length > index) {
throw new IndexOutOfBoundsException("from: " + from + " length: " + length);
}
final int[] newData = new int[data.length];
System.arraycopy(data, 0, newData, 0, from);
System.arraycopy(data, from + length, newData, from, data.length - from - length);
data = newData;
index -= length;
}
/**
* Returns the element at position {@code pos}.
*
* @param pos
* position of the element to return
* @return the element at position {@code pos}
* @throws IndexOutOfBoundsException
* if {@code pos} is out of bounds
* {@code index < 0 || index >= size()}
*/
public int get(final int pos) {
if (pos < 0 || pos >= index) {
throw new IndexOutOfBoundsException();
}
return data[pos];
}
/**
* Returns the {@code length} elements starting at {@code from}.
*
* @param from
* position of the first element
* @param length
* number of elements
* @return the {@code length} elements starting at {@code from}
* @throws IndexOutOfBoundsException
* if {@code from} or {@code length} is negative, or if the
* range defined by {@code from} and {@code length} is out of
* bounds
*/
public int[] get(final int from, final int length) {
if (from < 0) {
throw new IndexOutOfBoundsException("from must not be negative, but was: " + from);
}
if (length < 0) {
throw new IndexOutOfBoundsException("length must not be negative, but was: " + length);
}
if (from + length > index) {
throw new IndexOutOfBoundsException("from: " + from + " length: " + length);
}
final int[] result = new int[length];
System.arraycopy(data, from, result, 0, length);
return result;
}
/**
* Returns an array containing all elements of this list.
*
* @return an array containing all elements of this list
*/
public int[] toArray() {
return get(0, index);
}
private void ensureCapacity(final int newElements) {
final int requiredCapacity = index + newElements;
if (requiredCapacity - 1 >= data.length) {
final int newCapacity = data.length * 2 > requiredCapacity ? data.length * 2 : requiredCapacity;
final int[] newData = new int[newCapacity];
System.arraycopy(data, 0, newData, 0, index);
data = newData;
}
}
/**
* Reduces the capacity to the size of the list.
* <p>
* Call this method to reduce the memory consumption of this list.
*/
public void trim() {
final int[] newData = new int[index];
System.arraycopy(data, 0, newData, 0, index);
data = newData;
}
@Override
public int hashCode() {
if (index == 0) {
return 0;
}
int result = 1;
for (int i = 0; i < index; i++) {
result = 31 * result + data[i];
}
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final IntList other = (IntList) obj;
if (index != other.index) {
return false;
}
for (int i = 0; i < index; i++) {
if (data[i] != other.data[i]) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,403 @@
package org.lucares.collections;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Test;
public class IntListTest {
@Test
public void testEmptyList() {
final IntList list = new IntList();
Assert.assertEquals(true, list.isEmpty());
}
@Test
public void testCopyConstructor() {
final IntList list = new IntList();
list.addAll(1, 2, 3, 4, 5);
final IntList copy = new IntList(list);
Assert.assertArrayEquals(copy.toArray(), list.toArray());
}
@Test
public void testInvalidInitialCapacity() {
try {
new IntList(-1);
Assert.fail();
} catch (final IllegalArgumentException e) {
// expected
}
}
@Test
public void testAdd() {
final IntList list = new IntList();
list.addAll();
Assert.assertTrue(list.isEmpty());
Assert.assertEquals(0, list.size());
list.add(1);
Assert.assertFalse(list.isEmpty());
Assert.assertEquals(1, list.size());
list.add(2);
Assert.assertEquals(2, list.size());
Assert.assertEquals(1, list.get(0));
Assert.assertEquals(2, list.get(1));
}
@Test
public void testInsert() {
final IntList list = new IntList();
list.insert(0, 1);
Assert.assertArrayEquals(new int[] { 1 }, list.toArray());
list.insert(1, 2);
Assert.assertArrayEquals(new int[] { 1, 2 }, list.toArray());
list.insert(1, 3);
Assert.assertArrayEquals(new int[] { 1, 3, 2 }, list.toArray());
list.insert(2, 4, 4, 4);
Assert.assertArrayEquals(new int[] { 1, 3, 4, 4, 4, 2 }, list.toArray());
list.insert(6, 5, 5);
Assert.assertArrayEquals(new int[] { 1, 3, 4, 4, 4, 2, 5, 5 }, list.toArray());
}
@Test
public void testInsertOutOfBounds() {
final IntList list = new IntList();
try {
list.insert(-1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.insert(1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.add(1);
list.insert(2, 2);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testSet() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3, 4, 5);
list.set(0, 10);
Assert.assertArrayEquals(new int[] { 10, 1, 2, 3, 4, 5 }, list.toArray());
list.set(1, 11);
Assert.assertArrayEquals(new int[] { 10, 11, 2, 3, 4, 5 }, list.toArray());
list.set(5, 55);
Assert.assertArrayEquals(new int[] { 10, 11, 2, 3, 4, 55 }, list.toArray());
}
@Test
public void testSetOutOfBounds() {
final IntList list = new IntList();
try {
list.set(-1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.set(1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
list.addAll(0, 1, 2, 3, 4, 5);
try {
list.set(6, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testGrow() {
final IntList list = new IntList(1);
final int size = 100;
for (int i = 0; i < size; i++) {
list.add(i);
}
Assert.assertEquals(size, list.size());
for (int i = 0; i < size; i++) {
Assert.assertEquals(i, list.get(i));
}
}
@Test
public void testGrowIntListOfCapacityNull() {
final IntList list = new IntList(0);
final int size = 100;
for (int i = 0; i < size; i++) {
list.add(i);
}
Assert.assertEquals(size, list.size());
for (int i = 0; i < size; i++) {
Assert.assertEquals(i, list.get(i));
}
}
@Test
public void testAddArray() {
final IntList list = new IntList();
final int size = 100;
final int[] ints = ThreadLocalRandom.current().ints(size).toArray();
list.addAll(ints);
Assert.assertEquals(size, list.size());
for (int i = 0; i < size; i++) {
Assert.assertEquals(ints[i], list.get(i));
}
final int[] anotherInts = ThreadLocalRandom.current().ints(size).toArray();
list.addAll(anotherInts);
Assert.assertEquals(size * 2, list.size());
for (int i = 0; i < size; i++) {
Assert.assertEquals(anotherInts[i], list.get(size + i));
}
}
@Test
public void testGetArray() {
final IntList list = new IntList();
final int size = 100;
final int[] ints = ThreadLocalRandom.current().ints(size).toArray();
list.addAll(ints);
final int length = 20;
final int from = 10;
final int[] actualInts = list.get(from, length);
for (int i = 0; i < length; i++) {
Assert.assertEquals(actualInts[i], list.get(from + i));
Assert.assertEquals(actualInts[i], ints[from + i]);
}
}
@Test
public void testGetOutOfBounds() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3);
try {
list.get(-1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.get(4);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testGetArrayOutOfBounds() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3);
try {
list.get(-1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.get(1, -1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.get(3, 2);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.get(4, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testToArrayEmptyLIst() {
final IntList list = new IntList();
final int[] actual = list.toArray();
Assert.assertArrayEquals(actual, new int[0]);
}
@Test
public void testRemove() {
final IntList list = new IntList();
final int size = 10;
final int[] ints = new int[size];
for (int i = 0; i < size; i++) {
ints[i] = i;
}
list.addAll(ints);
list.remove(2, 0);
Assert.assertArrayEquals(ints, list.toArray());
list.remove(9, 1);
final int[] expectedA = removeElements(ints, 9);
Assert.assertArrayEquals(expectedA, list.toArray());
list.remove(0, 1);
final int[] expectedB = removeElements(expectedA, 0);
Assert.assertArrayEquals(expectedB, list.toArray());
list.remove(7, 1);
final int[] expectedC = removeElements(expectedB, 7);
Assert.assertArrayEquals(expectedC, list.toArray());
list.remove(3, 3);
final int[] expectedD = removeElements(expectedC, 3, 4, 5);
Assert.assertArrayEquals(expectedD, list.toArray());
list.remove(0, 4);
final int[] expectedE = removeElements(expectedD, 0, 1, 2, 3);
Assert.assertArrayEquals(expectedE, list.toArray());
}
@Test
public void testRemoveOutOfRange() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3);
try {
list.remove(-1, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(1, -1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(3, 2);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(4, 1);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
}
@Test
public void testTrim() {
final IntList list = new IntList();
list.addAll(0, 1, 2, 3);
list.trim();
Assert.assertEquals(4, list.getCapacity());
}
@Test
public void testTrimOnEmptyList() {
final IntList list = new IntList();
list.trim();
Assert.assertEquals(0, list.getCapacity());
}
@Test
public void testHashCodeEquals() {
final IntList a = new IntList();
final IntList b = new IntList();
a.addAll(1, 2, 3, 4);
b.addAll(1, 2, 3, 4);
Assert.assertTrue(a.equals(b));
Assert.assertEquals(a.hashCode(), b.hashCode());
// trim only one of them
// we want to compare the actual content
a.trim();
Assert.assertTrue(a.equals(b));
Assert.assertEquals(a.hashCode(), b.hashCode());
// change one value
a.remove(2, 1);
a.insert(2, 99);
Assert.assertFalse(a.equals(b));
Assert.assertNotEquals(a.hashCode(), b.hashCode());
}
private int[] removeElements(final int[] data, final int... removedElements) {
final int[] result = new int[data.length - removedElements.length];
final List<Integer> blacklist = Arrays.stream(removedElements).boxed().collect(Collectors.toList());
int j = 0;
for (int i = 0; i < data.length; i++) {
if (!blacklist.contains(i)) {
result[j] = data[i];
j++;
}
}
return result;
}
}