X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fpliteratorutils.php;h=33dbfab19300abf7469dfba5b192417146b1cfa8;hb=f3f26e2b84a4ddd6c1daf3013bfa8adc252b727a;hp=230dc5ae14e2ffc810bb3df4a0d835dee974992f;hpb=a52d0af8a572ecffdcb0d2c160358173bb9be28f;p=platal.git diff --git a/classes/pliteratorutils.php b/classes/pliteratorutils.php index 230dc5a..33dbfab 100644 --- a/classes/pliteratorutils.php +++ b/classes/pliteratorutils.php @@ -1,6 +1,6 @@ ids = array_keys($iterators); - if (is_array($callbacks)) { + $v = array_values($callbacks); + if (is_array($v[0])) { $this->callbacks = $callbacks; } else { $this->callbacks = array(); @@ -656,6 +697,7 @@ class PlParallelIterator implements PlIterator foreach ($this->ids as $id) { $this->stepped[$id] = false; + $this->over[$id] = false; $this->current_elts[$id] = null; $this->callback_res[$id] = null; } @@ -667,12 +709,24 @@ class PlParallelIterator implements PlIterator return; } + // Don't do anything if the iterator is at its end + if ($this->over[$id]) { + $this->stepped[$id] = true; + return; + } + $it = $this->iterators[$id]; $nxt = $it->next(); + $this->stepped[$id] = true; + if ($nxt === null) { + $this->over[$id] = true; + $this->current_elts[$id] = null; + $this->callback_res[$id] = null; + return; + } $res = call_user_func($this->callbacks[$id], $nxt); $this->current_elts[$id] = $nxt; $this->callback_res[$id] = $res; - $this->stepped[$id] = true; } private function stepAll() @@ -685,7 +739,7 @@ class PlParallelIterator implements PlIterator public function next() { $this->stepAll(); - if ($this->current_elts[$this->master_id] == null) { + if ($this->current_elts[$this->master_id] === null) { return null; } @@ -756,5 +810,48 @@ class _GetObjectPropertyCallback } } +// Wrapper class to build a SPL iterator from a PlIterator +class SPLIterator implements Iterator +{ + private $it; + private $pos; + private $value; + + public function __construct(PlIterator $it) + { + $this->it = $it; + $this->pos = 0; + $this->value = $this->it->next(); + } + + public function rewind() + { + if ($this->pos != 0) { + throw new Exception("Rewind not supported on this iterator"); + } + } + + public function current() + { + return $this->value; + } + + public function key() + { + return $this->pos; + } + + public function next() + { + ++$this->pos; + $this->value = $this->it->next(); + } + + public function valid() + { + return !!$this->value; + } +} + // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: ?>