add *List.range() and *List.rangeClosed()

This commit is contained in:
2018-09-08 08:48:47 +02:00
parent 336905fafe
commit 1d0d3e6d2b
4 changed files with 356 additions and 230 deletions

View File

@@ -67,10 +67,8 @@ public final class IntList implements Serializable, Cloneable {
/** /**
* Create a new {@link IntList}. * Create a new {@link IntList}.
* *
* @param initialCapacity * @param initialCapacity initial capacity
* initial capacity * @throws IllegalArgumentException if initial capacity is negative
* @throws IllegalArgumentException
* if initial capacity is negative
*/ */
public IntList(final int initialCapacity) { public IntList(final int initialCapacity) {
if (initialCapacity < 0 || initialCapacity > MAX_ARRAY_SIZE) { 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}. * Create a new {@link IntList} with a copy of the elements of {@code intList}.
* *
* @param intList * @param intList the list to copy
* the list to copy * @throws NullPointerException if the specified {@link IntList} is null
* @throws NullPointerException
* if the specified {@link IntList} is null
*/ */
public IntList(final IntList intList) { public IntList(final IntList intList) {
data = EMPTY_ARRAY; 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. * Create a new {@link IntList} with a copy of the given elements.
* *
* @param values * @param values the values
* the values
* @return the list * @return the list
* @throws NullPointerException * @throws NullPointerException if the specified array is null
* if the specified array is null
*/ */
public static IntList of(final int... values) { public static IntList of(final int... values) {
final IntList result = new IntList(values.length); final IntList result = new IntList(values.length);
@@ -109,6 +103,46 @@ public final class IntList implements Serializable, Cloneable {
return result; 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. * 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. * Adds {@code value} to the list.
* *
* @param value * @param value the value to add
* the value to add
*/ */
public void add(final int value) { public void add(final int value) {
ensureCapacity(1); ensureCapacity(1);
@@ -169,14 +202,11 @@ public final class IntList implements Serializable, Cloneable {
/** /**
* Inserts {@code values} at position {@code pos} into the list. * Inserts {@code values} at position {@code pos} into the list.
* *
* @param pos * @param pos the position to insert the elements
* the position to insert the elements * @param values the elements to insert
* @param values * @throws IndexOutOfBoundsException if pos is out of bounds
* the elements to insert * {@code pos < 0 || pos > size()}
* @throws IndexOutOfBoundsException * @throws NullPointerException if the given array is null
* 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) { 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}. * Set the value {@code value} at position {@code pos}.
* *
* @param pos * @param pos the position to overwrite
* the position to overwrite * @param value the new value
* @param value * @throws IndexOutOfBoundsException if pos is out of bounds
* the new value * {@code pos < 0 || pos >= size()}
* @throws IndexOutOfBoundsException
* if pos is out of bounds {@code pos < 0 || pos >= size()}
*/ */
public void set(final int pos, final int value) { 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. * Add {@code values} to the list.
* *
* @param values * @param values the values to add
* the values to add * @throws NullPointerException if the given array is null
* @throws NullPointerException
* if the given array is null
*/ */
public void addAll(final int... values) { public void addAll(final int... values) {
ensureCapacity(values.length); ensureCapacity(values.length);
@@ -272,10 +298,8 @@ public final class IntList implements Serializable, Cloneable {
/** /**
* Add all value of the given list. * Add all value of the given list.
* *
* @param list * @param list the list
* the list * @throws NullPointerException if the given list is null
* @throws NullPointerException
* if the given list is null
*/ */
public void addAll(final IntList list) { public void addAll(final IntList list) {
ensureCapacity(list.size()); 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 * This method does not release any memory. Call {@link #trim()} to free unused
* memory. * memory.
* *
* @param fromIndex * @param fromIndex index of the first element to remove
* index of the first element to remove * @param toIndex the index of the last element to remove (exclusive)
* @param toIndex * @throws IndexOutOfBoundsException if {@code fromIndex} or {@code toIndex} is
* the index of the last element to remove (exclusive) * negative, or if the range defined by
* @throws IndexOutOfBoundsException * {@code fromIndex} and {@code toIndex} is
* if {@code fromIndex} or {@code toIndex} is negative, or if the * out of bounds
* range defined by {@code fromIndex} and {@code toIndex} is out of
* bounds
* @see #trim() * @see #trim()
*/ */
public void remove(final int fromIndex, final int toIndex) { 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 * {@code retain}. If {@code retain} is not sorted, then the complexity is
* O(n*m). * O(n*m).
* *
* @param remove * @param remove the elements to remove
* the elements to remove * @throws NullPointerException if the specified {@link IntList} is null
* @throws NullPointerException
* if the specified {@link IntList} is null
* @see #trim() * @see #trim()
*/ */
public void removeAll(final IntList remove) { public void removeAll(final IntList remove) {
@@ -369,10 +389,8 @@ public final class IntList implements Serializable, Cloneable {
* memory. * memory.
* *
* *
* @param predicate * @param predicate the predicate
* the predicate * @throws NullPointerException if the specified predicate is null
* @throws NullPointerException
* if the specified predicate is null
* @see #trim() * @see #trim()
*/ */
public void removeIf(final IntPredicate predicate) { 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 * {@code retain}. If {@code retain} is not sorted, then the complexity is
* O(n*m). * O(n*m).
* *
* @param retain * @param retain the elements to retain
* the elements to retain * @throws NullPointerException if the specified {@link IntList} is null
* @throws NullPointerException
* if the specified {@link IntList} is null
* @see #trim() * @see #trim()
* @see #intersection(IntList, IntList) * @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}. * Replaces all values in the list by applying {@code operator}.
* *
* @param operator * @param operator the operator
* the operator * @throws NullPointerException if the specified {@link UnaryIntOperator} is
* @throws NullPointerException * null
* if the specified {@link UnaryIntOperator} is null
*/ */
public void replaceAll(final UnaryIntOperator operator) { public void replaceAll(final UnaryIntOperator operator) {
@@ -440,11 +455,9 @@ public final class IntList implements Serializable, Cloneable {
/** /**
* Returns the element at position {@code pos}. * Returns the element at position {@code pos}.
* *
* @param pos * @param pos position of the element to return
* position of the element to return
* @return the element at position {@code pos} * @return the element at position {@code pos}
* @throws IndexOutOfBoundsException * @throws IndexOutOfBoundsException if {@code pos} is out of bounds
* if {@code pos} is out of bounds
* {@code index < 0 || index >= size()} * {@code index < 0 || index >= size()}
*/ */
public int get(final int pos) { 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}. * Returns the {@code length} elements starting at {@code from}.
* *
* @param from * @param from position of the first element
* position of the first element * @param length number of elements
* @param length
* number of elements
* @return the {@code length} elements starting at {@code from} * @return the {@code length} elements starting at {@code from}
* @throws IndexOutOfBoundsException * @throws IndexOutOfBoundsException if {@code from} or {@code length} is
* if {@code from} or {@code length} is negative, or if the range * negative, or if the range defined by
* defined by {@code from} and {@code length} is out of bounds * {@code from} and {@code length} is out of
* bounds
*/ */
public int[] get(final int from, final int length) { public int[] get(final int from, final int length) {
if (from < 0) { if (from < 0) {
@@ -494,8 +506,7 @@ public final class IntList implements Serializable, Cloneable {
* all elements. A new array is returned otherwise. * all elements. A new array is returned otherwise.
* *
* @param input * @param input
* @throws NullPointerException * @throws NullPointerException if the specified array is null
* if the specified array is null
* @return an array containing all elements of this list * @return an array containing all elements of this list
*/ */
public int[] toArray(final int[] input) { 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. * Shuffles the list. The permutation is uniformly distributed.
* *
* @param random * @param random the random number generator used
* the random number generator used
*/ */
public void shuffle(final Random random) { public void shuffle(final Random random) {
@@ -625,8 +635,7 @@ public final class IntList implements Serializable, Cloneable {
* <p> * <p>
* This method uses a binary search algorithm if the list is sorted. * This method uses a binary search algorithm if the list is sorted.
* *
* @param value * @param value the value
* the value
* @return the index, or -1 * @return the index, or -1
* @see #isSorted() * @see #isSorted()
*/ */
@@ -640,17 +649,14 @@ public final class IntList implements Serializable, Cloneable {
* <p> * <p>
* This method uses a binary search algorithm if the list is sorted. * This method uses a binary search algorithm if the list is sorted.
* *
* @param value * @param value the value
* the value * @param offset the offset (inclusive). There is no invalid value. If the
* @param offset * offset is negative, then it behaves as if it was 0. If it is
* the offset (inclusive). There is no invalid value. If the offset * &gt;= {@link #size()}, then -1 is returned.
* is negative, then it behaves as if it was 0. If it is &gt;=
* {@link #size()}, then -1 is returned.
* @return the index, or -1 * @return the index, or -1
* @throws ArrayIndexOutOfBoundsException * @throws ArrayIndexOutOfBoundsException if offset is negative, or larger than
* if offset is negative, or larger than the size of the list * the size of the list
* @throws IllegalArgumentException * @throws IllegalArgumentException if offset is negative
* if offset is negative
* @see #isSorted() * @see #isSorted()
*/ */
public int indexOf(final int value, final int offset) { public int indexOf(final int value, final int offset) {
@@ -680,8 +686,7 @@ public final class IntList implements Serializable, Cloneable {
* <p> * <p>
* This method uses a binary search algorithm if the list is sorted. * This method uses a binary search algorithm if the list is sorted.
* *
* @param value * @param value the value
* the value
* @return the index, or -1 * @return the index, or -1
* @see #isSorted() * @see #isSorted()
*/ */
@@ -695,13 +700,11 @@ public final class IntList implements Serializable, Cloneable {
* <p> * <p>
* This method uses a binary search algorithm if the list is sorted. * This method uses a binary search algorithm if the list is sorted.
* *
* @param value * @param value the value
* the value * @param fromIndex the index the start the search from (inclusive). There is no
* @param fromIndex
* the index the start the search from (inclusive). There is no
* invalid input. If {@code fromIndex} is &lt; 0, then -1 is * invalid input. If {@code fromIndex} is &lt; 0, then -1 is
* returned. If it is larger than the length of the list, then it * returned. If it is larger than the length of the list, then
* behaves as if it were {@link #size()}-1. * it behaves as if it were {@link #size()}-1.
* @return the index, or -1 * @return the index, or -1
* @see #isSorted() * @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 * 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. * of the shorter list and m the length of the longer list.
* *
* @param a * @param a a sorted {@link IntList}
* a sorted {@link IntList} * @param b a sorted {@link IntList}
* @param b
* a sorted {@link IntList}
* @return {@link IntList} containing all elements that are in {@code a} and * @return {@link IntList} containing all elements that are in {@code a} and
* {@code b} * {@code b}
* @throws NullPointerException * @throws NullPointerException if {@code a} or {@code b} is null
* if {@code a} or {@code b} is null
* @see #retainAll(IntList) * @see #retainAll(IntList)
* @see #trim() * @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 * 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. * the first list and m the length of the second list.
* *
* @param a * @param a first list
* first list * @param b second list
* @param b
* second list
* @return the intersection * @return the intersection
*/ */
private static IntList intersectionSorted(final IntList a, final IntList b) { 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 * Implements an algorithm with O(n*m), where n is the length of the shorter
* list and m the length of the longer list. * list and m the length of the longer list.
* *
* @param a * @param a first list
* first list * @param b second list
* @param b
* second list
* @return the intersection * @return the intersection
*/ */
private static IntList intersectionUnsorted(final IntList a, final IntList b) { 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 * list is not sorted, then the time complexity is O(m*log(m)), where m is the
* length of the longer list. * length of the longer list.
* *
* @param a * @param a the first list
* the first list * @param b the second list
* @param b
* the second list
* @return the union of both lists * @return the union of both lists
*/ */
public static IntList union(final IntList a, final IntList b) { public static IntList union(final IntList a, final IntList b) {

View File

@@ -52,10 +52,8 @@ public final class LongList implements Serializable, Cloneable {
/** /**
* Create a new {@link LongList}. * Create a new {@link LongList}.
* *
* @param initialCapacity * @param initialCapacity initial capacity
* initial capacity * @throws IllegalArgumentException if initial capacity is negative
* @throws IllegalArgumentException
* if initial capacity is negative
*/ */
public LongList(final int initialCapacity) { public LongList(final int initialCapacity) {
if (initialCapacity < 0 || initialCapacity > MAX_ARRAY_SIZE) { 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 * Create a new {@link LongList} with a copy of the elements of
* {@code longList}. * {@code longList}.
* *
* @param longList * @param longList the list to copy
* the list to copy * @throws NullPointerException if the specified {@link LongList} is null
* @throws NullPointerException
* if the specified {@link LongList} is null
*/ */
public LongList(final LongList longList) { public LongList(final LongList longList) {
data = EMPTY_ARRAY; 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. * Create a new {@link LongList} with a copy of the given elements.
* *
* @param values * @param values the values
* the values
* @return the list * @return the list
* @throws NullPointerException * @throws NullPointerException if the specified array is null
* if the specified array is null
*/ */
public static LongList of(final long... values) { public static LongList of(final long... values) {
final LongList result = new LongList(values.length); final LongList result = new LongList(values.length);
@@ -95,6 +89,50 @@ public final class LongList implements Serializable, Cloneable {
return result; 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. * 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. * Adds {@code value} to the list.
* *
* @param value * @param value the value to add
* the value to add
*/ */
public void add(final long value) { public void add(final long value) {
ensureCapacity(1); ensureCapacity(1);
@@ -155,14 +192,11 @@ public final class LongList implements Serializable, Cloneable {
/** /**
* Inserts {@code values} at position {@code pos} into the list. * Inserts {@code values} at position {@code pos} into the list.
* *
* @param pos * @param pos the position to insert the elements
* the position to insert the elements * @param values the elements to insert
* @param values * @throws IndexOutOfBoundsException if pos is out of bounds
* the elements to insert * {@code pos < 0 || pos > size()}
* @throws IndexOutOfBoundsException * @throws NullPointerException if the given array is null
* 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) { 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}. * Set the value {@code value} at position {@code pos}.
* *
* @param pos * @param pos the position to overwrite
* the position to overwrite * @param value the new value
* @param value * @throws IndexOutOfBoundsException if pos is out of bounds
* the new value * {@code pos < 0 || pos >= size()}
* @throws IndexOutOfBoundsException
* if pos is out of bounds {@code pos < 0 || pos >= size()}
*/ */
public void set(final int pos, final long value) { 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. * Add {@code values} to the list.
* *
* @param values * @param values the values to add
* the values to add * @throws NullPointerException if the given array is null
* @throws NullPointerException
* if the given array is null
*/ */
public void addAll(final long... values) { public void addAll(final long... values) {
ensureCapacity(values.length); ensureCapacity(values.length);
@@ -258,10 +288,8 @@ public final class LongList implements Serializable, Cloneable {
/** /**
* Add all value of the given list. * Add all value of the given list.
* *
* @param list * @param list the list
* the list * @throws NullPointerException if the given list is null
* @throws NullPointerException
* if the given list is null
*/ */
public void addAll(final LongList list) { public void addAll(final LongList list) {
ensureCapacity(list.size()); 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 * This method does not release any memory. Call {@link #trim()} to free unused
* memory. * memory.
* *
* @param fromIndex * @param fromIndex index of the first element to remove
* index of the first element to remove * @param toIndex the index of the last element to remove (exclusive)
* @param toIndex * @throws IndexOutOfBoundsException if {@code fromIndex} or {@code toIndex} is
* the index of the last element to remove (exclusive) * negative, or if the range defined by
* @throws IndexOutOfBoundsException * {@code fromIndex} and {@code toIndex} is
* if {@code fromIndex} or {@code toIndex} is negative, or if the * out of bounds
* range defined by {@code fromIndex} and {@code toIndex} is out of
* bounds
* @see #trim() * @see #trim()
*/ */
public void remove(final int fromIndex, final int toIndex) { 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 * {@code retain}. If {@code retain} is not sorted, then the complexity is
* O(n*m). * O(n*m).
* *
* @param remove * @param remove the elements to remove
* the elements to remove * @throws NullPointerException if the specified {@link LongList} is null
* @throws NullPointerException
* if the specified {@link LongList} is null
* @see #trim() * @see #trim()
*/ */
public void removeAll(final LongList remove) { public void removeAll(final LongList remove) {
@@ -355,10 +379,8 @@ public final class LongList implements Serializable, Cloneable {
* memory. * memory.
* *
* *
* @param predicate * @param predicate the predicate
* the predicate * @throws NullPointerException if the specified predicate is null
* @throws NullPointerException
* if the specified predicate is null
* @see #trim() * @see #trim()
*/ */
public void removeIf(final LongPredicate predicate) { 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 * {@code retain}. If {@code retain} is not sorted, then the complexity is
* O(n*m). * O(n*m).
* *
* @param retain * @param retain the elements to retain
* the elements to retain * @throws NullPointerException if the specified {@link LongList} is null
* @throws NullPointerException
* if the specified {@link LongList} is null
* @see #trim() * @see #trim()
* @see #intersection(LongList, LongList) * @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}. * Replaces all values in the list by applying {@code operator}.
* *
* @param operator * @param operator the operator
* the operator * @throws NullPointerException if the specified {@link UnaryLongOperator} is
* @throws NullPointerException * null
* if the specified {@link UnaryLongOperator} is null
*/ */
public void replaceAll(final UnaryLongOperator operator) { public void replaceAll(final UnaryLongOperator operator) {
@@ -426,11 +445,9 @@ public final class LongList implements Serializable, Cloneable {
/** /**
* Returns the element at position {@code pos}. * Returns the element at position {@code pos}.
* *
* @param pos * @param pos position of the element to return
* position of the element to return
* @return the element at position {@code pos} * @return the element at position {@code pos}
* @throws IndexOutOfBoundsException * @throws IndexOutOfBoundsException if {@code pos} is out of bounds
* if {@code pos} is out of bounds
* {@code index < 0 || index >= size()} * {@code index < 0 || index >= size()}
*/ */
public long get(final int pos) { 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}. * Returns the {@code length} elements starting at {@code from}.
* *
* @param from * @param from position of the first element
* position of the first element * @param length number of elements
* @param length
* number of elements
* @return the {@code length} elements starting at {@code from} * @return the {@code length} elements starting at {@code from}
* @throws IndexOutOfBoundsException * @throws IndexOutOfBoundsException if {@code from} or {@code length} is
* if {@code from} or {@code length} is negative, or if the range * negative, or if the range defined by
* defined by {@code from} and {@code length} is out of bounds * {@code from} and {@code length} is out of
* bounds
*/ */
public long[] get(final int from, final int length) { public long[] get(final int from, final int length) {
if (from < 0) { if (from < 0) {
@@ -480,8 +496,7 @@ public final class LongList implements Serializable, Cloneable {
* all elements. A new array is returned otherwise. * all elements. A new array is returned otherwise.
* *
* @param input * @param input
* @throws NullPointerException * @throws NullPointerException if the specified array is null
* if the specified array is null
* @return an array containing all elements of this list * @return an array containing all elements of this list
*/ */
public long[] toArray(final long[] input) { 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. * Shuffles the list. The permutation is uniformly distributed.
* *
* @param random * @param random the random number generator used
* the random number generator used
*/ */
public void shuffle(final Random random) { public void shuffle(final Random random) {
@@ -611,8 +625,7 @@ public final class LongList implements Serializable, Cloneable {
* <p> * <p>
* This method uses a binary search algorithm if the list is sorted. * This method uses a binary search algorithm if the list is sorted.
* *
* @param value * @param value the value
* the value
* @return the index, or -1 * @return the index, or -1
* @see #isSorted() * @see #isSorted()
*/ */
@@ -626,17 +639,14 @@ public final class LongList implements Serializable, Cloneable {
* <p> * <p>
* This method uses a binary search algorithm if the list is sorted. * This method uses a binary search algorithm if the list is sorted.
* *
* @param value * @param value the value
* the value * @param offset the offset (inclusive). There is no invalid value. If the
* @param offset * offset is negative, then it behaves as if it was 0. If it is
* the offset (inclusive). There is no invalid value. If the offset * &gt;= {@link #size()}, then -1 is returned.
* is negative, then it behaves as if it was 0. If it is &gt;=
* {@link #size()}, then -1 is returned.
* @return the index, or -1 * @return the index, or -1
* @throws ArrayIndexOutOfBoundsException * @throws ArrayIndexOutOfBoundsException if offset is negative, or larger than
* if offset is negative, or larger than the size of the list * the size of the list
* @throws IllegalArgumentException * @throws IllegalArgumentException if offset is negative
* if offset is negative
* @see #isSorted() * @see #isSorted()
*/ */
public int indexOf(final long value, final int offset) { public int indexOf(final long value, final int offset) {
@@ -666,8 +676,7 @@ public final class LongList implements Serializable, Cloneable {
* <p> * <p>
* This method uses a binary search algorithm if the list is sorted. * This method uses a binary search algorithm if the list is sorted.
* *
* @param value * @param value the value
* the value
* @return the index, or -1 * @return the index, or -1
* @see #isSorted() * @see #isSorted()
*/ */
@@ -681,13 +690,11 @@ public final class LongList implements Serializable, Cloneable {
* <p> * <p>
* This method uses a binary search algorithm if the list is sorted. * This method uses a binary search algorithm if the list is sorted.
* *
* @param value * @param value the value
* the value * @param fromIndex the index the start the search from (inclusive). There is no
* @param fromIndex
* the index the start the search from (inclusive). There is no
* invalid input. If {@code fromIndex} is &lt; 0, then -1 is * invalid input. If {@code fromIndex} is &lt; 0, then -1 is
* returned. If it is larger than the length of the list, then it * returned. If it is larger than the length of the list, then
* behaves as if it were {@link #size()}-1. * it behaves as if it were {@link #size()}-1.
* @return the index, or -1 * @return the index, or -1
* @see #isSorted() * @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 * 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. * of the shorter list and m the length of the longer list.
* *
* @param a * @param a a sorted {@link LongList}
* a sorted {@link LongList} * @param b a sorted {@link LongList}
* @param b
* a sorted {@link LongList}
* @return {@link LongList} containing all elements that are in {@code a} and * @return {@link LongList} containing all elements that are in {@code a} and
* {@code b} * {@code b}
* @throws NullPointerException * @throws NullPointerException if {@code a} or {@code b} is null
* if {@code a} or {@code b} is null
* @see #retainAll(LongList) * @see #retainAll(LongList)
* @see #trim() * @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 * 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. * the first list and m the length of the second list.
* *
* @param a * @param a first list
* first list * @param b second list
* @param b
* second list
* @return the intersection * @return the intersection
*/ */
private static LongList intersectionSorted(final LongList a, final LongList b) { 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 * Implements an algorithm with O(n*m), where n is the length of the shorter
* list and m the length of the longer list. * list and m the length of the longer list.
* *
* @param a * @param a first list
* first list * @param b second list
* @param b
* second list
* @return the intersection * @return the intersection
*/ */
private static LongList intersectionUnsorted(final LongList a, final LongList b) { 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 * list is not sorted, then the time complexity is O(m*log(m)), where m is the
* length of the longer list. * length of the longer list.
* *
* @param a * @param a the first list
* the first list * @param b the second list
* @param b
* the second list
* @return the union of both lists * @return the union of both lists
*/ */
public static LongList union(final LongList a, final LongList b) { public static LongList union(final LongList a, final LongList b) {

View File

@@ -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 @Test
public void testAdd() { public void testAdd() {
// setting initial size to one, so that the first add does not need to resize, // setting initial size to one, so that the first add does not need to resize,

View File

@@ -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 @Test
public void testAdd() { public void testAdd() {
// setting initial size to one, so that the first add does not need to resize, // setting initial size to one, so that the first add does not need to resize,