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
|
|
|
public function toArray(): array |
47
|
|
|
{ |
48
|
|
|
return $this->sfa->toArray(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function validIndex(int $index) |
52
|
|
|
{ |
53
|
|
|
return $index >= 0 && $index < count($this); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Countable |
58
|
|
|
*/ |
59
|
|
|
public function count(): int |
60
|
|
|
{ |
61
|
|
|
return count($this->sfa); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Iterator |
66
|
|
|
*/ |
67
|
|
|
public function current() |
68
|
|
|
{ |
69
|
|
|
return $this->sfa->current(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function key(): int |
73
|
|
|
{ |
74
|
|
|
return $this->sfa->key(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function next() |
78
|
|
|
{ |
79
|
|
|
return $this->sfa->next(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function rewind() |
83
|
|
|
{ |
84
|
|
|
return $this->sfa->rewind(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function valid() |
88
|
|
|
{ |
89
|
|
|
return $this->sfa->valid(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* ArrayAccess |
94
|
|
|
*/ |
95
|
|
|
public function offsetExists($offset): bool |
96
|
|
|
{ |
97
|
|
|
return is_integer($offset) |
98
|
|
|
&& $this->validIndex($offset) |
99
|
|
|
&& $this->sfa->offsetExists($offset); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function offsetGet($offset) |
103
|
|
|
{ |
104
|
|
|
return $this->sfa->offsetGet($offset); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function offsetSet($offset, $value) |
108
|
|
|
{ |
109
|
|
|
return is_integer($offset) |
110
|
|
|
&& $this->validIndex($offset) |
111
|
|
|
&& $this->sfa->offsetSet($offset, $value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function offsetUnset($offset) |
115
|
|
|
{ |
116
|
|
|
return is_integer($offset) |
117
|
|
|
&& $this->validIndex($offset) |
118
|
|
|
&& $this->sfa->offsetUnset($offset); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function clear() |
122
|
|
|
{ |
123
|
|
|
return $this->sfa->clear(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function getMainTraversable(): Traversable |
127
|
|
|
{ |
128
|
|
|
return $this->sfa; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function setTraversable(Traversable $traversable) |
132
|
|
|
{ |
133
|
|
|
$this->sfa = $traversable; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
*/ |
139
|
|
|
public static function fromItems(Traversable $array) |
140
|
|
|
{ |
141
|
|
|
// We can only do it this way if we can count it |
142
|
|
|
if ($array instanceof Countable) { |
143
|
|
|
$sfa = new SplFixedArray(count($array)); |
144
|
|
|
|
145
|
|
|
foreach ($array as $i => $elem) { |
146
|
|
|
$sfa[$i] = $elem; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return new static($sfa); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// If we can't count it, it's simplest to iterate into an array first |
153
|
|
|
return static::fromArray(iterator_to_array($array)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
|
|
public static function fromArray(array $array) |
160
|
|
|
{ |
161
|
|
|
return new static(SplFixedArray::fromArray($array)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritDoc |
166
|
|
|
*/ |
167
|
|
|
public function copy() |
168
|
|
|
{ |
169
|
|
|
return static::fromArray($this->toArray()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritDoc |
174
|
|
|
*/ |
175
|
|
|
public function contains(...$values): bool |
176
|
|
|
{ |
177
|
|
|
foreach ($values as $value) { |
178
|
|
|
if (!$this->find($value)) { |
|
|
|
|
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return true; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
|
|
public function first() |
190
|
|
|
{ |
191
|
|
|
if ($this->isEmpty()) { |
|
|
|
|
192
|
|
|
throw new UnderflowException(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $this[0]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @inheritDoc |
200
|
|
|
*/ |
201
|
|
|
public function get(int $index) |
202
|
|
|
{ |
203
|
|
|
if (! $this->validIndex($index)) { |
204
|
|
|
throw new OutOfRangeException(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this[$index]; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @inheritDoc |
212
|
|
|
*/ |
213
|
|
|
public function last() |
214
|
|
|
{ |
215
|
|
|
if ($this->isEmpty()) { |
216
|
|
|
throw new UnderflowException(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $this[count($this) - 1]; |
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
abstract protected function checkCapacity(); |
223
|
|
|
} |
224
|
|
|
|