Class FastQueue<T>

  • All Implemented Interfaces:
    java.io.Serializable

    public class FastQueue<T>
    extends FastAccess<T>
    Growable array designed for fast access which creates, recycles and in general owns all of its elements.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      FastQueue​(int initialMaxSize, Factory<T> factory)
      Constructor which allows new instances to be created using a lambda
      FastQueue​(java.lang.Class<T> type, Factory<T> factory)
      Constructor which allows new instances to be created using a lambda
      FastQueue​(Factory<T> factory)
      Constructor which allows new instances to be created using a lambda and determines the class by creating a new instance.
      FastQueue​(Factory<T> factory, Process<T> reset)
      User provided factory function and reset function.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      boolean contains​(java.lang.Object o)  
      void copyAll​(java.util.List<T> list, FastQueue.Set<T> setter)
      Grows the array and adds all the items in list.
      java.util.List<T> copyIntoList​(java.util.List<T> ret)  
      protected T createInstance()
      Deprecated.
      void flip()  
      T[] getData()  
      int getSize()  
      java.lang.Class<T> getType()  
      T grow()
      Returns a new element of data.
      void growArray​(int length)
      Increases the size of the internal array without changing the shape's size.
      int indexOf​(T obj)
      Returns the first index which equals() obj.
      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
      boolean isDeclare()  
      boolean isUnused​(T object)
      Checks to see if the object is in the unused list.
      T remove​(int index)
      Removes an element from the queue and preserves the order of all elements.
      void remove​(int[] indexes, int fromIndex, int toIndex, java.util.List<T> workSpace)
      Removes the indexes from the queue.
      T removeSwap​(int index)
      Removes the specified index from the array by swapping it with last element.
      T removeTail()
      Shrinks the size of the array by one and returns the element stored at the former last element.
      void reset()  
      void resize​(int length)
      Changes the size to the specified length.
      void setData​(T[] data)  
      void setSize​(int size)  
      void shuffle​(java.util.Random rand)  
      void swap​(int idx0, int idx1)  
      java.util.List<T> toList()
      Returns a wrapper around FastQueue that allows it to act as a read only list.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FastQueue

        public FastQueue​(java.lang.Class<T> type,
                         Factory<T> factory)
        Constructor which allows new instances to be created using a lambda
      • FastQueue

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

        public FastQueue​(Factory<T> factory,
                         Process<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
      • FastQueue

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

      • 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 java.util.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
                           java.util.List<T> workSpace)
        Removes the indexes from the queue. 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 void 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 void copyAll​(java.util.List<T> list,
                            FastQueue.Set<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 queue 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.
      • 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
      • growArray

        public void growArray​(int length)
        Increases the size of the internal array without changing the shape's size. If the array is already larger than the specified length then nothing is done. Elements previously stored in the array are copied over is a new internal array is declared.
        Parameters:
        length - Requested size of internal array.
      • resize

        public void resize​(int length)
        Changes the size to the specified length. Equivalent to calling growArray(int) and this.size = N.
        Parameters:
        length - The new size of the queue
      • contains

        public boolean contains​(java.lang.Object o)
      • shuffle

        public void shuffle​(java.util.Random rand)
      • createInstance

        @Deprecated
        protected T createInstance()
        Deprecated.
        This function will be removed eventually and the factory used directly. DO NOT USE IN NEW CODE
      • copyIntoList

        public java.util.List<T> copyIntoList​(java.util.List<T> ret)
      • indexOf

        public int indexOf​(T obj)
        Returns the first index which equals() obj. -1 is there is no match
        Parameters:
        obj - The object being searched for
        Returns:
        index or -1 if not found
      • flip

        public void flip()
      • swap

        public void swap​(int idx0,
                         int idx1)
      • 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 java.lang.Class<T> getType()