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

partialIntersection()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 16
c 0
b 0
f 0
nc 14
nop 2
dl 0
loc 32
ccs 17
cts 17
cp 1
crap 7
rs 8.8333
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