justblackbird / stable-priority-queue
Stable implementation of priority queue
Installs: 21
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/justblackbird/stable-priority-queue
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: 2025-09-22 23:18:53 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