Package squidpony.squidmath
Class IntVLA
java.lang.Object
squidpony.squidmath.IntVLA
- All Implemented Interfaces:
Serializable
,Cloneable
public class IntVLA extends Object implements Serializable, Cloneable
A resizable, ordered or unordered variable-length int array. Avoids boxing that occurs with ArrayList of Integer.
If unordered, this class avoids a memory copy when removing elements (the last element is moved to the removed
element's position).
Was called IntArray in libGDX; to avoid confusion with the fixed-length primitive array type, VLA (variable-length array) was chosen as a different name. Copied from LibGDX by Tommy Ettinger on 10/1/2015.
Was called IntArray in libGDX; to avoid confusion with the fixed-length primitive array type, VLA (variable-length array) was chosen as a different name. Copied from LibGDX by Tommy Ettinger on 10/1/2015.
- Author:
- Nathan Sweet
- See Also:
- Serialized Form
-
Field Summary
-
Constructor Summary
Constructors Constructor Description IntVLA()
Creates an ordered array with a capacity of 16.IntVLA(int capacity)
Creates an ordered array with the specified capacity.IntVLA(int[] array)
Creates a new ordered array containing the elements in the specified array.IntVLA(int[] array, int startIndex, int count)
Creates a new array containing the elements in the specified array.IntVLA(IntVLA array)
Creates a new array containing the elements in the specific array. -
Method Summary
Modifier and Type Method Description void
add(int value)
void
addAll(int... array)
void
addAll(int[] array, int offset, int length)
void
addAll(IntSet set)
void
addAll(IntVLA array)
void
addAll(IntVLA array, int offset, int length)
void
addFractionRange(int start, int end, int fraction)
void
addRange(int start, int end)
void
clear()
Object
clone()
boolean
contains(int value)
IntVLA
copy()
static IntVLA
deserializeFromString(String data)
int[]
ensureCapacity(int additionalCapacity)
Increases the size of the backing array to accommodate the specified number of additional items.boolean
equals(Object object)
int
first()
Returns the first item.int
get(int index)
int
getRandomElement(IRNG random)
long
hash64()
Hashes this IntVLA usingCrossHash.Water
, getting a 64-bit result.int
hashCode()
Hashes this IntVLA usingCrossHash.Water
, getting a 32-bit result.int
hashHive()
Gets a mid-quality hash code using the 32-bit part of the Hive algorithm; this uses only int math and so is an excellent option on GWT.int
hashWisp()
Gets a low-to-mid-quality hash code quickly using the Wisp algorithm; you should preferhashHive()
in code that needs to run on GWT, since that method avoids the long math that is expensive on GWT.void
incr(int index, int value)
Adds value to the item in the IntVLA at index.int
indexOf(int value)
Tries to find the first occurrence ofvalue
in this IntVLA, and returns the index that value appears at if it is present, or -1 if it is not present.void
insert(int index, int value)
boolean
isEmpty()
int
lastIndexOf(int value)
int
moveToFirst(int value)
Moves the specified value to the first index and returns its previous index.int
moveToLast(int value)
Moves the specified value to the last index and returns its previous index.void
mul(int index, int value)
int
peek()
Returns the last item.int
pop()
Removes and returns the last item.boolean
removeAll(IntVLA array)
Removes from this array all of elements contained in the specified array.int
removeIndex(int index)
Removes and returns the item at the specified index.void
removeRange(int start, int end)
Removes the items between the specified indices, inclusive.int
removeValue(int value)
Removes the first occurrence of the requested value, and returns the index it was removed at (-1 if not found)IntVLA
reorder(int... ordering)
Given an array or varargs of replacement indices for the values of this IntVLA, reorders this so the first item in the returned version is the same asget(ordering[0])
(with some care taken for negative or too-large indices), the second item in the returned version is the same asget(ordering[1])
, etc.protected int[]
resize(int newSize)
void
reverse()
void
set(int index, int value)
int[]
setSize(int newSize)
Sets the array 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.IntVLA
shuffle(IRNG random)
Shuffles this IntVLA in place using the given IRNG.void
sort()
void
swap(int first, int second)
int[]
toArray()
String
toString()
String
toString(String separator)
void
truncate(int newSize)
Reduces the size of the array to the specified size.static IntVLA
with(int... array)
-
Field Details
-
Constructor Details
-
IntVLA
public IntVLA()Creates an ordered array with a capacity of 16. -
IntVLA
Creates an ordered array with the specified capacity. -
IntVLA
Creates a new array containing the elements in the specific array. The new array will be ordered if the specific array is ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown. -
IntVLA
Creates a new ordered array 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. -
IntVLA
Creates a new array 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
- the int array to copy fromstartIndex
- the first index in array to copy fromcount
- the number of ints to copy from array into this IntVLA
-
-
Method Details
-
add
-
addAll
-
addAll
-
addAll
-
addAll
-
addAll
-
addRange
-
addFractionRange
-
get
-
set
-
incr
Adds value to the item in the IntVLA at index. Calling it "add" would overlap with the collection method.- Parameters:
index
- the index of the item to add tovalue
- how much to add to the item at index (this may be negative, positive, or 0)
-
mul
-
insert
-
swap
-
reorder
Given an array or varargs of replacement indices for the values of this IntVLA, reorders this so the first item in the returned version is the same asget(ordering[0])
(with some care taken for negative or too-large indices), the second item in the returned version is the same asget(ordering[1])
, etc.
Negative indices are considered reversed distances from the end of ordering, so -1 refers to the same index asordering[ordering.length - 1]
. If ordering is smaller than this IntVLA, only the indices up to the length of ordering will be modified. If ordering is larger than this IntVLA, only as many indices will be affected as this IntVLA's size, and reversed distances are measured from the end of this IntVLA instead of the end of ordering. Duplicate values in ordering will produce duplicate values in the returned IntVLA.
This method modifies this IntVLA in-place and also returns it for chaining.- Parameters:
ordering
- an array or varargs of int indices, where the nth item in ordering changes the nth item in this IntVLA to have the value currently in this IntVLA at the index specified by the value in ordering- Returns:
- this for chaining, after modifying it in-place
-
contains
-
indexOf
Tries to find the first occurrence ofvalue
in this IntVLA, and returns the index that value appears at if it is present, or -1 if it is not present.- Parameters:
value
- a value to search for in this- Returns:
- the first index of value, if found, or -1 otherwise
-
lastIndexOf
-
removeValue
Removes the first occurrence of the requested value, and returns the index it was removed at (-1 if not found)- Parameters:
value
- a value in this IntVLA to remove- Returns:
- the index the value was found and removed at, or -1 if it was not present
-
removeIndex
Removes and returns the item at the specified index. -
removeRange
Removes the items between the specified indices, inclusive. -
removeAll
Removes from this array all of elements contained in the specified array.- Returns:
- true if this array was modified.
-
moveToFirst
Moves the specified value to the first index and returns its previous index. If value is not present, this returns -1.- Parameters:
value
- an int value that should already be in this IntVLA- Returns:
- the previous index of value, or -1 if it was not present
-
moveToLast
Moves the specified value to the last index and returns its previous index. If value is not present, this returns -1.- Parameters:
value
- an int value that should already be in this IntVLA- Returns:
- the previous index of value, or -1 if it was not present
-
pop
Removes and returns the last item. -
peek
Returns the last item. -
first
Returns the first item. -
clear
-
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
-
ensureCapacity
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
-
setSize
Sets the array size, leaving any values beyond the current size undefined.- Returns:
items
-
resize
-
sort
-
reverse
-
truncate
Reduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is taken. -
getRandomElement
-
shuffle
Shuffles this IntVLA in place using the given IRNG.- Parameters:
random
- an IRNG, such as an RNG, used to generate the shuffled order- Returns:
- this object, modified, after shuffling
-
toArray
-
copy
-
clone
-
hashCode
Hashes this IntVLA usingCrossHash.Water
, getting a 32-bit result. The same algorithm is used byhash64()
, just with different constants and a different final step here. -
hashWisp
Gets a low-to-mid-quality hash code quickly using the Wisp algorithm; you should preferhashHive()
in code that needs to run on GWT, since that method avoids the long math that is expensive on GWT. You are fine usinghashCode()
when you need quality and low collison rates; Wisp tends to collide too often on some data sets.- Returns:
- a 32-bit hash code that has OK quality, but worse than
hashCode()
-
hashHive
Gets a mid-quality hash code using the 32-bit part of the Hive algorithm; this uses only int math and so is an excellent option on GWT. It is relatively slow on desktop platforms compared tohashCode()
, which uses a higher-quality and sometimes faster algorithm, but only has that speed advantage on 64-bit JVMs.hash64()
uses almost exactly the same algorithm as hashCode(), both inCrossHash.Water
.- Returns:
- a 32-bit hash code that should show very little bias toward any bits
-
hash64
Hashes this IntVLA usingCrossHash.Water
, getting a 64-bit result. The same algorithm is used byhashCode()
, just with different constants and a different final step here.- Returns:
- a 64-bit hash code
-
equals
-
toString
-
toString
-
deserializeFromString
-
with
- See Also:
IntVLA(int[])
-
isEmpty
-