Package com.github.tommyettinger.gand.ds
Class IntList
java.lang.Object
com.github.tommyettinger.gand.ds.IntList
- All Implemented Interfaces:
com.badlogic.gdx.utils.Json.Serializable,IntCollection
- Direct Known Subclasses:
IntDeque
public class IntList
extends Object
implements IntCollection, com.badlogic.gdx.utils.Json.Serializable
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
Nested ClassesModifier and TypeClassDescriptionstatic classAIntIterator, plusListIteratormethods, over the elements of a IntList. -
Field Summary
FieldsModifier and TypeFieldDescriptionint[]protected IntList.IntListIteratorprotected IntList.IntListIteratorprotected int -
Constructor Summary
ConstructorsConstructorDescriptionIntList()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 TypeMethodDescriptionbooleanadd(int value) voidadd(int value1, int value2) voidadd(int value1, int value2, int value3) voidadd(int value1, int value2, int value3, int value4) booleanaddAll(int... array) booleanaddAll(int[] array, int offset, int length) booleanbooleanvoidclear()Effectively removes all items from this IntList.booleancontains(int value) booleancontainsAll(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.voiddiv(int index, int value) booleanduplicateRange(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.booleanintfirst()Returns the first item.intget(int index) inthashCode()intindexOf(int value) Returns the first index in this list that contains the specified value, or -1 if it is not present.voidinsert(int index, int value) booleanisEmpty()Returns true if the list is empty.iterator()Returns a Java 8 primitive iterator over the int items in this IntList.intlastIndexOf(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.voidminus(int index, int value) booleannotEmpty()Returns true if the list has one or more items, or false otherwise.intpeek()Returns the last item.plus(int value) Addsvalueto each item in this IntList, stores it in this and returns it.voidplus(int index, int value) intpop()Removes and returns the last item.intReturns a random item from the list, or zero if the list is empty.voidread(com.badlogic.gdx.utils.Json json, com.badlogic.gdx.utils.JsonValue jsonData) rem(int value) Gets the remainder of each item in this IntList withvalue, stores it in this and returns it.voidrem(int index, int value) booleanremove(int value) Removes the first occurrence ofvaluefrom this IntList, returning true if anything was removed.booleanRemoves from this IntList all occurrences of any elements contained in the specified collection.intremoveAt(int index) Removes and returns the item at the specified index.booleanRemoves from this IntList element-wise occurrences of elements contained in the specified collection.voidremoveRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive.protected int[]resize(int newSize) booleanretainAll(IntCollection other) Removes all items from this IntList that are not present somewhere inother, any number of times.voidreverse()voidset(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.voidintsize()voidsort()voidsort(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)ifcis null.voidSorts all elements according to the order induced by the specified comparator usingIntComparators.sort(int[], int, int, IntComparator).voidswap(int first, int second) times(int value) Multiplies each item in this IntList byvalue, stores it in this and returns it.voidtimes(int index, int value) int[]toArray()Allocates a new int array withsizeelements and fills it with the items in this.int[]toArray(int[] array) Ifarray.lengthat least equal tosize(), this copies the contents of this intoarrayand 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()voidtruncate(int newSize) Reduces the size of the list to the specified size.static IntListwith(int item) static IntListwith(int... array) voidwrite(com.badlogic.gdx.utils.Json json) Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods 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 inarrayto 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:
sizein interfaceIntCollection
-
add
public boolean add(int value) - Specified by:
addin 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:
addAllin interfaceIntCollection
-
addAll
public boolean addAll(int[] array, int offset, int length) - Specified by:
addAllin interfaceIntCollection
-
get
public int get(int index) -
set
public void set(int index, int value) -
plus
public void plus(int index, int value) -
plus
Addsvalueto 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:
containsin 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- an 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- an 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 ofvaluefrom this IntList, returning true if anything was removed. Otherwise, this returns false.- Specified by:
removein 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:
removeAllin 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. Ifchas the same contents as this IntList or has additional items, then removing each ofcwill clear this.- Specified by:
removeEachin 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:
retainAllin 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:
firstin 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:
notEmptyin 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:
isEmptyin 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 aintitem isn't a reference, it doesn't need to be set to null.- Specified by:
clearin 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). Ifcis 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)ifcis 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- aRandomor a subclass, such as any from juniper- Returns:
- a randomly selected item from this, or
0if this is empty
-
toArray
public int[] toArray()Allocates a new int array withsizeelements and fills it with the items in this.- Specified by:
toArrayin interfaceIntCollection- Returns:
- a new int array with the same contents as this
-
toArray
public int[] toArray(int[] array) Ifarray.lengthat least equal tosize(), this copies the contents of this intoarrayand 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:
toArrayin interfaceIntCollection- Parameters:
array- an 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:
hashCodein interfaceIntCollection- Overrides:
hashCodein classObject
-
equals
- Specified by:
equalsin interfaceIntCollection- Overrides:
equalsin 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:
iteratorin interfaceIntCollection- Returns:
- a
IntIterator; use its nextInt() method instead of next()
-
with
-
with
- See Also:
-
write
public void write(com.badlogic.gdx.utils.Json json) - Specified by:
writein interfacecom.badlogic.gdx.utils.Json.Serializable
-
read
public void read(com.badlogic.gdx.utils.Json json, com.badlogic.gdx.utils.JsonValue jsonData) - Specified by:
readin interfacecom.badlogic.gdx.utils.Json.Serializable
-