Passed
Push — master ( 23f099...2790c0 )
by Smoren
02:31
created

partialIntersection()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 10
c 1
b 0
f 1
nc 5
nop 3
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\PartialIntersection;
6
7
use Smoren\PartialIntersection\Util\JustifyMultipleIterator;
8
use Smoren\PartialIntersection\Util\NoValueMonad;
9
use Smoren\PartialIntersection\Util\UsageMap;
10
11
class MultisetIterableImplementation
12
{
13
    /**
14
     * @template T
15
     *
16
     * @param bool $strict
17
     * @param int $m
18
     * @param iterable<T> ...$multisets
19
     *
20
     * @return \Generator<T>
21
     */
22 374
    public static function partialIntersection(bool $strict, int $m, iterable ...$multisets): \Generator
23
    {
24 374
        $usageMap = new UsageMap($strict);
25
26 374
        $multipleIterator = new JustifyMultipleIterator(...$multisets);
27
28 374
        foreach ($multipleIterator as $index => $values) {
29 318
            foreach ($values as $owner => $value) {
30 318
                if ($value instanceof NoValueMonad) {
31 121
                    continue;
32
                }
33
34 318
                $usageMap->addUsage($value, (string)$owner);
35
36 318
                if ($usageMap->getOwnersCount($value) === $m) {
37 208
                    yield $index => $value;
38 208
                    $usageMap->deleteUsage($value);
39
                }
40
            }
41
        }
42
    }
43
}
44