Package gaiasky

Class DoubleArray

java.lang.Object
gaiasky.DoubleArray

public class DoubleArray extends Object
A resizable, ordered or unordered double array. Avoids the boxing that occurs with ArrayList. If unordered, this class avoids a memory copy when removing elements (the last element is moved to the removed element's position).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    double[]
     
    boolean
     
    int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an ordered array with a capacity of 16.
    DoubleArray​(boolean ordered, double[] array, int startIndex, int count)
    Creates a new array containing the elements in the specified array.
    DoubleArray​(boolean ordered, int capacity)
     
    DoubleArray​(double[] array)
    Creates a new ordered array containing the elements in the specified array.
    DoubleArray​(int capacity)
    Creates an ordered array with the specified capacity.
    Creates a new array containing the elements in the specific array.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add​(double value)
     
    void
    add​(double value1, double value2)
     
    void
    add​(double value1, double value2, double value3)
     
    void
    add​(double value1, double value2, double value3, double value4)
     
    void
    addAll​(double... array)
     
    void
    addAll​(double[] array, int offset, int length)
     
    void
    addAll​(DoubleArray array)
     
    void
    addAll​(DoubleArray array, int offset, int length)
     
    void
     
    boolean
    contains​(double value)
     
    double[]
    ensureCapacity​(int additionalCapacity)
    Increases the size of the backing array to accommodate the specified number of additional items.
    boolean
    equals​(Object object)
    Returns false if either array is unordered.
    boolean
    equals​(Object object, double epsilon)
    Returns false if either array is unordered.
    double
    Returns the first item.
    double
    get​(int index)
     
    int
     
    void
    incr​(double value)
     
    void
    incr​(int index, double value)
     
    int
    indexOf​(double value)
     
    void
    insert​(int index, double value)
     
    void
    insertRange​(int index, int count)
    Inserts the specified number of items at the specified index.
    boolean
    Returns true if the array is empty.
    int
    lastIndexOf​(double value)
     
    void
    mul​(double value)
     
    void
    mul​(int index, double value)
     
    boolean
    Returns true if the array has one or more items.
    double
    Returns the last item.
    double
    pop()
    Removes and returns the last item.
    double
    Returns a random item from the array, or zero if the array is empty.
    boolean
    Removes from this array all of elements contained in the specified array.
    double
    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.
    boolean
    removeValue​(double value)
     
    protected double[]
    resize​(int newSize)
     
    void
     
    void
    set​(int index, double value)
     
    double[]
    setSize​(int newSize)
    Sets the array size, leaving any values beyond the current size undefined.
    double[]
    Reduces the size of the backing array to the size of the actual items.
    void
     
    void
     
    void
    swap​(int first, int second)
     
    double[]
     
     
    toString​(String separator)
     
    void
    truncate​(int newSize)
    Reduces the size of the array to the specified size.
    with​(double... array)
     

    Methods inherited from class java.lang.Object

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

    • items

      public double[] items
    • size

      public int size
    • ordered

      public boolean ordered
  • Constructor Details

    • DoubleArray

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

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

      public DoubleArray(boolean ordered, int capacity)
      Parameters:
      ordered - If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
      capacity - Any elements added beyond this will cause the backing array to be grown.
    • DoubleArray

      public DoubleArray(DoubleArray 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.
    • DoubleArray

      public DoubleArray(double[] 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.
    • DoubleArray

      public DoubleArray(boolean ordered, double[] 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:
      ordered - If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
  • Method Details

    • add

      public void add(double value)
    • add

      public void add(double value1, double value2)
    • add

      public void add(double value1, double value2, double value3)
    • add

      public void add(double value1, double value2, double value3, double value4)
    • addAll

      public void addAll(DoubleArray array)
    • addAll

      public void addAll(DoubleArray array, int offset, int length)
    • addAll

      public void addAll(double... array)
    • addAll

      public void addAll(double[] array, int offset, int length)
    • get

      public double get(int index)
    • set

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

      public void incr(int index, double value)
    • incr

      public void incr(double value)
    • mul

      public void mul(int index, double value)
    • mul

      public void mul(double value)
    • insert

      public void insert(int index, double value)
    • insertRange

      public void insertRange(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.
    • swap

      public void swap(int first, int second)
    • contains

      public boolean contains(double value)
    • indexOf

      public int indexOf(double value)
    • lastIndexOf

      public int lastIndexOf(double value)
    • removeValue

      public boolean removeValue(double value)
    • removeIndex

      public double 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(DoubleArray array)
      Removes from this array all of elements contained in the specified array.
      Returns:
      true if this array was modified.
    • pop

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

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

      public double first()
      Returns the first item.
    • notEmpty

      public boolean notEmpty()
      Returns true if the array has one or more items.
    • isEmpty

      public boolean isEmpty()
      Returns true if the array is empty.
    • clear

      public void clear()
    • shrink

      public double[] 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 double[] 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 double[] setSize(int newSize)
      Sets the array size, leaving any values beyond the current size undefined.
      Returns:
      items
    • resize

      protected double[] resize(int newSize)
    • sort

      public void sort()
    • reverse

      public void reverse()
    • shuffle

      public void shuffle()
    • 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.
    • random

      public double random()
      Returns a random item from the array, or zero if the array is empty.
    • toArray

      public double[] toArray()
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object object)
      Returns false if either array is unordered.
      Overrides:
      equals in class Object
    • equals

      public boolean equals(Object object, double epsilon)
      Returns false if either array is unordered.
    • toString

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

      public String toString(String separator)
    • with

      public static DoubleArray with(double... array)
      See Also:
      DoubleArray(double[])