Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit e6b97af

Browse files
committed
Made SplPriorityQueue honor insert order for equal priorities
- Overwrote insert() method to enforce FIFO order for values of the same priority - Zend\Stdlib\PriorityQueue now utilizes Zend\Stdlib\SplPriorityQueue internally - Zend\SignalSlot\Filter\FilterIterator now utilizes Zend\Stdlib\SplPriorityQueue internally
1 parent 9206663 commit e6b97af

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

src/PriorityQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
5454
* Inner queue class to use for iteration
5555
* @var string
5656
*/
57-
protected $queueClass = 'SplPriorityQueue';
57+
protected $queueClass = 'Zend\Stdlib\SplPriorityQueue';
5858

5959
/**
6060
* Actual items aggregated in the priority queue. Each item is an array

src/SplPriorityQueue.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,39 @@
2828
/**
2929
* Serializable version of SplPriorityQueue
3030
*
31+
* Also, provides predictable heap order for datums added with the same priority
32+
* (i.e., they will be emitted in the same order they are enqueued).
33+
*
3134
* @category Zend
3235
* @package Zend_Stdlib
3336
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
3437
* @license http://framework.zend.com/license/new-bsd New BSD License
3538
*/
3639
class SplPriorityQueue extends \SplPriorityQueue implements Serializable
3740
{
41+
/**
42+
* @var int Seed used to ensure queue order for items of the same priority
43+
*/
44+
protected $serial = PHP_INT_MAX;
45+
46+
/**
47+
* Insert a value with a given priority
48+
*
49+
* Utilizes {@var $serial} to ensure that values of equal priority are
50+
* emitted in the same order in which they are inserted.
51+
*
52+
* @param mixed $datum
53+
* @param mixed $priority
54+
* @return void
55+
*/
56+
public function insert($datum, $priority)
57+
{
58+
if (!is_array($priority)) {
59+
$priority = array($priority, $this->serial--);
60+
}
61+
parent::insert($datum, $priority);
62+
}
63+
3864
/**
3965
* Serialize to an array
4066
*
@@ -60,7 +86,7 @@ public function toArray()
6086
// Return only the data
6187
$return = array();
6288
foreach ($array as $item) {
63-
$return[$item['priority']] = $item['data'];
89+
$return[] = $item['data'];
6490
}
6591

6692
return $return;

test/SplPriorityQueueTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ public function setUp()
4343
$this->queue->insert('bat', 1);
4444
}
4545

46+
public function testMaintainsInsertOrderForDataOfEqualPriority()
47+
{
48+
$queue = new SplPriorityQueue();
49+
$queue->insert('foo', 1000);
50+
$queue->insert('bar', 1000);
51+
$queue->insert('baz', 1000);
52+
$queue->insert('bat', 1000);
53+
54+
$expected = array('foo', 'bar', 'baz', 'bat');
55+
$test = array();
56+
foreach ($queue as $datum) {
57+
$test[] = $datum;
58+
}
59+
$this->assertEquals($expected, $test);
60+
}
61+
4662
public function testSerializationAndDeserializationShouldMaintainState()
4763
{
4864
$s = serialize($this->queue);
@@ -64,10 +80,10 @@ public function testSerializationAndDeserializationShouldMaintainState()
6480
public function testCanRetrieveQueueAsArray()
6581
{
6682
$expected = array(
67-
4 => 'bar',
68-
3 => 'foo',
69-
2 => 'baz',
70-
1 => 'bat',
83+
'bar',
84+
'foo',
85+
'baz',
86+
'bat',
7187
);
7288
$test = $this->queue->toArray();
7389
$this->assertSame($expected, $test, var_export($test, 1));

0 commit comments

Comments
 (0)