add *List.range() and *List.rangeClosed()
This commit is contained in:
@@ -67,10 +67,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Create a new {@link IntList}.
|
||||
*
|
||||
* @param initialCapacity
|
||||
* initial capacity
|
||||
* @throws IllegalArgumentException
|
||||
* if initial capacity is negative
|
||||
* @param initialCapacity initial capacity
|
||||
* @throws IllegalArgumentException if initial capacity is negative
|
||||
*/
|
||||
public IntList(final int initialCapacity) {
|
||||
if (initialCapacity < 0 || initialCapacity > MAX_ARRAY_SIZE) {
|
||||
@@ -84,10 +82,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Create a new {@link IntList} with a copy of the elements of {@code intList}.
|
||||
*
|
||||
* @param intList
|
||||
* the list to copy
|
||||
* @throws NullPointerException
|
||||
* if the specified {@link IntList} is null
|
||||
* @param intList the list to copy
|
||||
* @throws NullPointerException if the specified {@link IntList} is null
|
||||
*/
|
||||
public IntList(final IntList intList) {
|
||||
data = EMPTY_ARRAY;
|
||||
@@ -97,11 +93,9 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Create a new {@link IntList} with a copy of the given elements.
|
||||
*
|
||||
* @param values
|
||||
* the values
|
||||
* @param values the values
|
||||
* @return the list
|
||||
* @throws NullPointerException
|
||||
* if the specified array is null
|
||||
* @throws NullPointerException if the specified array is null
|
||||
*/
|
||||
public static IntList of(final int... values) {
|
||||
final IntList result = new IntList(values.length);
|
||||
@@ -109,6 +103,46 @@ public final class IntList implements Serializable, Cloneable {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link IntList} with values from {@code startInclusive} to
|
||||
* {@code endExclusive}-1.
|
||||
*
|
||||
* @param startInclusive the lower bound (inclusive)
|
||||
* @param endExclusive the upper bound (exclusive)
|
||||
* @return the {@link IntList}
|
||||
*/
|
||||
public static IntList range(final int startInclusive, final int endExclusive) {
|
||||
if (startInclusive >= endExclusive) {
|
||||
return new IntList(0);
|
||||
} else {
|
||||
final IntList result = new IntList(endExclusive - startInclusive);
|
||||
for (int i = startInclusive; i < endExclusive; i++) {
|
||||
result.add(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link IntList} with values from {@code startInclusive} to
|
||||
* {@code endInclusive}.
|
||||
*
|
||||
* @param startInclusive the lower bound (inclusive)
|
||||
* @param endInclusive the upper bound (inclusive)
|
||||
* @return the {@link IntList}
|
||||
*/
|
||||
public static IntList rangeClosed(final int startInclusive, final int endInclusive) {
|
||||
if (startInclusive > endInclusive) {
|
||||
return new IntList(0);
|
||||
} else {
|
||||
final IntList result = new IntList(endInclusive - startInclusive + 1);
|
||||
for (int i = startInclusive; i <= endInclusive; i++) {
|
||||
result.add(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this list contains no elements.
|
||||
*
|
||||
@@ -152,8 +186,7 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Adds {@code value} to the list.
|
||||
*
|
||||
* @param value
|
||||
* the value to add
|
||||
* @param value the value to add
|
||||
*/
|
||||
public void add(final int value) {
|
||||
ensureCapacity(1);
|
||||
@@ -169,14 +202,11 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* 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 the given array is null
|
||||
* @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 the given array is null
|
||||
*/
|
||||
public void insert(final int pos, final int... values) {
|
||||
|
||||
@@ -222,12 +252,10 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* 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()}
|
||||
* @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) {
|
||||
|
||||
@@ -250,10 +278,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Add {@code values} to the list.
|
||||
*
|
||||
* @param values
|
||||
* the values to add
|
||||
* @throws NullPointerException
|
||||
* if the given array is null
|
||||
* @param values the values to add
|
||||
* @throws NullPointerException if the given array is null
|
||||
*/
|
||||
public void addAll(final int... values) {
|
||||
ensureCapacity(values.length);
|
||||
@@ -272,10 +298,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Add all value of the given list.
|
||||
*
|
||||
* @param list
|
||||
* the list
|
||||
* @throws NullPointerException
|
||||
* if the given list is null
|
||||
* @param list the list
|
||||
* @throws NullPointerException if the given list is null
|
||||
*/
|
||||
public void addAll(final IntList list) {
|
||||
ensureCapacity(list.size());
|
||||
@@ -305,14 +329,12 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* This method does not release any memory. Call {@link #trim()} to free unused
|
||||
* memory.
|
||||
*
|
||||
* @param fromIndex
|
||||
* index of the first element to remove
|
||||
* @param toIndex
|
||||
* the index of the last element to remove (exclusive)
|
||||
* @throws IndexOutOfBoundsException
|
||||
* if {@code fromIndex} or {@code toIndex} is negative, or if the
|
||||
* range defined by {@code fromIndex} and {@code toIndex} is out of
|
||||
* bounds
|
||||
* @param fromIndex index of the first element to remove
|
||||
* @param toIndex the index of the last element to remove (exclusive)
|
||||
* @throws IndexOutOfBoundsException if {@code fromIndex} or {@code toIndex} is
|
||||
* negative, or if the range defined by
|
||||
* {@code fromIndex} and {@code toIndex} is
|
||||
* out of bounds
|
||||
* @see #trim()
|
||||
*/
|
||||
public void remove(final int fromIndex, final int toIndex) {
|
||||
@@ -352,10 +374,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* {@code retain}. If {@code retain} is not sorted, then the complexity is
|
||||
* O(n*m).
|
||||
*
|
||||
* @param remove
|
||||
* the elements to remove
|
||||
* @throws NullPointerException
|
||||
* if the specified {@link IntList} is null
|
||||
* @param remove the elements to remove
|
||||
* @throws NullPointerException if the specified {@link IntList} is null
|
||||
* @see #trim()
|
||||
*/
|
||||
public void removeAll(final IntList remove) {
|
||||
@@ -369,10 +389,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* memory.
|
||||
*
|
||||
*
|
||||
* @param predicate
|
||||
* the predicate
|
||||
* @throws NullPointerException
|
||||
* if the specified predicate is null
|
||||
* @param predicate the predicate
|
||||
* @throws NullPointerException if the specified predicate is null
|
||||
* @see #trim()
|
||||
*/
|
||||
public void removeIf(final IntPredicate predicate) {
|
||||
@@ -410,10 +428,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* {@code retain}. If {@code retain} is not sorted, then the complexity is
|
||||
* O(n*m).
|
||||
*
|
||||
* @param retain
|
||||
* the elements to retain
|
||||
* @throws NullPointerException
|
||||
* if the specified {@link IntList} is null
|
||||
* @param retain the elements to retain
|
||||
* @throws NullPointerException if the specified {@link IntList} is null
|
||||
* @see #trim()
|
||||
* @see #intersection(IntList, IntList)
|
||||
*/
|
||||
@@ -424,10 +440,9 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Replaces all values in the list by applying {@code operator}.
|
||||
*
|
||||
* @param operator
|
||||
* the operator
|
||||
* @throws NullPointerException
|
||||
* if the specified {@link UnaryIntOperator} is null
|
||||
* @param operator the operator
|
||||
* @throws NullPointerException if the specified {@link UnaryIntOperator} is
|
||||
* null
|
||||
*/
|
||||
public void replaceAll(final UnaryIntOperator operator) {
|
||||
|
||||
@@ -440,11 +455,9 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Returns the element at position {@code pos}.
|
||||
*
|
||||
* @param pos
|
||||
* position of the element to return
|
||||
* @param pos position of the element to return
|
||||
* @return the element at position {@code pos}
|
||||
* @throws IndexOutOfBoundsException
|
||||
* if {@code pos} is out of bounds
|
||||
* @throws IndexOutOfBoundsException if {@code pos} is out of bounds
|
||||
* {@code index < 0 || index >= size()}
|
||||
*/
|
||||
public int get(final int pos) {
|
||||
@@ -457,14 +470,13 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Returns the {@code length} elements starting at {@code from}.
|
||||
*
|
||||
* @param from
|
||||
* position of the first element
|
||||
* @param length
|
||||
* number of elements
|
||||
* @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
|
||||
* @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) {
|
||||
@@ -494,8 +506,7 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* all elements. A new array is returned otherwise.
|
||||
*
|
||||
* @param input
|
||||
* @throws NullPointerException
|
||||
* if the specified array is null
|
||||
* @throws NullPointerException if the specified array is null
|
||||
* @return an array containing all elements of this list
|
||||
*/
|
||||
public int[] toArray(final int[] input) {
|
||||
@@ -534,8 +545,7 @@ public final class IntList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Shuffles the list. The permutation is uniformly distributed.
|
||||
*
|
||||
* @param random
|
||||
* the random number generator used
|
||||
* @param random the random number generator used
|
||||
*/
|
||||
public void shuffle(final Random random) {
|
||||
|
||||
@@ -625,8 +635,7 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* <p>
|
||||
* This method uses a binary search algorithm if the list is sorted.
|
||||
*
|
||||
* @param value
|
||||
* the value
|
||||
* @param value the value
|
||||
* @return the index, or -1
|
||||
* @see #isSorted()
|
||||
*/
|
||||
@@ -640,17 +649,14 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* <p>
|
||||
* This method uses a binary search algorithm if the list is sorted.
|
||||
*
|
||||
* @param value
|
||||
* the value
|
||||
* @param offset
|
||||
* the offset (inclusive). There is no invalid value. If the offset
|
||||
* is negative, then it behaves as if it was 0. If it is >=
|
||||
* {@link #size()}, then -1 is returned.
|
||||
* @param value the value
|
||||
* @param offset the offset (inclusive). There is no invalid value. If the
|
||||
* offset is negative, then it behaves as if it was 0. If it is
|
||||
* >= {@link #size()}, then -1 is returned.
|
||||
* @return the index, or -1
|
||||
* @throws ArrayIndexOutOfBoundsException
|
||||
* if offset is negative, or larger than the size of the list
|
||||
* @throws IllegalArgumentException
|
||||
* if offset is negative
|
||||
* @throws ArrayIndexOutOfBoundsException if offset is negative, or larger than
|
||||
* the size of the list
|
||||
* @throws IllegalArgumentException if offset is negative
|
||||
* @see #isSorted()
|
||||
*/
|
||||
public int indexOf(final int value, final int offset) {
|
||||
@@ -680,8 +686,7 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* <p>
|
||||
* This method uses a binary search algorithm if the list is sorted.
|
||||
*
|
||||
* @param value
|
||||
* the value
|
||||
* @param value the value
|
||||
* @return the index, or -1
|
||||
* @see #isSorted()
|
||||
*/
|
||||
@@ -695,13 +700,11 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* <p>
|
||||
* This method uses a binary search algorithm if the list is sorted.
|
||||
*
|
||||
* @param value
|
||||
* the value
|
||||
* @param fromIndex
|
||||
* the index the start the search from (inclusive). There is no
|
||||
* @param value the value
|
||||
* @param fromIndex the index the start the search from (inclusive). There is no
|
||||
* invalid input. If {@code fromIndex} is < 0, then -1 is
|
||||
* returned. If it is larger than the length of the list, then it
|
||||
* behaves as if it were {@link #size()}-1.
|
||||
* returned. If it is larger than the length of the list, then
|
||||
* it behaves as if it were {@link #size()}-1.
|
||||
* @return the index, or -1
|
||||
* @see #isSorted()
|
||||
*/
|
||||
@@ -870,14 +873,11 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* list is not sorted, then the time complexity is O(n*m), where n is the length
|
||||
* of the shorter list and m the length of the longer list.
|
||||
*
|
||||
* @param a
|
||||
* a sorted {@link IntList}
|
||||
* @param b
|
||||
* a sorted {@link IntList}
|
||||
* @param a a sorted {@link IntList}
|
||||
* @param b a sorted {@link IntList}
|
||||
* @return {@link IntList} containing all elements that are in {@code a} and
|
||||
* {@code b}
|
||||
* @throws NullPointerException
|
||||
* if {@code a} or {@code b} is null
|
||||
* @throws NullPointerException if {@code a} or {@code b} is null
|
||||
* @see #retainAll(IntList)
|
||||
* @see #trim()
|
||||
*/
|
||||
@@ -896,10 +896,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* Implements an intersection algorithm with O(n+m), where n is the length of
|
||||
* the first list and m the length of the second list.
|
||||
*
|
||||
* @param a
|
||||
* first list
|
||||
* @param b
|
||||
* second list
|
||||
* @param a first list
|
||||
* @param b second list
|
||||
* @return the intersection
|
||||
*/
|
||||
private static IntList intersectionSorted(final IntList a, final IntList b) {
|
||||
@@ -939,10 +937,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* Implements an algorithm with O(n*m), where n is the length of the shorter
|
||||
* list and m the length of the longer list.
|
||||
*
|
||||
* @param a
|
||||
* first list
|
||||
* @param b
|
||||
* second list
|
||||
* @param a first list
|
||||
* @param b second list
|
||||
* @return the intersection
|
||||
*/
|
||||
private static IntList intersectionUnsorted(final IntList a, final IntList b) {
|
||||
@@ -985,10 +981,8 @@ public final class IntList implements Serializable, Cloneable {
|
||||
* list is not sorted, then the time complexity is O(m*log(m)), where m is the
|
||||
* length of the longer list.
|
||||
*
|
||||
* @param a
|
||||
* the first list
|
||||
* @param b
|
||||
* the second list
|
||||
* @param a the first list
|
||||
* @param b the second list
|
||||
* @return the union of both lists
|
||||
*/
|
||||
public static IntList union(final IntList a, final IntList b) {
|
||||
|
||||
@@ -52,10 +52,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Create a new {@link LongList}.
|
||||
*
|
||||
* @param initialCapacity
|
||||
* initial capacity
|
||||
* @throws IllegalArgumentException
|
||||
* if initial capacity is negative
|
||||
* @param initialCapacity initial capacity
|
||||
* @throws IllegalArgumentException if initial capacity is negative
|
||||
*/
|
||||
public LongList(final int initialCapacity) {
|
||||
if (initialCapacity < 0 || initialCapacity > MAX_ARRAY_SIZE) {
|
||||
@@ -70,10 +68,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* Create a new {@link LongList} with a copy of the elements of
|
||||
* {@code longList}.
|
||||
*
|
||||
* @param longList
|
||||
* the list to copy
|
||||
* @throws NullPointerException
|
||||
* if the specified {@link LongList} is null
|
||||
* @param longList the list to copy
|
||||
* @throws NullPointerException if the specified {@link LongList} is null
|
||||
*/
|
||||
public LongList(final LongList longList) {
|
||||
data = EMPTY_ARRAY;
|
||||
@@ -83,11 +79,9 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Create a new {@link LongList} with a copy of the given elements.
|
||||
*
|
||||
* @param values
|
||||
* the values
|
||||
* @param values the values
|
||||
* @return the list
|
||||
* @throws NullPointerException
|
||||
* if the specified array is null
|
||||
* @throws NullPointerException if the specified array is null
|
||||
*/
|
||||
public static LongList of(final long... values) {
|
||||
final LongList result = new LongList(values.length);
|
||||
@@ -95,6 +89,50 @@ public final class LongList implements Serializable, Cloneable {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link LongList} with values from {@code startInclusive} to
|
||||
* {@code endExclusive}-1.
|
||||
*
|
||||
* @param startInclusive the lower bound (inclusive)
|
||||
* @param endExclusive the upper bound (exclusive)
|
||||
* @return the {@link IntList}
|
||||
*/
|
||||
public static LongList range(final long startInclusive, final long endExclusive) {
|
||||
if (startInclusive >= endExclusive) {
|
||||
return new LongList(0);
|
||||
} else if (endExclusive - startInclusive > MAX_ARRAY_SIZE) {
|
||||
throw new IllegalArgumentException("Range of more than " + MAX_ARRAY_SIZE + " is not supported.");
|
||||
} else {
|
||||
final LongList result = new LongList((int) (endExclusive - startInclusive));
|
||||
for (long i = startInclusive; i < endExclusive; i++) {
|
||||
result.add(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link LongList} with values from {@code startInclusive} to
|
||||
* {@code endInclusive}.
|
||||
*
|
||||
* @param startInclusive the lower bound (inclusive)
|
||||
* @param endInclusive the upper bound (inclusive)
|
||||
* @return the {@link IntList}
|
||||
*/
|
||||
public static LongList rangeClosed(final long startInclusive, final long endInclusive) {
|
||||
if (startInclusive > endInclusive) {
|
||||
return new LongList(0);
|
||||
} else if (endInclusive - startInclusive + 1 > MAX_ARRAY_SIZE) {
|
||||
throw new IllegalArgumentException("Range of more than " + MAX_ARRAY_SIZE + " is not supported.");
|
||||
} else {
|
||||
final LongList result = new LongList((int) (endInclusive - startInclusive + 1));
|
||||
for (long i = startInclusive; i <= endInclusive; i++) {
|
||||
result.add(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this list contains no elements.
|
||||
*
|
||||
@@ -138,8 +176,7 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Adds {@code value} to the list.
|
||||
*
|
||||
* @param value
|
||||
* the value to add
|
||||
* @param value the value to add
|
||||
*/
|
||||
public void add(final long value) {
|
||||
ensureCapacity(1);
|
||||
@@ -155,14 +192,11 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* 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 the given array is null
|
||||
* @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 the given array is null
|
||||
*/
|
||||
public void insert(final int pos, final long... values) {
|
||||
|
||||
@@ -208,12 +242,10 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* 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()}
|
||||
* @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 long value) {
|
||||
|
||||
@@ -236,10 +268,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Add {@code values} to the list.
|
||||
*
|
||||
* @param values
|
||||
* the values to add
|
||||
* @throws NullPointerException
|
||||
* if the given array is null
|
||||
* @param values the values to add
|
||||
* @throws NullPointerException if the given array is null
|
||||
*/
|
||||
public void addAll(final long... values) {
|
||||
ensureCapacity(values.length);
|
||||
@@ -258,10 +288,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Add all value of the given list.
|
||||
*
|
||||
* @param list
|
||||
* the list
|
||||
* @throws NullPointerException
|
||||
* if the given list is null
|
||||
* @param list the list
|
||||
* @throws NullPointerException if the given list is null
|
||||
*/
|
||||
public void addAll(final LongList list) {
|
||||
ensureCapacity(list.size());
|
||||
@@ -291,14 +319,12 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* This method does not release any memory. Call {@link #trim()} to free unused
|
||||
* memory.
|
||||
*
|
||||
* @param fromIndex
|
||||
* index of the first element to remove
|
||||
* @param toIndex
|
||||
* the index of the last element to remove (exclusive)
|
||||
* @throws IndexOutOfBoundsException
|
||||
* if {@code fromIndex} or {@code toIndex} is negative, or if the
|
||||
* range defined by {@code fromIndex} and {@code toIndex} is out of
|
||||
* bounds
|
||||
* @param fromIndex index of the first element to remove
|
||||
* @param toIndex the index of the last element to remove (exclusive)
|
||||
* @throws IndexOutOfBoundsException if {@code fromIndex} or {@code toIndex} is
|
||||
* negative, or if the range defined by
|
||||
* {@code fromIndex} and {@code toIndex} is
|
||||
* out of bounds
|
||||
* @see #trim()
|
||||
*/
|
||||
public void remove(final int fromIndex, final int toIndex) {
|
||||
@@ -338,10 +364,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* {@code retain}. If {@code retain} is not sorted, then the complexity is
|
||||
* O(n*m).
|
||||
*
|
||||
* @param remove
|
||||
* the elements to remove
|
||||
* @throws NullPointerException
|
||||
* if the specified {@link LongList} is null
|
||||
* @param remove the elements to remove
|
||||
* @throws NullPointerException if the specified {@link LongList} is null
|
||||
* @see #trim()
|
||||
*/
|
||||
public void removeAll(final LongList remove) {
|
||||
@@ -355,10 +379,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* memory.
|
||||
*
|
||||
*
|
||||
* @param predicate
|
||||
* the predicate
|
||||
* @throws NullPointerException
|
||||
* if the specified predicate is null
|
||||
* @param predicate the predicate
|
||||
* @throws NullPointerException if the specified predicate is null
|
||||
* @see #trim()
|
||||
*/
|
||||
public void removeIf(final LongPredicate predicate) {
|
||||
@@ -396,10 +418,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* {@code retain}. If {@code retain} is not sorted, then the complexity is
|
||||
* O(n*m).
|
||||
*
|
||||
* @param retain
|
||||
* the elements to retain
|
||||
* @throws NullPointerException
|
||||
* if the specified {@link LongList} is null
|
||||
* @param retain the elements to retain
|
||||
* @throws NullPointerException if the specified {@link LongList} is null
|
||||
* @see #trim()
|
||||
* @see #intersection(LongList, LongList)
|
||||
*/
|
||||
@@ -410,10 +430,9 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Replaces all values in the list by applying {@code operator}.
|
||||
*
|
||||
* @param operator
|
||||
* the operator
|
||||
* @throws NullPointerException
|
||||
* if the specified {@link UnaryLongOperator} is null
|
||||
* @param operator the operator
|
||||
* @throws NullPointerException if the specified {@link UnaryLongOperator} is
|
||||
* null
|
||||
*/
|
||||
public void replaceAll(final UnaryLongOperator operator) {
|
||||
|
||||
@@ -426,11 +445,9 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Returns the element at position {@code pos}.
|
||||
*
|
||||
* @param pos
|
||||
* position of the element to return
|
||||
* @param pos position of the element to return
|
||||
* @return the element at position {@code pos}
|
||||
* @throws IndexOutOfBoundsException
|
||||
* if {@code pos} is out of bounds
|
||||
* @throws IndexOutOfBoundsException if {@code pos} is out of bounds
|
||||
* {@code index < 0 || index >= size()}
|
||||
*/
|
||||
public long get(final int pos) {
|
||||
@@ -443,14 +460,13 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Returns the {@code length} elements starting at {@code from}.
|
||||
*
|
||||
* @param from
|
||||
* position of the first element
|
||||
* @param length
|
||||
* number of elements
|
||||
* @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
|
||||
* @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 long[] get(final int from, final int length) {
|
||||
if (from < 0) {
|
||||
@@ -480,8 +496,7 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* all elements. A new array is returned otherwise.
|
||||
*
|
||||
* @param input
|
||||
* @throws NullPointerException
|
||||
* if the specified array is null
|
||||
* @throws NullPointerException if the specified array is null
|
||||
* @return an array containing all elements of this list
|
||||
*/
|
||||
public long[] toArray(final long[] input) {
|
||||
@@ -520,8 +535,7 @@ public final class LongList implements Serializable, Cloneable {
|
||||
/**
|
||||
* Shuffles the list. The permutation is uniformly distributed.
|
||||
*
|
||||
* @param random
|
||||
* the random number generator used
|
||||
* @param random the random number generator used
|
||||
*/
|
||||
public void shuffle(final Random random) {
|
||||
|
||||
@@ -611,8 +625,7 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* <p>
|
||||
* This method uses a binary search algorithm if the list is sorted.
|
||||
*
|
||||
* @param value
|
||||
* the value
|
||||
* @param value the value
|
||||
* @return the index, or -1
|
||||
* @see #isSorted()
|
||||
*/
|
||||
@@ -626,17 +639,14 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* <p>
|
||||
* This method uses a binary search algorithm if the list is sorted.
|
||||
*
|
||||
* @param value
|
||||
* the value
|
||||
* @param offset
|
||||
* the offset (inclusive). There is no invalid value. If the offset
|
||||
* is negative, then it behaves as if it was 0. If it is >=
|
||||
* {@link #size()}, then -1 is returned.
|
||||
* @param value the value
|
||||
* @param offset the offset (inclusive). There is no invalid value. If the
|
||||
* offset is negative, then it behaves as if it was 0. If it is
|
||||
* >= {@link #size()}, then -1 is returned.
|
||||
* @return the index, or -1
|
||||
* @throws ArrayIndexOutOfBoundsException
|
||||
* if offset is negative, or larger than the size of the list
|
||||
* @throws IllegalArgumentException
|
||||
* if offset is negative
|
||||
* @throws ArrayIndexOutOfBoundsException if offset is negative, or larger than
|
||||
* the size of the list
|
||||
* @throws IllegalArgumentException if offset is negative
|
||||
* @see #isSorted()
|
||||
*/
|
||||
public int indexOf(final long value, final int offset) {
|
||||
@@ -666,8 +676,7 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* <p>
|
||||
* This method uses a binary search algorithm if the list is sorted.
|
||||
*
|
||||
* @param value
|
||||
* the value
|
||||
* @param value the value
|
||||
* @return the index, or -1
|
||||
* @see #isSorted()
|
||||
*/
|
||||
@@ -681,13 +690,11 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* <p>
|
||||
* This method uses a binary search algorithm if the list is sorted.
|
||||
*
|
||||
* @param value
|
||||
* the value
|
||||
* @param fromIndex
|
||||
* the index the start the search from (inclusive). There is no
|
||||
* @param value the value
|
||||
* @param fromIndex the index the start the search from (inclusive). There is no
|
||||
* invalid input. If {@code fromIndex} is < 0, then -1 is
|
||||
* returned. If it is larger than the length of the list, then it
|
||||
* behaves as if it were {@link #size()}-1.
|
||||
* returned. If it is larger than the length of the list, then
|
||||
* it behaves as if it were {@link #size()}-1.
|
||||
* @return the index, or -1
|
||||
* @see #isSorted()
|
||||
*/
|
||||
@@ -850,14 +857,11 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* list is not sorted, then the time complexity is O(n*m), where n is the length
|
||||
* of the shorter list and m the length of the longer list.
|
||||
*
|
||||
* @param a
|
||||
* a sorted {@link LongList}
|
||||
* @param b
|
||||
* a sorted {@link LongList}
|
||||
* @param a a sorted {@link LongList}
|
||||
* @param b a sorted {@link LongList}
|
||||
* @return {@link LongList} containing all elements that are in {@code a} and
|
||||
* {@code b}
|
||||
* @throws NullPointerException
|
||||
* if {@code a} or {@code b} is null
|
||||
* @throws NullPointerException if {@code a} or {@code b} is null
|
||||
* @see #retainAll(LongList)
|
||||
* @see #trim()
|
||||
*/
|
||||
@@ -876,10 +880,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* Implements an intersection algorithm with O(n+m), where n is the length of
|
||||
* the first list and m the length of the second list.
|
||||
*
|
||||
* @param a
|
||||
* first list
|
||||
* @param b
|
||||
* second list
|
||||
* @param a first list
|
||||
* @param b second list
|
||||
* @return the intersection
|
||||
*/
|
||||
private static LongList intersectionSorted(final LongList a, final LongList b) {
|
||||
@@ -919,10 +921,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* Implements an algorithm with O(n*m), where n is the length of the shorter
|
||||
* list and m the length of the longer list.
|
||||
*
|
||||
* @param a
|
||||
* first list
|
||||
* @param b
|
||||
* second list
|
||||
* @param a first list
|
||||
* @param b second list
|
||||
* @return the intersection
|
||||
*/
|
||||
private static LongList intersectionUnsorted(final LongList a, final LongList b) {
|
||||
@@ -965,10 +965,8 @@ public final class LongList implements Serializable, Cloneable {
|
||||
* list is not sorted, then the time complexity is O(m*log(m)), where m is the
|
||||
* length of the longer list.
|
||||
*
|
||||
* @param a
|
||||
* the first list
|
||||
* @param b
|
||||
* the second list
|
||||
* @param a the first list
|
||||
* @param b the second list
|
||||
* @return the union of both lists
|
||||
*/
|
||||
public static LongList union(final LongList a, final LongList b) {
|
||||
|
||||
@@ -45,6 +45,69 @@ public class IntListTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRange() {
|
||||
final IntList expected = IntList.of(1, 2, 3, 4, 5);
|
||||
final IntList list = IntList.range(1, 6);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(5, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangeWithNegativeValues() {
|
||||
final IntList expected = IntList.of(Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2);
|
||||
final IntList list = IntList.range(Integer.MIN_VALUE, Integer.MIN_VALUE + 3);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(3, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyRange() {
|
||||
final IntList expected = IntList.of();
|
||||
final IntList list = IntList.range(1, 1);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(0, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeRange() {
|
||||
final IntList expected = IntList.of();
|
||||
final IntList list = IntList.range(1, 0);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(0, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangeClosed() {
|
||||
final IntList expected = IntList.of(1, 2, 3, 4, 5);
|
||||
final IntList list = IntList.rangeClosed(1, 5);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(5, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangeClosedWithNegativeValues() {
|
||||
final IntList expected = IntList.of(Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2);
|
||||
final IntList list = IntList.rangeClosed(Integer.MIN_VALUE, Integer.MIN_VALUE + 2);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(3, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneElementRangeClosed() {
|
||||
final IntList expected = IntList.of(1);
|
||||
final IntList list = IntList.rangeClosed(1, 1);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(1, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyRangeClosed() {
|
||||
final IntList expected = IntList.of();
|
||||
final IntList list = IntList.rangeClosed(1, 0);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
// setting initial size to one, so that the first add does not need to resize,
|
||||
|
||||
@@ -45,6 +45,77 @@ public class LongListTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRange() {
|
||||
final LongList expected = LongList.of(1, 2, 3, 4, 5);
|
||||
final LongList list = LongList.range(1, 6);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(5, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangeWithNegativeValues() {
|
||||
final LongList expected = LongList.of(Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2);
|
||||
final LongList list = LongList.range(Long.MIN_VALUE, Long.MIN_VALUE + 3);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(3, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyRange() {
|
||||
final LongList expected = LongList.of();
|
||||
final LongList list = LongList.range(1, 1);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(0, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeRange() {
|
||||
final LongList expected = LongList.of();
|
||||
final LongList list = LongList.range(1, 0);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(0, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangeClosed() {
|
||||
final IntList expected = IntList.of(1, 2, 3, 4, 5);
|
||||
final IntList list = IntList.rangeClosed(1, 5);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(5, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangeClosedWithNegativeValues() {
|
||||
final LongList expected = LongList.of(Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 2);
|
||||
final LongList list = LongList.rangeClosed(Long.MIN_VALUE, Long.MIN_VALUE + 2);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(3, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneElementRangeClosed() {
|
||||
final LongList expected = LongList.of(1);
|
||||
final LongList list = LongList.rangeClosed(1, 1);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(1, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyRangeClosed() {
|
||||
final LongList expected = LongList.of();
|
||||
final LongList list = LongList.rangeClosed(1, 0);
|
||||
Assertions.assertArrayEquals(expected.toArray(), list.toArray());
|
||||
Assertions.assertEquals(0, list.getCapacity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangeClosedTooBig() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> {
|
||||
LongList.range(0, Integer.MAX_VALUE + 1L);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
// setting initial size to one, so that the first add does not need to resize,
|
||||
|
||||
Reference in New Issue
Block a user