Class DogArray<T>

java.lang.Object
org.ddogleg.struct.FastAccess<T>
org.ddogleg.struct.DogArray<T>
All Implemented Interfaces:
Serializable

public class DogArray<T> extends FastAccess<T>
Growable array which automatically creates, recycles, and resets its elements. Access to internal variables is provided for high performant code. If a reset function is provided then when an object is created or recycled the reset function is called with the objective of giving the object a repeatable initial state.
See Also:
  • Constructor Details

    • DogArray

      public DogArray(Class<T> type, Factory<T> factory)
      Constructor which allows new instances to be created using a lambda
    • DogArray

      public DogArray(Factory<T> factory)
      Constructor which allows new instances to be created using a lambda and determines the class by creating a new instance.
    • DogArray

      public DogArray(Factory<T> factory, DProcess<T> reset)
      User provided factory function and reset function.
      Parameters:
      factory - Creates new instances
      reset - Called whenever an element is recycled and needs to be reset
    • DogArray

      public DogArray(Factory<T> factory, DProcess<T> reset, DProcess<T> initialize)
      User provided factory function and reset function.
      Parameters:
      factory - Creates new instances
      reset - Called whenever an element is recycled and needs to be reset
      initialize - Called after a new instance is created
    • DogArray

      public DogArray(int initialMaxSize, Factory<T> factory)
      Constructor which allows new instances to be created using a lambda
  • Method Details

    • init

      protected void init(int initialMaxSize, Factory<T> factory)
      Data structure initialization is done here so that child classes can declay initialization until they are ready
    • toList

      public List<T> toList()
      Returns a wrapper around FastQueue that allows it to act as a read only list. There is little overhead in using this interface. NOTE: The same instead of a list is returned each time. Be careful when writing concurrent code and create a copy.
      Specified by:
      toList in class FastAccess<T>
      Returns:
      List wrapper.
    • remove

      public void remove(int[] indexes, int fromIndex, int toIndex, @Nullable @Nullable List<T> workSpace)
      Removes the indexes from the array. This is done by swapping removed elements with the last element. O(N) copies.
      Parameters:
      indexes - Index of elements which are to be removed. This will be modified
      fromIndex - the index of the first element, inclusive, to be sorted
      toIndex - the index of the last element, exclusive, to be sorted
      workSpace - Optional internal workspace. Can be set to null.
    • removeTail

      public T removeTail()
      Shrinks the size of the array by one and returns the element stored at the former last element.
      Returns:
      The last element in the list that was removed.
    • reset

      public DogArray<T> reset()
    • grow

      public T grow()
      Returns a new element of data. If there are new data elements available then array will automatically grow.
      Returns:
      A new instance.
    • copyAll

      public <S> void copyAll(List<S> list, DogArray.Set<S,T> setter)
      Grows the array and adds all the items in list. Values are copied using the provided function
    • remove

      public T remove(int index)
      Removes an element from the array and preserves the order of all elements. This is done by shifting elements in the array down one and placing the removed element at the old end of the list. O(N) runtime.
      Specified by:
      remove in class FastAccess<T>
      Parameters:
      index - Index of the element being removed
      Returns:
      The object removed.
    • remove

      public boolean remove(T target)
      Searches for and removes the 'target' from the list. Returns true if the target was found. If false then the target was never found and no change has been made. This is an O(N) operation.
      Parameters:
      target - Object to remove from the list
      Returns:
      true if the target was found and removed
    • removeSwap

      public T removeSwap(int index)
      Removes the specified index from the array by swapping it with last element. Does not preserve order but has a runtime of O(1).
      Specified by:
      removeSwap in class FastAccess<T>
      Parameters:
      index - The index to be removed.
      Returns:
      The removed object
    • reserve

      public void reserve(int length)
      Ensures that the internal array has at least `length` elements. If it does not then a new internal array is created with the specified length and elements from the old are copied into the new. The `size` does not change.
      Parameters:
      length - Requested minimum internal array length
    • resize

      public DogArray<T> resize(int length, DProcess<T> configure)
      Resize with a configuration operator. Equivalent to calling reserve(int) and this.size = N, then applying the 'configure' operator to each new element. NOTE: The 'reset' operator is applied before the 'configure' operator.
      Parameters:
      length - The new size of the array
      configure - Operator that the "new" element is passed in to.
    • resize

      public DogArray<T> resize(int length, DProcessIdx<T> configure)
      Resize with a configuration operator. Equivalent to calling reserve(int) and this.size = N, then applying the 'configure' operator to each new element. NOTE: The 'reset' operator is applied before the 'configure' operator.
      Parameters:
      length - The new size of the array
      configure - Operator that the "new" element is passed in to along with the index of the element.
    • resize

      public DogArray<T> resize(int newSize)
      Changes the size to the specified length. Equivalent to calling reserve(int) and this.size = N. All new elements will be passed in to reset.
      Parameters:
      newSize - New array size
    • resetResize

      @Deprecated public void resetResize(int newSize)
      Deprecated.
      Convenience functions that calls reset first before resize(int, org.ddogleg.struct.DProcess<T>).
      Parameters:
      newSize - New array size
    • resetResize

      @Deprecated public void resetResize(int newSize, DProcessIdx<T> configure)
      Deprecated.
      Convenience functions that calls reset first before resize(int, org.ddogleg.struct.DProcess<T>), then applies the configure function for each element..
      Parameters:
      newSize - New array size
      configure - Operator that the "new" element is passed in to along with the index of the element.
    • shuffle

      public void shuffle(Random rand)
      Randomly shuffles elements in the list. O(N) complexity.
      Parameters:
      rand - random seed.
    • shuffle

      public void shuffle(Random rand, int numShuffle)
      Shuffle where it will only shuffle up to the specified number of elements. This is useful when you want to randomly select up to N elements in the list. When shuffling, The first i < N elements is randomly selected out from an element from i+1 to N-1.
      Parameters:
      numShuffle - The maximum number of elements that will be shuffled
      rand - random seed.
    • createInstance

      protected T createInstance()
      Creates a new instance of elements stored in this array
    • copyIntoList

      public List<T> copyIntoList(List<T> ret)
    • isUnused

      public boolean isUnused(T object)
      Checks to see if the object is in the unused list.
    • getData

      public T[] getData()
    • setData

      public void setData(T[] data)
    • getSize

      public int getSize()
    • setSize

      public void setSize(int size)
    • isDeclare

      public final boolean isDeclare()
    • getType

      public Class<T> getType()