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.
Author:
Nathan Sweet
See Also:
Serialized Form
  • Field Summary

    Fields 
    Modifier and Type Field Description
    int[] items  
    int size  
  • 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 using CrossHash.Water, getting a 64-bit result.
    int hashCode()
    Hashes this IntVLA using CrossHash.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 prefer hashHive() 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 of value 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 as get(ordering[0]) (with some care taken for negative or too-large indices), the second item in the returned version is the same as get(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)  

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • IntVLA

      public IntVLA()
      Creates an ordered array with a capacity of 16.
    • IntVLA

      public IntVLA​(int capacity)
      Creates an ordered array with the specified capacity.
    • IntVLA

      public IntVLA​(IntVLA array)
      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

      public IntVLA​(int[] array)
      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

      public IntVLA​(int[] array, int startIndex, int count)
      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 from
      startIndex - the first index in array to copy from
      count - the number of ints to copy from array into this IntVLA
  • Method Details

    • add

      public void add​(int value)
    • addAll

      public void addAll​(IntVLA array)
    • addAll

      public void addAll​(IntVLA array, int offset, int length)
    • addAll

      public void addAll​(IntSet set)
    • addAll

      public void addAll​(int... array)
    • addAll

      public void addAll​(int[] array, int offset, int length)
    • addRange

      public void addRange​(int start, int end)
    • addFractionRange

      public void addFractionRange​(int start, int end, int fraction)
    • get

      public int get​(int index)
    • set

      public void set​(int index, int value)
    • incr

      public void incr​(int index, int value)
      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 to
      value - how much to add to the item at index (this may be negative, positive, or 0)
    • mul

      public void mul​(int index, int value)
    • insert

      public void insert​(int index, int value)
    • swap

      public void swap​(int first, int second)
    • reorder

      public 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 as get(ordering[0]) (with some care taken for negative or too-large indices), the second item in the returned version is the same as get(ordering[1]), etc.
      Negative indices are considered reversed distances from the end of ordering, so -1 refers to the same index as ordering[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

      public boolean contains​(int value)
    • indexOf

      public int indexOf​(int value)
      Tries to find the first occurrence of value 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

      public int lastIndexOf​(int value)
    • removeValue

      public int removeValue​(int value)
      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

      public int removeIndex​(int index)
      Removes and returns the item at the specified index.
    • removeRange

      public void removeRange​(int start, int end)
      Removes the items between the specified indices, inclusive.
    • removeAll

      public boolean removeAll​(IntVLA array)
      Removes from this array all of elements contained in the specified array.
      Returns:
      true if this array was modified.
    • moveToFirst

      public int moveToFirst​(int value)
      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

      public int moveToLast​(int value)
      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

      public int pop()
      Removes and returns the last item.
    • peek

      public int peek()
      Returns the last item.
    • first

      public int first()
      Returns the first item.
    • clear

      public void clear()
    • 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
    • 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
    • setSize

      public int[] setSize​(int newSize)
      Sets the array size, leaving any values beyond the current size undefined.
      Returns:
      items
    • resize

      protected int[] resize​(int newSize)
    • sort

      public void sort()
    • reverse

      public void reverse()
    • truncate

      public void truncate​(int newSize)
      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

      public int getRandomElement​(IRNG random)
    • shuffle

      public IntVLA shuffle​(IRNG random)
      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

      public int[] toArray()
    • copy

      public IntVLA copy()
    • clone

      public Object clone()
      Overrides:
      clone in class Object
    • hashCode

      public int hashCode()
      Hashes this IntVLA using CrossHash.Water, getting a 32-bit result. The same algorithm is used by hash64(), just with different constants and a different final step here.
      Overrides:
      hashCode in class Object
      Returns:
      a 32-bit hash code
    • hashWisp

      public int hashWisp()
      Gets a low-to-mid-quality hash code quickly using the Wisp algorithm; you should prefer hashHive() in code that needs to run on GWT, since that method avoids the long math that is expensive on GWT. You are fine using hashCode() 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

      public 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. It is relatively slow on desktop platforms compared to hashCode(), 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 in CrossHash.Water.
      Returns:
      a 32-bit hash code that should show very little bias toward any bits
    • hash64

      public long hash64()
      Hashes this IntVLA using CrossHash.Water, getting a 64-bit result. The same algorithm is used by hashCode(), just with different constants and a different final step here.
      Returns:
      a 64-bit hash code
    • equals

      public boolean equals​(Object object)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString​(String separator)
    • deserializeFromString

      public static IntVLA deserializeFromString​(String data)
    • with

      public static IntVLA with​(int... array)
      See Also:
      IntVLA(int[])
    • isEmpty

      public boolean isEmpty()