1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DataStructures for PHP |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/SiroDiaz/DataStructures |
6
|
|
|
* @copyright Copyright (c) 2017 Siro Díaz Palazón |
7
|
|
|
* @license https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License) |
8
|
|
|
*/ |
9
|
|
|
namespace DataStructures\Lists; |
10
|
|
|
|
11
|
|
|
use DataStructures\Lists\Interfaces\ListInterface; |
12
|
|
|
use DataStructures\Lists\Traits\{CountTrait, ArrayAccessTrait}; |
13
|
|
|
use OutOfBoundsException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ArrayList |
17
|
|
|
* |
18
|
|
|
* This class is an implementation of a list based in native arrays. |
19
|
|
|
* The access time is, in general, O(1) since all in PHP is a hash table. |
20
|
|
|
* |
21
|
|
|
* @author Siro Diaz Palazon <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ArrayList implements ListInterface { |
24
|
|
|
use ArrayAccessTrait; |
25
|
|
|
|
26
|
|
|
private $data; |
27
|
|
|
private $current; |
28
|
|
|
private $position; |
29
|
|
|
private $size; |
30
|
|
|
|
31
|
|
|
public function __construct() { |
32
|
|
|
$this->data = []; |
33
|
|
|
$this->size = 0; |
34
|
|
|
$this->position = 0; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add a new node in the specified index. |
39
|
|
|
* |
40
|
|
|
* @param integer $index the position. |
41
|
|
|
* @param mixed $data the data to be stored. |
42
|
|
|
*/ |
43
|
|
|
public function insert($index, $data) { |
44
|
|
|
array_splice($this->data, $index, 0, $data); |
45
|
|
|
$this->size++; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Removes all the array items. |
50
|
|
|
*/ |
51
|
|
|
public function clear() { |
52
|
|
|
$this->data = []; |
53
|
|
|
$this->size = 0; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the array in the especified index. |
58
|
|
|
* |
59
|
|
|
* @param integer $index Index that must be greater than 0 |
60
|
|
|
* and lower than the list size. |
61
|
|
|
* @return mixed The data stored in the given index |
62
|
|
|
* @throws OutOfBoundsException if index is out bounds. |
63
|
|
|
*/ |
64
|
|
|
public function get($index) { |
65
|
|
|
if($index < 0 || $index > $this->size - 1) { |
66
|
|
|
throw new OutOfBoundsException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->data[$index]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the last node with O(1). |
74
|
|
|
* |
75
|
|
|
* @return mixed null if the array is empty. |
76
|
|
|
*/ |
77
|
|
|
public function getLast() { |
78
|
|
|
if(!$this->empty()) { |
79
|
|
|
return $this->data[$this->size - 1]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getAll() { |
86
|
|
|
foreach($this->data as $data) { |
87
|
|
|
yield $data; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Removes and returns the last item in the array. |
93
|
|
|
* |
94
|
|
|
* @return mixed data in node. |
95
|
|
|
*/ |
96
|
|
|
public function pop() { |
97
|
|
|
return $this->delete(($this->size === 0) ? 0 : $this->size - 1); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Adds at the end of the list new node containing |
102
|
|
|
* the data to be stored. |
103
|
|
|
* |
104
|
|
|
* @param mixed $data The data |
105
|
|
|
*/ |
106
|
|
|
public function push($data) { |
107
|
|
|
$this->data[] = $data; |
108
|
|
|
$this->size++; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
*/ |
114
|
|
|
public function contains($data) : bool { |
115
|
|
|
return in_array($data, $this->data); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* |
120
|
|
|
*/ |
121
|
|
|
public function indexOf($data) { |
122
|
|
|
return array_search($data, $this->data, true); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
*/ |
128
|
|
|
public function lastIndexOf($data) { |
129
|
|
|
for($i = $this->size - 1; $i >= 0; $i--) { |
130
|
|
|
if($this->data[$i] === $data) { |
131
|
|
|
return $i; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
*/ |
141
|
|
|
public function remove($data) { |
142
|
|
|
$i = 0; |
143
|
|
|
|
144
|
|
|
while($i < $this->size) { |
145
|
|
|
if($this->data[$i] === $data) { |
146
|
|
|
$aux = $this->data[$i]; |
147
|
|
|
array_splice($this->data, $i, 1); |
148
|
|
|
|
149
|
|
|
return $aux; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$i++; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Adds at the beginning a node in the list. |
160
|
|
|
* |
161
|
|
|
* @param mixed $data |
162
|
|
|
* @return mixed the data stored. |
163
|
|
|
*/ |
164
|
|
|
public function unshift($data) { |
165
|
|
|
$this->insert(0, $data); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Delete a node in the given position and returns it back. |
170
|
|
|
* |
171
|
|
|
* @param integer $index the position. |
172
|
|
|
* @throws OutOfBoundsException if index is negative |
173
|
|
|
* or is greater than the size of the list. |
174
|
|
|
*/ |
175
|
|
|
public function delete($index) { |
176
|
|
|
if($this->size === 0 || $index < 0 || $index > $this->size - 1) { |
177
|
|
|
throw new OutOfBoundsException(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$data = $this->data[$index]; |
181
|
|
|
array_splice($this->data, $index, 1); |
182
|
|
|
$this->size--; |
183
|
|
|
|
184
|
|
|
return $data; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Deletes the first node of the list and returns it. |
189
|
|
|
* |
190
|
|
|
* @return mixed the data. |
191
|
|
|
*/ |
192
|
|
|
public function shift() { |
193
|
|
|
return $this->delete(0); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns array stored in the data attribute. |
198
|
|
|
* |
199
|
|
|
* @return array data stored in all nodes. |
200
|
|
|
*/ |
201
|
|
|
public function toArray() : array { |
202
|
|
|
return $this->data; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Reset the cursor position. |
207
|
|
|
*/ |
208
|
|
|
public function rewind() { |
209
|
|
|
$this->position = 0; |
210
|
|
|
$this->current = $this->data[$this->position]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the current node data. |
215
|
|
|
* |
216
|
|
|
* @return mixed |
217
|
|
|
*/ |
218
|
|
|
public function current() { |
219
|
|
|
return $this->current; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Key or index that indicates the cursor position. |
224
|
|
|
* |
225
|
|
|
* @return integer The current position. |
226
|
|
|
*/ |
227
|
|
|
public function key() { |
228
|
|
|
return $this->position; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Move the cursor to the next node and increments the |
233
|
|
|
* position counter. |
234
|
|
|
*/ |
235
|
|
|
public function next() { |
236
|
|
|
++$this->position; |
237
|
|
|
$this->current = $this->data[$this->position]; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns if the current pointer position is valid. |
242
|
|
|
* |
243
|
|
|
* @return boolean true if pointer is not last, else false. |
244
|
|
|
*/ |
245
|
|
|
public function valid() { |
246
|
|
|
return $this->position < $this->size; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Binds to count() method. This is equal to make $this->tree->size(). |
251
|
|
|
* |
252
|
|
|
* @return integer the tree size. 0 if it is empty. |
253
|
|
|
*/ |
254
|
|
|
public function count() { |
255
|
|
|
return $this->size; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Returns the array size. |
260
|
|
|
* |
261
|
|
|
* @return int the length |
262
|
|
|
*/ |
263
|
|
|
public function size() : int { |
264
|
|
|
return $this->size; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Checks if the list is empty. |
269
|
|
|
* |
270
|
|
|
* @return boolean true if is empty, else false. |
271
|
|
|
*/ |
272
|
|
|
public function empty() : bool { |
273
|
|
|
return $this->size === 0; |
274
|
|
|
} |
275
|
|
|
} |