FastPriorityQueue
in package
implements
Iterator, Countable, Serializable
This is an efficient implementation of an integer priority queue in PHP
This class acts like a queue with insert() and extract(), removing the elements from the queue and it also acts like an Iterator without removing the elements. This behaviour can be used in mixed scenarios with high performance boost.
Tags
Table of Contents
Interfaces
- Iterator
- Countable
- Serializable
Constants
- EXTR_BOTH = \SplPriorityQueue::EXTR_BOTH
- EXTR_DATA = \SplPriorityQueue::EXTR_DATA
- EXTR_PRIORITY = \SplPriorityQueue::EXTR_PRIORITY
Properties
- $count : int
- Total number of elements in the queue
- $extractFlag : self::EXTR_*
- $index : int
- Index of the current element in the queue
- $maxPriority : int|null
- Max priority
- $priorities : array<int, int>
- Array of priorities
- $subIndex : int
- Sub index of the current element in the same priority level
- $subPriorities : array<string|int, mixed>
- Array of priorities used for the iteration
- $values : array<int, array<int, TValue>>
- Elements of the queue, divided by priorities
Methods
- __serialize() : array<string|int, mixed>
- __unserialize() : void
- contains() : bool
- Does the queue contain the given datum?
- count() : int
- Get the total number of elements in the queue
- current() : TValue|int|TValue|false, priority: int}|false
- Get the current element in the queue
- extract() : TValue|int|TValue, priority: int}|false
- Extract an element in the queue according to the priority and the order of insertion
- hasPriority() : bool
- Does the queue have an item with the given priority?
- insert() : void
- Insert an element in the queue with a specified priority
- isEmpty() : bool
- Check if the queue is empty
- key() : int
- Get the index of the current element in the queue
- next() : mixed
- Set the iterator pointer to the next element in the queue without removing the previous element
- remove() : bool
- Remove an item from the queue
- rewind() : mixed
- Rewind the current iterator
- serialize() : string
- Serialize
- setExtractFlags() : void
- Set the extract flag
- toArray() : array<int, TValue|int|TValue, priority: int}>
- Serialize to an array
- unserialize() : void
- Deserialize
- valid() : bool
- Check if the current iterator is valid
- nextAndRemove() : void
- Set the iterator pointer to the next element in the queue removing the previous element
Constants
EXTR_BOTH
public
mixed
EXTR_BOTH
= \SplPriorityQueue::EXTR_BOTH
EXTR_DATA
public
mixed
EXTR_DATA
= \SplPriorityQueue::EXTR_DATA
EXTR_PRIORITY
public
mixed
EXTR_PRIORITY
= \SplPriorityQueue::EXTR_PRIORITY
Properties
$count
Total number of elements in the queue
protected
int
$count
= 0
$extractFlag
protected
self::EXTR_*
$extractFlag
= self::EXTR_DATA
$index
Index of the current element in the queue
protected
int
$index
= 0
$maxPriority
Max priority
protected
int|null
$maxPriority
$priorities
Array of priorities
protected
array<int, int>
$priorities
= []
$subIndex
Sub index of the current element in the same priority level
protected
int
$subIndex
= 0
$subPriorities
Array of priorities used for the iteration
protected
array<string|int, mixed>
$subPriorities
= []
$values
Elements of the queue, divided by priorities
protected
array<int, array<int, TValue>>
$values
= []
Methods
__serialize()
public
__serialize() : array<string|int, mixed>
Return values
array<string|int, mixed>__unserialize()
public
__unserialize(array<string|int, mixed> $data) : void
Parameters
- $data : array<string|int, mixed>
contains()
Does the queue contain the given datum?
public
contains(mixed $datum) : bool
Parameters
- $datum : mixed
Return values
boolcount()
Get the total number of elements in the queue
public
count() : int
Attributes
Return values
intcurrent()
Get the current element in the queue
public
current() : TValue|int|TValue|false, priority: int}|false
Attributes
Return values
TValue|int|TValue|false, priority: int}|falseextract()
Extract an element in the queue according to the priority and the order of insertion
public
extract() : TValue|int|TValue, priority: int}|false
Return values
TValue|int|TValue, priority: int}|falsehasPriority()
Does the queue have an item with the given priority?
public
hasPriority(int $priority) : bool
Parameters
- $priority : int
Return values
boolinsert()
Insert an element in the queue with a specified priority
public
insert(TValue $value, int $priority) : void
Parameters
- $value : TValue
- $priority : int
isEmpty()
Check if the queue is empty
public
isEmpty() : bool
Return values
boolkey()
Get the index of the current element in the queue
public
key() : int
Attributes
Return values
intnext()
Set the iterator pointer to the next element in the queue without removing the previous element
public
next() : mixed
Attributes
remove()
Remove an item from the queue
public
remove(mixed $datum) : bool
This is different than ; its purpose is to dequeue an item.
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.
rewind()
Rewind the current iterator
public
rewind() : mixed
Attributes
serialize()
Serialize
public
serialize() : string
Return values
stringsetExtractFlags()
Set the extract flag
public
setExtractFlags(self::EXTR_* $flag) : void
Parameters
- $flag : self::EXTR_*
toArray()
Serialize to an array
public
toArray() : array<int, TValue|int|TValue, priority: int}>
Array will be priority => data pairs
Return values
array<int, TValue|int|TValue, priority: int}>unserialize()
Deserialize
public
unserialize(string $data) : void
Parameters
- $data : string
valid()
Check if the current iterator is valid
public
valid() : bool
Attributes
Return values
boolnextAndRemove()
Set the iterator pointer to the next element in the queue removing the previous element
protected
nextAndRemove() : void