Advertisement
fruffl

ArraySegment

Mar 2nd, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.27 KB | None | 0 0
  1. <?PHP
  2.     ABSTRACT CLASS ProtoArraySegment EXTENDS Proto
  3.     {
  4.         protected $__reference  = NULL;
  5.         protected $__length = NULL;
  6.         protected $__start  = NULL;
  7.         protected $__first  = NULL;
  8.         protected $__last   = NULL;
  9.        
  10.         public function __construct(ProtoArray &$array, $start = NULL, $length = NULL)
  11.         {
  12.             $this->__reference =& $array;
  13.            
  14.             $this->__start  = $start;
  15.             $this->__length = $length;
  16.        
  17.             $this->__first = ($start === NULL)
  18.                 ? $array->bottom()
  19.                 : $start;
  20.                
  21.             $this->__last = ($length === NULL)
  22.                 ? $array->peek()
  23.                 : $array->peek($start + $length - 1);
  24.         }
  25.     }
  26.  
  27.     CLASS TArraySegment EXTENDS System\ProtoArraySegment IMPLEMENTS System\iIterator
  28.     {
  29.         const ITERATE_MODE_FIFO = 0x00000000;
  30.         const ITERATE_MODE_LIFO = 0x00000001;
  31.        
  32.         private $__itMode   = 0;
  33.         private $__index    = 0;
  34.         public function __construct(TArray &$array, $start = NULL, $length = NULL)
  35.         {
  36.             if(NULL !== $start)
  37.             {                  
  38.                 if(!is_integer($start))
  39.                     throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER);
  40.                    
  41.                 if($start < 0)
  42.                     throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER_NOT_POSITIVE_OR_ZERO,
  43.                         ['value' => $start]);
  44.             }
  45.            
  46.             if(NULL !== $length)
  47.             {                  
  48.                 if(!is_integer($length))
  49.                     throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER);
  50.                    
  51.                 if($length <= 0)
  52.                     throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER_NOT_POSITIVE,
  53.                         ['value' => $length]);
  54.             }
  55.            
  56.             $this->__index = $start;
  57.             parent::__construct($array, $start, $length);
  58.         }
  59.        
  60.         final public function getIteratorMode()
  61.         {
  62.             return $this->__itMode;
  63.         }
  64.        
  65.         final public function rewind()
  66.         {
  67.             return $this->__index = $this->__first;
  68.         }
  69.        
  70.         final public function current()
  71.         {
  72.             return $this->__reference->offsetGet($this->__index);
  73.         }
  74.        
  75.         final public function key()
  76.         {
  77.             return $this->__index;
  78.         }
  79.        
  80.         final public function next()
  81.         {
  82.             return ++$this->__index;
  83.         }
  84.        
  85.         final public function prev()
  86.         {
  87.         }
  88.        
  89.         final public function valid()
  90.         {
  91.             return $this->__reference->offsetExists($this->__index) && $this->__index >= $this->__first && $this->__index <= $this->__last;
  92.         }
  93.     }
  94.  
  95.  
  96. $a = new tArray(["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]);
  97. $b = new tArraySegment($a, 2, 5); //"brown", "fox", "jumps", "over", "the"
  98.  
  99. $a[3] = 'LION';
  100.  
  101. var_dump($a);
  102. var_dump($b); //"brown", "LION", "jumps", "over", "the"
  103.  
  104.  
  105. foreach($b as $k => $v)
  106.     var_dump($k.': '.$v);
  107. ?>
  108. object(ILLI\System\Collection\TArray)#2 (4) {
  109.   ["__itMode":"ILLI\System\Collection\TArray":private]=>
  110.   int(0)
  111.   ["__index":"ILLI\System\Collection\TArray":private]=>
  112.   int(0)
  113.   ["__array":"ILLI\System\ProtoArray":private]=>
  114.   array(9) {
  115.     [0]=>
  116.     string(3) "The"
  117.     [1]=>
  118.     string(5) "quick"
  119.     [2]=>
  120.     string(5) "brown"
  121.     [3]=>
  122.     string(4) "LION"
  123.     [4]=>
  124.     string(5) "jumps"
  125.     [5]=>
  126.     string(4) "over"
  127.     [6]=>
  128.     string(3) "the"
  129.     [7]=>
  130.     string(4) "lazy"
  131.     [8]=>
  132.     string(3) "dog"
  133.   }
  134.   ["__protMode":"ILLI\System\ProtoArray":private]=>
  135.   int(0)
  136. }
  137. object(ILLI\System\Collection\TArraySegment)#3 (7) {
  138.   ["__itMode":"ILLI\System\Collection\TArraySegment":private]=>
  139.   int(0)
  140.   ["__index":"ILLI\System\Collection\TArraySegment":private]=>
  141.   int(2)
  142.   ["__reference":protected]=>
  143.   &object(ILLI\System\Collection\TArray)#2 (4) {
  144.     ["__itMode":"ILLI\System\Collection\TArray":private]=>
  145.     int(0)
  146.     ["__index":"ILLI\System\Collection\TArray":private]=>
  147.     int(0)
  148.     ["__array":"ILLI\System\ProtoArray":private]=>
  149.     array(9) {
  150.       [0]=>
  151.       string(3) "The"
  152.       [1]=>
  153.       string(5) "quick"
  154.       [2]=>
  155.       string(5) "brown"
  156.       [3]=>
  157.       string(4) "LION"
  158.       [4]=>
  159.       string(5) "jumps"
  160.       [5]=>
  161.       string(4) "over"
  162.       [6]=>
  163.       string(3) "the"
  164.       [7]=>
  165.       string(4) "lazy"
  166.       [8]=>
  167.       string(3) "dog"
  168.     }
  169.     ["__protMode":"ILLI\System\ProtoArray":private]=>
  170.     int(0)
  171.   }
  172.   ["__length":protected]=>
  173.   int(5)
  174.   ["__start":protected]=>
  175.   int(2)
  176.   ["__first":protected]=>
  177.   int(2)
  178.   ["__last":protected]=>
  179.   int(6)
  180. }
  181. string(8) "2: brown"
  182. string(7) "3: LION"
  183. string(8) "4: jumps"
  184. string(7) "5: over"
  185. string(6) "6: the"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement