Class CircularQueue<E>
- java.lang.Object
-
- org.chocosolver.util.objects.queues.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 booleanaddFirst(E e)Put a new element at the head of the queue.booleanaddLast(E e)Put a new element at the tail of the queue.voidclear()Removes all the content of the queueEget(int index)Get theindexelement of the queue, 0 being the last elementintindexOf(E elem)Search an element equal to the parameter in theCircularQueue, and return its index (0 is the last element)booleanisEmpty()EpollFirst()Removes the first element of the queue and returns it
This method is the main reason we re-wrote the class.EpollLast()Removes the last element of the queue and returns it
This method is the main reason we re-wrote the class.Eremove()This method is the main reason we re-wrote the class.Eremove(int index)Removes theindexelement of the queue and removes the resulting gap
This method is the main reason we re-wrote the class.booleanremove(E e)Remove the first element equal to the value given as parameterintsize()Get the current number of elements
-
-
-
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 theindexelement 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. TheCircularQueuegrows 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. TheCircularQueuegrows 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 theCircularQueue, and return its index (0 is the last element)- Parameters:
elem- element to query in theCircularQueue- 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 theindexelement 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 theCircularQueue- Returns:
- if the value has been removed
-
-