justblackbird / stable-priority-queue
Stable implementation of priority queue
0.1.0
2020-08-22 11:49 UTC
Requires
- php: ^7.4
Requires (Dev)
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: ^3.5
- vimeo/psalm: ^3.14
This package is auto-updated.
Last update: 2024-10-22 21:23:34 UTC
README
Stable implementation of priority queue data structure in PHP.
Why
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"
!
Installation
composer require justblackbird/stable-priority-queue
Usage
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.
License
MIT (c) Dmitry Simushev