Posted By

mscribellito on 10/13/11


Tagged


Versions (?)

ArrayList


 / Published in: PHP
 

  1. <?php
  2.  
  3. /**
  4.  * @name ArrayList
  5.  * @author Michael Scribellito
  6.  * @version 1.0 (10/12/2011)
  7.  */
  8. class ArrayList {
  9.  
  10. /**
  11.   * The array into which the elements of the ArrayList are stored.
  12.   *
  13.   * @var array
  14.   */
  15. private $data;
  16.  
  17. /**
  18.   * The size of the ArrayList (the number of elements it contains).
  19.   *
  20.   * @var integer
  21.   */
  22. private $size;
  23.  
  24. /**
  25.   * Constructs an empty list.
  26.   */
  27. public function __construct() {
  28.  
  29. $this->initialize();
  30.  
  31. }
  32.  
  33. /**
  34.   * Appends the specified element to the end of this list.
  35.   *
  36.   * @param mixed $element element to be appended to this list
  37.   *
  38.   * @return boolean true
  39.   */
  40. public function add($element) {
  41.  
  42. $this->data[] = $element;
  43.  
  44. $this->size++;
  45.  
  46. return true;
  47.  
  48. }
  49.  
  50. /**
  51.   * Checks if the given index is in range. If not, throws an exception.
  52.   *
  53.   * @param integer $index index to check
  54.   *
  55.   * @throws Exception
  56.   */
  57. private function rangeCheck($index) {
  58.  
  59. if (is_int($index) == false) {
  60.  
  61. throw new Exception("Index must be numeric");
  62.  
  63. }
  64.  
  65. if ($index < 0 || $index >= $this->size) {
  66.  
  67. throw new Exception(sprintf("Index out of bounds: Index: %d, Size: %s", $this->size(), $index));
  68.  
  69. }
  70.  
  71. }
  72.  
  73. /**
  74.   * Removes all of the elements from this list. The list will be empty after
  75.   * this call returns.
  76.   *
  77.   * @return void
  78.   */
  79. public function clear() {
  80.  
  81. $this->initialize();
  82.  
  83. }
  84.  
  85. /**
  86.   * Returns the element at the specified position in this list.
  87.   *
  88.   * @param integer $index index of the element to return
  89.   *
  90.   * @return mixed the element at the specified position in this list
  91.   *
  92.   * @throws Exception
  93.   */
  94. public function get($index) {
  95.  
  96. $this->rangeCheck($index);
  97.  
  98. return $this->data[$index];
  99.  
  100. }
  101.  
  102. /**
  103.   * Initializes this list.
  104.   *
  105.   * @return void
  106.   */
  107. private function initialize() {
  108.  
  109. $this->data = array();
  110.  
  111. $this->size = 0;
  112.  
  113. }
  114.  
  115. /**
  116.   * Returns true if this list contains no elements
  117.   *
  118.   * @return boolean true if this list contains no elements
  119.   */
  120. public function isEmpty() {
  121.  
  122. return $this->size == 0;
  123.  
  124. }
  125.  
  126. /**
  127.   *
  128.   * @return ArrayIterator
  129.   */
  130. public function iterator() {
  131.  
  132. return new ArrayIterator($this->data);
  133.  
  134. }
  135.  
  136. /**
  137.   * Removes the element at the specified position in this list. Shifts any
  138.   * subsequent elements to the left (subtracts one from their indices).
  139.   *
  140.   * @param integer $index the index of the element to be removed
  141.   *
  142.   * @return mixed the element that was removed from the list
  143.   *
  144.   * @throws Exception
  145.   */
  146. public function remove($index) {
  147.  
  148. $temp = null;
  149.  
  150. $this->rangeCheck($index);
  151.  
  152. $old = $this->get($index);
  153.  
  154. for ($i = $index; $i < $this->size() - 1; $i++) {
  155.  
  156. $this->set($i, $this->get($i + 1));
  157.  
  158. }
  159.  
  160. unset($this->data[$i]);
  161.  
  162. $this->size--;
  163.  
  164. return $old;
  165.  
  166. }
  167.  
  168. /**
  169.   * Replaces the element at the specified position in this list with the specified element.
  170.   *
  171.   * @param integer $index index of the element to replace
  172.   * @param mixed $element element to be stored at the specified position
  173.   *
  174.   * @return mixed the element previously at the specified position
  175.   *
  176.   * @throws Exception
  177.   */
  178. public function set($index, $element) {
  179.  
  180. $this->rangeCheck($index);
  181.  
  182. $old = $this->get($index);
  183.  
  184. $this->data[$index] = $element;
  185.  
  186. return $old;
  187.  
  188. }
  189.  
  190. /**
  191.   * Returns the number of elements in this list.
  192.   *
  193.   * @return int the number of elements in this list
  194.   */
  195. public function size() {
  196.  
  197. return $this->size;
  198.  
  199. }
  200.  
  201. /**
  202.   * Returns an array of this list.
  203.   *
  204.   * @return array an array of this list
  205.   */
  206. public function toArray() {
  207.  
  208. return $this->data;
  209.  
  210. }
  211.  
  212. /**
  213.   * Returns a string representation of this list.
  214.   *
  215.   * @return string string representation of this list
  216.   */
  217. public function toString() {
  218.  
  219. return "[ " . implode(", ", $this->data) . " ]";
  220.  
  221. }
  222.  
  223. }

Report this snippet  

You need to login to post a comment.