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