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

Commit

Permalink
Merge branch 'hotfix/ZF2-114' of https://github.com/andriesss/zf2 int…
Browse files Browse the repository at this point in the history
…o hotfix/zf2-114
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 29 deletions.
70 changes: 41 additions & 29 deletions src/PriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
* Re-usable, serializable priority queue implementation
*
* SplPriorityQueue acts as a heap; on iteration, each item is removed from the
* queue. If you wish to re-use such a queue, you need to clone it first. This
* queue. If you wish to re-use such a queue, you need to clone it first. This
* makes for some interesting issues if you wish to delete items from the queue,
* or, as already stated, iterate over it multiple times.
*
* This class aggregates items for the queue itself, but also composes an
* This class aggregates items for the queue itself, but also composes an
* "inner" iterator in the form of an SplPriorityQueue object for performing
* the actual iteration.
*
Expand Down Expand Up @@ -73,9 +73,9 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable
* Insert an item into the queue
*
* Priority defaults to 1 (low priority) if none provided.
*
* @param mixed $data
* @param int $priority
*
* @param mixed $data
* @param int $priority
* @return PriorityQueue
*/
public function insert($data, $priority = 1)
Expand All @@ -95,11 +95,11 @@ public function insert($data, $priority = 1)
* This is different than {@link extract()}; its purpose is to dequeue an
* item.
*
* This operation is potentially expensive, as it requires
* This operation is potentially expensive, as it requires
* re-initialization and re-population of the inner queue.
*
*
* Note: this removes the first item matching the provided item found. If
* the same item has been added multiple times, it will not remove other
* the same item has been added multiple times, it will not remove other
* instances.
*
* @param mixed $datum
Expand Down Expand Up @@ -128,7 +128,7 @@ public function remove($datum)

/**
* Is the queue empty?
*
*
* @return bool
*/
public function isEmpty()
Expand All @@ -138,7 +138,7 @@ public function isEmpty()

/**
* How many items are in the queue?
*
*
* @return int
*/
public function count()
Expand All @@ -148,7 +148,7 @@ public function count()

/**
* Peek at the top node in the queue, based on priority.
*
*
* @return mixed
*/
public function top()
Expand All @@ -157,8 +157,8 @@ public function top()
}

/**
* Extract a node from the inner queue and sift up
*
* Extract a node from the inner queue and sift up
*
* @return mixed
*/
public function extract()
Expand All @@ -171,11 +171,11 @@ public function extract()
*
* SplPriorityQueue acts as a heap, which typically implies that as items
* are iterated, they are also removed. This does not work for situations
* where the queue may be iterated multiple times. As such, this class
* aggregates the values, and also injects an SplPriorityQueue. This method
* retrieves the inner queue object, and clones it for purposes of
* where the queue may be iterated multiple times. As such, this class
* aggregates the values, and also injects an SplPriorityQueue. This method
* retrieves the inner queue object, and clones it for purposes of
* iteration.
*
*
* @return SplPriorityQueue
*/
public function getIterator()
Expand All @@ -186,7 +186,7 @@ public function getIterator()

/**
* Serialize the data structure
*
*
* @return string
*/
public function serialize()
Expand All @@ -198,8 +198,8 @@ public function serialize()
* Unserialize a string into a PriorityQueue object
*
* Serialization format is compatible with {@link Zend\Stdlib\SplPriorityQueue}
*
* @param string $data
*
* @param string $data
* @return void
*/
public function unserialize($data)
Expand All @@ -215,8 +215,8 @@ public function unserialize($data)
* By default, returns only the item data, and in the order registered (not
* sorted). You may provide one of the EXTR_* flags as an argument, allowing
* the ability to return priorities or both data and priority.
*
* @param int $flag
*
* @param int $flag
* @return array
*/
public function toArray($flag = self::EXTR_DATA)
Expand All @@ -242,8 +242,8 @@ public function toArray($flag = self::EXTR_DATA)
*
* Please see {@link getIterator()} for details on the necessity of an
* internal queue class. The class provided should extend SplPriorityQueue.
*
* @param string $class
*
* @param string $class
* @return PriorityQueue
*/
public function setInternalQueueClass($class)
Expand All @@ -254,8 +254,8 @@ public function setInternalQueueClass($class)

/**
* Does the queue contain the given datum?
*
* @param mixed $datum
*
* @param mixed $datum
* @return bool
*/
public function contains($datum)
Expand All @@ -270,8 +270,8 @@ public function contains($datum)

/**
* Does the queue have an item with the given priority?
*
* @param int $priority
*
* @param int $priority
* @return bool
*/
public function hasPriority($priority)
Expand All @@ -286,7 +286,7 @@ public function hasPriority($priority)

/**
* Get the inner priority queue instance
*
*
* @return SplPriorityQueue
*/
protected function getQueue()
Expand All @@ -302,4 +302,16 @@ protected function getQueue()
}
return $this->queue;
}

/**
* Add support for deep cloning
*
* @return void
*/
public function __clone()
{
if (null !== $this->queue) {
$this->queue = clone $this->queue;
}
}
}
33 changes: 33 additions & 0 deletions test/PriorityQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
*/
class PriorityQueueTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PriorityQueue
*/
protected $queue;

public function setUp()
{
$this->queue = new PriorityQueue();
Expand Down Expand Up @@ -132,4 +137,32 @@ public function testCanTestForExistenceOfPriorityInQueue()
$this->assertTrue($this->queue->hasPriority(3));
$this->assertFalse($this->queue->hasPriority(1000));
}

public function testCloningAlsoClonesQueue()
{
$foo = new \stdClass();
$foo->name = 'bar';

$queue = new PriorityQueue();
$queue->insert($foo, 1);
$queue->insert($foo, 2);

$queueClone = clone $queue;

while (!$queue->isEmpty()) {
$this->assertSame($foo, $queue->top());
$queue->remove($queue->top());
}

$this->assertTrue($queue->isEmpty());
$this->assertFalse($queueClone->isEmpty());
$this->assertEquals(2, $queueClone->count());

while (!$queueClone->isEmpty()) {
$this->assertSame($foo, $queueClone->top());
$queueClone->remove($queueClone->top());
}

$this->assertTrue($queueClone->isEmpty());
}
}

0 comments on commit 1bb67ac

Please sign in to comment.