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\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
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* MBHFramework |
23
|
|
|
* |
24
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
25
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
26
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
trait Sequenceable |
30
|
|
|
{ |
31
|
|
|
use Collection; |
32
|
|
|
use Sort { |
33
|
|
|
Sort::heapSort as heapSortWithCallback; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Map elements to a new Sequenceable via a callback |
38
|
|
|
* |
39
|
|
|
* @param callable $callback Function to map new data |
40
|
|
|
* @return Sequenceable |
41
|
|
|
*/ |
42
|
|
|
public function map(callable $callback): SequenceableInterface |
43
|
|
|
{ |
44
|
|
|
$count = count($this); |
|
|
|
|
45
|
|
|
$sfa = new SplFixedArray($count); |
46
|
|
|
|
47
|
|
|
for ($i = 0; $i < $count; $i++) { |
48
|
|
|
$sfa[$i] = $callback($this[$i], $i, $this); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return new static($sfa); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* forEach, or "walk" the data |
56
|
|
|
* Exists primarily to provide a consistent interface, though it's seldom |
57
|
|
|
* any better than a simple php foreach. Mainly useful for chaining. |
58
|
|
|
* Named walk for historic reasons - forEach is reserved in PHP |
59
|
|
|
* |
60
|
|
|
* @param callable $callback Function to call on each element |
61
|
|
|
* @return Sequenceable |
62
|
|
|
*/ |
63
|
|
|
public function walk(callable $callback): SequenceableInterface |
64
|
|
|
{ |
65
|
|
|
foreach ($this as $i => $elem) { |
66
|
|
|
$callback($elem, $i, $this); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Filter out elements |
74
|
|
|
* |
75
|
|
|
* @param callable $callback Function to filter out on false |
76
|
|
|
* @return Sequenceable |
77
|
|
|
*/ |
78
|
|
|
public function filter(callable $callback): SequenceableInterface |
79
|
|
|
{ |
80
|
|
|
$count = count($this); |
|
|
|
|
81
|
|
|
$sfa = new SplFixedArray($count); |
82
|
|
|
$newCount = 0; |
83
|
|
|
|
84
|
|
|
foreach ($this as $elem) { |
85
|
|
|
if ($callback($elem)) { |
86
|
|
|
$sfa[$newCount++] = $elem; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$sfa->setSize($newCount); |
91
|
|
|
return new static($sfa); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Reduce to a single value |
96
|
|
|
* |
97
|
|
|
* @param callable $callback Callback( |
98
|
|
|
* mixed $previous, mixed $current[, mixed $index, mixed $immArray] |
99
|
|
|
* ):mixed Callback to run reducing function |
100
|
|
|
* @param mixed $accumulator Initial value for first argument |
101
|
|
|
*/ |
102
|
|
|
public function reduce(callable $callback, $accumulator = null) |
103
|
|
|
{ |
104
|
|
|
foreach ($this as $i => $elem) { |
105
|
|
|
$accumulator = $callback($accumulator, $elem, $i, $this); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $accumulator; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Join a set of strings together. |
113
|
|
|
* |
114
|
|
|
* @param string $token Main token to put between elements |
115
|
|
|
* @param string $secondToken If set, $token on left $secondToken on right |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function join(string $token = ',', string $secondToken = null): string |
119
|
|
|
{ |
120
|
|
|
$str = ""; |
121
|
|
|
if ($secondToken) { |
|
|
|
|
122
|
|
|
foreach ($this as $i => $elem) { |
123
|
|
|
$str .= $token . (string) $elem . $secondToken; |
124
|
|
|
} |
125
|
|
|
} else { |
126
|
|
|
$this->rewind(); |
|
|
|
|
127
|
|
|
while ($this->valid()) { |
|
|
|
|
128
|
|
|
$str .= (string) $this->current(); |
|
|
|
|
129
|
|
|
$this->next(); |
|
|
|
|
130
|
|
|
if ($this->valid()) { |
131
|
|
|
$str .= $token; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $str; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Take a slice of the array |
141
|
|
|
* |
142
|
|
|
* @param int $begin Start index of slice |
143
|
|
|
* @param int $end End index of slice |
144
|
|
|
* @return Sequenceable |
145
|
|
|
*/ |
146
|
|
|
public function slice(int $begin = 0, int $end = null): SequenceableInterface |
147
|
|
|
{ |
148
|
|
|
$it = new SliceIterator($this, $begin, $end); |
|
|
|
|
149
|
|
|
return new static($it); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Concat to the end of this array |
154
|
|
|
* |
155
|
|
|
* @param Traversable,... |
156
|
|
|
* @return Sequenceable |
157
|
|
|
*/ |
158
|
|
|
public function concat(): SequenceableInterface |
159
|
|
|
{ |
160
|
|
|
$args = func_get_args(); |
161
|
|
|
array_unshift($args, $this); |
162
|
|
|
|
163
|
|
|
// Concat this iterator, and variadic args |
164
|
|
|
$class = new ReflectionClass('Mbh\Iterator\ConcatIterator'); |
165
|
|
|
$concatIt = $class->newInstanceArgs($args); |
166
|
|
|
|
167
|
|
|
// Create as new immutable's iterator |
168
|
|
|
return new static($concatIt); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Find a single element |
173
|
|
|
* |
174
|
|
|
* @param callable $callback The test to run on each element |
175
|
|
|
* @return mixed The element we found |
176
|
|
|
*/ |
177
|
|
|
public function find(callable $callback) |
178
|
|
|
{ |
179
|
|
|
foreach ($this as $i => $elem) { |
180
|
|
|
if ($callback($elem, $i, $this)) { |
181
|
|
|
return $elem; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Return a new sorted Sequenceable |
188
|
|
|
* |
189
|
|
|
* @param callable $callback The sort callback |
190
|
|
|
* @return Sequenceable |
191
|
|
|
*/ |
192
|
|
|
public function sort(callable $callback = null) |
193
|
|
|
{ |
194
|
|
|
if ($callback) { |
195
|
|
|
return $this->mergeSort($callback); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this->arraySort(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Sort a new Sequenceable by filtering through a heap. |
203
|
|
|
* Tends to run much faster than array or merge sorts, since you're only |
204
|
|
|
* sorting the pointers, and the sort function is running in a highly |
205
|
|
|
* optimized space. |
206
|
|
|
* |
207
|
|
|
* @param SplHeap $heap The heap to run for sorting |
208
|
|
|
* @return Sequenceable |
209
|
|
|
*/ |
210
|
|
|
public function heapSort(SplHeap $heap): SequenceableInterface |
211
|
|
|
{ |
212
|
|
|
foreach ($this as $item) { |
213
|
|
|
$heap->insert($item); |
214
|
|
|
} |
215
|
|
|
return static::fromItems($heap); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Creates a shallow copy of the collection. |
220
|
|
|
* |
221
|
|
|
* @return CollectionInterface a shallow copy of the collection. |
222
|
|
|
*/ |
223
|
|
|
public function copy(): CollectionInterface |
224
|
|
|
{ |
225
|
|
|
return static::fromArray($this->toArray()); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|