1 | <?php |
||
23 | class ArrayList implements ListInterface { |
||
24 | use CountTrait; |
||
25 | use ArrayAccessTrait; |
||
26 | |||
27 | private $data; |
||
28 | private $current; |
||
29 | private $position; |
||
30 | private $size; |
||
31 | |||
32 | public function __construct() { |
||
33 | $this->data = []; |
||
34 | $this->size = 0; |
||
35 | $this->position = 0; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Add a new node in the specified index. |
||
40 | * |
||
41 | * @param integer $index the position. |
||
42 | * @param mixed $data the data to be stored. |
||
43 | */ |
||
44 | public function insert($index, $data) { |
||
45 | array_splice($this->data, $index, 0, $data); |
||
46 | $this->size++; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Removes all the array items. |
||
51 | */ |
||
52 | public function clear() { |
||
53 | $this->data = []; |
||
54 | $this->size = 0; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Returns the array in the especified index. |
||
59 | * |
||
60 | * @param integer $index Index that must be greater than 0 |
||
61 | * and lower than the list size. |
||
62 | * @return mixed The data stored in the given index |
||
63 | * @throws OutOfBoundsException if index is out bounds. |
||
64 | */ |
||
65 | public function get($index) { |
||
66 | if($index < 0 || $index > $this->size - 1) { |
||
67 | throw new OutOfBoundsException(); |
||
68 | } |
||
69 | |||
70 | return $this->data[$index]; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Returns the last node with O(1). |
||
75 | * |
||
76 | * @return mixed null if the array is empty. |
||
77 | */ |
||
78 | public function getLast() { |
||
79 | if(!$this->empty()) { |
||
80 | return $this->data[$this->size - 1]; |
||
81 | } |
||
82 | |||
83 | return null; |
||
84 | } |
||
85 | |||
86 | public function getAll() { |
||
87 | foreach($this->data as $data) { |
||
88 | yield $data; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Removes and returns the last item in the array. |
||
94 | * |
||
95 | * @return mixed data in node. |
||
96 | */ |
||
97 | public function pop() { |
||
100 | |||
101 | /** |
||
102 | * Adds at the end of the list new node containing |
||
103 | * the data to be stored. |
||
104 | * |
||
105 | * @param mixed $data The data |
||
106 | */ |
||
107 | public function push($data) { |
||
111 | |||
112 | /** |
||
113 | * |
||
114 | */ |
||
115 | public function contains($data) : bool { |
||
116 | return in_array($data, $this->data); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | */ |
||
122 | public function indexOf($data) { |
||
125 | |||
126 | /** |
||
127 | * Adds at the beginning a node in the list. |
||
128 | * |
||
129 | * @param mixed $data |
||
130 | * @return mixed the data stored. |
||
131 | */ |
||
132 | public function unshift($data) { |
||
135 | |||
136 | /** |
||
137 | * Delete a node in the given position and returns it back. |
||
138 | * |
||
139 | * @param integer $index the position. |
||
140 | * @throws OutOfBoundsException if index is negative |
||
141 | * or is greater than the size of the list. |
||
142 | */ |
||
143 | public function delete($index) { |
||
154 | |||
155 | /** |
||
156 | * Deletes the first node of the list and returns it. |
||
157 | * |
||
158 | * @return mixed the data. |
||
159 | */ |
||
160 | public function shift() { |
||
163 | |||
164 | /** |
||
165 | * Returns array stored in the data attribute. |
||
166 | * |
||
167 | * @return array data stored in all nodes. |
||
168 | */ |
||
169 | public function toArray() : array { |
||
172 | |||
173 | /** |
||
174 | * Reset the cursor position. |
||
175 | */ |
||
176 | public function rewind() { |
||
180 | |||
181 | /** |
||
182 | * Returns the current node data. |
||
183 | * |
||
184 | * @return mixed |
||
185 | */ |
||
186 | public function current() { |
||
189 | |||
190 | /** |
||
191 | * Key or index that indicates the cursor position. |
||
192 | * |
||
193 | * @return integer The current position. |
||
194 | */ |
||
195 | public function key() { |
||
198 | |||
199 | /** |
||
200 | * Move the cursor to the next node and increments the |
||
201 | * position counter. |
||
202 | */ |
||
203 | public function next() { |
||
207 | |||
208 | /** |
||
209 | * Returns if the current pointer position is valid. |
||
210 | * |
||
211 | * @return boolean true if pointer is not last, else false. |
||
212 | */ |
||
213 | public function valid() { |
||
216 | |||
217 | /** |
||
218 | * Binds to count() method. This is equal to make $this->tree->size(). |
||
219 | * |
||
220 | * @return integer the tree size. 0 if it is empty. |
||
221 | */ |
||
222 | public function count() { |
||
225 | |||
226 | /** |
||
227 | * Returns the array size. |
||
228 | * |
||
229 | * @return int the length |
||
230 | */ |
||
231 | public function size() : int { |
||
234 | |||
235 | /** |
||
236 | * Checks if the list is empty. |
||
237 | * |
||
238 | * @return boolean true if is empty, else false. |
||
239 | */ |
||
240 | public function empty() : bool { |
||
243 | } |