1
|
|
|
<?php namespace Mbh\Collection\Traits; |
2
|
|
|
|
3
|
|
|
use Mbh\Collection\Interfaces\Collection as CollectionInterface; |
4
|
|
|
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface; |
5
|
|
|
use Mbh\Collection\FixedArray; |
6
|
|
|
use Mbh\Collection\CallbackHeap; |
7
|
|
|
use Mbh\Iterator\SliceIterator; |
8
|
|
|
use Mbh\Iterator\ConcatIterator; |
9
|
|
|
use SplFixedArray; |
10
|
|
|
use SplHeap; |
11
|
|
|
use SplStack; |
12
|
|
|
use LimitIterator; |
13
|
|
|
use Iterator; |
14
|
|
|
use ArrayAccess; |
15
|
|
|
use Countable; |
16
|
|
|
use CallbackFilterIterator; |
17
|
|
|
use JsonSerializable; |
18
|
|
|
use RuntimeException; |
19
|
|
|
use Traversable; |
20
|
|
|
use ReflectionClass; |
21
|
|
|
use UnderflowException; |
22
|
|
|
use OutOfRangeException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* MBHFramework |
26
|
|
|
* |
27
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
28
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
29
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
trait Sequenceable |
33
|
|
|
{ |
34
|
|
|
protected $sfa = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create an fixed array |
38
|
|
|
* |
39
|
|
|
* @param Traversable $array data |
40
|
|
|
*/ |
41
|
|
|
protected function __construct(Traversable $array) |
42
|
|
|
{ |
43
|
|
|
$this->sfa = $array; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
|
|
public static function fromArray(array $array) |
50
|
|
|
{ |
51
|
|
|
return new static(SplFixedArray::fromArray($array)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
|
|
public static function fromItems(Traversable $array) |
58
|
|
|
{ |
59
|
|
|
// We can only do it this way if we can count it |
60
|
|
|
if ($array instanceof Countable) { |
61
|
|
|
$sfa = new SplFixedArray(count($array)); |
62
|
|
|
|
63
|
|
|
foreach ($array as $i => $elem) { |
64
|
|
|
$sfa[$i] = $elem; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new static($sfa); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// If we can't count it, it's simplest to iterate into an array first |
71
|
|
|
return static::fromArray(iterator_to_array($array)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function contains(...$values): bool |
78
|
|
|
{ |
79
|
|
|
foreach ($values as $value) { |
80
|
|
|
if (!$this->find($value)) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
|
|
public function copy() |
92
|
|
|
{ |
93
|
|
|
return static::fromArray($this->toArray()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
|
|
public function first() |
100
|
|
|
{ |
101
|
|
|
if ($this->isEmpty()) { |
102
|
|
|
throw new UnderflowException(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this[0]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc |
110
|
|
|
*/ |
111
|
|
|
public function get(int $index) |
112
|
|
|
{ |
113
|
|
|
if (! $this->validIndex($index)) { |
114
|
|
|
throw new OutOfRangeException(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this[$index]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
|
|
public function insert(int $index, ...$values) |
124
|
|
|
{ |
125
|
|
|
if (!$this->validIndex($index) && $index !== $this->count()) { |
126
|
|
|
throw new OutOfRangeException(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$slice = $this->slice($index, $this->count()); |
|
|
|
|
130
|
|
|
$this->setSize($index); |
131
|
|
|
|
132
|
|
|
$this->pushAll($values); |
133
|
|
|
$this->pushAll($slice); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
*/ |
139
|
|
|
public function last() |
140
|
|
|
{ |
141
|
|
|
if ($this->isEmpty()) { |
142
|
|
|
throw new UnderflowException(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this[$this->count() - 1]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @inheritDoc |
150
|
|
|
*/ |
151
|
|
|
public function pop() |
152
|
|
|
{ |
153
|
|
|
if ($this->isEmpty()) { |
154
|
|
|
throw new UnderflowException(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$value = $this->last(); |
158
|
|
|
unset($this[$this->count() - 1]); |
159
|
|
|
|
160
|
|
|
$this->checkCapacity(); |
161
|
|
|
|
162
|
|
|
return $value; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Pushes all values of either an array or traversable object. |
167
|
|
|
*/ |
168
|
|
|
private function pushAll($values) |
169
|
|
|
{ |
170
|
|
|
$size = $this->getSize(); |
171
|
|
|
|
172
|
|
|
foreach ($values as $value) { |
173
|
|
|
$this->setSize(++$size); |
174
|
|
|
$this[$size - 1] = $value; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->checkCapacity(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritDoc |
182
|
|
|
*/ |
183
|
|
|
public function push(...$values) |
184
|
|
|
{ |
185
|
|
|
$this->pushAll($values); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function toArray(): array |
189
|
|
|
{ |
190
|
|
|
return $this->sfa->toArray(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function validIndex(int $index) |
194
|
|
|
{ |
195
|
|
|
return $index >= 0 && $index < $this->getSize(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Countable |
200
|
|
|
*/ |
201
|
|
|
public function count(): int |
202
|
|
|
{ |
203
|
|
|
return count($this->sfa); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Iterator |
208
|
|
|
*/ |
209
|
|
|
public function current() |
210
|
|
|
{ |
211
|
|
|
return $this->sfa->current(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function key(): int |
215
|
|
|
{ |
216
|
|
|
return $this->sfa->key(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function next() |
220
|
|
|
{ |
221
|
|
|
return $this->sfa->next(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function rewind() |
225
|
|
|
{ |
226
|
|
|
return $this->sfa->rewind(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function valid() |
230
|
|
|
{ |
231
|
|
|
return $this->sfa->valid(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* ArrayAccess |
236
|
|
|
*/ |
237
|
|
|
public function offsetExists($offset): bool |
238
|
|
|
{ |
239
|
|
|
return is_integer($offset) |
240
|
|
|
&& $this->validIndex($offset) |
241
|
|
|
&& $this->sfa->offsetExists($offset); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function offsetGet($offset) |
245
|
|
|
{ |
246
|
|
|
return $this->sfa->offsetGet($offset); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function offsetSet($offset, $value) |
250
|
|
|
{ |
251
|
|
|
if ($offset === null) { |
252
|
|
|
$this->push($value); |
253
|
|
|
} elseif (is_integer($offset) && $this->validIndex($offset)) { |
254
|
|
|
$this->sfa->offsetSet($offset, $value); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function offsetUnset($offset) |
259
|
|
|
{ |
260
|
|
|
return is_integer($offset) |
261
|
|
|
&& $this->validIndex($offset) |
262
|
|
|
&& $this->sfa->offsetUnset($offset); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function clear() |
266
|
|
|
{ |
267
|
|
|
return $this->sfa->clear(); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
protected function getMainTraversable(): Traversable |
271
|
|
|
{ |
272
|
|
|
return $this->sfa; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
protected function setTraversable(Traversable $traversable) |
276
|
|
|
{ |
277
|
|
|
$this->sfa = $traversable; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Gets the size of the array. |
282
|
|
|
* |
283
|
|
|
* @return int |
284
|
|
|
*/ |
285
|
|
|
protected function getSize(): int |
286
|
|
|
{ |
287
|
|
|
return $this->sfa->getSize(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Change the size of an array to the new size of size. |
292
|
|
|
* If size is less than the current array size, any values after the |
293
|
|
|
* new size will be discarded. If size is greater than the current |
294
|
|
|
* array size, the array will be padded with NULL values. |
295
|
|
|
* |
296
|
|
|
* @param int $size The new array size. This should be a value between 0 |
297
|
|
|
* and PHP_INT_MAX. |
298
|
|
|
* @return bool Returns TRUE on success or FALSE on failure. |
299
|
|
|
*/ |
300
|
|
|
protected function setSize(int $size): bool |
301
|
|
|
{ |
302
|
|
|
return $this->sfa->setSize($size); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
abstract protected function checkCapacity(); |
306
|
|
|
|
307
|
|
|
abstract public function isEmpty(): bool; |
308
|
|
|
|
309
|
|
|
abstract public function find(callable $callback); |
310
|
|
|
} |
311
|
|
|
|