Stable implementation of priority queue data structure in PHP.
There is an implementation of priority queue in SPL: SplPriorityQueue
. The problem is it is unstable. Take a look at the example below:
$q = new \SplPriorityQueue();
$q->insert(1, 0);
$q->insert(2, 0);
$q->insert(3, 0);
$q->insert(4, 0);
while (!$q->isEmpty()) {
echo $q->extract() . " ";
}
This example retrieves a string "1 4 3 2"
and not "1 2 3 4"
. This library provides an implementation that extracts values of the same priorites in the order they came in. The example above will return "1 2 3 4"
!
composer require justblackbird/stable-priority-queue
use JustBlackBird\StablePriorityQueue\Queue;
$q = new Queue();
$q->insert(1, 0);
$q->insert(2, 0);
$q->insert(3, 0);
$q->insert(4, 0);
while (!$q->isEmpty()) {
echo $q->extract() . " ";
}
// "1 2 3 4" will be outputted.
MIT (c) Dmitry Simushev