2 /***************************************************************************
3 * Copyright (C) 2003-2009 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)
33 return new PlArrayIterator($array, $depth);
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);
65 /** Iterates over an array.
67 class PlArrayIterator
implements PlIterator
72 private $_its = array();
79 public function __construct(array &$array, $depth = 1)
81 $this->array =& $array;
82 $this->depth
= $depth;
83 $this->_total
= $this->count($array, $depth - 1);
85 $this->_first
= false
;
88 for ($i = 0 ; $i < $depth ; ++
$i) {
90 $this->_its
[] = $array;
92 $this->_its
[] = current($this->_its
[$i - 1]);
94 reset($this->_its
[$i]);
98 private function count(array &$array, $depth)
101 return count($array);
104 foreach ($array as &$item) {
105 $sum +
= $this->count($item, $depth - 1);
111 private function nextArray($depth)
116 $this->_its
[$depth] = next($this->_its
[$depth - 1]);
117 if ($this->_its
[$depth] === false
) {
118 $this->nextArray($depth - 1);
119 if ($this->_its
[$depth - 1] === false
) {
122 $this->_its
[$depth] = current($this->_its
[$depth - 1]);
124 reset($this->_its
[$depth]);
127 public function next()
130 $this->_first
= ($this->_total
> 0 && $this->_pos
== 1);
131 $this->_last
= ($this->_pos
== $this->_total
);
132 if ($this->_pos
> $this->_total
) {
136 $val = current($this->_its
[$this->depth
- 1]);
137 if ($val === false
) {
138 $this->nextArray($this->depth
- 1);
139 $val = current($this->_its
[$this->depth
- 1]);
140 if ($val === false
) {
145 for ($i = 0 ; $i < $this->depth
; ++
$i) {
146 $keys[] = key($this->_its
[$i]);
148 next($this->_its
[$this->depth
- 1]);
149 return array('keys' => $keys,
153 public function total()
155 return $this->_total
;
158 public function first()
160 return $this->_first
;
163 public function last()
170 /** Iterator that return the result of a merge of several iterators.
172 class PlMergeIterator
implements PlIterator
174 /* The heap is field with entries with the form:
175 * array('it' => id of the iterator this entry come from,
176 * 'value' => value of the entry).
179 private $preComputed = false
;
185 public function __construct(array $iterators, $callback, $sorted = true
)
187 $this->heap
= new PlHeap(array($this, 'compare'));
189 $this->comparator
= $callback;
191 $this->iterators
= $iterators;
192 foreach ($this->iterators
as $key => &$it) {
193 $this->_total +
= $it->total();
195 if (!is_null($item)) {
196 $this->heap
->push(array('it' => $key, 'value' => $item));
200 $this->preComputed
= true
;
201 foreach ($iterators as $key => &$it) {
202 $this->_total +
= $it->total();
203 while (!is_null($item = $it->next())) {
204 $this->heap
->push(array('it' => $key, 'value' => $item));
211 /** Compare two entries of the heap using the comparator of the user.
213 public function compare($a, $b)
215 $cp = call_user_func($this->comparator
, $a['value'], $b['value']);
217 return $a['it'] - $b['it'];
222 public function total()
224 return $this->_total
;
227 public function next()
230 $entry = $this->heap
->pop();
231 if (is_null($entry)) {
234 if ($this->preComputed
) {
235 return $entry['value'];
238 $item = $this->iterators
[$it]->next();
239 if (!is_null($item)) {
240 $this->heap
->push(array('it' => $it, 'value' => $item));
242 return $entry['value'];
245 public function last()
247 return $this->heap
->count() == 0;
250 public function first()
252 return $this->pos
== 1;
256 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: