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\Traits\{CountTrait, ArrayAccessTrait}; |
12
|
|
|
use DataStructures\Lists\ListAbstract; |
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 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
|
|
|
/** |
84
|
|
|
* Returns the last node with O(1). |
85
|
|
|
* |
86
|
|
|
* @return mixed null if the array is empty. |
87
|
|
|
*/ |
88
|
|
|
public function getLast() { |
89
|
|
|
if(!$this->empty()) { |
90
|
|
|
return $this->data[$this->size - 1]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getAll() { |
97
|
|
|
foreach($this->data as $data) { |
98
|
|
|
yield $data; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
public function contains($data) : bool { |
106
|
|
|
return in_array($data, $this->data); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
|
|
public function indexOf($data) { |
113
|
|
|
return array_search($data, $this->data, true); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
|
|
public function lastIndexOf($data) { |
120
|
|
|
for($i = $this->size - 1; $i >= 0; $i--) { |
121
|
|
|
if($this->data[$i] === $data) { |
122
|
|
|
return $i; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
|
|
public function remove($data) { |
133
|
|
|
$i = 0; |
134
|
|
|
|
135
|
|
|
while($i < $this->size) { |
136
|
|
|
if($this->data[$i] === $data) { |
137
|
|
|
$aux = $this->data[$i]; |
138
|
|
|
array_splice($this->data, $i, 1); |
139
|
|
|
|
140
|
|
|
return $aux; |
141
|
|
|
} |
142
|
|
|
$i++; |
143
|
|
|
} |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Delete a node in the given position and returns it back. |
149
|
|
|
* |
150
|
|
|
* @param integer $index the position. |
151
|
|
|
* @throws OutOfBoundsException if index is negative |
152
|
|
|
* or is greater than the size of the list. |
153
|
|
|
*/ |
154
|
|
|
public function delete($index) { |
155
|
|
|
if($this->size === 0 || $index < 0 || $index > $this->size - 1) { |
156
|
|
|
throw new OutOfBoundsException(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$data = $this->data[$index]; |
160
|
|
|
array_splice($this->data, $index, 1); |
161
|
|
|
$this->size--; |
162
|
|
|
|
163
|
|
|
return $data; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
protected function deleteBeginning() {} |
167
|
|
|
|
168
|
|
|
protected function deleteAt($index) {} |
169
|
|
|
|
170
|
|
|
protected function deleteEnd() {} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns array stored in the data attribute. |
174
|
|
|
* |
175
|
|
|
* @return array data stored in all nodes. |
176
|
|
|
*/ |
177
|
|
|
public function toArray() : array { |
178
|
|
|
return $this->data; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Reset the cursor position. |
183
|
|
|
*/ |
184
|
|
|
public function rewind() { |
185
|
|
|
$this->position = 0; |
186
|
|
|
$this->current = $this->data[$this->position]; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the current node data. |
191
|
|
|
* |
192
|
|
|
* @return mixed |
193
|
|
|
*/ |
194
|
|
|
public function current() { |
195
|
|
|
return $this->current; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Key or index that indicates the cursor position. |
200
|
|
|
* |
201
|
|
|
* @return integer The current position. |
202
|
|
|
*/ |
203
|
|
|
public function key() { |
204
|
|
|
return $this->position; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Move the cursor to the next node and increments the |
209
|
|
|
* position counter. |
210
|
|
|
*/ |
211
|
|
|
public function next() { |
212
|
|
|
++$this->position; |
213
|
|
|
$this->current = $this->data[$this->position]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns if the current pointer position is valid. |
218
|
|
|
* |
219
|
|
|
* @return boolean true if pointer is not last, else false. |
220
|
|
|
*/ |
221
|
|
|
public function valid() { |
222
|
|
|
return $this->position < $this->size; |
223
|
|
|
} |
224
|
|
|
} |