Package org.agrona.collections
Class IntArrayQueue
Queue of ints which stores the elements without boxing. Null is represented by a special
nullValue()
.
The IntArrayQueue.IntIterator
is cached by default to avoid allocation unless directed to do so in the constructor.
Note: This class is not threadsafe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal class
SpecialisedIterator
from which the value can be retrieved without boxing viaIntArrayQueue.IntIterator.nextValue()
. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Default representation of null for an element.private int[]
private int
private IntArrayQueue.IntIterator
static final int
Minimum capacity for the queue which must also be a power of 2.private final int
private final boolean
private int
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new queue defaulting toMIN_CAPACITY
capacity,DEFAULT_NULL_VALUE
and cached iterators.IntArrayQueue
(int nullValue) Construct a new queue defaulting toMIN_CAPACITY
capacity and cached iterators.IntArrayQueue
(int initialCapacity, int nullValue) Construct a new queue default to cached iterators.IntArrayQueue
(int initialCapacity, int nullValue, boolean shouldAvoidAllocation) Construct a new queue providing all the config options. -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addInt
(int element) Offer an element to the tail of the queue without boxing.int
capacity()
The current capacity for the collection.void
clear()
element()
int
Peek at the element on the head of the queue without boxing.void
void
forEachInt
(IntConsumer action) Iterate over the collection without boxing.private void
boolean
isEmpty()
iterator()
int
The value representing a null element.boolean
boolean
offerInt
(int element) Offer an element to the tail of the queue without boxing.peek()
int
peekInt()
Peek at the element on the head of the queue without boxing.poll()
int
pollInt()
Poll the element from the head of the queue without boxing.remove()
int
Remove the element at the head of the queue without boxing.int
size()
toString()
Methods inherited from class java.util.AbstractQueue
addAll
Methods inherited from class java.util.AbstractCollection
contains, containsAll, remove, removeAll, retainAll, toArray, toArray
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
contains, containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Field Details
-
DEFAULT_NULL_VALUE
public static final int DEFAULT_NULL_VALUEDefault representation of null for an element.- See Also:
-
MIN_CAPACITY
public static final int MIN_CAPACITYMinimum capacity for the queue which must also be a power of 2.- See Also:
-
shouldAvoidAllocation
private final boolean shouldAvoidAllocation -
head
private int head -
tail
private int tail -
nullValue
private final int nullValue -
elements
private int[] elements -
iterator
-
-
Constructor Details
-
IntArrayQueue
public IntArrayQueue()Construct a new queue defaulting toMIN_CAPACITY
capacity,DEFAULT_NULL_VALUE
and cached iterators. -
IntArrayQueue
public IntArrayQueue(int nullValue) Construct a new queue defaulting toMIN_CAPACITY
capacity and cached iterators.- Parameters:
nullValue
- cannot be stored in the queue and used as a sentinel.
-
IntArrayQueue
public IntArrayQueue(int initialCapacity, int nullValue) Construct a new queue default to cached iterators.- Parameters:
initialCapacity
- for the queue which will be rounded up to the nearest power of 2.nullValue
- which cannot be stored in the queue and used as a sentinel.
-
IntArrayQueue
public IntArrayQueue(int initialCapacity, int nullValue, boolean shouldAvoidAllocation) Construct a new queue providing all the config options.- Parameters:
initialCapacity
- for the queue which will be rounded up to the nearest power of 2.nullValue
- which cannot be stored in the queue and used as a sentinel.shouldAvoidAllocation
- true to cache the iterator otherwise false to allocate a new iterator each time.
-
-
Method Details
-
nullValue
public int nullValue()The value representing a null element.- Returns:
- value representing a null element.
-
capacity
public int capacity()The current capacity for the collection.- Returns:
- the current capacity for the collection.
-
size
public int size()- Specified by:
size
in interfaceCollection<Integer>
- Specified by:
size
in classAbstractCollection<Integer>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<Integer>
- Overrides:
isEmpty
in classAbstractCollection<Integer>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<Integer>
- Overrides:
clear
in classAbstractQueue<Integer>
-
offer
-
offerInt
public boolean offerInt(int element) Offer an element to the tail of the queue without boxing.- Parameters:
element
- to be offered to the queue.- Returns:
- will always be true as long as the underlying array can be expanded.
-
add
- Specified by:
add
in interfaceCollection<Integer>
- Specified by:
add
in interfaceQueue<Integer>
- Overrides:
add
in classAbstractQueue<Integer>
-
addInt
public boolean addInt(int element) Offer an element to the tail of the queue without boxing.- Parameters:
element
- to be offered to the queue.- Returns:
- will always be true as long as the underlying array can be expanded.
-
peek
-
peekInt
public int peekInt()Peek at the element on the head of the queue without boxing.- Returns:
- the element at the head of the queue without removing it.
-
poll
-
pollInt
public int pollInt()Poll the element from the head of the queue without boxing.- Returns:
- the element at the head of the queue removing it. If empty then
nullValue
.
-
remove
-
element
-
elementInt
public int elementInt()Peek at the element on the head of the queue without boxing.- Returns:
- the element at the head of the queue without removing it.
- Throws:
NoSuchElementException
- if the queue is empty.
-
removeInt
public int removeInt()Remove the element at the head of the queue without boxing.- Returns:
- the element at the head of the queue.
- Throws:
NoSuchElementException
- if the queue is empty.
-
toString
- Overrides:
toString
in classAbstractCollection<Integer>
-
forEach
-
forEachInt
Iterate over the collection without boxing.- Parameters:
action
- to be taken for each element.
-
iterator
- Specified by:
iterator
in interfaceCollection<Integer>
- Specified by:
iterator
in interfaceIterable<Integer>
- Specified by:
iterator
in classAbstractCollection<Integer>
-
increaseCapacity
private void increaseCapacity()
-