+
+ // The `next()` method advances the internal array pointer, so we need to use the `reset()` function,
+ // otherwise we would lose all elements before the place the pointer points.
+ reset($this->values[$this->maxPriority]);
+
+ $this->index = $currentIndex;
+ $this->subIndex = $currentSubIndex;
+
+ // If the array is empty we need to destroy the unnecessary priority,
+ // otherwise we would end up with an incorrect value of `$this->count`
+ // {@see \Zend\Stdlib\FastPriorityQueue::nextAndRemove()}.
+ if (empty($this->values[$this->maxPriority])) {
+ unset($this->values[$this->maxPriority]);
+ unset($this->priorities[$this->maxPriority]);
+ if ($this->maxPriority === $currentPriority) {
+ $this->subIndex = 0;
+ }
+ }
+
+ $this->maxPriority = empty($this->priorities) ? null : max($this->priorities);