1 | <?php |
||
23 | class ArrayList extends ListAbstract { |
||
24 | use ArrayAccessTrait; |
||
25 | |||
26 | private $data; |
||
27 | private $current; |
||
28 | private $position; |
||
29 | |||
30 | public function __construct() { |
||
31 | $this->data = []; |
||
32 | $this->size = 0; |
||
33 | $this->position = 0; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * {@inheritDoc} |
||
38 | */ |
||
39 | public function insert($index, $data) { |
||
40 | array_splice($this->data, $index, 0, $data); |
||
41 | $this->size++; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * {@inheritDoc} |
||
46 | */ |
||
47 | protected function insertAt($index, $data) {} |
||
48 | |||
49 | /** |
||
50 | * {@inheritDoc} |
||
51 | */ |
||
52 | protected function insertEnd($data) {} |
||
53 | |||
54 | /** |
||
55 | * {@inheritDoc} |
||
56 | */ |
||
57 | protected function insertBeginning($data) {} |
||
58 | |||
59 | /** |
||
60 | * Removes all the array items. |
||
61 | */ |
||
62 | public function clear() { |
||
63 | $this->data = []; |
||
64 | $this->size = 0; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Returns the array in the especified index. |
||
69 | * |
||
70 | * @param integer $index Index that must be greater than 0 |
||
71 | * and lower than the list size. |
||
72 | * @return mixed The data stored in the given index |
||
73 | * @throws OutOfBoundsException if index is out bounds. |
||
74 | */ |
||
75 | public function get($index) { |
||
76 | if($index < 0 || $index > $this->size - 1) { |
||
77 | throw new OutOfBoundsException(); |
||
78 | } |
||
79 | |||
80 | return $this->data[$index]; |
||
81 | } |
||
82 | |||
83 | protected function search($index) {} |
||
84 | |||
85 | /** |
||
86 | * Returns the last node with O(1). |
||
87 | * |
||
88 | * @return mixed null if the array is empty. |
||
89 | */ |
||
90 | public function getLast() { |
||
91 | if(!$this->empty()) { |
||
92 | return $this->data[$this->size - 1]; |
||
93 | } |
||
94 | |||
95 | return null; |
||
96 | } |
||
97 | |||
98 | public function getAll() { |
||
99 | foreach($this->data as $data) { |
||
100 | yield $data; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritDoc} |
||
106 | */ |
||
107 | public function contains($data) : bool { |
||
108 | return in_array($data, $this->data); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | public function indexOf($data) { |
||
115 | return array_search($data, $this->data, true); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * {@inheritDoc} |
||
120 | */ |
||
121 | public function lastIndexOf($data) { |
||
122 | for($i = $this->size - 1; $i >= 0; $i--) { |
||
123 | if($this->data[$i] === $data) { |
||
124 | return $i; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return false; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * {@inheritDoc} |
||
133 | */ |
||
134 | public function remove($data) { |
||
135 | $i = 0; |
||
136 | |||
137 | while($i < $this->size) { |
||
138 | if($this->data[$i] === $data) { |
||
139 | $aux = $this->data[$i]; |
||
140 | array_splice($this->data, $i, 1); |
||
141 | |||
142 | return $aux; |
||
143 | } |
||
144 | $i++; |
||
145 | } |
||
146 | return null; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Delete a node in the given position and returns it back. |
||
151 | * |
||
152 | * @param integer $index the position. |
||
153 | * @throws OutOfBoundsException if index is negative |
||
154 | * or is greater than the size of the list. |
||
155 | */ |
||
156 | public function delete($index) { |
||
167 | |||
168 | /** |
||
169 | * {@inheritDoc} |
||
170 | */ |
||
171 | protected function deleteBeginning() {} |
||
172 | |||
173 | /** |
||
174 | * {@inheritDoc} |
||
175 | */ |
||
176 | protected function deleteAt($index) {} |
||
177 | |||
178 | /** |
||
179 | * {@inheritDoc} |
||
180 | */ |
||
181 | protected function deleteEnd() {} |
||
182 | |||
183 | /** |
||
184 | * Returns array stored in the data attribute. |
||
185 | * |
||
186 | * @return array data stored in all nodes. |
||
187 | */ |
||
188 | public function toArray() : array { |
||
191 | |||
192 | /** |
||
193 | * Reset the cursor position. |
||
194 | */ |
||
195 | public function rewind() { |
||
199 | |||
200 | /** |
||
201 | * Returns the current node data. |
||
202 | * |
||
203 | * @return mixed |
||
204 | */ |
||
205 | public function current() { |
||
209 | |||
210 | /** |
||
211 | * Key or index that indicates the cursor position. |
||
212 | * |
||
213 | * @return integer The current position. |
||
214 | */ |
||
215 | public function key() { |
||
218 | |||
219 | /** |
||
220 | * Move the cursor to the next node and increments the |
||
221 | * position counter. |
||
222 | */ |
||
223 | public function next() { |
||
226 | |||
227 | /** |
||
228 | * Returns if the current pointer position is valid. |
||
229 | * |
||
230 | * @return boolean true if pointer is not last, else false. |
||
231 | */ |
||
232 | public function valid() { |
||
235 | } |