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.

category Zend
package Zend_Stdlib
copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
license New BSD License

 Methods

Does the queue contain the given datum?

contains(mixed $datum) : boolean

Parameters

$datum

mixed

Returns

boolean

How many items are in the queue?

count() : integer

Returns

integer

Extract a node from the inner queue and sift up

extract() : mixed

Returns

mixed

Retrieve the inner iterator

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.

Returns

\SplPriorityQueue

Does the queue have an item with the given priority?

hasPriority(integer $priority) : boolean

Parameters

$priority

integer

Returns

boolean

Insert an item into the queue

insert(mixed $data, integer $priority = 1) : \Zend_Stdlib_PriorityQueue

Priority defaults to 1 (low priority) if none provided.

Parameters

$data

mixed

$priority

integer

Returns

\Zend_Stdlib_PriorityQueue

Is the queue empty?

isEmpty() : boolean

Returns

boolean

Remove an item from the queue

remove(mixed $datum) : boolean

This is different than \extract(); 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

Returns

booleanFalse if the item was not found, true otherwise.

Return data from an internal item

returnData(array $item) : mixed

Used as a lambda in toArray().

Parameters

$item

array

Returns

mixed

Return priority from an internal item

returnPriority(array $item) : mixed

Used as a lambda in toArray().

Parameters

$item

array

Returns

mixed

Serialize the data structure

serialize() : string

Returns

string

Specify the internal queue class

setInternalQueueClass(string $class) : \Zend_Stdlib_PriorityQueue

Please see \getIterator() for details on the necessity of an internal queue class. The class provided should extend SplPriorityQueue.

Parameters

$class

string

Returns

\Zend_Stdlib_PriorityQueue

Serialize to an array

toArray(integer $flag = self::EXTR_DATA) : array

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

integer

Returns

array

Peek at the top node in the queue, based on priority.

top() : mixed

Returns

mixed

Unserialize a string into a Zend_Stdlib_PriorityQueue object

unserialize(string $data) : void

Serialization format is compatible with Zend_Stdlib_SplPriorityQueue

Parameters

$data

string

Get the inner priority queue instance

getQueue() : \Zend_Stdlib_SplPriorityQueue

Returns

\Zend_Stdlib_SplPriorityQueue

 Properties

 

Actual items aggregated in the priority queue. Each item is an array with keys "data" and "priority".

$items : array

Default

array()
 

Inner queue object

$queue : \SplPriorityQueue

Default

 

Inner queue class to use for iteration

$queueClass : string

Default

'Zend_Stdlib_SplPriorityQueue'

 Constants

 

EXTR_BOTH

EXTR_BOTH = 3 
 

EXTR_DATA

EXTR_DATA = 1 
 

EXTR_PRIORITY

EXTR_PRIORITY = 2