HumHub Documentation (unofficial)

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.

Tags
template

TValue

template

TPriority of int

implements

IteratorAggregate<array-key, TValue>

Table of Contents

Interfaces

Countable
IteratorAggregate
Serializable

Constants

EXTR_BOTH  = 0x3
EXTR_DATA  = 0x1
EXTR_PRIORITY  = 0x2

Properties

$items  : array<int, TPriority}>
Actual items aggregated in the priority queue. Each item is an array with keys "data" and "priority".
$queue  : SplPriorityQueue<TPriority, TValue>|null
Inner queue object
$queueClass  : SplPriorityQueue>
Inner queue class to use for iteration

Methods

__clone()  : void
Add support for deep cloning
__serialize()  : array<int, TPriority}>
Magic method used for serializing of an instance.
__unserialize()  : void
Magic method used to rebuild an instance.
contains()  : bool
Does the queue contain the given datum?
count()  : int
How many items are in the queue?
extract()  : TValue
Extract a node from the inner queue and sift up
getIterator()  : SplPriorityQueue<TPriority, TValue>
Retrieve the inner iterator
hasPriority()  : bool
Does the queue have an item with the given priority?
insert()  : $this
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()  : $this
Specify the internal queue class
toArray()  : array<string|int, mixed>
Serialize to an 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.
top()  : TValue
Peek at the top node in the queue, based on priority.
unserialize()  : void
Unserialize a string into a PriorityQueue object
getQueue()  : SplPriorityQueue<TPriority, TValue>
Get the inner priority queue instance

Constants

Properties

$items

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

protected array<int, TPriority}> $items = []

$queue

Inner queue object

protected SplPriorityQueue<TPriority, TValue>|null $queue

$queueClass

Inner queue class to use for iteration

protected SplPriorityQueue> $queueClass = \Laminas\Stdlib\SplPriorityQueue::class

Methods

__clone()

Add support for deep cloning

public __clone() : void

__serialize()

Magic method used for serializing of an instance.

public __serialize() : array<int, TPriority}>
Return values
array<int, TPriority}>

__unserialize()

Magic method used to rebuild an instance.

public __unserialize(array<int, TPriority}$data) : void
Parameters
$data : array<int, TPriority}>

Data array.

contains()

Does the queue contain the given datum?

public contains(TValue $datum) : bool
Parameters
$datum : TValue
Return values
bool

extract()

Extract a node from the inner queue and sift up

public extract() : TValue
Return values
TValue

getIterator()

Retrieve the inner iterator

public getIterator() : SplPriorityQueue<TPriority, TValue>

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.

Attributes
#[ReturnTypeWillChange]
Return values
SplPriorityQueue<TPriority, TValue>

hasPriority()

Does the queue have an item with the given priority?

public hasPriority(TPriority $priority) : bool
Parameters
$priority : TPriority
Return values
bool

insert()

Insert an item into the queue

public insert(TValue $data[, TPriority $priority = 1 ]) : $this

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

Parameters
$data : TValue
$priority : TPriority = 1
Return values
$this

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(SplPriorityQueue> $class) : $this

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

Parameters
$class : SplPriorityQueue>
Return values
$this

toArray()

Serialize to an 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.

public toArray([int $flag = self::EXTR_DATA ]) : array<string|int, mixed>
Parameters
$flag : int = self::EXTR_DATA
Tags
psalm-return

($flag is self::EXTR_BOTH ? list<array{data: TValue, priority: TPriority}> : $flag is self::EXTR_PRIORITY ? list<TPriority> : list<TValue> )

Return values
array<string|int, mixed>

top()

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

public top() : TValue
Return values
TValue

unserialize()

Unserialize a string into a PriorityQueue object

public unserialize(string $data) : void

Serialization format is compatible with

Parameters
$data : string

getQueue()

Get the inner priority queue instance

protected getQueue() : SplPriorityQueue<TPriority, TValue>
Tags
throws
DomainException
psalm-assert

!null $this->queue

Return values
SplPriorityQueue<TPriority, TValue>

        
On this page

Search results