Class ArrayQueue<E>

java.lang.Object
net.hydromatic.morel.util.ArrayQueue<E>
Type Parameters:
E - Element type

public class ArrayQueue<E> extends Object
Like a list, but poll() (equivalent to remove(0) is O(1).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private E[]
     
    private int
     
    private int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty ArrayQueue.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static int
    add(int i, int j, int modulus)
     
    void
    add(E e)
    Adds an element to the tail.
    Returns a view of the contents as a list.
    private static int
    dec(int i, int modulus)
     
    void
    forEach(Consumer<? super E> consumer)
    Calls a consumer with each element, in order.
    get(int i)
    Returns the element at position i.
    private void
    Increases the capacity by 1.
    private static int
    inc(int i, int modulus)
     
    boolean
    Returns whether this queue is empty.
     
    @Nullable E
    Removes the element at the head of this queue, or returns null.
    remove(int i)
    Removes element i from the queue in O(1) time.
    set(int i, E e)
    Sets the element at position i.
    int
    Returns the number of elements in this queue.
    private static int
    sub(int i, int j, int modulus)
     
     

    Methods inherited from class java.lang.Object

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

    • elements

      private E[] elements
    • start

      private int start
    • end

      private int end
  • Constructor Details

    • ArrayQueue

      public ArrayQueue()
      Creates an empty ArrayQueue.
  • Method Details

    • toString

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

      public @Nullable E poll()
      Removes the element at the head of this queue, or returns null.
    • size

      public int size()
      Returns the number of elements in this queue.
    • isEmpty

      public boolean isEmpty()
      Returns whether this queue is empty. (Same as size() == 0.)
    • get

      public E get(int i)
      Returns the element at position i.
    • set

      public E set(int i, E e)
      Sets the element at position i.
    • add

      public void add(E e)
      Adds an element to the tail.
    • grow

      private void grow()
      Increases the capacity by 1.
    • asList

      public List<E> asList()
      Returns a view of the contents as a list.
    • inc

      private static int inc(int i, int modulus)
    • dec

      private static int dec(int i, int modulus)
    • add

      private static int add(int i, int j, int modulus)
    • sub

      private static int sub(int i, int j, int modulus)
    • forEach

      public void forEach(Consumer<? super E> consumer)
      Calls a consumer with each element, in order.
    • remove

      public E remove(int i)
      Removes element i from the queue in O(1) time.

      If i is the first element, removes it (equivalent to calling poll(); if i is the last element, removes it; otherwise moves the last element into position i and shortens the queue.

    • listIterator

      public ListIterator<E> listIterator()