rm trailing white space
[mediawiki.git] / PHPTAL-NP-0.7.0 / libs / Types / OHash.php
blob2f1293a2a87d947267b9838a50970ec480725c11
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";
27 /**
28 * Wrapper for oriented object associative arrays.
30 * This class implements a simple interface quite like java to handle
31 * associatives arrays (Hashtables).
33 * Note:
35 * Some problems may occurs with object references until php4.4 to avoid
36 * unwanted object copy, use setRef() method with objects or pass Ref objects
37 * to set().
39 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
41 class OHash
43 var $_hash;
45 /**
46 * OHash constructor.
48 * php4.4 compliant constructor
50 * @param array $array optional -- An associative php array
52 function __construct($array=array())
54 $this->_hash = array();
55 $keys = array_keys($array);
56 foreach ($keys as $key) {
57 $this->_hash[$key] = ref($array[$key]);
61 /**
62 * OHash constructor.
64 * php4.4 compliant constructor
66 * @param array $array optional -- An associative php array
68 function OHash($array=array())
70 $this->__construct($array);
73 /**
74 * Set a value in this hash.
76 * @param string $key
77 * @param string $value
79 function set($key, $value)
81 $this->_hash[$key] = ref($value);
84 /**
85 * Reference set.
87 * Until php4.4, it's the only way to avoid object/variable copy.
89 * @param string $key
90 * @param reference $value
92 function setRef($key, &$value)
94 $this->_hash[$key] = ref($value);
97 /**
98 * Set a map of values.
100 * @param mixed $hash An Hash object or an associative php array.
102 function setAll($hash)
104 if (!is_array($hash) && is_a($hash, 'OHash')) {
105 $hash = $hash->toHash();
107 $keys = array_keys($hash);
108 foreach ($keys as $key) {
109 $this->_hash[$key] = ref($hash[$key]);
114 * Retrieve value associated to specified key.
116 * @param string $key
117 * @return reference
119 function &get($key)
121 if ($this->containsKey($key)) {
122 return $this->_hash[$key]->obj;
124 return null;
128 * Remove element associated to specified key from hash.
130 * @param string $key
132 function remove($key)
134 $keys = $this->keys();
135 $i = array_search($key, $keys);
136 if ($i !== false) {
137 // unset hash element to fix many bugs that should appear while
138 // iterating and using references to this element.
139 unset($this->_hash[$key]);
140 // return array_splice($this->_hash, $i, 1);
145 * Remove an element from the Hash.
147 * @param mixed $o -- Element to remove from Hash
149 function removeElement(&$o)
151 $i = 0;
152 $found = false;
153 foreach ($this->_hash as $key => $value) {
154 if ($value->obj === $o || $value == $o) {
155 $found = $i;
156 break;
158 $i++;
160 if ($found !== false) {
161 return array_splice($this->_hash, $found, 1);
166 * Returns true is hashtable empty.
168 * @return boolean
170 function isEmpty()
172 return $this->size() == 0;
176 * Retrieve hash size (number of elements).
178 * @return int
180 function size()
182 return count($this->_hash);
186 * Retrieve hash values array.
188 * @return hashtable
190 function &values()
192 $v = array();
193 foreach ($this->_hash as $key => $ref) {
194 $v[] =& $ref->obj;
196 return $v;
200 * Retrieve hash keys array.
202 * @return array
204 function keys()
206 return array_keys($this->_hash);
210 * Retrieve an hash iterator ready to use.
212 * @return HashIterator
214 function getNewIterator()
216 return new HashIterator($this);
220 * Test if this hash contains specified key.
222 * @param string $key
223 * @return boolean
225 function containsKey($key)
227 return array_key_exists($key, $this->_hash);
231 * Test if this hash contains specified value.
233 * @param mixed $value -- The value to search
234 * @return boolean
236 function containsValue($value)
238 foreach ($this->_hash as $k => $v) {
239 if ($v->obj === $value || $v == $value) {
240 return true;
243 return false;
247 * Returns the php array (hashtable) handled by this object.
249 * @return hashtable
251 function &toHash()
253 $result = array();
254 foreach ($this->_hash as $key => $value) {
255 $result[$key] =& $value->obj;
257 return $result;
261 * Create an Hash object from a simple php array variable.
263 * This method assumes that the array is composed of serial key, value
264 * elements. For each pair of array element, the former will be used as key
265 * and the latter as value.
267 * @param array $array -- php array.
269 * @return Hash
270 * @static 1
272 function ArrayToHash($array)
274 $h = new OHash();
275 while (count($array) > 1) {
276 $key = array_shift($array);
277 $h->set($key, array_shift($array));
279 return $h;
283 * Generate and return a string representation of this hashtable.
285 * @return string
287 function toString()
289 return Types::_hashToString($this->toHash());
293 * Sort hashtable on its keys.
295 function sort()
297 ksort($this->_hash);
303 * Hash iterator.
305 * This kind of iterators are used to walk throug Hash objects.
307 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
309 class HashIterator
311 var $_src;
312 var $_values;
313 var $_keys;
314 var $_key;
315 var $_value;
316 var $_i = -1;
317 var $_end = false;
320 * Iterator constructor.
322 * @param mixed $hash -- OHash object or associative php array.
324 function HashIterator(&$hash)
326 return $this->__construct($hash);
330 * Iterator php4.4 compliant constructor.
332 * @param mixed $hash -- OHash object or associative php array.
334 * @throws TypeError
335 * When $hash is not an array or an Hash object.
337 function __construct(&$hash)
339 if (is_array($hash)) {
340 $this->_src = new OHash($hash);
341 } else if (is_a($hash, 'ohash')) {
342 $this->_src = &$hash;
343 } else {
344 $err = new TypeError('HashIterator requires associative array or OHash');
345 // return EX::raise($err);
346 die($err->toString());
347 return PEAR::raiseError($err);
349 $this->reset();
353 // iterator logics
357 * Reset iterator to first element.
359 function reset()
361 // store a copy of hash references so a modification of source data
362 // won't affect iterator.
363 $this->_values = $this->_src->_hash;
364 $this->_keys = $this->_src->keys();
366 if (count($this->_keys) == 0) {
367 $this->_end = true;
368 return;
371 $this->_i = 0;
372 $this->_end = false;
373 $this->_key = $this->_keys[0];
374 $this->_value = $this->_values[$this->_key];
378 * Test is end of iterator is not reached.
380 function isValid()
382 return !$this->_end;
386 * Return next Hash item.
388 * This method also set _key and _value.
390 * @return mixed (by reference) or false if end reached
392 function &next()
394 if ($this->_end || ++$this->_i >= count($this->_keys)){
395 $this->_end = true;
396 return null;
399 $this->_key = $this->_keys[$this->_i];
400 $this->_value = $this->_values[$this->_key];
401 return $this->_value->obj;
405 // getters
409 * Return current iterator key.
411 * @return string
413 function key()
415 return $this->_key;
419 * Return current index (position in iteration).
421 * @return int
423 function index()
425 return $this->_i;
429 * Return current iterator value.
431 * @return mixed
433 function &value()
435 return $this->_value->obj;
439 // optional implementations
443 * Remove current iterated item from source.
445 function remove()
447 $this->_src->remove($this->_key);