Passed
Push — master ( c5c96a...1eca3f )
by Smoren
03:24
created

IntegerSetIterableImplementation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B partialIntersection() 0 24 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\PartialIntersection;
6
7
use Smoren\PartialIntersection\Util\IteratorFactory;
8
9
class IntegerSetIterableImplementation
10
{
11
    /**
12
     * @param int $m
13
     * @param iterable<int> ...$sets
14
     * @return \Generator<int>
15
     */
16 105
    public static function partialIntersection(int $m, iterable ...$sets): \Generator
17
    {
18 105
        $iterator = new \MultipleIterator(\MultipleIterator::MIT_NEED_ANY);
19
20 105
        foreach ($sets as $set) {
21 97
            $iterator->attachIterator(IteratorFactory::makeIterator($set));
22
        }
23
24 105
        $usageMap = [];
25
26 105
        foreach ($iterator as $values) {
27 77
            foreach ($values as $value) {
28 77
                if ($value === null) {
29 25
                    continue;
30
                }
31
32 77
                if (!isset($usageMap[$value])) {
33 77
                    $usageMap[$value] = 0;
34
                }
35
36 77
                $usageMap[$value]++;
37
38 77
                if ($usageMap[$value] === $m) {
39 48
                    yield $value;
40
                }
41
            }
42
        }
43
    }
44
}
45