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

Commit ad899a6

Browse files
committed
Merge pull request #60 from peterr/fix/infinite-loop-and-remove-method
\Zend\Stdlib\FastPriorityQueue - an infinite loop and bugs in remove()
2 parents 43d2099 + 5f4b887 commit ad899a6

File tree

2 files changed

+89
-6
lines changed

2 files changed

+89
-6
lines changed

src/FastPriorityQueue.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class FastPriorityQueue implements Iterator, Countable, Serializable
5757
/**
5858
* Max priority
5959
*
60-
* @var integer
60+
* @var integer|null
6161
*/
62-
protected $maxPriority = 0;
62+
protected $maxPriority = null;
6363

6464
/**
6565
* Total number of elements in the queue
@@ -86,7 +86,7 @@ class FastPriorityQueue implements Iterator, Countable, Serializable
8686
* Insert an element in the queue with a specified priority
8787
*
8888
* @param mixed $value
89-
* @param integer $priority a positive integer
89+
* @param integer $priority
9090
*/
9191
public function insert($value, $priority)
9292
{
@@ -96,7 +96,7 @@ public function insert($value, $priority)
9696
$this->values[$priority][] = $value;
9797
if (! isset($this->priorities[$priority])) {
9898
$this->priorities[$priority] = $priority;
99-
$this->maxPriority = max($priority, $this->maxPriority);
99+
$this->maxPriority = $this->maxPriority === null ? $priority : max($priority, $this->maxPriority);
100100
}
101101
++$this->count;
102102
}
@@ -132,11 +132,35 @@ public function extract()
132132
*/
133133
public function remove($datum)
134134
{
135+
$currentIndex = $this->index;
136+
$currentSubIndex = $this->subIndex;
137+
$currentPriority = $this->maxPriority;
138+
135139
$this->rewind();
136140
while ($this->valid()) {
137141
if (current($this->values[$this->maxPriority]) === $datum) {
138142
$index = key($this->values[$this->maxPriority]);
139143
unset($this->values[$this->maxPriority][$index]);
144+
145+
// The `next()` method advances the internal array pointer, so we need to use the `reset()` function,
146+
// otherwise we would lose all elements before the place the pointer points.
147+
reset($this->values[$this->maxPriority]);
148+
149+
$this->index = $currentIndex;
150+
$this->subIndex = $currentSubIndex;
151+
152+
// If the array is empty we need to destroy the unnecessary priority,
153+
// otherwise we would end up with an incorrect value of `$this->count`
154+
// {@see \Zend\Stdlib\FastPriorityQueue::nextAndRemove()}.
155+
if (empty($this->values[$this->maxPriority])) {
156+
unset($this->values[$this->maxPriority]);
157+
unset($this->priorities[$this->maxPriority]);
158+
if ($this->maxPriority === $currentPriority) {
159+
$this->subIndex = 0;
160+
}
161+
}
162+
163+
$this->maxPriority = empty($this->priorities) ? null : max($this->priorities);
140164
--$this->count;
141165
return true;
142166
}
@@ -191,11 +215,15 @@ public function key()
191215
*/
192216
protected function nextAndRemove()
193217
{
218+
$key = key($this->values[$this->maxPriority]);
219+
194220
if (false === next($this->values[$this->maxPriority])) {
195221
unset($this->priorities[$this->maxPriority]);
196222
unset($this->values[$this->maxPriority]);
197-
$this->maxPriority = empty($this->priorities) ? 0 : max($this->priorities);
223+
$this->maxPriority = empty($this->priorities) ? null : max($this->priorities);
198224
$this->subIndex = -1;
225+
} else {
226+
unset($this->values[$this->maxPriority][$key]);
199227
}
200228
++$this->index;
201229
++$this->subIndex;
@@ -211,7 +239,7 @@ public function next()
211239
if (false === next($this->values[$this->maxPriority])) {
212240
unset($this->subPriorities[$this->maxPriority]);
213241
reset($this->values[$this->maxPriority]);
214-
$this->maxPriority = empty($this->subPriorities) ? 0 : max($this->subPriorities);
242+
$this->maxPriority = empty($this->subPriorities) ? null : max($this->subPriorities);
215243
$this->subIndex = -1;
216244
}
217245
++$this->index;

test/FastPriorityQueueTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,59 @@ public function testIterativelyRemovingItemsShouldRemoveAllItems()
288288
$this->assertFalse($queue->contains($listener), sprintf('Listener %s remained in queue', $i));
289289
}
290290
}
291+
292+
public function testRemoveShouldNotAffectExtract()
293+
{
294+
// Removing an element with low priority
295+
$queue = new FastPriorityQueue();
296+
$queue->insert('a1', 1);
297+
$queue->insert('a2', 1);
298+
$queue->insert('b', 2);
299+
$queue->remove('a1');
300+
$expected = ['b', 'a2'];
301+
$test = [];
302+
while ($value = $queue->extract()) {
303+
$test[] = $value;
304+
}
305+
$this->assertEquals($expected, $test);
306+
$this->assertTrue($queue->isEmpty());
307+
308+
// Removing an element in the middle of a set of elements with the same priority
309+
$queue->insert('a1', 1);
310+
$queue->insert('a2', 1);
311+
$queue->insert('a3', 1);
312+
$queue->remove('a2');
313+
$expected = ['a1', 'a3'];
314+
$test = [];
315+
while ($value = $queue->extract()) {
316+
$test[] = $value;
317+
}
318+
$this->assertEquals($expected, $test);
319+
$this->assertTrue($queue->isEmpty());
320+
321+
// Removing an element with high priority
322+
$queue->insert('a', 1);
323+
$queue->insert('b', 2);
324+
$queue->remove('b');
325+
$expected = ['a'];
326+
$test = [];
327+
while ($value = $queue->extract()) {
328+
$test[] = $value;
329+
}
330+
$this->assertEquals($expected, $test);
331+
$this->assertTrue($queue->isEmpty());
332+
}
333+
334+
public function testZeroPriority()
335+
{
336+
$queue = new FastPriorityQueue();
337+
$queue->insert('a', 0);
338+
$queue->insert('b', 1);
339+
$expected = ['b', 'a'];
340+
$test = [];
341+
foreach ($queue as $value) {
342+
$test[] = $value;
343+
}
344+
$this->assertEquals($expected, $test);
345+
}
291346
}

0 commit comments

Comments
 (0)