/** 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);
+ }
+
+ /** Build an iterator that will iterate over the given set of iterators, returning consistent
+ * sets of values (i.e only the values for which the result of the callback is the same as that
+ * for the master)
+ * @param iterators The set of iterators
+ * @param callbacks A list of callbacks (one for each iterator), or a single, common, callback
+ * @param master The id of the "master" iterator in the first list
+ */
+ public static function parallelIterator(array $iterators, $callbacks, $master)
+ {
+ return new PlParallelIterator($iterators, $callbacks, $master);
}
/** Returns the callback for '$x -> $x[$key]';
$callback = new _GetArrayValueCallback($key);
return array($callback, 'get');
}
+
+ /** Returns the callback for '$x -> $x->prop';
+ * @param $property The property to retrieve
+ * @return a callback
+ */
+ public static function objectPropertyCallback($property)
+ {
+ $callback = new _GetObjectPropertyCallback($property);
+ return array($callback, 'get');
+ }
}
/** Iterates over an array.
}
-class PlFilterIterator implements PlIterator {
+class PlFilterIterator implements PlIterator
+{
+ private $pos;
private $source;
private $callback;
private $element;
- private $start;
public function __construct(PlIterator $source, $callback)
{
- $this->source = $source;
+ $this->source = $source;
$this->callback = $callback;
- $this->start = true;
- $this->element = null;
+ $this->pos = 0;
+ $this->element = $this->fetchNext();
}
private function fetchNext()
{
do {
$current = $this->source->next();
- if (!$current) {
- $this->element = null;
- $this->start = false;
- return;
- }
- $res = call_user_func($this->callback, $current);
- if ($res) {
- $this->element = $current;
- return;
+ if (is_null($current) || call_user_func($this->callback, $current)) {
+ return $current;
}
} while (true);
}
public function next()
{
- if ($this->element && $this->start) {
- $this->start = false;
- }
+ ++$this->pos;
$elt = $this->element;
- if ($elt) {
- $this->fetchNext();
+ if (!is_null($this->element)) {
+ $this->element = $this->fetchNext();
}
return $elt;
}
public function first()
{
- return $this->start;
+ return $this->pos == 1;
}
public function last()
{
- return !$this->start && !$this->element;
+ return is_null($this->element);
}
}
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;
}
+/** Builds an iterator over a set of iterators, from which one is given as 'master';
+ * The arguments are :
+ * - An array of iterators, to iterate simultaneously
+ * - An array of callbacks (one attached to each iterator), or a single callback (to
+ * use for all iterators)
+ * - The id of the 'master' iterator
+ *
+ * This ParallelIterator will iterate over the iterators, and, at each
+ * step of the master iterator, it will apply the callbacks to the corresponding
+ * iterators and return the values of the "slaves" for which the callback returned the
+ * same value as the callback of the master.
+ *
+ * The callback should compute some kind of index, and never return the same value
+ * twice for a given iterator
+ *
+ * It is assumed that, if the callback for a slave doesn't have the same value
+ * as the value for the master, this means that there is a "hole" in the values
+ * of that slave.
+ *
+ * Example :
+ * - The callback is 'get the first cell'
+ * - The master is :
+ * [0, 1], [1, 13], [2, 42]
+ * - The first slave (slave1) is :
+ * [0, 'a'], [2, 'b']
+ * - The second slave (slave2) is :
+ * [1, 42], [2, 0]
+ * The resulting iterator would yield :
+ * - array(master => [0, 1], slave1 => [0, 'a'], slave2 => null)
+ * - array(master => [1, 13], slave1 => null, slave2 => [1, 42])
+ * - array(master => [2, 42], slave1 => [1, 'b'], slave2 => [2, 0])
+ */
+class PlParallelIterator implements PlIterator
+{
+ private $iterators;
+ private $ids;
+ private $callbacks;
+
+ private $master_id;
+ private $master;
+
+ private $stepped;
+ private $current_elts = array();
+ private $callback_res = array();
+
+ public function __construct(array $iterators, $callbacks, $master)
+ {
+ $this->iterators = $iterators;
+ $this->master_id = $master;
+ $this->master = $iterators[$master];
+
+ $this->ids = array_keys($iterators);
+
+ if (is_array($callbacks)) {
+ $this->callbacks = $callbacks;
+ } else {
+ $this->callbacks = array();
+ foreach ($this->ids as $id) {
+ $this->callbacks[$id] = $callbacks;
+ }
+ }
+
+ foreach ($this->ids as $id) {
+ $this->stepped[$id] = false;
+ $this->current_elts[$id] = null;
+ $this->callback_res[$id] = null;
+ }
+ }
+
+ private function step($id)
+ {
+ if ($this->stepped[$id]) {
+ return;
+ }
+
+ $it = $this->iterators[$id];
+ $nxt = $it->next();
+ $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()
+ {
+ foreach ($this->ids as $id) {
+ $this->step($id);
+ }
+ }
+
+ public function next()
+ {
+ $this->stepAll();
+ if ($this->current_elts[$this->master_id] == null) {
+ return null;
+ }
+
+ $res = array();
+ $master = $this->callback_res[$this->master_id];
+ foreach ($this->ids as $id) {
+ if ($this->callback_res[$id] == $master) {
+ $res[$id] = $this->current_elts[$id];
+ $this->stepped[$id] = false;
+ } else {
+ $res[$id] = null;
+ }
+ }
+
+ return $res;
+ }
+
+ public function first()
+ {
+ return $this->master->first();
+ }
+
+ public function total()
+ {
+ return $this->master->total();
+ }
+
+ public function last()
+ {
+ return $this->master->last();
+ }
+}
+
// Wrapper class for 'arrayValueCallback' (get field $key of the given array)
class _GetArrayValueCallback
{
public function get(array $arr)
{
- if (array_key_exists($key, $arr)) {
- return $arr[$key];
+ if (array_key_exists($this->key, $arr)) {
+ return $arr[$this->key];
} else {
return null;
}
}
}
+// Wrapper class for 'objectPropertyCallback' (get property ->$blah of the given object)
+class _GetObjectPropertyCallback
+{
+ private $property;
+
+ public function __construct($property)
+ {
+ $this->property = $property;
+ }
+
+ public function get($obj)
+ {
+ $p = $this->property;
+ return @$obj->$p;
+ }
+}
+
// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
?>