Advertisement
fruffl

Class ArraySegment

Mar 3rd, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.65 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_System
  6.      * @package    ILLI
  7.      * @subpackage System
  8.      * @link       http://illi.be
  9.      * @license    http://l.illi.be
  10.      * @copyright  ILLI Conference
  11.      */
  12.     NAMESPACE ILLI\System;
  13.  
  14.     /**
  15.      * ILLI System Abstract Prototype ArraySegment
  16.      *
  17.      * Delimits a section of a one-dimensional array.
  18.      *
  19.      * ProtoArraySegment is a wrapper around a ProtoArray that delimits a range of
  20.      * elements in that array. Multiple ProtoArraySegment instances refer to the
  21.      * same original ProtoArray and can overlap.
  22.      *
  23.      * The getReference method returns the entire original ProtoArray Instance,
  24.      * not a copy of the ProtoArray.
  25.      *
  26.      * <code>
  27.      * <?PHP
  28.      * function writeArray(tArray $arrSeg)
  29.      * {
  30.      *  print "===\n";
  31.      *  print "tArray\n";
  32.      *  foreach($arrSeg as $k => $v)
  33.      *      var_dump($k.': '.$v);
  34.      * }
  35.      * function writeSeg(tArraySegment $arrSeg)
  36.      * {
  37.      *  print "===\n";
  38.      *  printf('tArraySegment [start-index: %d|length: %d|last-index: %d]'."\n",
  39.      *      $arrSeg->getStart(),
  40.      *      $arrSeg->getLength(),
  41.      *      $arrSeg->getLast());
  42.      *     
  43.      *  foreach($arrSeg as $k => $v)
  44.      *      var_dump($k.': '.$v);
  45.      * }
  46.      *
  47.      * $a = new tArray(["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]);
  48.      *
  49.      * $b = new tArraySegment($a, 6, 5);
  50.      * $c = new tArraySegment($a, NULL, 5);
  51.      * $d = new tArraySegment($a, 2, 5);
  52.      * $e = new tArraySegment($a, 2, NULL);
  53.      *
  54.      * print "Defaults\n";
  55.      * writeArray($a);  //"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"
  56.      * writeSeg($b);    //"the", "lazy", "dog"
  57.      * writeSeg($c);    //"The", "quick", "brown", "fox", "jumps"
  58.      * writeSeg($d);    //"brown", "fox", "jumps", "over", "the"
  59.      * writeSeg($e);    //"brown", "fox", "jumps", "over", "the", "lazy", "dog"
  60.      *
  61.      * print "\n\nChanging a value in the tArray\n";
  62.      * $a[6] = 'TREES';
  63.      *
  64.      * writeArray($a);  //"The", "quick", "brown", "fox", "jumps", "over", "TREES", "lazy", "dog"
  65.      * writeSeg($b);    //"TREES", "lazy", "dog"
  66.      * writeSeg($c);    //"The", "quick", "brown", "fox", "jumps"
  67.      * writeSeg($d);    //"brown", "fox", "jumps", "over", "TREES"
  68.      * writeSeg($e);    //"brown", "fox", "jumps", "over", "TREES", "lazy", "dog"
  69.      *
  70.      * print "\n\nChanging a value in a tArraySegment\n";
  71.      * $b[3] = 'LION';
  72.      *
  73.      * writeArray($a);  //"The", "quick", "brown", "LION", "jumps", "over", "TREES", "lazy", "dog"
  74.      * writeSeg($b);    //"TREES", "lazy", "dog"
  75.      * writeSeg($c);    //"The", "quick", "brown", "LION", "jumps"
  76.      * writeSeg($d);    //"brown", "LION", "jumps", "over", "TREES"
  77.      * writeSeg($e);    //"brown", "LION", "jumps", "over", "TREES", "lazy", "dog"
  78.      * ?>
  79.      * </code>
  80.      *
  81.      * The ArrayAccess for the instance of ProtoArraySegment is limited by the
  82.      * initial-values index-start and length.
  83.      *
  84.      * <code>
  85.      * <?PHP
  86.      * $a = new tArray(["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]);
  87.      * $b = new tArraySegment($a, 1, 3);
  88.      *
  89.      * print "Defaults\n";
  90.      * writeArray($a);  //"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"
  91.      * writeSeg($b);    //"quick", "brown", "fox"
  92.      *
  93.      * $b[6] = 'BANANAS'; // throws ILLI\System\ArgumentOutOfRangeException
  94.      * ?>
  95.      * </code>
  96.      *
  97.      * @category   ILLI_System
  98.      * @package    ILLI
  99.      * @subpackage System
  100.      * @namespace  ILLI\System
  101.      * @link       http://illi.be
  102.      * @license    http://l.illi.be
  103.      * @copyright  ILLI Conference
  104.      * @since      2.0.0-1
  105.      * @version    2.0.0-1
  106.      * @abstract
  107.      */
  108.     ABSTRACT CLASS ProtoArraySegment EXTENDS Proto IMPLEMENTS iArrayAccess
  109.     {
  110.         private $__REFERENCE    = NULL;
  111.        
  112.         private $__length   = NULL;
  113.         private $__start    = NULL;
  114.         private $__first    = NULL;
  115.         private $__last     = NULL;
  116.        
  117.         public function __construct(ProtoArray &$array, $start = NULL, $length = NULL)
  118.         {
  119.             if(NULL === $array)
  120.                 throw new ArgumentNullException;
  121.            
  122.             if(NULL !== $start)
  123.             {
  124.                 if(!is_integer($start))
  125.                     throw new ArgumentException(E::ARGUMENT_EXPECTED_INTEGER);
  126.                    
  127.                 if($start < 0)
  128.                     throw new ArgumentException(E::ARGUMENT_EXPECTED_INTEGER_NOT_POSITIVE_OR_ZERO,
  129.                         ['value' => $start]);
  130.             }
  131.            
  132.             if(NULL !== $length)
  133.             {
  134.                 if(!is_integer($length))
  135.                     throw new ArgumentException(E::ARGUMENT_EXPECTED_INTEGER);
  136.                    
  137.                 if($length < 0)
  138.                     throw new ArgumentException(E::ARGUMENT_EXPECTED_INTEGER_NOT_POSITIVE_OR_ZERO,
  139.                         ['value' => $length]);
  140.             }
  141.            
  142.             $this->__REFERENCE  =& $array;
  143.             $this->__start      = $start;
  144.             $this->__length     = $length;     
  145.             $this->__first      = $start === NULL
  146.                             ? $array->bottom()
  147.                             : $start;
  148.             $this->__last       = $length === NULL
  149.                             ? $array->peek()
  150.                             : $array->peek($start + $length - 1);
  151.         }
  152.        
  153.         protected function getReference()
  154.         {
  155.             return $this->__REFERENCE;
  156.         }
  157.        
  158.         public function offsetAccessible($offset)
  159.         {
  160.             return $offset >= $this->getFirst() && $offset <= $this->getLast();
  161.         }
  162.        
  163.         public function getStart()
  164.         {
  165.             return $this->__start;
  166.         }
  167.        
  168.         public function getLength()
  169.         {
  170.             return $this->__length;
  171.         }
  172.        
  173.         public function getFirst()
  174.         {
  175.             return $this->__first;
  176.         }
  177.        
  178.         public function getLast()
  179.         {
  180.             return $this->__last;
  181.         }
  182.        
  183.         public function offsetSet($offset, $value)
  184.         {
  185.             if(NULL === $offset)
  186.                 throw new ArgumentNullException;
  187.                
  188.             if(!is_scalar($offset))
  189.                 throw new ArgumentException(E::ARGUMENT_EXPECTED_SCALAR);
  190.                
  191.             if(FALSE === $this->offsetAccessible($offset))
  192.                 throw new ArgumentOutOfRangeException(E::ARGUMENT_OUT_OF_ARRAY_INDEX,
  193.                     ['key' => $offset]);
  194.                
  195.             $this->__REFERENCE->offsetSet($offset, $value);
  196.             return $this;
  197.         }
  198.        
  199.         public function offsetExists($offset)
  200.         {
  201.             if(NULL === $offset)
  202.                 throw new ArgumentNullException;
  203.                
  204.             if(!is_scalar($offset))
  205.                 throw new ArgumentException(E::ARGUMENT_EXPECTED_SCALAR);
  206.                
  207.             return $this->__REFERENCE->offsetExists($offset) && $this->offsetAccessible($offset);
  208.         }
  209.        
  210.         public function offsetUnset($offset)
  211.         {
  212.             if(NULL === $offset)
  213.                 throw new ArgumentNullException;
  214.                
  215.             if(!is_scalar($offset))
  216.                 throw new ArgumentException(E::ARGUMENT_EXPECTED_SCALAR);
  217.                
  218.             if(FALSE === $this->offsetAccessible($offset))
  219.                 throw new ArgumentOutOfRangeException(E::ARGUMENT_OUT_OF_ARRAY_INDEX,
  220.                     ['key' => $offset]);
  221.                
  222.             $this->__REFERENCE->offsetUnset($offset);
  223.                
  224.             return $this;
  225.         }
  226.        
  227.         public function offsetGet($offset)
  228.         {
  229.             if(NULL === $offset)
  230.                 throw new ArgumentNullException;
  231.                
  232.             if(!is_scalar($offset))
  233.                 throw new ArgumentException(E::ARGUMENT_EXPECTED_SCALAR);
  234.                
  235.             if(FALSE === $this->offsetAccessible($offset))
  236.                 throw new ArgumentOutOfRangeException(E::ARGUMENT_OUT_OF_ARRAY_INDEX,
  237.                     ['key' => $offset]);
  238.                
  239.             return $this->__REFERENCE->offsetGet($offset);
  240.         }
  241.     }
  242.  
  243. <?PHP
  244.     /**
  245.      * ILLI
  246.      *
  247.      * @category   ILLI_System_Collection
  248.      * @package    ILLI
  249.      * @subpackage SystemCollection
  250.      * @link       http://illi.be
  251.      * @license    http://l.illi.be
  252.      * @copyright  ILLI Conference
  253.      */
  254.     NAMESPACE ILLI\System\Collection;
  255.     USE ILLI\System as System;
  256.  
  257.     /**
  258.      * ILLI System Collection Datatype ArraySegment
  259.      *
  260.      * @category   ILLI_System_Collection
  261.      * @package    ILLI
  262.      * @subpackage SystemCollection
  263.      * @namespace  ILLI\System\Collection
  264.      * @link       http://illi.be
  265.      * @license    http://l.illi.be
  266.      * @copyright  ILLI Conference
  267.      * @since      2.0.0-1
  268.      * @version    2.0.0-1
  269.      * @abstract
  270.      */
  271.     CLASS TArraySegment EXTENDS System\ProtoArraySegment IMPLEMENTS System\iIterator
  272.     {
  273.         const ITERATE_MODE_FIFO = 0x00000000;
  274.         const ITERATE_MODE_LIFO = 0x00000001;
  275.        
  276.         private $__itMode   = 0;
  277.         private $__index    = 0;
  278.        
  279.         public function __construct(TArray &$array, $start = NULL, $length = NULL)
  280.         {
  281.             if(NULL !== $start)
  282.             {                  
  283.                 if(!is_integer($start))
  284.                     throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER);
  285.                    
  286.                 if($start < 0)
  287.                     throw new System\ArgumentException(System\E::ARGUMENT_EXPECTED_INTEGER_NOT_POSITIVE_OR_ZERO,
  288.                         ['value' => $start]);
  289.             }
  290.            
  291.             $this->__index = $start;
  292.             parent::__construct($array, $start, $length);
  293.         }
  294.        
  295.         final public function getIteratorMode()
  296.         {
  297.             return $this->__itMode;
  298.         }
  299.        
  300.         final public function rewind()
  301.         {
  302.             return $this->__index = $this->getFirst();
  303.         }
  304.        
  305.         final public function current()
  306.         {
  307.             return $this->getReference()->offsetGet($this->__index);
  308.         }
  309.        
  310.         final public function key()
  311.         {
  312.             return $this->__index;
  313.         }
  314.        
  315.         final public function next()
  316.         {
  317.             return ++$this->__index;
  318.         }
  319.        
  320.         final public function prev()
  321.         {
  322.         }
  323.        
  324.         final public function valid()
  325.         {
  326.             return $this->offsetExists($this->__index);
  327.         }
  328.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement