PriorityQueue
in package
implements
Countable, IteratorAggregate, Serializable
Re-usable, serializable priority queue implementation
SplPriorityQueue acts as a heap; on iteration, each item is removed from the queue. If you wish to re-use such a queue, you need to clone it first. This makes for some interesting issues if you wish to delete items from the queue, or, as already stated, iterate over it multiple times.
This class aggregates items for the queue itself, but also composes an "inner" iterator in the form of an SplPriorityQueue object for performing the actual iteration.
Interfaces, Classes, Traits and Enums
- Countable
- IteratorAggregate
- Serializable
Table of Contents
- EXTR_BOTH = 0x3
- EXTR_DATA = 0x1
- EXTR_PRIORITY = 0x2
- $items : array<string|int, mixed>
- Actual items aggregated in the priority queue. Each item is an array with keys "data" and "priority".
- $queue : SplPriorityQueue
- Inner queue object
- $queueClass : string
- Inner queue class to use for iteration
- __clone() : void
- Add support for deep cloning
- contains() : bool
- Does the queue contain the given datum?
- count() : int
- How many items are in the queue?
- extract() : mixed
- Extract a node from the inner queue and sift up
- getIterator() : SplPriorityQueue
- Retrieve the inner iterator
- hasPriority() : bool
- Does the queue have an item with the given priority?
- insert() : PriorityQueue
- Insert an item into the queue
- isEmpty() : bool
- Is the queue empty?
- remove() : bool
- Remove an item from the queue
- serialize() : string
- Serialize the data structure
- setInternalQueueClass() : PriorityQueue
- Specify the internal queue class
- toArray() : array<string|int, mixed>
- Serialize to an array
- top() : mixed
- Peek at the top node in the queue, based on priority.
- unserialize() : void
- Unserialize a string into a PriorityQueue object
- getQueue() : SplPriorityQueue
- Get the inner priority queue instance
Constants
EXTR_BOTH
public
mixed
EXTR_BOTH
= 0x3
EXTR_DATA
public
mixed
EXTR_DATA
= 0x1
EXTR_PRIORITY
public
mixed
EXTR_PRIORITY
= 0x2
Properties
$items
Actual items aggregated in the priority queue. Each item is an array with keys "data" and "priority".
protected
array<string|int, mixed>
$items
= []
$queue
Inner queue object
protected
SplPriorityQueue
$queue
$queueClass
Inner queue class to use for iteration
protected
string
$queueClass
= 'Zend\\Stdlib\\SplPriorityQueue'
Methods
__clone()
Add support for deep cloning
public
__clone() : void
Return values
void —contains()
Does the queue contain the given datum?
public
contains(mixed $datum) : bool
Parameters
- $datum : mixed
Return values
bool —count()
How many items are in the queue?
public
count() : int
Return values
int —extract()
Extract a node from the inner queue and sift up
public
extract() : mixed
Return values
mixed —getIterator()
Retrieve the inner iterator
public
getIterator() : SplPriorityQueue
SplPriorityQueue acts as a heap, which typically implies that as items are iterated, they are also removed. This does not work for situations where the queue may be iterated multiple times. As such, this class aggregates the values, and also injects an SplPriorityQueue. This method retrieves the inner queue object, and clones it for purposes of iteration.
Return values
SplPriorityQueue —hasPriority()
Does the queue have an item with the given priority?
public
hasPriority(int $priority) : bool
Parameters
- $priority : int
Return values
bool —insert()
Insert an item into the queue
public
insert(mixed $data[, int $priority = 1 ]) : PriorityQueue
Priority defaults to 1 (low priority) if none provided.
Parameters
- $data : mixed
- $priority : int = 1
Return values
PriorityQueue —isEmpty()
Is the queue empty?
public
isEmpty() : bool
Return values
bool —remove()
Remove an item from the queue
public
remove(mixed $datum) : bool
This is different than ; its purpose is to dequeue an item.
This operation is potentially expensive, as it requires re-initialization and re-population of the inner queue.
Note: this removes the first item matching the provided item found. If the same item has been added multiple times, it will not remove other instances.
Parameters
- $datum : mixed
Return values
bool —False if the item was not found, true otherwise.
serialize()
Serialize the data structure
public
serialize() : string
Return values
string —setInternalQueueClass()
Specify the internal queue class
public
setInternalQueueClass(string $class) : PriorityQueue
Please see for details on the necessity of an internal queue class. The class provided should extend SplPriorityQueue.
Parameters
- $class : string
Return values
PriorityQueue —toArray()
Serialize to an array
public
toArray([int $flag = self::EXTR_DATA ]) : array<string|int, mixed>
By default, returns only the item data, and in the order registered (not sorted). You may provide one of the EXTR_* flags as an argument, allowing the ability to return priorities or both data and priority.
Parameters
- $flag : int = self::EXTR_DATA
Return values
array<string|int, mixed> —top()
Peek at the top node in the queue, based on priority.
public
top() : mixed
Return values
mixed —unserialize()
Unserialize a string into a PriorityQueue object
public
unserialize(string $data) : void
Parameters
- $data : string
Return values
void —getQueue()
Get the inner priority queue instance
protected
getQueue() : SplPriorityQueue