1 | <?php |
||
8 | class HashSet implements SetInterface |
||
9 | { |
||
10 | /** @var array */ |
||
11 | protected $scalars = []; |
||
12 | |||
13 | /** @var array */ |
||
14 | protected $objects = []; |
||
15 | |||
16 | /** |
||
17 | * @param array $items |
||
18 | */ |
||
19 | 5 | public function __construct(array $items) |
|
20 | { |
||
21 | 5 | foreach ($items as $item) { |
|
22 | 4 | $this->add($item); |
|
23 | } |
||
24 | 5 | } |
|
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 1 | public function count() |
|
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | 2 | public function equal(SetInterface $other) |
|
38 | { |
||
39 | 2 | if (!$other instanceof HashSet) { |
|
40 | throw new InvalidArgumentException('unsupported type of Set.'); |
||
41 | } |
||
42 | 2 | if (count($other->scalars) !== count($this->scalars)) { |
|
43 | return false; |
||
44 | } |
||
45 | 2 | if (count($other->objects) !== count($this->objects)) { |
|
46 | return false; |
||
47 | } |
||
48 | 2 | foreach ($this->scalars as $key => $item) { |
|
49 | 2 | if (!array_key_exists($key, $other->scalars)) { |
|
50 | 2 | return false; |
|
51 | } |
||
52 | } |
||
53 | 2 | foreach ($other->scalars as $key => $item) { |
|
54 | 2 | if (!array_key_exists($key, $this->scalars)) { |
|
55 | 2 | return false; |
|
56 | } |
||
57 | } |
||
58 | 2 | foreach ($this->objects as $key => $item) { |
|
59 | 1 | if (!array_key_exists($key, $other->objects)) { |
|
60 | 1 | return false; |
|
61 | } |
||
62 | } |
||
63 | 2 | foreach ($other->objects as $key => $item) { |
|
64 | 1 | if (!array_key_exists($key, $this->objects)) { |
|
65 | 1 | return false; |
|
66 | } |
||
67 | } |
||
68 | 2 | return true; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public function intersection(SetInterface $other) |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function union(SetInterface $other) |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function difference(SetInterface $other) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 1 | public function has($item) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 4 | public function add($item) |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 1 | public function remove($item) |
|
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | 1 | public function getIterator() |
|
154 | } |
||
155 |