Passed
Push — master ( 056fa5...0b6560 )
by Smoren
02:10
created

symmetricDifference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
     *
13
     * @return array<int>
14
     */
15 31
    public static function partialIntersection(int $m, array ...$sets): array
16
    {
17 31
        $iterator = new \MultipleIterator(\MultipleIterator::MIT_NEED_ANY);
18
19 31
        foreach ($sets as $set) {
20 29
            $iterator->attachIterator(new \ArrayIterator($set));
21
        }
22
23 31
        $usageMap = [];
24 31
        $result = [];
25
26 31
        foreach ($iterator as $values) {
27 24
            foreach ($values as $value) {
28 24
                if ($value === null) {
29 10
                    continue;
30
                }
31
32 24
                if (!isset($usageMap[$value])) {
33 24
                    $usageMap[$value] = 0;
34
                }
35
36 24
                $usageMap[$value]++;
37
38 24
                if ($usageMap[$value] === $m) {
39 16
                    $result[] = $value;
40
                }
41
            }
42
        }
43
44 31
        sort($result);
45
46 31
        return $result;
47
    }
48
49
    /**
50
     * Symmetric difference of multiple sets.
51
     *
52
     * @param array<int> ...$sets
53
     * @return array
54
     */
55 1
    public static function symmetricDifference(array ...$sets): array
56
    {
57 1
        $union = array_values(array_unique(array_merge(...$sets)));
58 1
        $twoPartInt = self::partialIntersection(2, ...$sets);
59
60 1
        return array_values(array_diff($union, $twoPartInt));
61
    }
62
}
63