Class ExtendedPriorityQueue<T>
- Type Parameters:
T
- type of object in the queue.
It is based on BinaryArrayAddressableHeap, and it maintains memory of all the elements that have been inserted and removed (see getStatus(Object)
).
In the constructor, it is possible to decide if the queue must be a max priority or a min priority queue.
- Version:
- $Rev: 732 $
- Author:
- posenato
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
The possible state of an element with respect to aExtendedPriorityQueue
. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for minimum priority queue based on binary heap.ExtendedPriorityQueue
(boolean maximum) Default constructor for min/max priority queue based on binary heap.ExtendedPriorityQueue
(int initialCapacity) Default constructor for minimum priority queue based on binary heap.ExtendedPriorityQueue
(int initialCapacity, boolean maximum) General constructor for min/max priority queue based on binary heap. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
clear()
Makes the queue empty.void
Removes the element if it is present in the queue.Removes and returns the item with minimum (maximum) priority in this heap.it.unimi.dsi.fastutil.objects.AbstractObject2IntMap.BasicEntry
<T> Removes and returns the first entry in this heap.it.unimi.dsi.fastutil.objects.Object2IntMap
<T> it.unimi.dsi.fastutil.objects.ObjectList
<T> It runs in O(n) time.It runs in O(1) time.int
getPriority
(T item) Returns the priority of the item if it is or was present in the queue,Constants.INT_POS_INFINITE
if the item was never inserted, and this is a min queue,Constants.INT_NEG_INFINITE
if the item was never inserted and this is a max queue.Usually, in a priority queue, an object is first added and, possibly, removed.boolean
insertOrUpdate
(T item, int priority) Inserts or updates the given item and its priority into this heap.final boolean
isEmpty()
It runs in O(1) time.final long
size()
It runs in O(1) time.toString()
-
Constructor Details
-
ExtendedPriorityQueue
public ExtendedPriorityQueue(int initialCapacity) Default constructor for minimum priority queue based on binary heap.- Parameters:
initialCapacity
- initial capacity
-
ExtendedPriorityQueue
public ExtendedPriorityQueue(boolean maximum) Default constructor for min/max priority queue based on binary heap.- Parameters:
maximum
- true if it must be a maximum priority queue, false if it must be a minimum priority queue.
-
ExtendedPriorityQueue
public ExtendedPriorityQueue()Default constructor for minimum priority queue based on binary heap. -
ExtendedPriorityQueue
ExtendedPriorityQueue(int initialCapacity, boolean maximum) General constructor for min/max priority queue based on binary heap.- Parameters:
initialCapacity
- initial capacitymaximum
- true if it must be a maximum priority queue, false if it must be a minimum priority queue.
-
-
Method Details
-
clear
public final void clear()Makes the queue empty. -
delete
Removes the element if it is present in the queue.It runs in O(log n) time.
- Parameters:
element
- element to remove
-
extractFirst
Removes and returns the item with minimum (maximum) priority in this heap.It runs in O(1) time.
- Returns:
- the T with minimum priority.
-
extractFirstEntry
Removes and returns the first entry in this heap.It runs in O(1) time.
- Returns:
- the entry <T, int> with minimum priority.
-
getAllDeterminedPriorities
- Returns:
- the map (T, priority) of all the elements that are/have been in the queue.
It runs in O(n) time.
-
getElements
It runs in O(n) time.- Returns:
- the element present in the queue (without priority). An empty list, if there is no element.
-
getFirstEntry
It runs in O(1) time.- Returns:
- the minimum first entry of the queue without modifying it. If the queue is empty, it returns null.
-
getFirstPriority
- Returns:
- the first priority
-
getPriority
Returns the priority of the item if it is or was present in the queue,Constants.INT_POS_INFINITE
if the item was never inserted, and this is a min queue,Constants.INT_NEG_INFINITE
if the item was never inserted and this is a max queue.It runs in O(alpha) time because the element is searched in a companion hash table.
- Parameters:
item
- the search element.- Returns:
- the priority of the item.
-
getStatus
Usually, in a priority queue, an object is first added and, possibly, removed.This class remembers all objects that have been added to the queue.
Therefore, this method returns the possible state of an element (see
ExtendedPriorityQueue.Status
).It runs in O(alpha) time because the element is searched in a companion hash table.
- Parameters:
obj
- an object.- Returns:
- a
ExtendedPriorityQueue.Status
object. If obj is null or the queue is empty, it returnsExtendedPriorityQueue.Status.neverPresent
.
-
insertOrUpdate
Inserts or updates the given item and its priority into this heap.The update is performed only when the new value is lower/greater than the present one, according to the min/max type of the queue.
If the item was present (i.e., already extracted), the method does nothing.
It runs in O(log n) time.
- Parameters:
item
- an object.priority
- the value of priority- Returns:
- true if 'item' was inserted or updated, or was present with a value smaller/greater than the given one according to the min/max type of the queue; False if the item was present and extracted with a priority greater/smaller than the given one, according to the minimum/maximum type of the queue.
- Throws:
IllegalArgumentException
- if an item is null.
-
isEmpty
public final boolean isEmpty()It runs in O(1) time.- Returns:
- true if the queue is empty, false otherwise.
-
size
public final long size()It runs in O(1) time.- Returns:
- the number of elements in the queue.
-
toString
-