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

Commit

Permalink
Made SplPriorityQueue honor insert order for equal priorities
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
weierophinney committed Jan 13, 2011
1 parent 9206663 commit e6b97af
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/PriorityQueue.php
Expand Up @@ -54,7 +54,7 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
* Inner queue class to use for iteration
* @var string
*/
protected $queueClass = 'SplPriorityQueue';
protected $queueClass = 'Zend\Stdlib\SplPriorityQueue';

/**
* Actual items aggregated in the priority queue. Each item is an array
Expand Down
28 changes: 27 additions & 1 deletion src/SplPriorityQueue.php
Expand Up @@ -28,13 +28,39 @@
/**
* Serializable version of SplPriorityQueue
*
* Also, provides predictable heap order for datums added with the same priority
* (i.e., they will be emitted in the same order they are enqueued).
*
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class SplPriorityQueue extends \SplPriorityQueue implements Serializable
{
/**
* @var int Seed used to ensure queue order for items of the same priority
*/
protected $serial = PHP_INT_MAX;

/**
* Insert a value with a given priority
*
* Utilizes {@var $serial} to ensure that values of equal priority are
* emitted in the same order in which they are inserted.
*
* @param mixed $datum
* @param mixed $priority
* @return void
*/
public function insert($datum, $priority)
{
if (!is_array($priority)) {
$priority = array($priority, $this->serial--);
}
parent::insert($datum, $priority);
}

/**
* Serialize to an array
*
Expand All @@ -60,7 +86,7 @@ public function toArray()
// Return only the data
$return = array();
foreach ($array as $item) {
$return[$item['priority']] = $item['data'];
$return[] = $item['data'];
}

return $return;
Expand Down
24 changes: 20 additions & 4 deletions test/SplPriorityQueueTest.php
Expand Up @@ -43,6 +43,22 @@ public function setUp()
$this->queue->insert('bat', 1);
}

public function testMaintainsInsertOrderForDataOfEqualPriority()
{
$queue = new SplPriorityQueue();
$queue->insert('foo', 1000);
$queue->insert('bar', 1000);
$queue->insert('baz', 1000);
$queue->insert('bat', 1000);

$expected = array('foo', 'bar', 'baz', 'bat');
$test = array();
foreach ($queue as $datum) {
$test[] = $datum;
}
$this->assertEquals($expected, $test);
}

public function testSerializationAndDeserializationShouldMaintainState()
{
$s = serialize($this->queue);
Expand All @@ -64,10 +80,10 @@ public function testSerializationAndDeserializationShouldMaintainState()
public function testCanRetrieveQueueAsArray()
{
$expected = array(
4 => 'bar',
3 => 'foo',
2 => 'baz',
1 => 'bat',
'bar',
'foo',
'baz',
'bat',
);
$test = $this->queue->toArray();
$this->assertSame($expected, $test, var_export($test, 1));
Expand Down

0 comments on commit e6b97af

Please sign in to comment.