add method removeAll

This commit is contained in:
2017-11-09 14:32:15 +01:00
parent db6ca1387d
commit 81e1d1f131
2 changed files with 64 additions and 1 deletions

View File

@@ -23,7 +23,6 @@ public final class IntList implements Serializable, Cloneable {
// TODO add retainAll for sorted lists // TODO add retainAll for sorted lists
// TODO add removeAll for sorted lists // TODO add removeAll for sorted lists
// TODO add removeIf // TODO add removeIf
// TODO add removeRange
// TODO add replace // TODO add replace
// TODO add lastIndexOf // TODO add lastIndexOf
// TODO remove bounds checks that are handled by java, e.g. negative indices // TODO remove bounds checks that are handled by java, e.g. negative indices
@@ -83,6 +82,19 @@ public final class IntList implements Serializable, Cloneable {
index = intList.size(); index = intList.size();
} }
/**
* Create a new {@link IntList} with a copy of the given elements.
*
* @param values
* the values
* @return the list
*/
public static IntList of(final int... values) {
final IntList result = new IntList(values.length);
result.addAll(values);
return result;
}
/** /**
* Returns {@code true} if this list contains no elements. * Returns {@code true} if this list contains no elements.
* *
@@ -228,6 +240,27 @@ public final class IntList implements Serializable, Cloneable {
index = index - (toIndex - fromIndex); index = index - (toIndex - fromIndex);
} }
/**
* Remove all elements that contained in the specified list.
*
* @param remove
* the elements to remove
*/
public void removeAll(final IntList remove) {
final int size = index;
int insertPosition = 0;
for (int i = 0; i < size; i++) {
final int current = data[i];
if (remove.indexOf(current) < 0) {
// keep current element
data[insertPosition] = current;
insertPosition++;
}
}
index = insertPosition;
}
/** /**
* Replaces all values in the list by applying {@code operator}. * Replaces all values in the list by applying {@code operator}.
* *

View File

@@ -632,4 +632,34 @@ public class IntListTest {
list.replaceAll(i -> i * 3); list.replaceAll(i -> i * 3);
Assert.assertArrayEquals(new int[0], list.toArray()); Assert.assertArrayEquals(new int[0], list.toArray());
} }
@Test
public void removeAll() {
final IntList list = IntList.of(-2, -1, 0, 1, 2, 3, 4, 5, 6);
final IntList remove = IntList.of(-1, 2, 4, 5);
list.removeAll(remove);
Assert.assertArrayEquals(new int[] { -2, 0, 1, 3, 6 }, list.toArray());
Assert.assertArrayEquals(new int[] { -1, 2, 4, 5 }, remove.toArray());
}
@Test
public void removeEmptyList() {
final IntList list = IntList.of(1, 2, 3, 4, 5, 6);
final IntList remove = new IntList();
list.removeAll(remove);
Assert.assertArrayEquals(new int[] { 1, 2, 3, 4, 5, 6 }, list.toArray());
Assert.assertArrayEquals(new int[] {}, remove.toArray());
}
@Test
public void removeAllFromEmptyList() {
final IntList list = new IntList();
final IntList remove = IntList.of(1);
list.removeAll(remove);
Assert.assertArrayEquals(new int[] {}, list.toArray());
Assert.assertEquals(0, list.size());
}
} }