1
|
|
|
<?php namespace Mbh\Collection\Traits; |
2
|
|
|
|
3
|
|
|
use Mbh\Collection\Interfaces\Collection as CollectionInterface; |
4
|
|
|
use Mbh\Collection\FixedArray; |
5
|
|
|
use Mbh\Collection\CallbackHeap; |
6
|
|
|
use Mbh\Iterator\SliceIterator; |
7
|
|
|
use Mbh\Iterator\ConcatIterator; |
8
|
|
|
use SplFixedArray; |
9
|
|
|
use SplHeap; |
10
|
|
|
use SplStack; |
11
|
|
|
use LimitIterator; |
12
|
|
|
use Iterator; |
13
|
|
|
use ArrayAccess; |
14
|
|
|
use Countable; |
15
|
|
|
use CallbackFilterIterator; |
16
|
|
|
use JsonSerializable; |
17
|
|
|
use RuntimeException; |
18
|
|
|
use Traversable; |
19
|
|
|
use ReflectionClass; |
20
|
|
|
use UnderflowException; |
21
|
|
|
use OutOfRangeException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* MBHFramework |
25
|
|
|
* |
26
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
27
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
28
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
trait ImmutableFunctional |
32
|
|
|
{ |
33
|
|
|
use Functional\ImmutableSort { |
34
|
|
|
Functional\ImmutableSort::heapSort as heapSortWithCallback; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function getSplFixedArrayAndSize() |
38
|
|
|
{ |
39
|
|
|
$count = $this->count(); |
40
|
|
|
return [new SplFixedArray($count), $count]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
|
|
public function any(callable $callback) |
47
|
|
|
{ |
48
|
|
|
list($sfa, $count) = $this->getSplFixedArrayAndSize(); |
49
|
|
|
|
50
|
|
|
for ($i = 0; $i < $count; $i++) { |
51
|
|
|
$sfa[$i] = $callback($this[$i], $i, $this); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $sfa; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
public function concat(...$args) |
61
|
|
|
{ |
62
|
|
|
array_unshift($args, $this); |
63
|
|
|
|
64
|
|
|
// Concat this iterator, and variadic args |
65
|
|
|
$class = new ReflectionClass('Mbh\Iterator\ConcatIterator'); |
66
|
|
|
$concatIt = $class->newInstanceArgs($args); |
67
|
|
|
|
68
|
|
|
// Create as new immutable's iterator |
69
|
|
|
return static::fromArray($concatIt->toArray()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
|
|
public function find(callable $callback) |
76
|
|
|
{ |
77
|
|
|
foreach ($this as $i => $elem) { |
78
|
|
|
if ($callback($elem, $i, $this)) { |
79
|
|
|
return $elem; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
|
|
public function filter(callable $callback) |
88
|
|
|
{ |
89
|
|
|
list($sfa, $count) = $this->getSplFixedArrayAndSize(); |
90
|
|
|
|
91
|
|
|
$newCount = 0; |
92
|
|
|
foreach ($this as $elem) { |
93
|
|
|
if ($callback($elem)) { |
94
|
|
|
$sfa[$newCount++] = $elem; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$sfa->setSize($newCount); |
99
|
|
|
return static::fromItems($sfa); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function heapSort(SplHeap $heap) |
106
|
|
|
{ |
107
|
|
|
foreach ($this as $item) { |
108
|
|
|
$heap->insert($item); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return static::fromItems($heap); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
|
|
public function join(string $token = ',', string $secondToken = null): string |
118
|
|
|
{ |
119
|
|
|
$str = ""; |
120
|
|
|
if ($secondToken !== null) { |
121
|
|
|
foreach ($this as $i => $elem) { |
122
|
|
|
$str .= $token . (string) $elem . $secondToken; |
123
|
|
|
} |
124
|
|
|
} else { |
125
|
|
|
$this->rewind(); |
126
|
|
|
while ($this->valid()) { |
127
|
|
|
$str .= (string) $this->current(); |
128
|
|
|
$this->next(); |
129
|
|
|
if ($this->valid()) { |
130
|
|
|
$str .= $token; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $str; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritDoc |
140
|
|
|
*/ |
141
|
|
|
public function map(callable $callback) |
142
|
|
|
{ |
143
|
|
|
list($sfa, $count) = $this->getSplFixedArrayAndSize(); |
144
|
|
|
|
145
|
|
|
for ($i = 0; $i < $count; $i++) { |
146
|
|
|
$sfa[$i] = $callback($this[$i], $i, $this); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return static::fromItems($sfa); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritDoc |
154
|
|
|
*/ |
155
|
|
|
public function reduce(callable $callback, $accumulator = null) |
156
|
|
|
{ |
157
|
|
|
foreach ($this as $i => $elem) { |
158
|
|
|
$accumulator = $callback($accumulator, $elem, $i, $this); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $accumulator; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritDoc |
166
|
|
|
*/ |
167
|
|
|
public function search($value) |
168
|
|
|
{ |
169
|
|
|
foreach ($this as $i => $elem) { |
170
|
|
|
if ($value === $elem) { |
171
|
|
|
return $i; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
|
|
public function slice(int $begin = 0, int $length = null) |
180
|
|
|
{ |
181
|
|
|
$end = $begin + $length; |
182
|
|
|
$it = new SliceIterator($this->getValues(), $begin, $end); |
183
|
|
|
return static::fromArray($it->toArray()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
|
|
public function splice(int $begin = 0, int $length = null, $replacement = []) |
190
|
|
|
{ |
191
|
|
|
$input = $this->toArray(); |
192
|
|
|
array_splice($input, $begin, $length, $replacement); |
193
|
|
|
return static::fromArray($input); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @inheritDoc |
198
|
|
|
*/ |
199
|
|
|
public function sort(callable $callback = null) |
200
|
|
|
{ |
201
|
|
|
if ($callback) { |
202
|
|
|
return $this->mergeSort($callback); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $this->arraySort(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @inheritDoc |
210
|
|
|
*/ |
211
|
|
|
public function walk(callable $callback) |
212
|
|
|
{ |
213
|
|
|
foreach ($this as $i => $elem) { |
214
|
|
|
$callback($elem, $i, $this); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
abstract public static function fromItems(Traversable $array); |
221
|
|
|
|
222
|
|
|
abstract protected function getValues(): Traversable; |
223
|
|
|
|
224
|
|
|
abstract public function count(): int; |
225
|
|
|
|
226
|
|
|
abstract public function current(); |
227
|
|
|
|
228
|
|
|
abstract public function next(); |
229
|
|
|
|
230
|
|
|
abstract public function rewind(); |
231
|
|
|
|
232
|
|
|
abstract public function valid(); |
233
|
|
|
} |
234
|
|
|
|