LinksUpdate uses SELECT FOR UPDATE, thus starting a new transaction. So we have to...
[mediawiki.git] / PHPTAL-NP-0.7.0 / libs / Types / OArray.php
blobdf61ea3b81cf3395a3a8e8622266dee500a81478
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 //
4 // Copyright (c) 2003 Laurent Bedubourg
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library 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 GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
21 //
23 require_once PT_IP . "/Types.php";
24 require_once PT_IP . "/Types/Errors.php";
25 require_once PT_IP . "/Types/Ref.php";
26 require_once PT_IP . "/Types/Iterator.php";
28 /**
29 * Class wrapper for oriented object arrays.
31 * This class implements a simple interface quite like java to handle array in
32 * a good old oo way.
34 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
36 class OArray extends Iterable
38 var $_array;
39 var $_size = 0;
41 /**
42 * OArray constructor.
44 * @param array $array optional
45 * The php array this vector will manage.
47 function OArray($array=array())
49 $this->__construct($array);
52 /**
53 * OArray php4.4 compliant constructor.
55 * @param array $array optional
56 * The php array this vector will manage.
58 function __construct($array=array())
60 $this->_array = array();
61 for ($i = 0; $i < count($array); $i++) {
62 $this->pushRef($array[$i]);
66 /**
67 * Retrieve an iterator ready to used.
68 * @return ArrayIterator
70 function getNewIterator()
72 return new ArrayIterator($this);
75 /**
76 * Returns the vector number of elements.
77 * @return int
79 function size()
81 return count($this->_array);
84 /**
85 * Returns true if this vector is empty, false otherwise.
86 * @return boolean
88 function isEmpty()
90 return $this->size() == 0;
93 /**
94 * Retrieve element at specified index.
95 * @return mixed
96 * @throws OutOfBounds
98 function &get($i)
100 if ($i > $this->size() || $i < 0) {
101 $err = new OutOfBounds("OArray index out of bounds ($i)");
102 // return EX::raise($err);
103 return PEAR::raiseError($err);
105 return $this->_array[$i]->obj;
109 * Retrieve index of specified element.
111 * @return int The element index of false if element not in vector.
113 function indexOf($element)
115 for ($i=0; $i < count($this->_array); $i++) {
116 if ($this->_array[$i]->obj === $element) {
117 return $i;
120 return false;
124 * Set the item at specified index.
126 * @throws OutOfBounds if index is greated that vector limit
127 * @return Old element
129 function &set($i, $item)
131 return $this->setRef($i, $item);
135 * Set the item at specified index (by reference).
137 * @throws OutOfBounds if index is greated that vector limit
138 * @return Old element
140 function &setRef($i, &$item)
142 if ($i > $this->size() || $i < 0) {
143 $err = new OutOfBounds("OArray index out of bounds ($i)");
144 // return EX::raise($err);
145 return PEAR::raiseError($err);
147 $temp = $this->_array[$i];
148 $this->_array[$i] = Ref($item);
149 return $temp->obj;
153 * Test if the vector contains the specified element.
155 * @param mixed $item The item we look for.
157 function contains($o)
159 for ($i = 0; $i < $this->size(); $i++) {
160 if ($this->_array[$i]->obj === $o) return true;
162 return false;
166 * Add an element to the vector.
168 * @param mixed $o The item to add.
170 function add($o)
172 $this->push($o);
176 * Remove object from this vector.
178 * @param mixed $o Object to remove.
179 * @return boolean true if object removed false if not found
181 function remove($o)
183 $i = $this->indexOf($o);
184 if ($i !== false) {
185 $this->removeIndex($i);
186 return true;
188 return false;
192 * Remove specified index from vector.
194 * @param int $i Index
195 * @throws OutOfBounds
197 function removeIndex($i)
199 if ($i > $this->size() || $i < 0 ) {
200 $err = new OutOfBounds("OArray index out of bounds ($i)");
201 // return EX::raise($err);
202 return PEAR::raiseError($err);
205 // $this->_array = array_splice($this->_array, $i, 1);
206 array_splice($this->_array, $i, 1);
210 * Clear vector.
212 function clear()
214 $this->_array = array();
218 * Add an element at the end of the vector (same as add()).
220 * @param mixed $o Item to append to vector.
222 function push($o)
224 array_push($this->_array, Ref($o));
228 * Add an element at the end of the vector (same as add()).
230 * @param mixed $o Item to append to vector.
232 function pushRef(&$o)
234 $this->_array[] = Ref($o);
238 * Retrieve vector values.
240 * The returned array contains references to internal data.
242 * @return array
244 function &values()
246 $v = array();
247 for ($i = 0; $i < $this->size(); $i++) {
248 $v[] =& $this->_array[$i]->obj;
250 return $v;
254 * Remove the last element of the vector and returns it.
256 * @return mixed
258 function &pop()
260 $ref = array_pop($this->_array);
261 return $ref->obj;
265 * Extract and return first element of OArray.
266 * @return mixed
268 function &shift()
270 $ref = array_shift($this->_array);
271 return$ $ref->obj;
275 * Retrieve a php array for this vector.
277 * @return array
279 function &toArray()
281 return $this->values();
285 * Retrieve a string representation of the array.
287 function toString()
289 return Types::_arrayToString($this->values());
294 * OArray itertor class.
296 * This kind of iterators are used to walk throug a OArray object.
298 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
300 class ArrayIterator extends Iterator
302 var $_src;
303 var $_values;
304 var $_value;
305 var $_i;
308 * Iterator constructor.
310 * @param mixed $oarray OArray object or php array.
312 function ArrayIterator(&$oarray)
314 return $this->__construct($oarray);
318 * PHP 4.4 compliant constructor.
320 * @param mixed $oarray OArray object or php array.
322 function __construct(&$oarray)
324 if (is_array($oarray)) {
325 $this->_src = new OArray($oarray);
326 } else if (is_a($oarray, 'OArray')) {
327 $this->_src = &$oarray;
328 } else {
329 // ignore error, should throw a bad type error or something like
330 // that but pear does not handle errors in constructors yet
331 $err = new TypeError("ArrayIterator requires OArray object or php array.",
332 PEAR_ERROR_DIE);
333 // return EX::raise($err);
334 return PEAR::raiseError($err);
336 $this->reset();
340 * Reset iterator to first array position.
342 function reset()
344 $this->_i = 0;
345 $this->_end = false;
346 $this->_values = $this->_src->_array;
347 if (count($this->_values) == 0) {
348 $this->_end = true;
349 return;
352 $this->_value = $this->_values[0];
356 * Returns next vector item value.
358 * @return mixed
359 * @throws OutOfBounds if iterator overpass the vector size.
361 function &next()
363 if ($this->_end || ++$this->_i >= count($this->_values)) {
364 $this->_end = true;
365 return null;
368 $this->_value = $this->_values[$this->_i];
369 return $this->_value->obj;
373 * Test if the iteractor has not reached its end.
374 * @return boolean
376 function isValid()
378 return !$this->_end;
382 * Retrieve current iterator index.
383 * @return int
385 function index()
387 return $this->_i;
391 * Retrieve the current iterator value.
392 * @return mixed
394 function &value()
396 return $this->_value->obj;
400 * Delete current index.
402 function remove()
404 $this->_src->remove($this->_value->obj);