Class LongQueue


  • public class LongQueue
    extends java.lang.Object
    A resizable, ordered array of longs with efficient add and remove at the beginning and end. Values in the backing array may wrap back to the beginning, making add and remove at the beginning and end O(1) (unless the backing array needs to resize when adding). Deque functionality is provided via removeLast() and addFirst(long).
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int head
      Index of first element.
      int size
      Number of elements in the queue.
      protected int tail
      Index of last element.
      protected long[] values
      Contains the values in the queue.
    • Constructor Summary

      Constructors 
      Constructor Description
      LongQueue()
      Creates a new LongQueue which can hold 16 values without needing to resize backing array.
      LongQueue​(int initialSize)
      Creates a new LongQueue which can hold the specified number of values without needing to resize backing array.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFirst​(long value)
      Prepend given value to the head.
      void addLast​(long value)
      Append given value to the tail.
      void clear()
      Removes all values from this queue.
      void ensureCapacity​(int additional)
      Increases the size of the backing array to accommodate the specified number of additional items.
      boolean equals​(java.lang.Object o)  
      long first()
      Returns the first (head) item in the queue (without removing it).
      long get​(int index)
      Retrieves the value in queue without removing it.
      int hashCode()  
      int indexOf​(long value)
      Returns the index of first occurrence of value in the queue, or -1 if no such value exists.
      boolean isEmpty()
      Returns true if the queue is empty.
      long last()
      Returns the last (tail) item in the queue (without removing it).
      boolean notEmpty()
      Returns true if the queue has one or more items.
      long removeFirst()
      Remove the first item from the queue.
      long removeIndex​(int index)
      Removes and returns the item at the specified index.
      long removeLast()
      Remove the last item from the queue.
      boolean removeValue​(long value)
      Removes the first instance of the specified value in the queue.
      protected void resize​(int newSize)
      Resize backing array.
      java.lang.String toString()  
      java.lang.String toString​(java.lang.String separator)  
      • Methods inherited from class java.lang.Object

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

      • values

        protected long[] values
        Contains the values in the queue. Head and tail indices go in a circle around this array, wrapping at the end.
      • head

        protected int head
        Index of first element. Logically smaller than tail. Unless empty, it points to a valid element inside queue.
      • tail

        protected int tail
        Index of last element. Logically bigger than head. Usually points to an empty position, but points to the head when full (size == values.length).
      • size

        public int size
        Number of elements in the queue.
    • Constructor Detail

      • LongQueue

        public LongQueue()
        Creates a new LongQueue which can hold 16 values without needing to resize backing array.
      • LongQueue

        public LongQueue​(int initialSize)
        Creates a new LongQueue which can hold the specified number of values without needing to resize backing array.
    • Method Detail

      • addLast

        public void addLast​(long value)
        Append given value to the tail. (enqueue to tail) Unless backing array needs resizing, operates in O(1) time.
      • addFirst

        public void addFirst​(long value)
        Prepend given value to the head. (enqueue to head) Unless backing array needs resizing, operates in O(1) time.
        See Also:
        addLast(long)
      • ensureCapacity

        public void ensureCapacity​(int additional)
        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.
      • resize

        protected void resize​(int newSize)
        Resize backing array. newSize must be bigger than current size.
      • removeFirst

        public long removeFirst()
        Remove the first item from the queue. (dequeue from head) Always O(1).
        Returns:
        removed value
        Throws:
        java.util.NoSuchElementException - when queue is empty
      • removeLast

        public long removeLast()
        Remove the last item from the queue. (dequeue from tail) Always O(1).
        Returns:
        removed
        Throws:
        java.util.NoSuchElementException - when queue is empty
        See Also:
        removeFirst()
      • indexOf

        public int indexOf​(long value)
        Returns the index of first occurrence of value in the queue, or -1 if no such value exists.
        Returns:
        An index of first occurrence of value in queue or -1 if no such value exists
      • removeValue

        public boolean removeValue​(long value)
        Removes the first instance of the specified value in the queue.
        Returns:
        true if value was found and removed, false otherwise
      • removeIndex

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

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

        public boolean isEmpty()
        Returns true if the queue is empty.
      • first

        public long first()
        Returns the first (head) item in the queue (without removing it).
        Throws:
        java.util.NoSuchElementException - when queue is empty
        See Also:
        addFirst(long), removeFirst()
      • last

        public long last()
        Returns the last (tail) item in the queue (without removing it).
        Throws:
        java.util.NoSuchElementException - when queue is empty
        See Also:
        addLast(long), removeLast()
      • get

        public long get​(int index)
        Retrieves the value in queue without removing it. Indexing is from the front to back, zero based. Therefore get(0) is the same as first().
        Throws:
        java.lang.IndexOutOfBoundsException - when the index is negative or >= size
      • clear

        public void clear()
        Removes all values from this queue.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toString

        public java.lang.String toString​(java.lang.String separator)
      • hashCode

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

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object