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);
90 /** Iterates over an array.
92 class PlArrayIterator
implements PlIterator
97 private $_its = array();
105 public function __construct(array &$array, $depth = 1, $flat = false
)
107 $this->array =& $array;
108 $this->depth
= $depth;
109 $this->_total
= $this->count($array, $depth - 1);
111 $this->_first
= false
;
112 $this->_last
= false
;
113 $this->_flat
= $flat;
115 for ($i = 0 ; $i < $depth ; ++
$i) {
117 $this->_its
[] = $array;
119 $this->_its
[] = current($this->_its
[$i - 1]);
121 reset($this->_its
[$i]);
125 private function count(array &$array, $depth)
128 return count($array);
131 foreach ($array as &$item) {
132 $sum +
= $this->count($item, $depth - 1);
138 private function nextArray($depth)
143 $this->_its
[$depth] = next($this->_its
[$depth - 1]);
144 if ($this->_its
[$depth] === false
) {
145 $this->nextArray($depth - 1);
146 if ($this->_its
[$depth - 1] === false
) {
149 $this->_its
[$depth] = current($this->_its
[$depth - 1]);
151 reset($this->_its
[$depth]);
154 public function next()
157 $this->_first
= ($this->_total
> 0 && $this->_pos
== 1);
158 $this->_last
= ($this->_pos
== $this->_total
);
159 if ($this->_pos
> $this->_total
) {
163 $val = current($this->_its
[$this->depth
- 1]);
164 if ($val === false
) {
165 $this->nextArray($this->depth
- 1);
166 $val = current($this->_its
[$this->depth
- 1]);
167 if ($val === false
) {
172 for ($i = 0 ; $i < $this->depth
; ++
$i) {
173 $keys[] = key($this->_its
[$i]);
175 next($this->_its
[$this->depth
- 1]);
179 return array('keys' => $keys,
184 public function total()
186 return $this->_total
;
189 public function first()
191 return $this->_first
;
194 public function last()
201 /** Iterator that return the result of a merge of several iterators.
203 class PlMergeIterator
implements PlIterator
205 /* The heap is field with entries with the form:
206 * array('it' => id of the iterator this entry come from,
207 * 'value' => value of the entry).
210 private $preComputed = false
;
216 public function __construct(array $iterators, $callback, $sorted = true
)
218 $this->heap
= new PlHeap(array($this, 'compare'));
220 $this->comparator
= $callback;
222 $this->iterators
= $iterators;
223 foreach ($this->iterators
as $key => &$it) {
224 $this->_total +
= $it->total();
226 if (!is_null($item)) {
227 $this->heap
->push(array('it' => $key, 'value' => $item));
231 $this->preComputed
= true
;
232 foreach ($iterators as $key => &$it) {
233 $this->_total +
= $it->total();
234 while (!is_null($item = $it->next())) {
235 $this->heap
->push(array('it' => $key, 'value' => $item));
242 /** Compare two entries of the heap using the comparator of the user.
244 public function compare($a, $b)
246 $cp = call_user_func($this->comparator
, $a['value'], $b['value']);
248 return $a['it'] - $b['it'];
253 public function total()
255 return $this->_total
;
258 public function next()
261 $entry = $this->heap
->pop();
262 if (is_null($entry)) {
265 if ($this->preComputed
) {
266 return $entry['value'];
269 $item = $this->iterators
[$it]->next();
270 if (!is_null($item)) {
271 $this->heap
->push(array('it' => $it, 'value' => $item));
273 return $entry['value'];
276 public function last()
278 return $this->heap
->count() == 0;
281 public function first()
283 return $this->pos
== 1;
288 class PlFilterIterator
implements PlIterator
{
294 public function __construct(PlIterator
$source, $callback)
296 $this->source
= $source;
297 $this->callback
= $callback;
299 $this->element
= null
;
302 private function fetchNext()
305 $current = $this->source
->next();
307 $this->element
= null
;
308 $this->start
= false
;
311 $res = call_user_func($this->callback
, $current);
313 $this->element
= $current;
319 public function next()
321 if ($this->element
&& $this->start
) {
322 $this->start
= false
;
324 $elt = $this->element
;
331 public function total()
333 /* This is an approximation since the correct total
334 * cannot be computed without fetching all the elements
336 return $this->source
->total();
339 public function first()
344 public function last()
346 return !$this->start
&& !$this->element
;
351 class PlMapIterator
implements PlIterator
356 public function __construct(PlIterator
$source, $callback)
358 $this->source
= $source;
359 $this->callback
= $callback;
362 public function next()
364 $elt = $this->source
->next();
366 return call_user_func($this->callback
, $elt);
372 public function total()
374 return $this->source
->total();
377 public function first()
379 return $this->source
->first();
382 public function last()
384 return $this->source
->last();
389 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: