|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Immutable\Set; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
|
7
|
|
|
Map, |
|
8
|
|
|
Sequence, |
|
9
|
|
|
Set, |
|
10
|
|
|
Type, |
|
11
|
|
|
ValidateArgument, |
|
12
|
|
|
Str, |
|
13
|
|
|
}; |
|
14
|
|
|
|
|
15
|
|
|
final class Primitive implements Implementation |
|
16
|
|
|
{ |
|
17
|
|
|
private string $type; |
|
18
|
|
|
private ValidateArgument $validate; |
|
19
|
|
|
private Sequence $values; |
|
20
|
|
|
|
|
21
|
80 |
|
public function __construct(string $type, ...$values) |
|
22
|
|
|
{ |
|
23
|
80 |
|
$this->type = $type; |
|
24
|
80 |
|
$this->validate = Type::of($type); |
|
25
|
80 |
|
$this->values = Sequence::of($type, ...$values)->distinct(); |
|
26
|
80 |
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function isOfType(string $type): bool |
|
29
|
|
|
{ |
|
30
|
1 |
|
return $this->type === $type; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public function type(): string |
|
34
|
|
|
{ |
|
35
|
1 |
|
return $this->type; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
15 |
|
public function size(): int |
|
39
|
|
|
{ |
|
40
|
15 |
|
return $this->values->size(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function count(): int |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->values->size(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
60 |
|
public function toArray(): array |
|
52
|
|
|
{ |
|
53
|
60 |
|
return $this->values->toArray(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
13 |
|
public function intersect(Implementation $set): self |
|
57
|
|
|
{ |
|
58
|
13 |
|
$self = $this->clear(); |
|
59
|
13 |
|
$self->values = $this->values->intersect( |
|
60
|
13 |
|
Sequence::of($this->type, ...$set->toArray()) |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
13 |
|
return $self; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
26 |
|
public function add($element): self |
|
67
|
|
|
{ |
|
68
|
26 |
|
if ($this->contains($element)) { |
|
69
|
4 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
26 |
|
$set = clone $this; |
|
73
|
26 |
|
$set->values = $this->values->add($element); |
|
74
|
|
|
|
|
75
|
26 |
|
return $set; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
28 |
|
public function contains($element): bool |
|
79
|
|
|
{ |
|
80
|
28 |
|
return $this->values->contains($element); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
public function remove($element): self |
|
84
|
|
|
{ |
|
85
|
2 |
|
if (!$this->contains($element)) { |
|
86
|
1 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
$index = $this->values->indexOf($element); |
|
90
|
2 |
|
$set = clone $this; |
|
91
|
2 |
|
$set->values = $this |
|
92
|
2 |
|
->values |
|
93
|
2 |
|
->clear() |
|
94
|
2 |
|
->append($this->values->slice(0, $index)) |
|
95
|
2 |
|
->append($this->values->slice($index + 1, $this->size())); |
|
96
|
|
|
|
|
97
|
2 |
|
return $set; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
public function diff(Implementation $set): self |
|
101
|
|
|
{ |
|
102
|
2 |
|
$self = clone $this; |
|
103
|
2 |
|
$self->values = $this->values->diff( |
|
104
|
2 |
|
Sequence::of($this->type, ...$set->toArray()) |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
2 |
|
return $self; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
11 |
|
public function equals(Implementation $set): bool |
|
111
|
|
|
{ |
|
112
|
11 |
|
if ($this->size() !== $set->size()) { |
|
113
|
3 |
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
11 |
|
return $this->intersect($set)->size() === $this->size(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
2 |
|
public function filter(callable $predicate): self |
|
120
|
|
|
{ |
|
121
|
2 |
|
$set = clone $this; |
|
122
|
2 |
|
$set->values = $this->values->filter($predicate); |
|
123
|
|
|
|
|
124
|
2 |
|
return $set; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
2 |
|
public function foreach(callable $function): void |
|
128
|
|
|
{ |
|
129
|
2 |
|
$this->values->foreach($function); |
|
130
|
2 |
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
public function groupBy(callable $discriminator): Map |
|
133
|
|
|
{ |
|
134
|
2 |
|
$map = $this->values->groupBy($discriminator); |
|
135
|
|
|
|
|
136
|
2 |
|
return $map->reduce( |
|
137
|
2 |
|
Map::of($map->keyType(), Set::class), |
|
138
|
|
|
function(Map $carry, $key, Sequence $values): Map { |
|
139
|
2 |
|
return $carry->put( |
|
140
|
2 |
|
$key, |
|
141
|
2 |
|
Set::of($this->type, ...$values->toArray()), |
|
142
|
|
|
); |
|
143
|
2 |
|
} |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
4 |
|
public function map(callable $function): self |
|
148
|
|
|
{ |
|
149
|
|
|
$function = function($value) use ($function) { |
|
150
|
4 |
|
$value = $function($value); |
|
151
|
4 |
|
($this->validate)($value, 1); |
|
152
|
|
|
|
|
153
|
2 |
|
return $value; |
|
154
|
4 |
|
}; |
|
155
|
|
|
|
|
156
|
4 |
|
return $this->reduce( |
|
157
|
4 |
|
$this->clear(), |
|
158
|
|
|
function(self $carry, $value) use ($function): self { |
|
159
|
4 |
|
return $carry->add($function($value)); |
|
160
|
4 |
|
} |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
2 |
|
public function partition(callable $predicate): Map |
|
165
|
|
|
{ |
|
166
|
2 |
|
$partitions = $this->values->partition($predicate); |
|
167
|
|
|
|
|
168
|
2 |
|
return Map::of('bool', Set::class) |
|
169
|
2 |
|
(true, Set::of($this->type, ...$partitions->get(true)->toArray())) |
|
170
|
2 |
|
(false, Set::of($this->type, ...$partitions->get(false)->toArray())); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
2 |
|
public function join(string $separator): Str |
|
174
|
|
|
{ |
|
175
|
2 |
|
return $this->values->join($separator); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
2 |
|
public function sort(callable $function): Sequence |
|
179
|
|
|
{ |
|
180
|
2 |
|
return $this->values->sort($function); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
2 |
|
public function merge(Implementation $set): self |
|
184
|
|
|
{ |
|
185
|
2 |
|
return $set->reduce( |
|
186
|
2 |
|
$this, |
|
187
|
|
|
function(self $carry, $value): self { |
|
188
|
2 |
|
return $carry->add($value); |
|
189
|
2 |
|
} |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
8 |
|
public function reduce($carry, callable $reducer) |
|
194
|
|
|
{ |
|
195
|
8 |
|
return $this->values->reduce($carry, $reducer); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
18 |
|
public function clear(): self |
|
199
|
|
|
{ |
|
200
|
18 |
|
return new self($this->type); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
2 |
|
public function empty(): bool |
|
204
|
|
|
{ |
|
205
|
2 |
|
return $this->values->empty(); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|