Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- ABSTRACT CLASS ProtoArraySegment EXTENDS Proto
- {
- protected $__reference = NULL;
- protected $__length = NULL;
- protected $__start = NULL;
- protected $__first = NULL;
- protected $__last = NULL;
- public function __construct(ProtoArray &$array, $start = NULL, $length = NULL)
- {
- $this->__reference =& $array;
- $this->__start = $start;
- $this->__length = $length;
- $this->__first = ($start === NULL)
- ? $array->bottom()
- : $start;
- $this->__last = ($length === NULL)
- ? $array->peek()
- : $array->peek($start + $length - 1);
- }
- }
- CLASS TArraySegment EXTENDS System\ProtoArraySegment IMPLEMENTS System\iIterator
- {
- const ITERATE_MODE_FIFO = 0x00000000;
- const ITERATE_MODE_LIFO = 0x00000001;
- private $__itMode = 0;
- private $__index = 0;
- public function __construct(TArray &$array, $start = NULL, $length = NULL)
- {
- if(NULL !== $start)
- {
- if(!is_integer($start))
- throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER);
- if($start < 0)
- throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER_NOT_POSITIVE_OR_ZERO,
- ['value' => $start]);
- }
- if(NULL !== $length)
- {
- if(!is_integer($length))
- throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER);
- if($length <= 0)
- throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER_NOT_POSITIVE,
- ['value' => $length]);
- }
- $this->__index = $start;
- parent::__construct($array, $start, $length);
- }
- final public function getIteratorMode()
- {
- return $this->__itMode;
- }
- final public function rewind()
- {
- return $this->__index = $this->__first;
- }
- final public function current()
- {
- return $this->__reference->offsetGet($this->__index);
- }
- final public function key()
- {
- return $this->__index;
- }
- final public function next()
- {
- return ++$this->__index;
- }
- final public function prev()
- {
- }
- final public function valid()
- {
- return $this->__reference->offsetExists($this->__index) && $this->__index >= $this->__first && $this->__index <= $this->__last;
- }
- }
- $a = new tArray(["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]);
- $b = new tArraySegment($a, 2, 5); //"brown", "fox", "jumps", "over", "the"
- $a[3] = 'LION';
- var_dump($a);
- var_dump($b); //"brown", "LION", "jumps", "over", "the"
- foreach($b as $k => $v)
- var_dump($k.': '.$v);
- ?>
- object(ILLI\System\Collection\TArray)#2 (4) {
- ["__itMode":"ILLI\System\Collection\TArray":private]=>
- int(0)
- ["__index":"ILLI\System\Collection\TArray":private]=>
- int(0)
- ["__array":"ILLI\System\ProtoArray":private]=>
- array(9) {
- [0]=>
- string(3) "The"
- [1]=>
- string(5) "quick"
- [2]=>
- string(5) "brown"
- [3]=>
- string(4) "LION"
- [4]=>
- string(5) "jumps"
- [5]=>
- string(4) "over"
- [6]=>
- string(3) "the"
- [7]=>
- string(4) "lazy"
- [8]=>
- string(3) "dog"
- }
- ["__protMode":"ILLI\System\ProtoArray":private]=>
- int(0)
- }
- object(ILLI\System\Collection\TArraySegment)#3 (7) {
- ["__itMode":"ILLI\System\Collection\TArraySegment":private]=>
- int(0)
- ["__index":"ILLI\System\Collection\TArraySegment":private]=>
- int(2)
- ["__reference":protected]=>
- &object(ILLI\System\Collection\TArray)#2 (4) {
- ["__itMode":"ILLI\System\Collection\TArray":private]=>
- int(0)
- ["__index":"ILLI\System\Collection\TArray":private]=>
- int(0)
- ["__array":"ILLI\System\ProtoArray":private]=>
- array(9) {
- [0]=>
- string(3) "The"
- [1]=>
- string(5) "quick"
- [2]=>
- string(5) "brown"
- [3]=>
- string(4) "LION"
- [4]=>
- string(5) "jumps"
- [5]=>
- string(4) "over"
- [6]=>
- string(3) "the"
- [7]=>
- string(4) "lazy"
- [8]=>
- string(3) "dog"
- }
- ["__protMode":"ILLI\System\ProtoArray":private]=>
- int(0)
- }
- ["__length":protected]=>
- int(5)
- ["__start":protected]=>
- int(2)
- ["__first":protected]=>
- int(2)
- ["__last":protected]=>
- int(6)
- }
- string(8) "2: brown"
- string(7) "3: LION"
- string(8) "4: jumps"
- string(7) "5: over"
- string(6) "6: the"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement