1 | <?php |
||
24 | class ArrayList extends ListAbstract { |
||
25 | use ArrayAccessTrait; |
||
26 | |||
27 | public $data; |
||
28 | private $current; |
||
29 | private $position; |
||
30 | |||
31 | public function __construct() { |
||
32 | $this->data = []; |
||
33 | $this->size = 0; |
||
34 | $this->position = 0; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * {@inheritDoc} |
||
39 | */ |
||
40 | public function insert($index, $data) { |
||
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | */ |
||
48 | protected function insertAt($index, $data) {} |
||
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | */ |
||
53 | protected function insertEnd($data) {} |
||
54 | |||
55 | /** |
||
56 | * {@inheritDoc} |
||
57 | */ |
||
58 | protected function insertBeginning($data) {} |
||
59 | |||
60 | /** |
||
61 | * Removes all the array items. |
||
62 | */ |
||
63 | public function clear() { |
||
64 | $this->data = []; |
||
65 | $this->size = 0; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * It execution time is O(1) because arrays in PHP are |
||
70 | * hashtables. |
||
71 | * {@inheritDoc} |
||
72 | */ |
||
73 | public function get($index) { |
||
74 | if($index < 0 || $index > $this->size - 1) { |
||
75 | throw new OutOfBoundsException(); |
||
76 | } |
||
77 | |||
78 | return $this->data[$index]; |
||
79 | } |
||
80 | |||
81 | protected function search($index) {} |
||
82 | |||
83 | /** |
||
84 | * {@inheritDoc} |
||
85 | */ |
||
86 | public function getLast() { |
||
87 | if(!$this->empty()) { |
||
88 | return $this->data[$this->size - 1]; |
||
89 | } |
||
90 | |||
91 | return null; |
||
92 | } |
||
93 | |||
94 | public function getAll() { |
||
95 | foreach($this->data as $data) { |
||
96 | yield $data; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritDoc} |
||
102 | */ |
||
103 | public function contains($data) : bool { |
||
104 | return in_array($data, $this->data); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritDoc} |
||
109 | */ |
||
110 | public function indexOf($data) { |
||
111 | return array_search($data, $this->data, true); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritDoc} |
||
116 | */ |
||
117 | public function lastIndexOf($data) { |
||
118 | for($i = $this->size - 1; $i >= 0; $i--) { |
||
119 | if($this->data[$i] === $data) { |
||
120 | return $i; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return false; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | public function remove($data) { |
||
131 | if($this->empty()) { |
||
132 | throw new NotFoundException(); |
||
133 | } |
||
134 | |||
135 | $i = 0; |
||
136 | while($i < $this->size) { |
||
137 | if($this->data[$i] === $data) { |
||
138 | $aux = $this->data[$i]; |
||
139 | array_splice($this->data, $i, 1); |
||
140 | $this->size--; |
||
141 | return $aux; |
||
142 | } |
||
143 | $i++; |
||
144 | } |
||
145 | |||
146 | throw new NotFoundException(); |
||
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 | } |