Passed
Pull Request — master (#24)
by Igor
02:46
created

HashSet::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/** @noinspection ClassReImplementsParentInterfaceInspection */
3
4
/**
5
 * @author Maxim Sokolovsky
6
 */
7
8
namespace WS\Utils\Collections;
9
10
use ArrayIterator;
11
12
class HashSet implements Set
13
{
14
    /**
15
     * @var HashMap
16
     */
17
    private $internalMap;
18 24
    public function __construct(?array $elements = null)
19
    {
20 24
        $this->internalMap = new HashMap();
21 24
        if ($elements !== null) {
22 23
            foreach ($elements as $element) {
23 14
                $this->add($element);
24
            }
25
        }
26 24
    }
27
28 22
    public function add($element): bool
29
    {
30 22
        return $this->internalMap->put($element, null);
31
    }
32
33 1
    public function stream(): Stream
34
    {
35 1
        return new SerialStream($this);
36
    }
37
38
    public function parallelStream($workersPool = null): Stream
39
    {
40
        return new ParallelStream($this, $workersPool);
41
    }
42
43 2
    public function merge(Collection $collection): bool
44
    {
45 2
        foreach ($collection as $item) {
46 2
            $this->add($item);
47
        }
48 2
        return true;
49
    }
50
51 2
    public function clear(): void
52
    {
53 2
        $this->internalMap = new HashMap();
54 2
    }
55
56 6
    public function remove($element): bool
57
    {
58 6
        return $this->internalMap->remove($element);
59
    }
60
61 7
    public function contains($element): bool
62
    {
63 7
        return $this->internalMap->containsKey($element);
64
    }
65
66 5
    public function equals(Collection $collection): bool
67
    {
68 5
        if ($this->size() !== $collection->size()) {
69 2
            return false;
70
        }
71 3
        foreach ($collection as $item) {
72 3
            if (!$this->contains($item)) {
73 3
                return false;
74
            }
75
        }
76 2
        return true;
77
    }
78
79 18
    public function size(): int
80
    {
81 18
        return $this->internalMap->size();
82
    }
83
84 1
    public function isEmpty(): bool
85
    {
86 1
        return $this->size() === 0;
87
    }
88
89 10
    public function toArray(): array
90
    {
91 10
        return $this->internalMap->keys()->toArray();
92
    }
93
94 1
    public function copy(): Collection
95
    {
96 1
        return new static($this->toArray());
97
    }
98
99 5
    public function getIterator()
100
    {
101 5
        return new ArrayIterator($this->toArray());
102
    }
103
104 1
    public function addAll(iterable $elements): bool
105
    {
106 1
        $res = true;
107 1
        foreach ($elements as $element) {
108 1
            !$this->add($element) && $res = false;
109
        }
110 1
        return $res;
111
    }
112
}
113