Package com.github.tommyettinger.gand.ds
Class IntList
java.lang.Object
com.github.tommyettinger.gand.ds.IntList
- All Implemented Interfaces:
IntCollection
A resizable, insertion-ordered int list. Primitive-backed, so it avoids the boxing that occurs with an ArrayList of Integer.
This tries to imitate most of the
List
interface, though it can't implement it without boxing its items.
Has a primitive iterator accessible via iterator()
.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
AIntIterator
, plusListIterator
methods, over the elements of a IntList. -
Field Summary
Modifier and TypeFieldDescriptionint[]
protected IntList.IntListIterator
protected IntList.IntListIterator
protected int
-
Constructor Summary
ConstructorDescriptionIntList()
Creates an ordered list with a capacity of 10.IntList
(int capacity) Creates an ordered list with the specified capacity.IntList
(int[] array) Creates a new list containing the elements in the specified array.IntList
(int[] array, int startIndex, int count) Creates a new list containing the elements in the specified array.IntList
(IntCollection coll) Creates a new list containing the items in the specified IntCollection.Creates a new list containing the elements in the given list. -
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(int value) void
add
(int value1, int value2) void
add
(int value1, int value2, int value3) void
add
(int value1, int value2, int value3, int value4) boolean
addAll
(int... array) boolean
addAll
(int[] array, int offset, int length) boolean
boolean
void
clear()
Effectively removes all items from this IntList.boolean
contains
(int value) boolean
containsAll
(IntList other) Returns true if this IntList contains, at least once, every item inother
; otherwise returns false.div
(int value) Divides each item in this IntList byvalue
, stores it in this and returns it.void
div
(int index, int value) boolean
duplicateRange
(int index, int count) Inserts the specified number of items at the specified index.int[]
ensureCapacity
(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items.boolean
int
first()
Returns the first item.int
get
(int index) int
hashCode()
int
indexOf
(int value) Returns the first index in this list that contains the specified value, or -1 if it is not present.void
insert
(int index, int value) boolean
isEmpty()
Returns true if the list is empty.iterator()
Returns a Java 8 primitive iterator over the int items in this IntList.int
lastIndexOf
(int value) Returns the last index in this list that contains the specified value, or -1 if it is not present.minus
(int value) Takes each item in this IntList and subtractsvalue
, stores it in this and returns it.void
minus
(int index, int value) boolean
notEmpty()
Returns true if the list has one or more items, or false otherwise.int
peek()
Returns the last item.plus
(int value) Addsvalue
to each item in this IntList, stores it in this and returns it.void
plus
(int index, int value) int
pop()
Removes and returns the last item.int
Returns a random item from the list, or zero if the list is empty.rem
(int value) Gets the remainder of each item in this IntList withvalue
, stores it in this and returns it.void
rem
(int index, int value) boolean
remove
(int value) Removes the first occurrence ofvalue
from this IntList, returning true if anything was removed.boolean
Removes from this IntList all occurrences of any elements contained in the specified collection.int
removeAt
(int index) Removes and returns the item at the specified index.boolean
Removes from this IntList element-wise occurrences of elements contained in the specified collection.void
removeRange
(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive.protected int[]
resize
(int newSize) boolean
retainAll
(IntCollection other) Removes all items from this IntList that are not present somewhere inother
, any number of times.void
reverse()
void
set
(int index, int value) int[]
setSize
(int newSize) Sets the list size, leaving any values beyond the current size undefined.int[]
shrink()
Reduces the size of the backing array to the size of the actual items.void
int
size()
void
sort()
void
sort
(int from, int to, IntComparator c) Sorts the specified range of elements according to the order induced by the specified comparator using mergesort, orArrays.sort(int[], int, int)
ifc
is null.void
Sorts all elements according to the order induced by the specified comparator usingIntComparators.sort(int[], int, int, IntComparator)
.void
swap
(int first, int second) times
(int value) Multiplies each item in this IntList byvalue
, stores it in this and returns it.void
times
(int index, int value) int[]
toArray()
Allocates a new int array withsize
elements and fills it with the items in this.int[]
toArray
(int[] array) Ifarray.length
at least equal tosize()
, this copies the contents of this intoarray
and returns it; otherwise, it allocates a new int array that can fit all the items in this, and proceeds to copy into that and return that.toString()
void
truncate
(int newSize) Reduces the size of the list to the specified size.static IntList
with
(int item) static IntList
with
(int... array) Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.github.tommyettinger.gand.ds.IntCollection
addAll, areEqual, containsAll, containsAll, containsAll, containsAny, containsAny, containsAny, forEach, removeAll, removeAll, removeEach, removeEach, removeIf
-
Field Details
-
items
public int[] items -
size
protected int size -
iterator1
-
iterator2
-
-
Constructor Details
-
IntList
public IntList()Creates an ordered list with a capacity of 10. -
IntList
public IntList(int capacity) Creates an ordered list with the specified capacity.- Parameters:
capacity
- Any elements added beyond this will cause the backing array to be grown.
-
IntList
Creates a new list containing the elements in the given list. The new list will be ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.- Parameters:
list
- another IntList (or IntBag) to copy from
-
IntList
public IntList(int[] array) Creates a new list containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.- Parameters:
array
- an int array to copy from
-
IntList
public IntList(int[] array, int startIndex, int count) Creates a new list containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.- Parameters:
array
- a non-null int array to add to this liststartIndex
- the first index inarray
to usecount
- how many items to use fromarray
-
IntList
Creates a new list containing the items in the specified IntCollection.- Parameters:
coll
- a primitive collection that will have its contents added to this
-
-
Method Details
-
size
public int size()- Specified by:
size
in interfaceIntCollection
-
add
public boolean add(int value) - Specified by:
add
in interfaceIntCollection
-
add
public void add(int value1, int value2) -
add
public void add(int value1, int value2, int value3) -
add
public void add(int value1, int value2, int value3, int value4) -
addAll
-
addAll
-
addAll
public boolean addAll(int... array) - Specified by:
addAll
in interfaceIntCollection
-
addAll
public boolean addAll(int[] array, int offset, int length) - Specified by:
addAll
in interfaceIntCollection
-
get
public int get(int index) -
set
public void set(int index, int value) -
plus
public void plus(int index, int value) -
plus
Addsvalue
to each item in this IntList, stores it in this and returns it. The presence of this method allows Kotlin code to use the+
operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).- Parameters:
value
- each item in this will be assigneditem + value
- Returns:
- this for chaining and Kotlin compatibility
-
times
public void times(int index, int value) -
times
Multiplies each item in this IntList byvalue
, stores it in this and returns it. The presence of this method allows Kotlin code to use the*
operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).- Parameters:
value
- each item in this will be assigneditem * value
- Returns:
- this for chaining and Kotlin compatibility
-
minus
public void minus(int index, int value) -
minus
Takes each item in this IntList and subtractsvalue
, stores it in this and returns it. This is just a minor convenience in Java, but the presence of this method allows Kotlin code to use the-
operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).- Parameters:
value
- each item in this will be assigneditem - value
- Returns:
- this for chaining and Kotlin compatibility
-
div
public void div(int index, int value) -
div
Divides each item in this IntList byvalue
, stores it in this and returns it. The presence of this method allows Kotlin code to use the/
operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).- Parameters:
value
- each item in this will be assigneditem / value
- Returns:
- this for chaining and Kotlin compatibility
-
rem
public void rem(int index, int value) -
rem
Gets the remainder of each item in this IntList withvalue
, stores it in this and returns it. The presence of this method allows Kotlin code to use the%
operator (though it shouldn't be used more than once in an expression, because this method modifies this IntList).- Parameters:
value
- each item in this will be assigneditem % value
- Returns:
- this for chaining and Kotlin compatibility
-
insert
public void insert(int index, int value) -
duplicateRange
public boolean duplicateRange(int index, int count) Inserts the specified number of items at the specified index. The new items will have values equal to the values at those indices before the insertion, and the previous values will be pushed to after the duplicated range.- Parameters:
index
- the first index to duplicatecount
- how many items to duplicate
-
swap
public void swap(int first, int second) -
contains
public boolean contains(int value) - Specified by:
contains
in interfaceIntCollection
-
containsAll
Returns true if this IntList contains, at least once, every item inother
; otherwise returns false.- Parameters:
other
- an IntList- Returns:
- true if this contains every item in
other
, otherwise false
-
indexOf
public int indexOf(int value) Returns the first index in this list that contains the specified value, or -1 if it is not present.- Parameters:
value
- a int value to search for- Returns:
- the first index of the given value, or -1 if it is not present
-
lastIndexOf
public int lastIndexOf(int value) Returns the last index in this list that contains the specified value, or -1 if it is not present.- Parameters:
value
- a int value to search for- Returns:
- the last index of the given value, or -1 if it is not present
-
remove
public boolean remove(int value) Removes the first occurrence ofvalue
from this IntList, returning true if anything was removed. Otherwise, this returns false.- Specified by:
remove
in interfaceIntCollection
- Parameters:
value
- the value to (attempt to) remove- Returns:
- true if a value was removed, false if the IntList is unchanged
-
removeAt
public int removeAt(int index) Removes and returns the item at the specified index. Note that this is equivalent toList.remove(int)
, but can't have that name because we also haveremove(int)
that removes a value, rather than an index.- Parameters:
index
- the index of the item to remove and return- Returns:
- the removed item
-
removeRange
public void removeRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive. Note that this takes different arguments than some other range-related methods; this needs a start index and an end index, rather than a count of items. This matches the behavior in the JDK collections.- Parameters:
start
- the first index to remove, inclusiveend
- the last index (after what should be removed), exclusive
-
removeAll
Removes from this IntList all occurrences of any elements contained in the specified collection.- Specified by:
removeAll
in interfaceIntCollection
- Parameters:
c
- a primitive collection of int items to remove fully, such as another IntList or a IntDeque- Returns:
- true if this list was modified.
-
removeEach
Removes from this IntList element-wise occurrences of elements contained in the specified collection. Note that if a value is present more than once in this IntList, only one of those occurrences will be removed for each occurrence of that value inc
. Ifc
has the same contents as this IntList or has additional items, then removing each ofc
will clear this.- Specified by:
removeEach
in interfaceIntCollection
- Parameters:
c
- a primitive collection of int items to remove one-by-one, such as another IntList or a IntDeque- Returns:
- true if this list was modified.
-
retainAll
Removes all items from this IntList that are not present somewhere inother
, any number of times.- Specified by:
retainAll
in interfaceIntCollection
- Parameters:
other
- a IntCollection that contains the items that this should keep, whenever present- Returns:
- true if this IntList changed as a result of this call, otherwise false
-
pop
public int pop()Removes and returns the last item.- Returns:
- the last item, removed from this
-
peek
public int peek()Returns the last item.- Returns:
- the last item, without modifying this
-
first
public int first()Returns the first item.- Specified by:
first
in interfaceIntCollection
- Returns:
- the first item, without modifying this
-
notEmpty
public boolean notEmpty()Returns true if the list has one or more items, or false otherwise.- Specified by:
notEmpty
in interfaceIntCollection
- Returns:
- true if the list has one or more items, or false otherwise
-
isEmpty
public boolean isEmpty()Returns true if the list is empty.- Specified by:
isEmpty
in interfaceIntCollection
- Returns:
- true if the list is empty, or false if it has any items
-
clear
public void clear()Effectively removes all items from this IntList. This is done simply by setting size to 0; because aint
item isn't a reference, it doesn't need to be set to null.- Specified by:
clear
in interfaceIntCollection
-
shrink
public int[] shrink()Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added.- Returns:
items
; this will be a different reference if this resized
-
ensureCapacity
public int[] ensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.- Returns:
items
; this will be a different reference if this resized
-
setSize
public int[] setSize(int newSize) Sets the list size, leaving any values beyond the current size undefined.- Returns:
items
; this will be a different reference if this resized to a larger capacity
-
resize
protected int[] resize(int newSize) -
sort
public void sort() -
sort
Sorts all elements according to the order induced by the specified comparator usingIntComparators.sort(int[], int, int, IntComparator)
. Ifc
is null, this instead delegates tosort()
, which usesArrays.sort(int[])
, and does not always run in-place.This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. The sorting algorithm is an in-place mergesort that is significantly slower than a standard mergesort, as its running time is O(n (log n)2), but it does not allocate additional memory; as a result, it can be used as a generic sorting algorithm.
- Parameters:
c
- the comparator to determine the order of the IntList
-
sort
Sorts the specified range of elements according to the order induced by the specified comparator using mergesort, orArrays.sort(int[], int, int)
ifc
is null. This purely usesIntComparators.sort(int[], int, int, IntComparator)
, and you can see its docs for more information.- Parameters:
from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.c
- the comparator to determine the order of the IntList
-
reverse
public void reverse() -
shuffle
-
truncate
public void truncate(int newSize) Reduces the size of the list to the specified size. If the list is already smaller than the specified size, no action is taken. -
random
Returns a random item from the list, or zero if the list is empty.- Parameters:
random
- aRandom
or a subclass, such as any from juniper- Returns:
- a randomly selected item from this, or
0
if this is empty
-
toArray
public int[] toArray()Allocates a new int array withsize
elements and fills it with the items in this.- Specified by:
toArray
in interfaceIntCollection
- Returns:
- a new int array with the same contents as this
-
toArray
public int[] toArray(int[] array) Ifarray.length
at least equal tosize()
, this copies the contents of this intoarray
and returns it; otherwise, it allocates a new int array that can fit all the items in this, and proceeds to copy into that and return that.- Specified by:
toArray
in interfaceIntCollection
- Parameters:
array
- a int array that will be modified if it can fitsize()
items- Returns:
array
, if it had sufficient size, or a new array otherwise, either with a copy of this
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceIntCollection
- Overrides:
hashCode
in classObject
-
equals
- Specified by:
equals
in interfaceIntCollection
- Overrides:
equals
in classObject
-
toString
-
toString
-
iterator
Returns a Java 8 primitive iterator over the int items in this IntList. Iterates in order.
This will reuse one of two iterators in this IntList; this does not allow nested iteration. UseIntListIterator(IntList)
to nest iterators.- Specified by:
iterator
in interfaceIntCollection
- Returns:
- a
IntIterator
; use its nextInt() method instead of next()
-
with
-
with
- See Also:
-