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 fromItems(Traversable $array) |
50
|
|
|
{ |
51
|
|
|
// We can only do it this way if we can count it |
52
|
|
|
if ($array instanceof Countable) { |
53
|
|
|
$sfa = new SplFixedArray(count($array)); |
54
|
|
|
|
55
|
|
|
foreach ($array as $i => $elem) { |
56
|
|
|
$sfa[$i] = $elem; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return new static($sfa); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// If we can't count it, it's simplest to iterate into an array first |
63
|
|
|
return static::fromArray(iterator_to_array($array)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public static function fromArray(array $array) |
70
|
|
|
{ |
71
|
|
|
return new static(SplFixedArray::fromArray($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 !== count($this)) { |
|
|
|
|
126
|
|
|
throw new OutOfRangeException(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// array_splice($this->array, $index, 0, $values); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
*/ |
135
|
|
|
public function last() |
136
|
|
|
{ |
137
|
|
|
if ($this->isEmpty()) { |
138
|
|
|
throw new UnderflowException(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this[count($this) - 1]; |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritDoc |
146
|
|
|
*/ |
147
|
|
|
public function pop() |
148
|
|
|
{ |
149
|
|
|
if ($this->isEmpty()) { |
150
|
|
|
throw new UnderflowException(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$value = $this->last(); |
154
|
|
|
unset($this[count($this) - 1]); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$this->checkCapacity(); |
157
|
|
|
|
158
|
|
|
return $value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Pushes all values of either an array or traversable object. |
164
|
|
|
*/ |
165
|
|
|
private function pushAll($values) |
166
|
|
|
{ |
167
|
|
|
foreach ($values as $value) { |
168
|
|
|
$size = $this->getSize(); |
169
|
|
|
$this->setSize($size + 1); |
170
|
|
|
$this[$size] = $value; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->checkCapacity(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
|
|
public function push(...$values) |
180
|
|
|
{ |
181
|
|
|
$this->pushAll($values); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function toArray(): array |
185
|
|
|
{ |
186
|
|
|
return $this->sfa->toArray(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
protected function validIndex(int $index) |
190
|
|
|
{ |
191
|
|
|
return $index >= 0 && $index < $this->getSize(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Countable |
196
|
|
|
*/ |
197
|
|
|
public function count(): int |
198
|
|
|
{ |
199
|
|
|
return count($this->sfa); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Iterator |
204
|
|
|
*/ |
205
|
|
|
public function current() |
206
|
|
|
{ |
207
|
|
|
return $this->sfa->current(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function key(): int |
211
|
|
|
{ |
212
|
|
|
return $this->sfa->key(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function next() |
216
|
|
|
{ |
217
|
|
|
return $this->sfa->next(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function rewind() |
221
|
|
|
{ |
222
|
|
|
return $this->sfa->rewind(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function valid() |
226
|
|
|
{ |
227
|
|
|
return $this->sfa->valid(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* ArrayAccess |
232
|
|
|
*/ |
233
|
|
|
public function offsetExists($offset): bool |
234
|
|
|
{ |
235
|
|
|
return is_integer($offset) |
236
|
|
|
&& $this->validIndex($offset) |
237
|
|
|
&& $this->sfa->offsetExists($offset); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function offsetGet($offset) |
241
|
|
|
{ |
242
|
|
|
return $this->sfa->offsetGet($offset); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function offsetSet($offset, $value) |
246
|
|
|
{ |
247
|
|
|
if ($offset === null) { |
248
|
|
|
$this->push($value); |
249
|
|
|
} elseif (is_integer($offset) && $this->validIndex($offset)) { |
250
|
|
|
$this->sfa->offsetSet($offset, $value); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function offsetUnset($offset) |
255
|
|
|
{ |
256
|
|
|
return is_integer($offset) |
257
|
|
|
&& $this->validIndex($offset) |
258
|
|
|
&& $this->sfa->offsetUnset($offset); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function clear() |
262
|
|
|
{ |
263
|
|
|
return $this->sfa->clear(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
protected function getMainTraversable(): Traversable |
267
|
|
|
{ |
268
|
|
|
return $this->sfa; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
protected function setTraversable(Traversable $traversable) |
272
|
|
|
{ |
273
|
|
|
$this->sfa = $traversable; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function getSize(): int |
277
|
|
|
{ |
278
|
|
|
return $this->sfa->getSize(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function setSize(int $size): bool |
282
|
|
|
{ |
283
|
|
|
return $this->sfa->setSize($size); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
abstract protected function checkCapacity(); |
287
|
|
|
} |
288
|
|
|
|