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 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() { |
98
|
|
|
return $this->delete(($this->size === 0) ? 0 : $this->size - 1); |
99
|
|
|
} |
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) { |
108
|
|
|
$this->data[] = $data; |
109
|
|
|
$this->size++; |
110
|
|
|
} |
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) { |
123
|
|
|
return array_search($data, $this->data, true); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
*/ |
129
|
|
|
public function lastIndexOf($data) { |
130
|
|
|
for($i = $this->size - 1; $i >= 0; $i--) { |
131
|
|
|
if($this->data[$i] === $data) { |
132
|
|
|
return $i; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Adds at the beginning a node in the list. |
141
|
|
|
* |
142
|
|
|
* @param mixed $data |
143
|
|
|
* @return mixed the data stored. |
144
|
|
|
*/ |
145
|
|
|
public function unshift($data) { |
146
|
|
|
$this->insert(0, $data); |
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) { |
157
|
|
|
if($this->size === 0 || $index < 0 || $index > $this->size - 1) { |
158
|
|
|
throw new OutOfBoundsException(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$data = $this->data[$index]; |
162
|
|
|
array_splice($this->data, $index, 1); |
163
|
|
|
$this->size--; |
164
|
|
|
|
165
|
|
|
return $data; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Deletes the first node of the list and returns it. |
170
|
|
|
* |
171
|
|
|
* @return mixed the data. |
172
|
|
|
*/ |
173
|
|
|
public function shift() { |
174
|
|
|
return $this->delete(0); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns array stored in the data attribute. |
179
|
|
|
* |
180
|
|
|
* @return array data stored in all nodes. |
181
|
|
|
*/ |
182
|
|
|
public function toArray() : array { |
183
|
|
|
return $this->data; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Reset the cursor position. |
188
|
|
|
*/ |
189
|
|
|
public function rewind() { |
190
|
|
|
$this->position = 0; |
191
|
|
|
$this->current = $this->data[$this->position]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns the current node data. |
196
|
|
|
* |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
|
public function current() { |
200
|
|
|
return $this->current; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Key or index that indicates the cursor position. |
205
|
|
|
* |
206
|
|
|
* @return integer The current position. |
207
|
|
|
*/ |
208
|
|
|
public function key() { |
209
|
|
|
return $this->position; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Move the cursor to the next node and increments the |
214
|
|
|
* position counter. |
215
|
|
|
*/ |
216
|
|
|
public function next() { |
217
|
|
|
++$this->position; |
218
|
|
|
$this->current = $this->data[$this->position]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns if the current pointer position is valid. |
223
|
|
|
* |
224
|
|
|
* @return boolean true if pointer is not last, else false. |
225
|
|
|
*/ |
226
|
|
|
public function valid() { |
227
|
|
|
return $this->position < $this->size; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Binds to count() method. This is equal to make $this->tree->size(). |
232
|
|
|
* |
233
|
|
|
* @return integer the tree size. 0 if it is empty. |
234
|
|
|
*/ |
235
|
|
|
public function count() { |
236
|
|
|
return $this->size; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Returns the array size. |
241
|
|
|
* |
242
|
|
|
* @return int the length |
243
|
|
|
*/ |
244
|
|
|
public function size() : int { |
245
|
|
|
return $this->size; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Checks if the list is empty. |
250
|
|
|
* |
251
|
|
|
* @return boolean true if is empty, else false. |
252
|
|
|
*/ |
253
|
|
|
public function empty() : bool { |
254
|
|
|
return $this->size === 0; |
255
|
|
|
} |
256
|
|
|
} |