morrisonlevi

class.ArrayList.php

Oct 18th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. <?php
  2.  
  3. class ArrayList implements ArrayAccess, IteratorAggreate, Countable {
  4.    
  5.     private $list = array();
  6.    
  7.     function add($value, $index=null) {
  8.         $this->offsetSet($index, $value);
  9.     }
  10.    
  11.     function addAll(array &$values) {
  12.         foreach($values as &$value) {
  13.             $this->add($value);
  14.         }
  15.     }
  16.    
  17.     function clear() {
  18.         $this->list = array();
  19.     }
  20.    
  21.     function contains($value) {
  22.         return in_array($this->list);
  23.     }
  24.    
  25.     function indexOf($value) {
  26.         foreach($this->list as $index=>$sValue) {
  27.             if($value===$sValue) {
  28.                 return $index;
  29.             }
  30.         }
  31.        
  32.         return -1;
  33.     }
  34.    
  35.     function isEmpty() {
  36.         return $this->count() == 0;
  37.     }
  38.    
  39.     function count() {
  40.         return count($this->list);
  41.     }
  42.    
  43.     function get($index) {
  44.         if (!is_numeric($offset) || $offset!=(int)$offset) {
  45.             throw new InvalidArgumentException(
  46.                 "Index to ".get_class($this)." must be an integer."
  47.             );
  48.         }
  49.        
  50.         $this->offsetGet($index);
  51.        
  52.     }
  53.    
  54.     function remove($index) {
  55.         $this->offsetUnset($index);
  56.     }
  57.    
  58.     function set($index, $value) {
  59.         $this->offsetSet($index, $value);
  60.     }
  61.    
  62.     function size() {
  63.         return $this->count();
  64.     }
  65.    
  66.     function toArray() {
  67.         return $this->list;
  68.     }
  69.  
  70.     function offsetExists($offset) {
  71.         if (!is_numeric($offset) || $offset!=(int)$offset) {
  72.             throw new InvalidArgumentException(
  73.                 "Index to ".get_class($this)." must be an integer."
  74.             );
  75.         }
  76.        
  77.         return array_key_exists($offset, $this->list);
  78.     }
  79.     function offsetGet($offset) {
  80.         if (!is_numeric($offset) || $offset!=(int)$offset) {
  81.             throw new InvalidArgumentException(
  82.                 "Index to ".get_class($this)." must be an integer."
  83.             );
  84.         }
  85.    
  86.         if (offsetExists($offset))
  87.             return $this->list[$offset];
  88.            
  89.         throw new OutOfBoundsException("Index '$offset' does not exist.");
  90.     }
  91.     function offsetSet($offset, $value) {
  92.         if ($offset==null) {
  93.             $this->list[] = $value;
  94.         } else{
  95.             if (!is_numeric($offset) || $offset!=(int)$offset) {
  96.                 throw new InvalidArgumentException(
  97.                     "Index to ".get_class($this)." must be an integer."
  98.                 );
  99.             }
  100.            
  101.             $this->list[$offset] = $value;
  102.         }
  103.        
  104.     }
  105.     function offsetUnset($offset) {
  106.         if (!is_numeric($offset) || $offset!=(int)$offset) {
  107.             throw new InvalidArgumentException(
  108.                 "Index to ".get_class($this)." must be an integer."
  109.             );
  110.         }
  111.         unset($this->list[$offset]);
  112.         $this->list = array_values($this->list);
  113.     }
  114.     function getIterator() {
  115.         return new ArrayIterator($this->list);
  116.     }
  117.  
  118. }
  119.  
  120. $list = new ArrayList();
  121.  
  122. $list->add(1);
  123. $list[1] = 2;
  124.  
  125.  
  126. ?>
  127.  
Advertisement
Add Comment
Please, Sign In to add comment