|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RunOpenCode\Component\Dataset\Operator; |
| 6 | + |
| 7 | +use RunOpenCode\Component\Dataset\AbstractStream; |
| 8 | +use RunOpenCode\Component\Dataset\Contract\OperatorInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Compress join operator. |
| 12 | + * |
| 13 | + * Compress join operator iterates over given collection and compresses items based on the predicate and join functions. |
| 14 | + * |
| 15 | + * Example usage: |
| 16 | + * |
| 17 | + * ```php |
| 18 | + * use RunOpenCode\Component\Dataset\Operator\CompressJoin; |
| 19 | + * |
| 20 | + * $compressJoin = new CompressJoin( |
| 21 | + * collection: new Dataset([1 => [1, 2], 2 => [1, 3], 3 => [1, 4], 4 => [2, 1], 5 => [2, 2]]), |
| 22 | + * predicate: static fn(array $values): bool => $values[0][0] === $values[1][0], |
| 23 | + * join: static fn(array $buffer): iterable => [ |
| 24 | + * $buffer[0][1][0] => array_map(static fn(array $record): int => $record[1][1], $buffer) |
| 25 | + * ], |
| 26 | + * ); |
| 27 | + * // $compressJoin will yield [1 => [2, 3, 4], 2 => [1, 2]] |
| 28 | + * ``` |
| 29 | + * |
| 30 | + * @template TKey |
| 31 | + * @template TValue |
| 32 | + * @template TModifiedKey |
| 33 | + * @template TModifiedValue |
| 34 | + * |
| 35 | + * @phpstan-type PredicateValues = array{TValue, TValue} |
| 36 | + * @phpstan-type PredicateKeys = array{TKey, TKey} |
| 37 | + * @phpstan-type Record = array{TKey, TValue} |
| 38 | + * @phpstan-type Buffer = list<Record> |
| 39 | + * @phpstan-type PredicateCallable = callable(PredicateValues, PredicateKeys=, Buffer=): bool |
| 40 | + * @phpstan-type JoinCallable = callable(Buffer): iterable<TModifiedKey, TModifiedValue> |
| 41 | + * |
| 42 | + * @extends AbstractStream<TModifiedKey, TModifiedValue> |
| 43 | + * @implements OperatorInterface<TModifiedKey, TModifiedValue> |
| 44 | + */ |
| 45 | +final class CompressJoin extends AbstractStream implements OperatorInterface |
| 46 | +{ |
| 47 | + private readonly \Closure $predicate; |
| 48 | + |
| 49 | + private readonly \Closure $join; |
| 50 | + |
| 51 | + /** |
| 52 | + * @param iterable<TKey, TValue> $collection Collection to iterate over. |
| 53 | + * @param PredicateCallable $predicate Callable predicate function to evaluate. |
| 54 | + * @param JoinCallable $join Callable join function to produce joined records. |
| 55 | + */ |
| 56 | + public function __construct( |
| 57 | + private readonly iterable $collection, |
| 58 | + callable $predicate, |
| 59 | + callable $join, |
| 60 | + ) { |
| 61 | + parent::__construct($collection); |
| 62 | + $this->predicate = $predicate(...); |
| 63 | + $this->join = $join(...); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@inheritdoc} |
| 68 | + */ |
| 69 | + protected function iterate(): \Traversable |
| 70 | + { |
| 71 | + /** @var Buffer $buffer */ |
| 72 | + $buffer = []; |
| 73 | + /** @var Record|null $previous */ |
| 74 | + $previous = null; |
| 75 | + |
| 76 | + foreach ($this->collection as $key => $value) { |
| 77 | + if (0 === \count($buffer)) { |
| 78 | + $previous = [$key, $value]; |
| 79 | + $buffer[] = $previous; |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + \assert(null !== $previous); |
| 84 | + |
| 85 | + if (($this->predicate)([$previous[1], $value], [$previous[0], $key], $buffer)) { |
| 86 | + $previous = [$key, $value]; |
| 87 | + $buffer[] = $previous; |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + yield from ($this->join)($buffer); |
| 92 | + $previous = [$key, $value]; |
| 93 | + $buffer = [$previous]; |
| 94 | + } |
| 95 | + |
| 96 | + if (0 !== \count($buffer)) { |
| 97 | + yield from ($this->join)($buffer); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments