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

IntegerSetArrayImplementation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 39
ccs 17
cts 17
cp 1
rs 10
wmc 7

1 Method

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