Posted By

mscribellito on 06/24/11


Tagged


Versions (?)

ArrayList


 / Published in: PHP
 

  1. <?php
  2.  
  3. class ArrayList implements Iterator {
  4.  
  5. private $a = array();
  6. private $N = 0;
  7. private $i = 0;
  8.  
  9. public function __construct($collection = NULL) {
  10.  
  11. $this->a = $collection;
  12. $this->N = count($collection);
  13. }
  14.  
  15. public function add($element) {
  16.  
  17. $this->a[$this->N++] = $element;
  18. }
  19.  
  20. public function addRange($collection) {
  21.  
  22. for ($i = 0, $l = count($collection); $i < $l; $i++) {
  23. $this->add($collection[$i]);
  24. }
  25. }
  26.  
  27. private function checkIndex() {
  28.  
  29. $indeces = func_get_args();
  30.  
  31. foreach ($indeces as $index) {
  32.  
  33. if (!$this->validIndex($index)) {
  34. throw new Exception('Index out of bounds');
  35. }
  36. }
  37. }
  38.  
  39. public function clear() {
  40.  
  41. $this->a = array();
  42. $this->N = 0;
  43. $this->i = 0;
  44. }
  45.  
  46. public function contains($element) {
  47.  
  48. return $this->indexOf($element) > -1 ? TRUE : FALSE;
  49. }
  50.  
  51. public function current() {
  52.  
  53. return $this->get($this->i);
  54. }
  55.  
  56. public function get($index) {
  57.  
  58. $this->checkIndex($index);
  59.  
  60. return $this->a[$index];
  61. }
  62.  
  63. public function getRange($fromIndex, $toIndex) {
  64.  
  65. $this->checkIndex($fromIndex, $toIndex);
  66.  
  67. $collection = array();
  68.  
  69. for ($i = $fromIndex; $i <= $toIndex; $i++) {
  70. $collection[] = $this->get($i);
  71. }
  72.  
  73. return $collection;
  74. }
  75.  
  76. public function indexOf($element, $fromIndex = NULL, $last = FALSE) {
  77.  
  78. $this->checkIndex($fromIndex);
  79.  
  80. $index = -1;
  81.  
  82. $start = $fromIndex === NULL ? 0 : $fromIndex;
  83.  
  84. for ($i = $start, $l = $this->size(); $i < $l; $i++) {
  85.  
  86. if ($this->a[$i] == $element) {
  87. $index = $i;
  88. if (!$last) {
  89. break;
  90. }
  91. }
  92. }
  93.  
  94. return $index;
  95. }
  96.  
  97. public function insert($element, $index) {
  98.  
  99. $this->checkIndex($index);
  100.  
  101. for ($i = $this->size(); $i > $index; $i--) {
  102. $this->a[$i] = $this->a[$i - 1];
  103. }
  104. $this->a[$index] = $element;
  105. $this->N++;
  106. }
  107.  
  108. public function insertRange($collection, $index) {
  109.  
  110. $this->checkIndex($index);
  111.  
  112. for ($i = 0, $l = count($collection); $i < $l; $i++) {
  113. $this->insert($collection[$i], $index + $i);
  114. }
  115. }
  116.  
  117. public function isEmpty() {
  118.  
  119. return $this->size() === 0 ? TRUE : FALSE;
  120. }
  121.  
  122. public function key() {
  123.  
  124. return $this->i;
  125. }
  126.  
  127. public function lastIndexOf($element, $fromIndex = NULL) {
  128.  
  129. $this->checkIndex($fromIndex);
  130.  
  131. return $this->indexOf($element, $fromIndex, TRUE);
  132. }
  133.  
  134. public function next() {
  135.  
  136. $this->i++;
  137. }
  138.  
  139. public function remove($element) {
  140.  
  141. $index = $this->indexOf($element);
  142.  
  143. if ($index > -1) {
  144. $this->removeAt($index);
  145. }
  146. }
  147.  
  148. public function removeAt($index) {
  149.  
  150. $this->checkIndex($index);
  151.  
  152. for ($i = $index, $l = $this->size(); $i < $l; $i++) {
  153. if (isset($this->a[$i + 1])) {
  154. $this->a[$i] = $this->a[$i + 1];
  155. }
  156. }
  157.  
  158. unset($this->a[$this->size() - 1]);
  159.  
  160. $this->N--;
  161. }
  162.  
  163. public function removeRange($fromIndex, $toIndex) {
  164.  
  165. $this->checkIndex($fromIndex, $toIndex);
  166.  
  167. for ($i = $fromIndex; $i <= $toIndex; $i++) {
  168. $this->remove($fromIndex);
  169. }
  170. }
  171.  
  172. public function reverse() {
  173.  
  174. for ($a = 0, $b = $this->N; $a < --$b; $a++) {
  175. $swap = $this->a[$a];
  176. $this->a[$a] = $this->a[$b];
  177. $this->a[$b] = $swap;
  178. }
  179. }
  180.  
  181. public function reverseRange($fromIndex, $toIndex) {
  182.  
  183. $this->checkIndex($fromIndex, $toIndex);
  184.  
  185. for ($a = $fromIndex, $b = $toIndex; $a < $b; $a++, $b--) {
  186. $swap = $this->a[$a];
  187. $this->a[$a] = $this->a[$b];
  188. $this->a[$b] = $swap;
  189. }
  190. }
  191.  
  192. public function rewind() {
  193.  
  194. $this->i = 0;
  195. }
  196.  
  197. public function set($index, $element) {
  198.  
  199. $this->checkIndex($index);
  200.  
  201. $this->a[$index] = $element;
  202. }
  203.  
  204. public function setRange($index, $collection) {
  205.  
  206. $this->checkIndex($index);
  207.  
  208. for ($i = 0, $l = count($collection); $i < $l; $i++, $index++) {
  209. $this->set($index, $collection[$i]);
  210. }
  211. }
  212.  
  213. public function size() {
  214.  
  215. return $this->N;
  216. }
  217.  
  218. public function valid() {
  219.  
  220. return $this->i < $this->N;
  221. }
  222.  
  223. public function validIndex($index) {
  224.  
  225. return $index >= 0 && $index < $this->size() ? TRUE : FALSE;
  226. }
  227.  
  228. }

Report this snippet  

You need to login to post a comment.