00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 require_once
PT_IP .
"/Types.php";
00024 require_once
PT_IP .
"/Types/Errors.php";
00025 require_once
PT_IP .
"/Types/Ref.php";
00026 require_once
PT_IP .
"/Types/Iterator.php";
00027
00036 class OArray extends Iterable
00037 {
00038 var
$_array;
00039 var
$_size = 0;
00040
00047 function
OArray($array=array())
00048 {
00049 $this->
__construct($array);
00050 }
00051
00058 function
__construct($array=array())
00059 {
00060 $this->_array = array();
00061
for (
$i = 0;
$i < count($array);
$i++) {
00062 $this->
pushRef($array[$i]);
00063 }
00064 }
00065
00070 function
getNewIterator()
00071 {
00072
return new ArrayIterator($
this);
00073 }
00074
00079 function
size()
00080 {
00081
return count($this->_array);
00082 }
00083
00088 function
isEmpty()
00089 {
00090
return $this->
size() == 0;
00091 }
00092
00098 function &
get($i)
00099 {
00100
if (
$i > $this->
size() ||
$i < 0) {
00101 $err =
new OutOfBounds(
"OArray index out of bounds ($i)");
00102
00103
return PEAR::raiseError($err);
00104 }
00105
return $this->_array[
$i]->obj;
00106 }
00107
00113 function
indexOf($element)
00114 {
00115
for (
$i=0;
$i < count($this->_array);
$i++) {
00116
if ($this->_array[
$i]->obj === $element) {
00117
return $i;
00118 }
00119 }
00120
return false;
00121 }
00122
00129 function &
set($i, $item)
00130 {
00131
return $this->
setRef($i, $item);
00132 }
00133
00140 function &
setRef($i, &$item)
00141 {
00142
if (
$i > $this->
size() ||
$i < 0) {
00143 $err =
new OutOfBounds(
"OArray index out of bounds ($i)");
00144
00145
return PEAR::raiseError($err);
00146 }
00147 $temp = $this->_array[
$i];
00148 $this->_array[
$i] =
Ref($item);
00149
return $temp->obj;
00150 }
00151
00157 function
contains($o)
00158 {
00159
for (
$i = 0;
$i < $this->
size();
$i++) {
00160
if ($this->_array[
$i]->obj === $o)
return true;
00161 }
00162
return false;
00163 }
00164
00170 function
add($o)
00171 {
00172 $this->
push($o);
00173 }
00174
00181 function
remove($o)
00182 {
00183
$i = $this->indexOf($o);
00184
if (
$i !==
false) {
00185 $this->
removeIndex($i);
00186
return true;
00187 }
00188
return false;
00189 }
00190
00197 function
removeIndex($i)
00198 {
00199
if (
$i > $this->
size() ||
$i < 0 ) {
00200 $err =
new OutOfBounds(
"OArray index out of bounds ($i)");
00201
00202
return PEAR::raiseError($err);
00203 }
00204
00205
00206 array_splice($this->_array, $i, 1);
00207 }
00208
00212 function
clear()
00213 {
00214 $this->_array = array();
00215 }
00216
00222 function
push($o)
00223 {
00224 array_push($this->_array,
Ref($o));
00225 }
00226
00232 function
pushRef(&$o)
00233 {
00234 $this->_array[] =
Ref($o);
00235 }
00236
00244 function &
values()
00245 {
00246 $v = array();
00247
for (
$i = 0;
$i < $this->
size();
$i++) {
00248 $v[] =& $this->_array[
$i]->obj;
00249 }
00250
return $v;
00251 }
00252
00258 function &
pop()
00259 {
00260 $ref = array_pop($this->_array);
00261
return $ref->obj;
00262 }
00263
00268 function &
shift()
00269 {
00270 $ref = array_shift($this->_array);
00271
return$ $ref->obj;
00272 }
00273
00279 function &
toArray()
00280 {
00281
return $this->
values();
00282 }
00283
00287 function
toString()
00288 {
00289
return Types::_arrayToString($this->values());
00290 }
00291 };
00292
00300 class ArrayIterator extends Iterator
00301 {
00302 var
$_src;
00303 var
$_values;
00304 var
$_value;
00305 var
$_i;
00306
00312 function
ArrayIterator(&$oarray)
00313 {
00314
return $this->__construct($oarray);
00315 }
00316
00322 function __construct(&$oarray)
00323 {
00324
if (is_array($oarray)) {
00325 $this->_src =
new OArray($oarray);
00326 }
else if (is_a($oarray, 'OArray')) {
00327 $this->_src = &$oarray;
00328 }
else {
00329
00330
00331 $err =
new TypeError(
"ArrayIterator requires OArray object or php array.",
00332
PEAR_ERROR_DIE);
00333
00334
return PEAR::raiseError($err);
00335 }
00336 $this->
reset();
00337 }
00338
00342 function
reset()
00343 {
00344 $this->_i = 0;
00345 $this->_end =
false;
00346 $this->_values = $this->_src->_array;
00347
if (count($this->_values) == 0) {
00348 $this->_end =
true;
00349
return;
00350 }
00351
00352 $this->_value = $this->_values[0];
00353 }
00354
00361 function &
next()
00362 {
00363
if ($this->_end || ++$this->_i >= count($this->_values)) {
00364 $this->_end =
true;
00365
return null;
00366 }
00367
00368 $this->_value = $this->_values[$this->_i];
00369
return $this->_value->obj;
00370 }
00371
00376 function
isValid()
00377 {
00378
return !$this->_end;
00379 }
00380
00385 function
index()
00386 {
00387
return $this->_i;
00388 }
00389
00394 function &
value()
00395 {
00396
return $this->_value->obj;
00397 }
00398
00402 function
remove()
00403 {
00404 $this->_src->remove($this->_value->obj);
00405 }
00406 }
00407
00408 ?>