Priority Queue¶
| Spice | |
|---|---|
PriorityQueue<T> struct¶
A priority queue in Spice is a commonly used data structure, in which every item has a priority attached to it. It is implemented as a binary max-heap stored in a contiguous array, so the item with the highest priority (the largest one according to the < operator) is always served first.
Time complexity:
Insert: O(log n)
Delete: O(log n)
Peek: O(1)
Priority queues pre-allocate space using an initial size and a resize factor to not have to re-allocate with every item pushed.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a priority queue pre-filled with a number of copies of a default value
Parameters
| Name | Type | Description |
|---|---|---|
initAllocItems |
unsigned long |
Number of items to insert |
defaultValue |
const T& |
Value to fill the priority queue with |
ctor¶
| Spice | |
|---|---|
Construct a priority queue, pre-allocating space for the given number of items
Parameters
| Name | Type | Description |
|---|---|---|
initAllocItems |
unsigned int |
Number of items to pre-allocate space for |
ctor¶
| Spice | |
|---|---|
Construct a priority queue, pre-allocating space for the given number of items
Parameters
| Name | Type | Description |
|---|---|---|
initAllocItems |
unsigned long |
Number of items to pre-allocate space for (default: INITIAL_CAPACITY) |
ctor¶
| Spice | |
|---|---|
Construct a priority queue as a deep copy of another priority queue
Parameters
| Name | Type | Description |
|---|---|---|
original |
const PriorityQueue<T>& |
Priority queue to copy |
Methods¶
push¶
| Spice | |
|---|---|
Add an item to the priority queue
Parameters
| Name | Type | Description |
|---|---|---|
item |
const T& |
pop¶
| Spice | |
|---|---|
Retrieve the item with the highest priority and remove it from the priority queue
Returns: T — Item with the highest priority
peek¶
| Spice | |
|---|---|
Retrieve the item with the highest priority without removing it from the priority queue
Returns: T& — Item with the highest priority
getSize¶
| Spice | |
|---|---|
Retrieve the current size of the priority queue
Returns: long — Current size of the priority queue
getCapacity¶
| Spice | |
|---|---|
Retrieve the current capacity of the priority queue
Returns: long — Current capacity of the priority queue
isEmpty¶
| Spice | |
|---|---|
Checks if the priority queue contains any items at the moment
Returns: bool — Empty or not empty
isFull¶
| Spice | |
|---|---|
Checks if the priority queue exhausts its capacity and needs to resize at the next call of push
Returns: bool — Full or not full
clear¶
| Spice | |
|---|---|
Removes all items from the priority queue
reserve¶
| Spice | |
|---|---|
Reserves itemCount items
Parameters
| Name | Type | Description |
|---|---|---|
itemCount |
unsigned long |
pack¶
| Spice | |
|---|---|
Frees allocated memory that is not used by the priority queue
Operators¶
operator=¶
| Spice | |
|---|---|
Copy-assign the contents of another priority queue into this one
Parameters
| Name | Type | Description |
|---|---|---|
newValue |
const PriorityQueue<T>& |
Priority queue to copy from |
operator==¶
| Spice | |
|---|---|
Check if two priority queues are equal, i.e. they hold the same items in the same priority order
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const PriorityQueue<T>& |
|
rhs |
const PriorityQueue<T>& |
Returns: bool — Equal or not equal
operator!=¶
| Spice | |
|---|---|
Check if two priority queues are not equal
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const PriorityQueue<T>& |
|
rhs |
const PriorityQueue<T>& |
Returns: bool — Not equal or equal