Happy New Year
[platal.git] / classes / pliteratorutils.php
CommitLineData
63f00a3f
FB
1<?php
2/***************************************************************************
a7f778a5 3 * Copyright (C) 2003-2009 Polytechnique.org *
63f00a3f
FB
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
5177a1b5
FB
22class PlIteratorUtils
23{
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);
30 */
31 public static function fromArray(array $array, $depth = 1)
32 {
33 return new PlArrayIterator($array, $depth);
34 }
35
36
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.
41 */
42 public static function sort(PlIterator $iterator, $callback)
43 {
44 $heap = new PlHeap($callback);
45 while ($item = $iterator->next()) {
46 $heap->push($item);
47 }
48 return $heap->iterator();
49 }
50
51
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.
57 */
58 public static function merge(array $iterators, $callback, $sorted = true)
59 {
60 return new PlMergeIterator($iterators, $callback, $sorted);
61 }
62}
63
64
65/** Iterates over an array.
66 */
63f00a3f
FB
67class PlArrayIterator implements PlIterator
68{
69 private $array;
70 private $depth;
71
72 private $_its = array();
73
74 private $_total;
75 private $_first;
76 private $_last;
77 private $_pos;
78
79 public function __construct(array &$array, $depth = 1)
80 {
81 $this->array =& $array;
82 $this->depth = $depth;
83 $this->_total = $this->count($array, $depth - 1);
84 $this->_pos = 0;
85 $this->_first = false;
86 $this->_last = false;
87
88 for ($i = 0 ; $i < $depth ; ++$i) {
89 if ($i == 0) {
90 $this->_its[] = $array;
91 } else {
92 $this->_its[] = current($this->_its[$i - 1]);
93 }
94 reset($this->_its[$i]);
95 }
96 }
97
98 private function count(array &$array, $depth)
99 {
100 if ($depth == 0) {
101 return count($array);
102 } else {
103 $sum = 0;
104 foreach ($array as &$item) {
105 $sum += $this->count($item, $depth - 1);
106 }
107 return $sum;
108 }
109 }
110
111 private function nextArray($depth)
112 {
113 if ($depth == 0) {
114 return;
115 }
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) {
120 return;
121 }
122 $this->_its[$depth] = current($this->_its[$depth - 1]);
123 }
124 reset($this->_its[$depth]);
125 }
126
127 public function next()
128 {
129 ++$this->_pos;
130 $this->_first = ($this->_total > 0 && $this->_pos == 1);
131 $this->_last = ($this->_pos == $this->_total);
132 if ($this->_pos > $this->_total) {
133 return null;
134 }
135
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) {
141 return null;
142 }
143 }
144 $keys = array();
145 for ($i = 0 ; $i < $this->depth ; ++$i) {
146 $keys[] = key($this->_its[$i]);
147 }
148 next($this->_its[$this->depth - 1]);
149 return array('keys' => $keys,
150 'value' => $val);
151 }
152
153 public function total()
154 {
155 return $this->_total;
156 }
157
158 public function first()
159 {
160 return $this->_first;
161 }
162
163 public function last()
164 {
165 return $this->_last;
166 }
167}
168
5177a1b5
FB
169
170/** Iterator that return the result of a merge of several iterators.
171 */
172class PlMergeIterator implements PlIterator
173{
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).
177 */
178 private $heap;
179 private $preComputed = false;
180 private $comparator;
181 private $iterators;
182 private $_total;
183 private $pos;
184
185 public function __construct(array $iterators, $callback, $sorted = true)
186 {
187 $this->heap = new PlHeap(array($this, 'compare'));
188 $this->_total = 0;
189 $this->comparator = $callback;
190 if ($sorted) {
191 $this->iterators = $iterators;
192 foreach ($this->iterators as $key => &$it) {
193 $this->_total += $it->total();
194 $item = $it->next();
195 if (!is_null($item)) {
196 $this->heap->push(array('it' => $key, 'value' => $item));
197 }
198 }
199 } else {
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));
205 }
206 }
207 }
208 $this->pos = 0;
209 }
210
211 /** Compare two entries of the heap using the comparator of the user.
212 */
213 public function compare($a, $b)
214 {
215 $cp = call_user_func($this->comparator, $a['value'], $b['value']);
216 if ($cp == 0) {
217 return $a['it'] - $b['it'];
218 }
219 return $cp;
220 }
221
222 public function total()
223 {
224 return $this->_total;
225 }
226
227 public function next()
228 {
229 ++$this->pos;
230 $entry = $this->heap->pop();
231 if (is_null($entry)) {
232 return null;
233 }
234 if ($this->preComputed) {
235 return $entry['value'];
236 }
237 $it = $entry['it'];
238 $item = $this->iterators[$it]->next();
239 if (!is_null($item)) {
240 $this->heap->push(array('it' => $it, 'value' => $item));
241 }
242 return $entry['value'];
243 }
244
245 public function last()
246 {
247 return $this->heap->count() == 0;
248 }
249
250 public function first()
251 {
252 return $this->pos == 1;
253 }
254}
255
63f00a3f
FB
256// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
257?>