Source for file ArrayList.php

Documentation is available at ArrayList.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Collections
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Provides the base class for a generic list (items can be accessed by index).
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Collections
  20. 20:  */
  21. 21: class ArrayList extends Collection implements IList
  22. 22: {
  23. 23:     /** @var int */
  24. 24:     protected $base = 0;
  25. 25:  
  26. 26:  
  27. 27:     /**
  28. 28:      * Inserts the specified element at the specified position in this list.
  29. 29:      * @param  int 
  30. 30:      * @param  mixed 
  31. 31:      * @return bool 
  32. 32:      * @throws ArgumentOutOfRangeException
  33. 33:      */
  34. 34:     public function insertAt($index$item)
  35. 35:     {
  36. 36:         $index -= $this->base;
  37. 37:         if ($index || $index count($this)) {
  38. 38:             throw new ArgumentOutOfRangeException;
  39. 39:         }
  40. 40:  
  41. 41:         $this->beforeAdd($item);
  42. 42:         $data $this->getArrayCopy();
  43. 43:         array_splice($data(int) $index0array($item));
  44. 44:         $this->setArray($data);
  45. 45:         return TRUE;
  46. 46:     }
  47. 47:  
  48. 48:  
  49. 49:  
  50. 50:     /**
  51. 51:      * Removes the first occurrence of the specified element.
  52. 52:      * @param  mixed 
  53. 53:      * @return bool  true if this list changed as a result of the call
  54. 54:      * @throws NotSupportedException
  55. 55:      */
  56. 56:     public function remove($item)
  57. 57:     {
  58. 58:         $this->updating();
  59. 59:  
  60. 60:         $index $this->search($item);
  61. 61:         if ($index === FALSE{
  62. 62:             return FALSE;
  63. 63:         else {
  64. 64:             $data $this->getArrayCopy();
  65. 65:             array_splice($data$index1);
  66. 66:             $this->setArray($data);
  67. 67:             return TRUE;
  68. 68:         }
  69. 69:     }
  70. 70:  
  71. 71:  
  72. 72:  
  73. 73:     /**
  74. 74:      * Returns the index of the first occurrence of the specified element,.
  75. 75:      * or FALSE if this list does not contain this element.
  76. 76:      * @param  mixed 
  77. 77:      * @return int|FALSE
  78. 78:      */
  79. 79:     public function indexOf($item)
  80. 80:     {
  81. 81:         $index $this->search($item);
  82. 82:         return $index === FALSE FALSE $this->base + $index;
  83. 83:     }
  84. 84:  
  85. 85:  
  86. 86:  
  87. 87:     /********************* interface \ArrayAccess ****************d*g**/
  88. 88:  
  89. 89:  
  90. 90:  
  91. 91:     /**
  92. 92:      * Replaces (or appends) the item (\ArrayAccess implementation).
  93. 93:      * @param  int index
  94. 94:      * @param  object 
  95. 95:      * @return void 
  96. 96:      * @throws InvalidArgumentException, \NotSupportedException, \ArgumentOutOfRangeException
  97. 97:      */
  98. 98:     public function offsetSet($index$item)
  99. 99:     {
  100. 100:         $this->beforeAdd($item);
  101. 101:  
  102. 102:         if ($index === NULL)  // append
  103. 103:             parent::offsetSet(NULL$item);
  104. 104:  
  105. 105:         else // replace
  106. 106:             $index -= $this->base;
  107. 107:             if ($index || $index >= count($this)) {
  108. 108:                 throw new ArgumentOutOfRangeException;
  109. 109:             }
  110. 110:             parent::offsetSet($index$item);
  111. 111:         }
  112. 112:     }
  113. 113:  
  114. 114:  
  115. 115:  
  116. 116:     /**
  117. 117:      * Returns item (\ArrayAccess implementation).
  118. 118:      * @param  int index
  119. 119:      * @return mixed 
  120. 120:      * @throws ArgumentOutOfRangeException
  121. 121:      */
  122. 122:     public function offsetGet($index)
  123. 123:     {
  124. 124:         $index -= $this->base;
  125. 125:         if ($index || $index >= count($this)) {
  126. 126:             throw new ArgumentOutOfRangeException;
  127. 127:         }
  128. 128:  
  129. 129:         return parent::offsetGet($index);
  130. 130:     }
  131. 131:  
  132. 132:  
  133. 133:  
  134. 134:     /**
  135. 135:      * Exists item? (\ArrayAccess implementation).
  136. 136:      * @param  int index
  137. 137:      * @return bool 
  138. 138:      */
  139. 139:     public function offsetExists($index)
  140. 140:     {
  141. 141:         $index -= $this->base;
  142. 142:         return $index >= && $index count($this);
  143. 143:     }
  144. 144:  
  145. 145:  
  146. 146:  
  147. 147:     /**
  148. 148:      * Removes the element at the specified position in this list.
  149. 149:      * @param  int index
  150. 150:      * @return void 
  151. 151:      * @throws NotSupportedException, \ArgumentOutOfRangeException
  152. 152:      */
  153. 153:     public function offsetUnset($index)
  154. 154:     {
  155. 155:         $this->updating();
  156. 156:  
  157. 157:         $index -= $this->base;
  158. 158:         if ($index || $index >= count($this)) {
  159. 159:             throw new ArgumentOutOfRangeException;
  160. 160:         }
  161. 161:  
  162. 162:         $data $this->getArrayCopy();
  163. 163:         array_splice($data(int) $index1);
  164. 164:         $this->setArray($data);
  165. 165:     }
  166. 166: