thomaslarsson / priorityqueue
升序/降序优先队列。在出队时保持具有相同优先级的节点的顺序。
v1.0.0
2013-10-02 07:12 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-23 16:31:50 UTC
README
升序/降序优先队列。在出队时保持具有相同优先级的节点的顺序。
安装
您可以使用Composer安装此包。
- 安装composer。
- 将以下依赖项添加到composer.json中
{
"require": {
"thomaslarsson/priorityqueue": "1.0.*"
}
}
现在该包已安装到您的vendor目录中。
用法
// Require composer's autoload
require 'vendor/autoload.php';
// Optional: Alias/import the package's namespace
use ThomasLarsson\PriorityQueue\MinPriorityQueue as MinPriorityQueue,
ThomasLarsson\PriorityQueue\MaxPriorityQueue as MaxPriorityQueue;
// Create a ascending queue (Use the package's namespace unless you aliased it)
$ascendingQueue = new MinPriorityQueue();
// ... OR a descending queue.
$descendingQueue = new MaxPriorityQueue(); // A decending queue
// Create some data sorted descending (Just to illustrate that it's working)
$ascendingQueue->insert(4, 0);
$ascendingQueue->insert(3, 0);
$ascendingQueue->insert(2, 0);
$ascendingQueue->insert(1, 0);
$ascendingQueue->insert(0, 0);
// Display the sorted result
foreach ( $ascendingQueue as $value )
{
echo $value . "\n";
}
修复了等优先级排序错误
ThomasLarsson/PriorityQueue基于SplPriorityQueue构建。此实现解决了两个或多个节点共享相似优先级时的问题。标准SPL实现将按无特定顺序(随机)出队具有相同优先级的节点,如手册中所述。
SPLPriorityQueue::compare()
注意:具有相同优先级的多个元素将按无特定顺序出队。