Class CircularQueue<E>


  • public class CircularQueue<E>
    extends Object
    A fixed sized circular queue optimized for removing first and last elements. Some few (essential regarding the usage) methods are implemented.
    Be aware of the size computation: the modulo operation is not efficient in java. On the other hand, the modulo of powers of 2 can alternatively be expressed as a bitwise AND operation:
    x % 2n == x & (2n - 1)
    That is why the size of the data is automatically set to the closest greater powers of 2 value.
    Since:
    29 sept. 2010
    Author:
    Charles Prud'homme
    • Constructor Summary

      Constructors 
      Constructor Description
      CircularQueue​(int size)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addFirst​(E e)
      Put a new element at the head of the queue.
      boolean addLast​(E e)
      Put a new element at the tail of the queue.
      void clear()
      Removes all the content of the queue
      E get​(int index)
      Get the index element of the queue, 0 being the last element
      int indexOf​(E elem)
      Search an element equal to the parameter in the CircularQueue, and return its index (0 is the last element)
      boolean isEmpty()  
      E pollFirst()
      Removes the first element of the queue and returns it
      This method is the main reason we re-wrote the class.
      E pollLast()
      Removes the last element of the queue and returns it
      This method is the main reason we re-wrote the class.
      E remove()
      This method is the main reason we re-wrote the class.
      E remove​(int index)
      Removes the index element of the queue and removes the resulting gap
      This method is the main reason we re-wrote the class.
      boolean remove​(E e)
      Remove the first element equal to the value given as parameter
      int size()
      Get the current number of elements
    • Constructor Detail

      • CircularQueue

        public CircularQueue​(int size)
    • Method Detail

      • isEmpty

        public boolean isEmpty()
        Returns:
        if the queue has no element (size == 0)
      • clear

        public void clear()
        Removes all the content of the queue
      • size

        public int size()
        Get the current number of elements
        Returns:
        current number of inserted elements in the queue
      • get

        public E get​(int index)
        Get the index element of the queue, 0 being the last element
        Parameters:
        index - index of the element to retrieve
        Returns:
        the element itself, or null if it does not exist
        See Also:
        indexOf(Object)
      • addFirst

        public boolean addFirst​(E e)
        Put a new element at the head of the queue. The CircularQueue grows by itself if it reaches its max capacity
        Parameters:
        e - element to add
        Returns:
        if the element has been added or not
      • addLast

        public boolean addLast​(E e)
        Put a new element at the tail of the queue. The CircularQueue grows by itself if it reaches its max capacity
        Parameters:
        e - element to add
        Returns:
        if the element has been added or not
      • indexOf

        public int indexOf​(E elem)
        Search an element equal to the parameter in the CircularQueue, and return its index (0 is the last element)
        Parameters:
        elem - element to query in the CircularQueue
        Returns:
        index of the element starting from the last of the CircularQueue, -1 if the element does not exists
      • pollFirst

        public E pollFirst()
        Removes the first element of the queue and returns it
        This method is the main reason we re-wrote the class. It is optimized for removing first and last elements but also allows you to remove in the middle of the list.
        Returns:
        first element of the queue
      • pollLast

        public E pollLast()
        Removes the last element of the queue and returns it
        This method is the main reason we re-wrote the class. It is optimized for removing first and last elements but also allows you to remove in the middle of the list.
        Returns:
        last element of the queue
      • remove

        public E remove()
        This method is the main reason we re-wrote the class. It is optimized for removing first and last elements but also allows you to remove in the middle of the list.
      • remove

        public E remove​(int index)
        Removes the index element of the queue and removes the resulting gap
        This method is the main reason we re-wrote the class. It is optimized for removing first and last elements but also allows you to remove in the middle of the list.
        Parameters:
        index -
        Returns:
        removed element
        See Also:
        indexOf(Object)
      • remove

        public boolean remove​(E e)
        Remove the first element equal to the value given as parameter
        Parameters:
        e - value to remove from the CircularQueue
        Returns:
        if the value has been removed