add method removeIf
This commit is contained in:
@@ -18,12 +18,7 @@ public final class IntList implements Serializable, Cloneable {
|
||||
// TODO support Iterator
|
||||
// TODO add mod counts
|
||||
// TODO support sublists
|
||||
// TODO add retainAll
|
||||
// TODO add removeAll
|
||||
// TODO add retainAll for sorted lists
|
||||
// TODO add removeAll for sorted lists
|
||||
// TODO add removeIf
|
||||
// TODO add replace
|
||||
// TODO add remember if list is sorted so we can use binary search in indexOf
|
||||
// TODO add lastIndexOf
|
||||
// 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);
|
||||
}
|
||||
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) {
|
||||
throw new NullPointerException("values parameter must not be null");
|
||||
@@ -261,6 +256,22 @@ public final class IntList implements Serializable, Cloneable {
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user