/
IsKeyedEnumerable.php
875 lines (743 loc) · 24 KB
/
IsKeyedEnumerable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
<?php
declare(strict_types=1);
namespace Elephox\Collection;
use AppendIterator;
use ArrayIterator;
use CachingIterator;
use CallbackFilterIterator;
use Countable;
use Elephox\Collection\Contract\GenericEnumerable;
use Elephox\Collection\Contract\GenericKeyedEnumerable;
use Elephox\Collection\Contract\GenericOrderedEnumerable;
use Elephox\Collection\Iterator\FlipIterator;
use Elephox\Collection\Iterator\KeySelectIterator;
use Elephox\Collection\Iterator\OrderedIterator;
use Elephox\Collection\Iterator\ReverseIterator;
use Elephox\Collection\Iterator\SelectIterator;
use Elephox\Collection\Iterator\UniqueByIterator;
use Elephox\Collection\Iterator\WhileIterator;
use EmptyIterator;
use InvalidArgumentException;
use Iterator;
use LimitIterator;
use MultipleIterator as ParallelIterator;
use NoRewindIterator;
use OutOfBoundsException;
use Stringable;
/**
* @psalm-suppress LessSpecificImplementedReturnType Psalm thinks TIteratorKey is always int...
*
* @psalm-type NonNegativeInteger = 0|positive-int
*
* @template TIteratorKey
* @template TSource
*/
trait IsKeyedEnumerable
{
// TODO: rewrite more functions to use iterators
/**
* @return Iterator<TIteratorKey, TSource>
*/
abstract public function getIterator(): Iterator;
/**
* @template TAccumulate
*
* @param callable(TAccumulate|null, TSource, TIteratorKey): TAccumulate $accumulator
* @param TAccumulate|null $seed
*
* @return TAccumulate
*/
public function aggregate(callable $accumulator, mixed $seed = null): mixed
{
$result = $seed;
foreach ($this->getIterator() as $elementKey => $element) {
$result = $accumulator($result, $element, $elementKey);
}
return $result;
}
public function all(callable $predicate): bool
{
foreach ($this->getIterator() as $elementKey => $element) {
if (!$predicate($element, $elementKey)) {
return false;
}
}
return true;
}
public function any(callable $predicate = null): bool
{
foreach ($this->getIterator() as $elementKey => $element) {
if ($predicate === null || $predicate($element, $elementKey)) {
return true;
}
}
return false;
}
public function append(mixed $key, mixed $value): GenericKeyedEnumerable
{
return new KeyedEnumerable(function () use ($value, $key) {
yield from $this->getIterator();
yield $key => $value;
});
}
/**
* @param callable(TSource, TIteratorKey): numeric $selector
*
* @return numeric
*/
public function average(callable $selector): int|float|string
{
$sum = null;
$count = 0;
foreach ($this->getIterator() as $elementKey => $element) {
$value = $selector($element, $elementKey);
/** @var null|numeric $sum */
if ($sum === null) {
$sum = $value;
} else {
$sum += $value;
}
$count++;
}
if ($count === 0) {
throw new EmptySequenceException();
}
/** @var numeric $sum */
return $sum / $count;
}
/**
* @param NonNegativeInteger $size
*
* @return GenericEnumerable<non-empty-list<TSource>>
*/
public function chunk(int $size): GenericEnumerable
{
if ($size <= 0) {
throw new InvalidArgumentException('Chunk size must be greater than zero.');
}
/** @var GenericEnumerable<non-empty-list<TSource>> */
return new Enumerable(function () use ($size) {
$chunk = [];
foreach ($this->getIterator() as $element) {
if (count($chunk) === $size) {
yield $chunk;
$chunk = [$element];
} else {
$chunk[] = $element;
}
}
if ($chunk) {
yield $chunk;
}
});
}
public function concat(GenericKeyedEnumerable ...$other): GenericKeyedEnumerable
{
return new KeyedEnumerable(function () use ($other) {
yield from $this;
foreach ($other as $enumerable) {
yield from $enumerable;
}
});
}
public function contains(mixed $value, ?callable $comparer = null): bool
{
$comparer ??= DefaultEqualityComparer::same(...);
foreach ($this->getIterator() as $element) {
if ($comparer($value, $element)) {
return true;
}
}
return false;
}
/**
* @psalm-suppress MoreSpecificImplementedParamType Psalm thinks the template params are set from OrderedEnumerable...
*
* @param null|callable(TSource, TIteratorKey, Iterator<TIteratorKey, TSource>): bool $predicate
*
* @return NonNegativeInteger
*/
public function count(callable $predicate = null): int
{
if ($predicate === null) {
$iterator = $this->getIterator();
if ($iterator instanceof Countable) {
/** @var NonNegativeInteger */
return $iterator->count();
}
} else {
$iterator = new CallbackFilterIterator($this->getIterator(), $predicate);
}
return iterator_count($iterator);
}
/**
* @param null|callable(TSource, TSource): bool $comparer
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function distinct(?callable $comparer = null): GenericKeyedEnumerable
{
$comparer ??= DefaultEqualityComparer::same(...);
$identity = static fn(mixed $element): mixed => $element;
/**
* @var Closure(TSource, TSource): bool $comparer
* @var Closure(TSource): TSource $identity
*/
return $this->distinctBy($identity, $comparer);
}
/**
* @template TCompareKey
*
* @param callable(TSource): TCompareKey $keySelector
* @param null|callable(TCompareKey, TCompareKey): bool $comparer
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function distinctBy(callable $keySelector, ?callable $comparer = null): GenericKeyedEnumerable
{
$comparer ??= DefaultEqualityComparer::same(...);
/**
* @var Closure(TSource, TSource): bool $comparer
* @var Closure(TSource): TSource $keySelector
*/
return new KeyedEnumerable(new UniqueByIterator($this->getIterator(), $keySelector(...), $comparer(...)));
}
/**
* @param GenericKeyedEnumerable<TSource, TIteratorKey> $other
* @param null|callable(TSource, TSource): bool $comparer
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function except(GenericKeyedEnumerable $other, ?callable $comparer = null): GenericKeyedEnumerable
{
$comparer ??= DefaultEqualityComparer::same(...);
return $this->exceptBy($other, fn (mixed $element): mixed => $element, $comparer);
}
/**
* @template TCompareKey
*
* @param GenericKeyedEnumerable<TIteratorKey, TSource> $other
* @param callable(TSource, TIteratorKey): TCompareKey $keySelector
* @param null|callable(TCompareKey, TCompareKey): bool $comparer
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function exceptBy(GenericKeyedEnumerable $other, callable $keySelector, ?callable $comparer = null): GenericKeyedEnumerable
{
$comparer ??= DefaultEqualityComparer::same(...);
return new KeyedEnumerable(function () use ($other, $keySelector, $comparer) {
/** @var Iterator<TCompareKey, TSource> $otherKeys */
$otherKeys = new CachingIterator(new SelectIterator($other->getIterator(), $keySelector(...)), CachingIterator::FULL_CACHE);
foreach ($this->getIterator() as $elementKey => $element) {
$key = $keySelector($element, $elementKey);
foreach ($otherKeys as $otherKey) {
if ($comparer($key, $otherKey)) {
continue 2;
}
}
yield $elementKey => $element;
}
});
}
public function first(?callable $predicate = null): mixed
{
foreach ($this->getIterator() as $elementKey => $element) {
if ($predicate === null || $predicate($element, $elementKey)) {
return $element;
}
}
throw new EmptySequenceException();
}
public function firstOrDefault(mixed $defaultValue, ?callable $predicate = null): mixed
{
foreach ($this->getIterator() as $elementKey => $element) {
if ($predicate === null || $predicate($element, $elementKey)) {
return $element;
}
}
return $defaultValue;
}
public function flip(): GenericKeyedEnumerable
{
return new KeyedEnumerable(new FlipIterator($this->getIterator()));
}
/**
* @param GenericKeyedEnumerable<TSource, TIteratorKey> $other
* @param null|callable(TSource, TSource): bool $comparer
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function intersect(GenericKeyedEnumerable $other, ?callable $comparer = null): GenericKeyedEnumerable
{
$comparer ??= DefaultEqualityComparer::same(...);
return $this->intersectBy($other, fn ($element): mixed => $element, $comparer);
}
/**
* @template TKey
*
* @param GenericKeyedEnumerable<TIteratorKey, TSource> $other
* @param callable(TSource, TIteratorKey): TKey $keySelector
* @param null|callable(TSource, TSource): bool $comparer
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function intersectBy(GenericKeyedEnumerable $other, callable $keySelector, ?callable $comparer = null): GenericKeyedEnumerable
{
$comparer ??= DefaultEqualityComparer::same(...);
return new KeyedEnumerable(function () use ($other, $keySelector, $comparer) {
$otherKeys = [];
foreach ($other->getIterator() as $otherElementKey => $otherElement) {
$otherKeys[] = $keySelector($otherElement, $otherElementKey);
}
foreach ($this->getIterator() as $elementKey => $element) {
$key = $keySelector($element, $elementKey);
foreach ($otherKeys as $otherKey) {
if ($comparer($key, $otherKey)) {
yield $elementKey => $element;
continue 2;
}
}
}
});
}
public function isEmpty(): bool
{
return $this->count() === 0;
}
/**
* @template TInner
* @template TInnerIteratorKey
* @template TCompareKey
* @template TResult
*
* @param GenericKeyedEnumerable<TInnerIteratorKey, TInner> $inner
* @param callable(TSource, TIteratorKey): TCompareKey $outerKeySelector
* @param callable(TInner, TInnerIteratorKey): TCompareKey $innerKeySelector
* @param callable(TSource, TInner, TIteratorKey, TInnerIteratorKey): TResult $resultSelector
* @param null|callable(TCompareKey, TCompareKey): bool $comparer
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function join(GenericKeyedEnumerable $inner, callable $outerKeySelector, callable $innerKeySelector, callable $resultSelector, ?callable $comparer = null): GenericKeyedEnumerable
{
$comparer ??= DefaultEqualityComparer::same(...);
return new KeyedEnumerable(function () use ($inner, $outerKeySelector, $innerKeySelector, $resultSelector, $comparer) {
$innerKeys = [];
$innerElements = [];
$innerElementKeys = [];
foreach ($inner->getIterator() as $innerElementKey => $innerElement) {
$innerKeys[] = $innerKeySelector($innerElement, $innerElementKey);
$innerElements[] = $innerElement;
$innerElementKeys[] = $innerElementKey;
}
foreach ($this->getIterator() as $outerElementKey => $outerElement) {
$outerKey = $outerKeySelector($outerElement, $outerElementKey);
foreach ($innerKeys as $index => $innerKey) {
if ($comparer($outerKey, $innerKey)) {
yield $outerElementKey => $resultSelector($outerElement, $innerElements[$index], $outerElementKey, $innerElementKeys[$index]);
}
}
}
});
}
public function last(?callable $predicate = null): mixed
{
$last = null;
foreach ($this->getIterator() as $elementKey => $element) {
if ($predicate === null || $predicate($element, $elementKey)) {
$last = $element;
}
}
if ($last === null) {
throw new EmptySequenceException();
}
return $last;
}
public function lastOrDefault(mixed $default, ?callable $predicate = null): mixed
{
$last = null;
foreach ($this->getIterator() as $elementKey => $element) {
if ($predicate === null || $predicate($element, $elementKey)) {
$last = $element;
}
}
return $last ?? $default;
}
/**
* @param callable(TSource, TIteratorKey): numeric $selector
*
* @return numeric
*/
public function max(callable $selector): int|float|string
{
/** @var Iterator<TIteratorKey, TSource> $iterator */
$iterator = $this->getIterator();
$iterator->rewind();
if (!$iterator->valid()) {
throw new EmptySequenceException();
}
$max = $selector($iterator->current(), $iterator->key());
$iterator->next();
while ($iterator->valid()) {
$max = max($max, $selector($iterator->current(), $iterator->key()));
$iterator->next();
}
return $max;
}
/**
* @param callable(TSource, TIteratorKey): numeric $selector
*
* @return numeric
*/
public function min(callable $selector): int|float|string
{
/** @var Iterator<TIteratorKey, TSource> $iterator */
$iterator = $this->getIterator();
$iterator->rewind();
if (!$iterator->valid()) {
throw new EmptySequenceException();
}
$min = $selector($iterator->current(), $iterator->key());
$iterator->next();
while($iterator->valid()) {
$min = min($min, $selector($iterator->current(), $iterator->key()));
$iterator->next();
}
return $min;
}
/**
* @template TCompareKey
*
* @param callable(TSource, TIteratorKey): TCompareKey $keySelector
* @param null|callable(TCompareKey, TCompareKey): int $comparer
*
* @return GenericOrderedEnumerable<TSource>
*/
public function orderBy(callable $keySelector, ?callable $comparer = null): GenericOrderedEnumerable
{
$comparer ??= DefaultEqualityComparer::compare(...);
return new OrderedEnumerable(new OrderedIterator($this->getIterator(), $keySelector(...), $comparer(...)));
}
/**
* @template TCompareKey
*
* @param callable(TSource, TIteratorKey): TCompareKey $keySelector
* @param null|callable(TCompareKey, TCompareKey): int $comparer
*
* @return GenericOrderedEnumerable<TSource>
*/
public function orderByDescending(callable $keySelector, ?callable $comparer = null): GenericOrderedEnumerable
{
$comparer ??= DefaultEqualityComparer::compare(...);
$comparer = DefaultEqualityComparer::invert($comparer);
/**
* @var Closure(mixed, mixed): int $comparer
*/
return new OrderedEnumerable(new OrderedIterator($this->getIterator(), $keySelector(...), $comparer(...)));
}
public function prepend(mixed $key, mixed $value): GenericKeyedEnumerable
{
return new KeyedEnumerable(function () use ($value, $key) {
yield $key => $value;
yield from $this->getIterator();
});
}
public function reverse(): GenericKeyedEnumerable
{
return new KeyedEnumerable(new ReverseIterator($this->getIterator()));
}
/**
* @template TResult
*
* @param callable(TSource, TIteratorKey): TResult $selector
*
* @return GenericKeyedEnumerable<TIteratorKey, TResult>
*/
public function select(callable $selector): GenericKeyedEnumerable
{
return new KeyedEnumerable(new SelectIterator($this->getIterator(), $selector(...)));
}
/**
* @template TCollection
* @template TCollectionKey
* @template TResult
*
* @param callable(TSource, TIteratorKey): GenericKeyedEnumerable<TCollectionKey, TCollection> $collectionSelector
* @param null|callable(TSource, TCollection, TIteratorKey, TCollectionKey): TResult $resultSelector
*
* @return GenericKeyedEnumerable<TCollectionKey, TResult>
*/
public function selectMany(callable $collectionSelector, ?callable $resultSelector = null): GenericKeyedEnumerable
{
/** @psalm-suppress UnusedClosureParam */
$resultSelector ??= static fn (mixed $element, mixed $collectionElement, mixed $elementKey, mixed $collectionElementKey): mixed => $collectionElement;
/** @var callable(TSource, TCollection, TIteratorKey, TCollectionKey): TResult $resultSelector */
return new KeyedEnumerable(function () use ($collectionSelector, $resultSelector) {
foreach ($this->getIterator() as $elementKey => $element) {
foreach ($collectionSelector($element, $elementKey) as $collectionElementKey => $collectionElement) {
yield $collectionElementKey => $resultSelector($element, $collectionElement, $elementKey, $collectionElementKey);
}
}
});
}
public function sequenceEqual(GenericKeyedEnumerable $other, ?callable $comparer = null): bool
{
$comparer ??= DefaultEqualityComparer::same(...);
/** @var callable(TSource, TSource, TIteratorKey, TIteratorKey): bool $comparer */
$mit = new ParallelIterator(ParallelIterator::MIT_KEYS_NUMERIC | ParallelIterator::MIT_NEED_ANY);
$mit->attachIterator($this->getIterator());
$mit->attachIterator($other->getIterator());
foreach ($mit as $keys => $values) {
/**
* @var array{TSource, TSource} $values
* @var array{TIteratorKey, TIteratorKey} $keys
*/
if (!$comparer($values[0], $values[1], $keys[0], $keys[1])) {
return false;
}
}
return true;
}
public function single(?callable $predicate = null): mixed
{
$matched = false;
$returnElement = null;
foreach ($this->getIterator() as $elementKey => $element) {
if ($predicate === null || $predicate($element, $elementKey)) {
if ($matched) {
throw new AmbiguousMatchException();
}
$matched = true;
$returnElement = $element;
}
}
if (!$matched) {
throw new EmptySequenceException();
}
return $returnElement;
}
public function singleOrDefault(mixed $default, ?callable $predicate = null): mixed
{
$matched = false;
$returnElement = null;
foreach ($this->getIterator() as $elementKey => $element) {
if ($predicate === null || $predicate($element, $elementKey)) {
if ($matched) {
throw new AmbiguousMatchException();
}
$matched = true;
$returnElement = $element;
}
}
return $matched ? $returnElement : $default;
}
public function skip(int $count): GenericKeyedEnumerable
{
return new KeyedEnumerable(new LimitIterator($this->getIterator(), $count));
}
public function skipLast(int $count): GenericKeyedEnumerable
{
$cachedIterator = new CachingIterator($this->getIterator(), CachingIterator::FULL_CACHE);
$cachedIterator->rewind();
while ($cachedIterator->valid()) {
$cachedIterator->next();
}
$size = count($cachedIterator);
$offset = $size - $count;
if ($offset >= 0) {
$iterator = new LimitIterator($cachedIterator, 0, $offset);
} else {
$iterator = new EmptyIterator();
}
return new KeyedEnumerable($iterator);
}
/**
* @param callable(TSource, TIteratorKey): bool $predicate
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function skipWhile(callable $predicate): GenericKeyedEnumerable
{
$iterator = $this->getIterator();
$whileIterator = new WhileIterator($iterator, $predicate(...));
$whileIterator->rewind();
while ($whileIterator->valid()) {
$whileIterator->next();
}
return new KeyedEnumerable(new NoRewindIterator($iterator));
}
/**
* @param callable(TSource, TIteratorKey): numeric $selector
*
* @return numeric
*/
public function sum(callable $selector): int|float|string
{
/** @var numeric */
return $this->aggregate(function (mixed $accumulator, mixed $element, mixed $elementKey) use ($selector) {
/**
* @var numeric $accumulator
* @var TSource $element
* @var TIteratorKey $elementKey
*/
return $accumulator + $selector($element, $elementKey);
}, 0);
}
public function take(int $count): GenericKeyedEnumerable
{
return new KeyedEnumerable(new LimitIterator($this->getIterator(), 0, $count));
}
public function takeLast(int $count): GenericKeyedEnumerable
{
$cachedIterator = new CachingIterator($this->getIterator(), CachingIterator::FULL_CACHE);
$cachedIterator->rewind();
while ($cachedIterator->valid()) {
$cachedIterator->next();
}
$size = count($cachedIterator);
$offset = $size - $count;
if ($offset < 0) {
return new KeyedEnumerable(new EmptyIterator());
}
return new KeyedEnumerable(new LimitIterator($cachedIterator, $offset));
}
/**
* @param callable(TSource, TIteratorKey): bool $predicate
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function takeWhile(callable $predicate): GenericKeyedEnumerable
{
return new KeyedEnumerable(new WhileIterator($this->getIterator(), $predicate(...)));
}
/**
* @return list<TSource>
*/
public function toList(): array
{
$list = [];
foreach ($this->getIterator() as $element) {
$list[] = $element;
}
return $list;
}
public function toArray(?callable $keySelector = null): array
{
/** @psalm-suppress UnusedClosureParam */
$keySelector ??= static fn(mixed $value, mixed $key): mixed => $key;
$array = [];
foreach ($this->getIterator() as $elementKey => $element) {
$key = $keySelector($element, $elementKey);
if ($key instanceof Stringable) {
$key = (string)$key;
}
if (!is_scalar($key)) {
throw new OutOfBoundsException('Invalid array key: ' . get_debug_type($key));
}
/** @var array-key $key */
$array[$key] = $element;
}
return $array;
}
public function keys(): GenericEnumerable
{
return new Enumerable(new FlipIterator($this->getIterator()));
}
public function values(): GenericEnumerable
{
return new Enumerable($this->getIterator());
}
public function union(GenericKeyedEnumerable $other, ?callable $comparer = null): GenericKeyedEnumerable
{
$comparer ??= DefaultEqualityComparer::same(...);
$identity = static fn(mixed $o): mixed => $o;
/**
* @var callable(TSource, TSource): bool $comparer
* @var callable(TSource): TSource $identity
*/
return $this->unionBy($other, $identity, $comparer);
}
/**
* @template TCompareKey
*
* @param GenericKeyedEnumerable<TIteratorKey, TSource> $other
* @param callable(TSource): TCompareKey $keySelector
* @param null|callable(TCompareKey, TCompareKey): bool $comparer
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function unionBy(GenericKeyedEnumerable $other, callable $keySelector, ?callable $comparer = null): GenericKeyedEnumerable
{
$comparer ??= DefaultEqualityComparer::same(...);
$append = new AppendIterator();
$append->append($this->getIterator());
$append->append($other->getIterator());
/**
* @var Closure(TSource): TCompareKey $keySelector
* @var Closure(TCompareKey, TCompareKey): bool $comparer
*/
return new KeyedEnumerable(new UniqueByIterator($append, $keySelector(...), $comparer(...)));
}
/**
* @psalm-suppress MoreSpecificImplementedParamType Psalm thinks TIteratorKey is always 0|positive-int...
*
* @param callable(TSource, TIteratorKey, Iterator<TIteratorKey, TSource>): bool $predicate
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function where(callable $predicate): GenericKeyedEnumerable
{
return new KeyedEnumerable(new CallbackFilterIterator($this->getIterator(), $predicate(...)));
}
/**
* @psalm-suppress MoreSpecificImplementedParamType Can't influence the signature of CallbackFilterIterator...
*
* @param callable(TIteratorKey, TSource, Iterator<TSource, TIteratorKey>): bool $predicate
*
* @return GenericKeyedEnumerable<TIteratorKey, TSource>
*/
public function whereKey(callable $predicate): GenericKeyedEnumerable
{
return new KeyedEnumerable(new FlipIterator(new CallbackFilterIterator(new FlipIterator($this->getIterator()), $predicate(...))));
}
/**
* @template TOther
* @template TOtherIteratorKey
* @template TResult
* @template TResultKey
*
* @param GenericKeyedEnumerable<TOtherIteratorKey, TOther> $other
* @param null|callable(TSource, TOther): TResult $resultSelector
* @param null|callable(TIteratorKey, TOtherIteratorKey): TResultKey $keySelector
*
* @return GenericKeyedEnumerable<TResultKey, TResult>
*/
public function zip(GenericKeyedEnumerable $other, ?callable $resultSelector = null, ?callable $keySelector = null): GenericKeyedEnumerable
{
$resultSelector ??= static fn (mixed $a, mixed $b): array => [$a, $b];
/** @psalm-suppress UnusedClosureParam */
$keySelector ??= static fn (mixed $a, mixed $b): mixed => $a;
$mit = new ParallelIterator(ParallelIterator::MIT_KEYS_NUMERIC | ParallelIterator::MIT_NEED_ALL);
$mit->attachIterator($this->getIterator());
$mit->attachIterator($other->getIterator());
/** @var ParallelIterator $mit */
/** @var GenericKeyedEnumerable<TResultKey, TResult> */
return new KeyedEnumerable(
/** @var SelectIterator<TResultKey, TResult> */
new SelectIterator(
/** @var KeySelectIterator<TResultKey, array{TSource, TOther}> */
new KeySelectIterator(
$mit,
static function (mixed $keys) use ($keySelector): mixed {
/** @var array{TIteratorKey, TOtherIteratorKey} $keys */
return $keySelector($keys[0], $keys[1]);
},
),
static function (mixed $values) use ($resultSelector): mixed {
/** @var array{TSource, TOther} $values */
return $resultSelector($values[0], $values[1]);
}
)
);
}
}