2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
24 /** Build an iterator over an array.
25 * @param array The array.
26 * @param depth The depth of iteration.
27 * @return an iterator that return entries in the form
28 * array(key => array(0 => key_for_depth0 [, 1 => key_for_depths1, ...]),
29 * value => the value);
31 public static function fromArray(array $array, $depth = 1, $flat = false
)
33 return new PlArrayIterator($array, $depth, $flat);
37 /** Sort an iterator using the given sort callback.
38 * @param iterator The iterator to sort.
39 * @param callback The callback for comparison.
40 * @return a new iterator with the entries sorted.
42 public static function sort(PlIterator
$iterator, $callback)
44 $heap = new PlHeap($callback);
45 while ($item = $iterator->next()) {
48 return $heap->iterator();
52 /** Merge several iterator into a unique one.
53 * @param iterators Array of iterators.
54 * @param callback The callback for comparison.
55 * @param sorted Tell wether the iterators are already sorted using the given callback.
56 * @return an iterator.
58 public static function merge(array $iterators, $callback, $sorted = true
)
60 return new PlMergeIterator($iterators, $callback, $sorted);
64 /** Build an iterator that contains only the elements of the given iterator that
65 * match the given condition. The condition should be a callback that takes an element
66 * and returns a boolean.
67 * @param iterator The source iterator
68 * @param callback The condition
71 public static function filter(PlIterator
$iterator, $callback)
73 return new PlFilterIterator($iterator, $callback);
77 /** Build an iterator that transforms the element of another iterator. The callback
78 * takes an element and transform it into another one. Be careful: if the result
79 * of the callback is null, the iteration ends.
80 * @param iterator The source iterator
81 * @param callback The transformer.
83 public static function map(PlIterator
$iterator, $callback)
85 return new PlMapIterator($iterator, $callback);
88 /** Build an iterator whose values are iterators too; such a 'subIterator' will end
89 * when the value of $callback changes
90 * @param iterator The source iterator
91 * @param callback The callback for detecting changes.
94 public static function subIterator(PlIterator
$iterator, $callback)
96 return new SubIterator($iterator, $callback);
99 /** Returns the callback for '$x -> $x[$key]';
100 * @param $key the index to retrieve in arrays
103 public static function arrayValueCallback($key)
105 $callback = new _GetArrayValueCallback($key);
106 return array($callback, 'get');
110 /** Iterates over an array.
112 class PlArrayIterator
implements PlIterator
117 private $_its = array();
125 public function __construct(array &$array, $depth = 1, $flat = false
)
127 $this->array =& $array;
128 $this->depth
= $depth;
129 $this->_total
= $this->count($array, $depth - 1);
131 $this->_first
= false
;
132 $this->_last
= false
;
133 $this->_flat
= $flat;
135 for ($i = 0 ; $i < $depth ; ++
$i) {
137 $this->_its
[] = $array;
139 $this->_its
[] = current($this->_its
[$i - 1]);
141 reset($this->_its
[$i]);
145 private function count(array &$array, $depth)
148 return count($array);
151 foreach ($array as &$item) {
152 $sum +
= $this->count($item, $depth - 1);
158 private function nextArray($depth)
163 $this->_its
[$depth] = next($this->_its
[$depth - 1]);
164 if ($this->_its
[$depth] === false
) {
165 $this->nextArray($depth - 1);
166 if ($this->_its
[$depth - 1] === false
) {
169 $this->_its
[$depth] = current($this->_its
[$depth - 1]);
171 reset($this->_its
[$depth]);
174 public function next()
177 $this->_first
= ($this->_total
> 0 && $this->_pos
== 1);
178 $this->_last
= ($this->_pos
== $this->_total
);
179 if ($this->_pos
> $this->_total
) {
183 $val = current($this->_its
[$this->depth
- 1]);
184 if ($val === false
) {
185 $this->nextArray($this->depth
- 1);
186 $val = current($this->_its
[$this->depth
- 1]);
187 if ($val === false
) {
192 for ($i = 0 ; $i < $this->depth
; ++
$i) {
193 $keys[] = key($this->_its
[$i]);
195 next($this->_its
[$this->depth
- 1]);
199 return array('keys' => $keys,
204 public function total()
206 return $this->_total
;
209 public function first()
211 return $this->_first
;
214 public function last()
221 /** Iterator that return the result of a merge of several iterators.
223 class PlMergeIterator
implements PlIterator
225 /* The heap is field with entries with the form:
226 * array('it' => id of the iterator this entry come from,
227 * 'value' => value of the entry).
230 private $preComputed = false
;
236 public function __construct(array $iterators, $callback, $sorted = true
)
238 $this->heap
= new PlHeap(array($this, 'compare'));
240 $this->comparator
= $callback;
242 $this->iterators
= $iterators;
243 foreach ($this->iterators
as $key => &$it) {
244 $this->_total +
= $it->total();
246 if (!is_null($item)) {
247 $this->heap
->push(array('it' => $key, 'value' => $item));
251 $this->preComputed
= true
;
252 foreach ($iterators as $key => &$it) {
253 $this->_total +
= $it->total();
254 while (!is_null($item = $it->next())) {
255 $this->heap
->push(array('it' => $key, 'value' => $item));
262 /** Compare two entries of the heap using the comparator of the user.
264 public function compare($a, $b)
266 $cp = call_user_func($this->comparator
, $a['value'], $b['value']);
268 return $a['it'] - $b['it'];
273 public function total()
275 return $this->_total
;
278 public function next()
281 $entry = $this->heap
->pop();
282 if (is_null($entry)) {
285 if ($this->preComputed
) {
286 return $entry['value'];
289 $item = $this->iterators
[$it]->next();
290 if (!is_null($item)) {
291 $this->heap
->push(array('it' => $it, 'value' => $item));
293 return $entry['value'];
296 public function last()
298 return $this->heap
->count() == 0;
301 public function first()
303 return $this->pos
== 1;
308 class PlFilterIterator
implements PlIterator
{
314 public function __construct(PlIterator
$source, $callback)
316 $this->source
= $source;
317 $this->callback
= $callback;
319 $this->element
= null
;
322 private function fetchNext()
325 $current = $this->source
->next();
327 $this->element
= null
;
328 $this->start
= false
;
331 $res = call_user_func($this->callback
, $current);
333 $this->element
= $current;
339 public function next()
341 if ($this->element
&& $this->start
) {
342 $this->start
= false
;
344 $elt = $this->element
;
351 public function total()
353 /* This is an approximation since the correct total
354 * cannot be computed without fetching all the elements
356 return $this->source
->total();
359 public function first()
364 public function last()
366 return !$this->start
&& !$this->element
;
371 class PlMapIterator
implements PlIterator
376 public function __construct(PlIterator
$source, $callback)
378 $this->source
= $source;
379 $this->callback
= $callback;
382 public function next()
384 $elt = $this->source
->next();
386 return call_user_func($this->callback
, $elt);
392 public function total()
394 return $this->source
->total();
397 public function first()
399 return $this->source
->first();
402 public function last()
404 return $this->source
->last();
408 class PlSubIterator
implements PlIterator
412 private $next = null
; // The next item, if it has been fetched too early by a subiterator
414 public function __construct(PlIterator
$source, $callback)
416 $this->source
= $source;
417 $this->callback
= $callback;
420 public function next()
425 return new PlInnerSubIterator($this->source
, $this->callback
, $this, $this->next
);
429 /** This will always be a too big number, but the actual result can't be easily computed
431 public function total()
433 return $this->source
->total();
436 public function last()
438 return ($this->source
->last() && $this->next
== null
);
441 public function first()
443 return $this->source
->first();
446 // Called by a subiterator to "rewind" the core iterator
447 public function setNext($item)
453 class PlInnerSubIterator
implements PlIterator
459 private $next; // Not null if the source has to be "rewinded"
461 private $curval = null
;
462 private $curelt = null
;
463 private $stepped = false
;
464 private $over = false
;
466 public function __construct(PlIterator
$source, $callback, PlSubIterator
$parent, $next = null
)
468 $this->source
= $source;
469 $this->callback
= $callback;
470 $this->parent
= $parent;
474 public function value()
477 return $this->curval
;
480 // Move one step, if the current element has been used
481 private function _step()
483 if ($this->stepped
) {
487 if ($this->next
!= null
) {
488 $this->curelt
= $this->next
;
491 $elt = $this->source
->next();
493 $this->stepped
= true
;
496 public function next()
499 $this->stepped
= false
;
502 $val = call_user_func($this->callback
, $this->elt
);
503 if ($val == $this->curval
) {
504 $this->curval
= $val;
507 $this->parent
->setNext($this->elt
);
514 /** This will always be a too big number, but the actual result can't be easily computed
516 public function total()
518 return $this->source
->total();
521 public function last()
526 public function first()
533 // Wrapper class for 'arrayValueCallback' (get field $key of the given array)
534 class _GetArrayValueCallback
538 public function __construct($key)
543 public function get(array $arr)
545 if (array_key_exists($this->key
, $arr)) {
546 return $arr[$this->key
];
553 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: