|
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
|
32 |
|
public function __construct(?array $elements = null) |
|
19
|
|
|
{ |
|
20
|
32 |
|
$this->internalMap = new HashMap(); |
|
21
|
32 |
|
if ($elements !== null) { |
|
22
|
24 |
|
foreach ($elements as $element) { |
|
23
|
15 |
|
$this->add($element); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
32 |
|
} |
|
27
|
|
|
|
|
28
|
30 |
|
public function add($element): bool |
|
29
|
|
|
{ |
|
30
|
30 |
|
return $this->internalMap->put($element, null); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public function stream(): Stream |
|
34
|
|
|
{ |
|
35
|
1 |
|
return new SerialStream($this); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
public function merge(Collection $collection): bool |
|
39
|
|
|
{ |
|
40
|
2 |
|
foreach ($collection as $item) { |
|
41
|
2 |
|
$this->add($item); |
|
42
|
|
|
} |
|
43
|
2 |
|
return true; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function clear(): void |
|
47
|
|
|
{ |
|
48
|
2 |
|
$this->internalMap = new HashMap(); |
|
49
|
2 |
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
public function remove($element): bool |
|
52
|
|
|
{ |
|
53
|
6 |
|
return $this->internalMap->remove($element); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
8 |
|
public function contains($element): bool |
|
57
|
|
|
{ |
|
58
|
8 |
|
return $this->internalMap->containsKey($element); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
public function equals(Collection $collection): bool |
|
62
|
|
|
{ |
|
63
|
5 |
|
if ($this->size() !== $collection->size()) { |
|
64
|
2 |
|
return false; |
|
65
|
|
|
} |
|
66
|
3 |
|
foreach ($collection as $item) { |
|
67
|
3 |
|
if (!$this->contains($item)) { |
|
68
|
3 |
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
2 |
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
18 |
|
public function size(): int |
|
75
|
|
|
{ |
|
76
|
18 |
|
return $this->internalMap->size(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function isEmpty(): bool |
|
80
|
|
|
{ |
|
81
|
1 |
|
return $this->size() === 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
16 |
|
public function toArray(): array |
|
85
|
|
|
{ |
|
86
|
16 |
|
return $this->internalMap->keys()->toArray(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
public function copy(): Collection |
|
90
|
|
|
{ |
|
91
|
1 |
|
return new static($this->toArray()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
5 |
|
public function getIterator() |
|
95
|
|
|
{ |
|
96
|
5 |
|
return new ArrayIterator($this->toArray()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public function addAll(iterable $elements): bool |
|
100
|
|
|
{ |
|
101
|
1 |
|
$res = true; |
|
102
|
1 |
|
foreach ($elements as $element) { |
|
103
|
1 |
|
!$this->add($element) && $res = false; |
|
104
|
|
|
} |
|
105
|
1 |
|
return $res; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|