/** Build an iterator whose values are iterators too; such a 'subIterator' will end
* when the value of $callback changes
* @param iterator The source iterator
- * @param callback The callback for detecting changes.
+ * @param callback The callback for detecting changes. XXX: Might be called twice on a given object
* @return an iterator
*/
public static function subIterator(PlIterator $iterator, $callback)
{
- return new SubIterator($iterator, $callback);
+ return new PlSubIterator($iterator, $callback);
}
/** Returns the callback for '$x -> $x[$key]';
private $curval = null;
private $curelt = null;
+ private $begin = true;
private $stepped = false;
private $over = false;
$this->curelt = $this->next;
$this->next = null;
} else {
- $elt = $this->source->next();
+ $this->curelt = $this->source->next();
}
+
+ if ($this->begin) {
+ $this->curval = call_user_func($this->callback, $this->curelt);
+ $this->begin = false;
+ }
+
$this->stepped = true;
}
$this->_step();
$this->stepped = false;
- if ($this->elt) {
- $val = call_user_func($this->callback, $this->elt);
+ if ($this->curelt) {
+ $val = call_user_func($this->callback, $this->curelt);
if ($val == $this->curval) {
$this->curval = $val;
- return $this->elt;
+ return $this->curelt;
} else {
- $this->parent->setNext($this->elt);
+ $this->parent->setNext($this->curelt);
}
}
$this->over = true;