|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Immutable; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @template T |
|
8
|
|
|
* @psalm-immutable |
|
9
|
|
|
*/ |
|
10
|
|
|
final class Set implements \Countable |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var Set\Implementation<T> */ |
|
13
|
|
|
private Set\Implementation $implementation; |
|
14
|
|
|
|
|
15
|
|
|
private function __construct(Set\Implementation $implementation) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->implementation = $implementation; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
78 |
|
/** |
|
21
|
|
|
* Add an element to the set |
|
22
|
78 |
|
* |
|
23
|
78 |
|
* Example: |
|
24
|
78 |
|
* <code> |
|
25
|
78 |
|
* Set::of()(1)(3) |
|
26
|
|
|
* </code> |
|
27
|
|
|
* |
|
28
|
|
|
* @param T $element |
|
|
|
|
|
|
29
|
|
|
* |
|
30
|
|
|
* @return self<T> |
|
31
|
|
|
*/ |
|
32
|
66 |
|
public function __invoke($element): self |
|
33
|
|
|
{ |
|
34
|
66 |
|
return new self(($this->implementation)($element)); |
|
35
|
|
|
} |
|
36
|
66 |
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @template V |
|
39
|
|
|
* @no-named-arguments |
|
40
|
|
|
* @psalm-pure |
|
41
|
|
|
* |
|
42
|
|
|
* @param V $values |
|
|
|
|
|
|
43
|
|
|
* |
|
44
|
3 |
|
* @return self<V> |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public static function of(...$values): self |
|
47
|
|
|
{ |
|
48
|
3 |
|
return new self(Set\Primitive::of(...$values)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* It will load the values inside the generator only upon the first use |
|
53
|
|
|
* of the set |
|
54
|
|
|
* |
|
55
|
|
|
* Use this mode when the amount of data may not fit in memory |
|
56
|
2 |
|
* |
|
57
|
|
|
* @template V |
|
58
|
|
|
* @psalm-pure |
|
59
|
2 |
|
* |
|
60
|
|
|
* @param \Generator<V> $generator |
|
61
|
2 |
|
* |
|
62
|
|
|
* @return self<V> |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function defer(\Generator $generator): self |
|
65
|
|
|
{ |
|
66
|
|
|
return new self(Set\Defer::of($generator)); |
|
67
|
6 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
6 |
|
* It will call the given function every time a new operation is done on the |
|
71
|
|
|
* set. This means the returned structure may not be truly immutable as |
|
72
|
6 |
|
* between the calls the underlying source may change. |
|
73
|
|
|
* |
|
74
|
|
|
* Use this mode when calling to an external source (meaning IO bound) such |
|
75
|
|
|
* as parsing a file or calling an API |
|
76
|
|
|
* |
|
77
|
|
|
* @template V |
|
78
|
1 |
|
* @psalm-pure |
|
79
|
|
|
* @psalm-type RegisterCleanup = callable(callable(): void): void |
|
80
|
|
|
* |
|
81
|
1 |
|
* @param callable(RegisterCleanup): \Generator<V> $generator |
|
82
|
|
|
* |
|
83
|
1 |
|
* @return self<V> |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function lazy(callable $generator): self |
|
86
|
|
|
{ |
|
87
|
|
|
return new self(Set\Lazy::of($generator)); |
|
88
|
|
|
} |
|
89
|
1 |
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @no-named-arguments |
|
92
|
1 |
|
* @psalm-pure |
|
93
|
|
|
* |
|
94
|
1 |
|
* @return self<mixed> |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function mixed(mixed ...$values): self |
|
97
|
|
|
{ |
|
98
|
|
|
return new self(Set\Primitive::of(...$values)); |
|
99
|
|
|
} |
|
100
|
1 |
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @no-named-arguments |
|
103
|
1 |
|
* @psalm-pure |
|
104
|
|
|
* |
|
105
|
1 |
|
* @return self<int> |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function ints(int ...$values): self |
|
108
|
22 |
|
{ |
|
109
|
|
|
/** @var self<int> */ |
|
110
|
22 |
|
$self = new self(Set\Primitive::of(...$values)); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
return $self; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
24 |
|
* @no-named-arguments |
|
117
|
|
|
* @psalm-pure |
|
118
|
24 |
|
* |
|
119
|
|
|
* @return self<float> |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public static function floats(float ...$values): self |
|
122
|
|
|
{ |
|
123
|
1 |
|
/** @var self<float> */ |
|
124
|
|
|
$self = new self(Set\Primitive::of(...$values)); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
return $self; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
/** |
|
130
|
|
|
* @no-named-arguments |
|
131
|
1 |
|
* @psalm-pure |
|
132
|
|
|
* |
|
133
|
|
|
* @return self<string> |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function strings(string ...$values): self |
|
136
|
|
|
{ |
|
137
|
|
|
/** @var self<string> */ |
|
138
|
|
|
$self = new self(Set\Primitive::of(...$values)); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
return $self; |
|
141
|
2 |
|
} |
|
142
|
|
|
|
|
143
|
2 |
|
/** |
|
144
|
|
|
* @no-named-arguments |
|
145
|
1 |
|
* @psalm-pure |
|
146
|
1 |
|
* |
|
147
|
1 |
|
* @return self<object> |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function objects(object ...$values): self |
|
150
|
1 |
|
{ |
|
151
|
|
|
/** @var self<object> */ |
|
152
|
|
|
$self = new self(Set\Primitive::of(...$values)); |
|
153
|
|
|
|
|
154
|
|
|
return $self; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function size(): int |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->implementation->size(); |
|
160
|
46 |
|
} |
|
161
|
|
|
|
|
162
|
46 |
|
public function count(): int |
|
163
|
|
|
{ |
|
164
|
45 |
|
return $this->implementation->size(); |
|
165
|
45 |
|
} |
|
166
|
|
|
|
|
167
|
45 |
|
/** |
|
168
|
|
|
* Intersect this set with the given one |
|
169
|
|
|
* |
|
170
|
|
|
* @param self<T> $set |
|
171
|
|
|
* |
|
172
|
|
|
* @return self<T> |
|
173
|
|
|
*/ |
|
174
|
|
|
public function intersect(self $set): self |
|
175
|
|
|
{ |
|
176
|
|
|
return new self($this->implementation->intersect( |
|
177
|
|
|
$set->implementation, |
|
178
|
|
|
)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Add an element to the set |
|
183
|
31 |
|
* |
|
184
|
|
|
* @param T $element |
|
185
|
31 |
|
* |
|
186
|
|
|
* @return self<T> |
|
187
|
|
|
*/ |
|
188
|
|
|
public function add($element): self |
|
189
|
|
|
{ |
|
190
|
|
|
return ($this)($element); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
/** |
|
194
|
|
|
* Check if the set contains the given element |
|
195
|
1 |
|
* |
|
196
|
|
|
* @param T $element |
|
197
|
1 |
|
*/ |
|
198
|
|
|
public function contains($element): bool |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->implementation->contains($element); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Remove the element from the set |
|
205
|
|
|
* |
|
206
|
|
|
* @param T $element |
|
207
|
1 |
|
* |
|
208
|
|
|
* @return self<T> |
|
209
|
1 |
|
*/ |
|
210
|
|
|
public function remove($element): self |
|
211
|
1 |
|
{ |
|
212
|
1 |
|
return new self($this->implementation->remove($element)); |
|
213
|
|
|
} |
|
214
|
1 |
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Return the diff between this set and the given one |
|
217
|
|
|
* |
|
218
|
|
|
* @param self<T> $set |
|
219
|
|
|
* |
|
220
|
|
|
* @return self<T> |
|
221
|
|
|
*/ |
|
222
|
|
|
public function diff(self $set): self |
|
223
|
|
|
{ |
|
224
|
2 |
|
return new self($this->implementation->diff( |
|
225
|
|
|
$set->implementation, |
|
226
|
2 |
|
)); |
|
227
|
|
|
} |
|
228
|
1 |
|
|
|
229
|
1 |
|
/** |
|
230
|
1 |
|
* Check if the given set is identical to this one |
|
231
|
|
|
* |
|
232
|
|
|
* @param self<T> $set |
|
233
|
1 |
|
*/ |
|
234
|
|
|
public function equals(self $set): bool |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->implementation->equals($set->implementation); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Return all elements that satisfy the given predicate |
|
241
|
10 |
|
* |
|
242
|
|
|
* @param callable(T): bool $predicate |
|
243
|
10 |
|
* |
|
244
|
|
|
* @return self<T> |
|
245
|
9 |
|
*/ |
|
246
|
|
|
public function filter(callable $predicate): self |
|
247
|
|
|
{ |
|
248
|
|
|
return new self($this->implementation->filter($predicate)); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Apply the given function to all elements of the set |
|
253
|
|
|
* |
|
254
|
|
|
* @param callable(T): void $function |
|
255
|
1 |
|
*/ |
|
256
|
|
|
public function foreach(callable $function): SideEffect |
|
257
|
1 |
|
{ |
|
258
|
1 |
|
return $this->implementation->foreach($function); |
|
259
|
|
|
} |
|
260
|
1 |
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Return a new map of pairs grouped by keys determined with the given |
|
263
|
|
|
* discriminator function |
|
264
|
|
|
* |
|
265
|
|
|
* @template D |
|
266
|
|
|
* |
|
267
|
|
|
* @param callable(T): D $discriminator |
|
268
|
1 |
|
* |
|
269
|
|
|
* @return Map<D, self<T>> |
|
270
|
1 |
|
*/ |
|
271
|
1 |
|
public function groupBy(callable $discriminator): Map |
|
272
|
|
|
{ |
|
273
|
|
|
return $this->implementation->groupBy($discriminator); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Return a new set by applying the given function to all elements |
|
278
|
|
|
* |
|
279
|
|
|
* @template S |
|
280
|
|
|
* |
|
281
|
|
|
* @param callable(T): S $function |
|
282
|
|
|
* |
|
283
|
|
|
* @return self<S> |
|
284
|
1 |
|
*/ |
|
285
|
|
|
public function map(callable $function): self |
|
286
|
1 |
|
{ |
|
287
|
|
|
return new self($this->implementation->map($function)); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Merge all sets created by each value from the original set |
|
292
|
|
|
* |
|
293
|
|
|
* @template S |
|
294
|
|
|
* |
|
295
|
|
|
* @param callable(T): self<S> $map |
|
296
|
2 |
|
* |
|
297
|
|
|
* @return self<S> |
|
298
|
2 |
|
*/ |
|
299
|
2 |
|
public function flatMap(callable $map): self |
|
300
|
|
|
{ |
|
301
|
1 |
|
/** |
|
302
|
|
|
* @psalm-suppress InvalidArgument |
|
303
|
|
|
* @psalm-suppress MixedArgument |
|
304
|
|
|
*/ |
|
305
|
|
|
return $this->reduce( |
|
306
|
|
|
self::of(), |
|
307
|
|
|
static fn(self $carry, $value) => $carry->merge($map($value)), |
|
308
|
|
|
); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
1 |
|
/** |
|
312
|
|
|
* Return a sequence of 2 sets partitioned according to the given predicate |
|
313
|
1 |
|
* |
|
314
|
|
|
* @param callable(T): bool $predicate |
|
315
|
|
|
* |
|
316
|
|
|
* @return Map<bool, self<T>> |
|
317
|
|
|
*/ |
|
318
|
|
|
public function partition(callable $predicate): Map |
|
319
|
|
|
{ |
|
320
|
|
|
return $this->implementation->partition($predicate); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
1 |
|
/** |
|
324
|
|
|
* Return a sequence sorted with the given function |
|
325
|
1 |
|
* |
|
326
|
|
|
* @param callable(T, T): int $function |
|
327
|
|
|
* |
|
328
|
|
|
* @return Sequence<T> |
|
329
|
|
|
*/ |
|
330
|
|
|
public function sort(callable $function): Sequence |
|
331
|
|
|
{ |
|
332
|
|
|
return $this->implementation->sort($function); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
2 |
|
/** |
|
336
|
|
|
* Create a new set with elements of both sets |
|
337
|
2 |
|
* |
|
338
|
|
|
* @param self<T> $set |
|
339
|
1 |
|
* |
|
340
|
1 |
|
* @return self<T> |
|
341
|
1 |
|
*/ |
|
342
|
|
|
public function merge(self $set): self |
|
343
|
|
|
{ |
|
344
|
1 |
|
return new self($this->implementation->merge( |
|
345
|
|
|
$set->implementation, |
|
346
|
|
|
)); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Reduce the set to a single value |
|
351
|
|
|
* |
|
352
|
|
|
* @template R |
|
353
|
|
|
* |
|
354
|
|
|
* @param R $carry |
|
355
|
|
|
* @param callable(R, T): R $reducer |
|
356
|
60 |
|
* |
|
357
|
|
|
* @return R |
|
358
|
60 |
|
*/ |
|
359
|
|
|
public function reduce($carry, callable $reducer) |
|
360
|
|
|
{ |
|
361
|
|
|
return $this->implementation->reduce($carry, $reducer); |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Return a set of the same type but without any value |
|
366
|
2 |
|
* |
|
367
|
|
|
* @return self<T> |
|
368
|
2 |
|
*/ |
|
369
|
2 |
|
public function clear(): self |
|
370
|
|
|
{ |
|
371
|
2 |
|
return new self($this->implementation->clear()); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
3 |
|
public function empty(): bool |
|
375
|
|
|
{ |
|
376
|
3 |
|
return $this->implementation->empty(); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* @param callable(T): bool $predicate |
|
381
|
|
|
* |
|
382
|
|
|
* @return Maybe<T> |
|
383
|
|
|
*/ |
|
384
|
|
|
public function find(callable $predicate): Maybe |
|
385
|
|
|
{ |
|
386
|
1 |
|
return $this->implementation->find($predicate); |
|
387
|
|
|
} |
|
388
|
1 |
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* @param callable(T): bool $predicate |
|
391
|
|
|
*/ |
|
392
|
|
|
public function matches(callable $predicate): bool |
|
393
|
|
|
{ |
|
394
|
|
|
/** @psalm-suppress MixedArgument */ |
|
395
|
|
|
return $this->reduce( |
|
|
|
|
|
|
396
|
|
|
true, |
|
|
|
|
|
|
397
|
|
|
static fn(bool $matches, $value): bool => $matches && $predicate($value), |
|
398
|
1 |
|
); |
|
399
|
|
|
} |
|
400
|
1 |
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* @param callable(T): bool $predicate |
|
403
|
|
|
*/ |
|
404
|
|
|
public function any(callable $predicate): bool |
|
405
|
|
|
{ |
|
406
|
|
|
return $this->find($predicate)->match( |
|
|
|
|
|
|
407
|
|
|
static fn() => true, |
|
408
|
|
|
static fn() => false, |
|
409
|
|
|
); |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
1 |
|
* @return list<T> |
|
|
|
|
|
|
414
|
|
|
*/ |
|
415
|
1 |
|
public function toList(): array |
|
416
|
|
|
{ |
|
417
|
|
|
/** |
|
418
|
|
|
* @psalm-suppress MixedAssignment |
|
419
|
|
|
* @var list<T> |
|
420
|
|
|
*/ |
|
421
|
|
|
return $this->reduce( |
|
|
|
|
|
|
422
|
|
|
[], |
|
|
|
|
|
|
423
|
|
|
static function(array $carry, $value): array { |
|
424
|
|
|
$carry[] = $value; |
|
425
|
|
|
|
|
426
|
|
|
return $carry; |
|
427
|
|
|
}, |
|
428
|
|
|
); |
|
429
|
|
|
} |
|
430
|
|
|
} |
|
431
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths