add method removeIf

This commit is contained in:
2017-11-10 10:35:51 +01:00
parent b50cb8fdca
commit 88e0179dd5
3 changed files with 119 additions and 7 deletions

View File

@@ -18,12 +18,7 @@ public final class IntList implements Serializable, Cloneable {
// TODO support Iterator // TODO support Iterator
// TODO add mod counts // TODO add mod counts
// TODO support sublists // TODO support sublists
// TODO add retainAll // TODO add remember if list is sorted so we can use binary search in indexOf
// TODO add removeAll
// TODO add retainAll for sorted lists
// TODO add removeAll for sorted lists
// TODO add removeIf
// 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
@@ -155,7 +150,7 @@ public final class IntList implements Serializable, Cloneable {
throw new IndexOutOfBoundsException("pos must not be negative, but was: " + pos); throw new IndexOutOfBoundsException("pos must not be negative, but was: " + pos);
} }
if (pos > index) { if (pos > index) {
throw new IndexOutOfBoundsException("pos must not smaller than size(), but was: " + pos); throw new IndexOutOfBoundsException("pos must be smaller than size(), but was: " + pos);
} }
if (values == null) { if (values == null) {
throw new NullPointerException("values parameter must not be null"); throw new NullPointerException("values parameter must not be null");
@@ -261,6 +256,22 @@ public final class IntList implements Serializable, Cloneable {
index = insertPosition; index = insertPosition;
} }
public void removeIf(final IntPredicate predicate) {
final int size = index;
int insertPosition = 0;
for (int i = 0; i < size; i++) {
final int current = data[i];
if (!predicate.test(current)) {
// keep current element
data[insertPosition] = current;
insertPosition++;
}
}
index = insertPosition;
}
/** /**
* Retains all elements contained in the specified list. * Retains all elements contained in the specified list.
* *

View File

@@ -0,0 +1,56 @@
package org.lucares.collections;
import java.util.function.Predicate;
/**
* A {@link Predicate} for primitive integers.
*/
@FunctionalInterface
public interface IntPredicate {
/**
* Evaluates the predicate.
*
* @param i
* the input argument
* @return {@code true} iff the input argument matches the predicate
*/
boolean test(int i);
/**
* Returns a predicate that represents the logical AND of {@code this} and
* another predicate. The and operation is short-circuiting.
*
* @param other
* the other predicate
* @return the result of combining both predicates with a logical AND operation
* @throws NullPointerException
* if {@code other} is null
*/
default IntPredicate and(final IntPredicate other) {
return (t) -> test(t) && other.test(t);
}
/**
* Returns a predicate that represents the logical OR of {@code this} and
* another predicate. The and operation is short-circuiting.
*
* @param other
* the other predicate
* @return the result of combining both predicates with a logical OR operation
* @throws NullPointerException
* if {@code other} is null
*/
default IntPredicate or(final IntPredicate other) {
return (t) -> test(t) || other.test(t);
}
/**
* Returns a predicate that negates the result of this predicate.
*
* @return the negation of {@code this}
*/
default IntPredicate negate() {
return (t) -> !test(t);
}
}

View File

@@ -692,4 +692,49 @@ public class IntListTest {
Assert.assertArrayEquals(new int[] {}, list.toArray()); Assert.assertArrayEquals(new int[] {}, list.toArray());
Assert.assertArrayEquals(new int[] {}, retain.toArray()); Assert.assertArrayEquals(new int[] {}, retain.toArray());
} }
@Test
public void testRemoveIf() {
final IntList list = IntList.of(1, 2, 3, 4, 5, 6);
list.removeIf(i -> i % 2 == 0);
Assert.assertArrayEquals(new int[] { 1, 3, 5 }, list.toArray());
}
@Test
public void testRemoveIfNegationOfPredicate() {
final IntList list = IntList.of(1, 2, 3, 4, 5, 6);
final IntPredicate predicate = i -> i % 2 == 0;
list.removeIf(predicate.negate());
Assert.assertArrayEquals(new int[] { 2, 4, 6 }, list.toArray());
}
@Test
public void testRemoveIfWithAndCombinedPredicates() {
final IntList list = IntList.of(1, 2, 3, 4, 5, 6);
final IntPredicate predicateA = i -> i % 2 == 0;
final IntPredicate predicateB = i -> i == 3 || i == 4;
list.removeIf(predicateA.and(predicateB));
Assert.assertArrayEquals(new int[] { 1, 2, 3, 5, 6 }, list.toArray());
}
@Test
public void testRemoveIfWithOrCombinedPredicates() {
final IntList list = IntList.of(1, 2, 3, 4, 5, 6);
final IntPredicate predicateA = i -> i % 2 == 0;
final IntPredicate predicateB = i -> i == 3 || i == 4;
list.removeIf(predicateA.or(predicateB));
Assert.assertArrayEquals(new int[] { 1, 5 }, list.toArray());
}
@Test
public void testRemoveIfOnEmptyList() {
final IntList list = IntList.of();
list.removeIf(i -> false);
Assert.assertArrayEquals(new int[] {}, list.toArray());
}
} }