|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\Immutable; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Immutable\Exception\CannotGroupEmptyStructure; |
|
7
|
|
|
|
|
8
|
|
|
final class Set implements \Countable |
|
9
|
|
|
{ |
|
10
|
|
|
private string $type; |
|
11
|
|
|
private ValidateArgument $validate; |
|
12
|
|
|
private Set\Implementation $implementation; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
59 |
|
private function __construct(string $type) |
|
18
|
|
|
{ |
|
19
|
59 |
|
$this->type = $type; |
|
20
|
59 |
|
$this->validate = Type::of($type); |
|
21
|
59 |
|
$this->implementation = new Set\Primitive($type); |
|
22
|
59 |
|
} |
|
23
|
|
|
|
|
24
|
54 |
|
public static function of(string $type, ...$values): self |
|
25
|
|
|
{ |
|
26
|
54 |
|
$self = new self($type); |
|
27
|
54 |
|
$self->implementation = new Set\Primitive($type, ...$values); |
|
28
|
|
|
|
|
29
|
54 |
|
return $self; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return self<mixed> |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public static function mixed(...$values): self |
|
36
|
|
|
{ |
|
37
|
1 |
|
$self = new self('mixed'); |
|
38
|
1 |
|
$self->implementation = new Set\Primitive('mixed', ...$values); |
|
39
|
|
|
|
|
40
|
1 |
|
return $self; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return self<int> |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public static function ints(int ...$values): self |
|
47
|
|
|
{ |
|
48
|
2 |
|
$self = new self('int'); |
|
49
|
2 |
|
$self->implementation = new Set\Primitive('int', ...$values); |
|
50
|
|
|
|
|
51
|
2 |
|
return $self; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return self<float> |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public static function floats(float ...$values): self |
|
58
|
|
|
{ |
|
59
|
1 |
|
$self = new self('float'); |
|
60
|
1 |
|
$self->implementation = new Set\Primitive('float', ...$values); |
|
61
|
|
|
|
|
62
|
1 |
|
return $self; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return self<string> |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public static function strings(string ...$values): self |
|
69
|
|
|
{ |
|
70
|
1 |
|
$self = new self('string'); |
|
71
|
1 |
|
$self->implementation = new Set\Primitive('string', ...$values); |
|
72
|
|
|
|
|
73
|
1 |
|
return $self; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return self<object> |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public static function objects(object ...$values): self |
|
80
|
|
|
{ |
|
81
|
1 |
|
$self = new self('object'); |
|
82
|
1 |
|
$self->implementation = new Set\Primitive('object', ...$values); |
|
83
|
|
|
|
|
84
|
1 |
|
return $self; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
19 |
|
public function isOfType(string $type): bool |
|
88
|
|
|
{ |
|
89
|
19 |
|
return $this->type === $type; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return the type of this set |
|
94
|
|
|
*/ |
|
95
|
23 |
|
public function type(): string |
|
96
|
|
|
{ |
|
97
|
23 |
|
return $this->type; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
public function size(): int |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->implementation->size(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function count(): int |
|
109
|
|
|
{ |
|
110
|
1 |
|
return $this->implementation->size(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
45 |
|
public function toArray(): array |
|
114
|
|
|
{ |
|
115
|
45 |
|
return $this->implementation->toArray(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Intersect this set with the given one |
|
120
|
|
|
* |
|
121
|
|
|
* @param self<T> $set |
|
122
|
|
|
* |
|
123
|
|
|
* @throws InvalidArgumentException If the sets are not of the same type |
|
124
|
|
|
* |
|
125
|
|
|
* @return self<T> |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function intersect(self $set): self |
|
128
|
|
|
{ |
|
129
|
2 |
|
assertSet($this->type, $set, 1); |
|
130
|
|
|
|
|
131
|
1 |
|
$newSet = clone $this; |
|
132
|
1 |
|
$newSet->implementation = $this->implementation->intersect( |
|
133
|
1 |
|
$set->implementation |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
1 |
|
return $newSet; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Add a element to the set |
|
141
|
|
|
* |
|
142
|
|
|
* @param T $element |
|
143
|
|
|
* |
|
144
|
|
|
* @return self<T> |
|
145
|
|
|
*/ |
|
146
|
24 |
|
public function add($element): self |
|
147
|
|
|
{ |
|
148
|
24 |
|
($this->validate)($element, 1); |
|
149
|
|
|
|
|
150
|
23 |
|
$self = clone $this; |
|
151
|
23 |
|
$self->implementation = $this->implementation->add($element); |
|
152
|
|
|
|
|
153
|
23 |
|
return $self; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Alias for add method in order to have a syntax similar to a true tuple |
|
158
|
|
|
* when constructing the set |
|
159
|
|
|
* |
|
160
|
|
|
* Example: |
|
161
|
|
|
* <code> |
|
162
|
|
|
* Set::of('int')(1)(3) |
|
163
|
|
|
* </code> |
|
164
|
|
|
* |
|
165
|
|
|
* @param T $element |
|
166
|
|
|
* |
|
167
|
|
|
* @return self<T> |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function __invoke($element): self |
|
170
|
|
|
{ |
|
171
|
1 |
|
return $this->add($element); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Check if the set contains the given element |
|
176
|
|
|
* |
|
177
|
|
|
* @param T $element |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function contains($element): bool |
|
180
|
|
|
{ |
|
181
|
1 |
|
($this->validate)($element, 1); |
|
182
|
|
|
|
|
183
|
1 |
|
return $this->implementation->contains($element); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Remove the element from the set |
|
188
|
|
|
* |
|
189
|
|
|
* @param T $element |
|
190
|
|
|
* |
|
191
|
|
|
* @return self<T> |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public function remove($element): self |
|
194
|
|
|
{ |
|
195
|
1 |
|
($this->validate)($element, 1); |
|
196
|
|
|
|
|
197
|
1 |
|
$self = clone $this; |
|
198
|
1 |
|
$self->implementation = $this->implementation->remove($element); |
|
199
|
|
|
|
|
200
|
1 |
|
return $self; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Return the diff between this set and the given one |
|
205
|
|
|
* |
|
206
|
|
|
* @param self<T> $set |
|
207
|
|
|
* |
|
208
|
|
|
* @return self<T> |
|
209
|
|
|
*/ |
|
210
|
2 |
|
public function diff(self $set): self |
|
211
|
|
|
{ |
|
212
|
2 |
|
assertSet($this->type, $set, 1); |
|
213
|
|
|
|
|
214
|
1 |
|
$self = clone $this; |
|
215
|
1 |
|
$self->implementation = $this->implementation->diff( |
|
216
|
1 |
|
$set->implementation |
|
217
|
|
|
); |
|
218
|
|
|
|
|
219
|
1 |
|
return $self; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Check if the given set is identical to this one |
|
224
|
|
|
* |
|
225
|
|
|
* @param self<T> $set |
|
226
|
|
|
*/ |
|
227
|
11 |
|
public function equals(self $set): bool |
|
228
|
|
|
{ |
|
229
|
11 |
|
assertSet($this->type, $set, 1); |
|
230
|
|
|
|
|
231
|
10 |
|
return $this->implementation->equals($set->implementation); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Return all elements that satisfy the given predicate |
|
236
|
|
|
* |
|
237
|
|
|
* @param callable(T): bool $predicate |
|
238
|
|
|
* |
|
239
|
|
|
* @return self<T> |
|
240
|
|
|
*/ |
|
241
|
1 |
|
public function filter(callable $predicate): self |
|
242
|
|
|
{ |
|
243
|
1 |
|
$set = clone $this; |
|
244
|
1 |
|
$set->implementation = $this->implementation->filter($predicate); |
|
245
|
|
|
|
|
246
|
1 |
|
return $set; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Apply the given function to all elements of the set |
|
251
|
|
|
* |
|
252
|
|
|
* @param callable(T): void $function |
|
253
|
|
|
*/ |
|
254
|
1 |
|
public function foreach(callable $function): void |
|
255
|
|
|
{ |
|
256
|
1 |
|
$this->implementation->foreach($function); |
|
257
|
1 |
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Return a new map of pairs grouped by keys determined with the given |
|
261
|
|
|
* discriminator function |
|
262
|
|
|
* |
|
263
|
|
|
* @param callable(T) $discriminator |
|
264
|
|
|
* |
|
265
|
|
|
* @throws CannotGroupEmptyStructure |
|
266
|
|
|
* |
|
267
|
|
|
* @return Map<mixed, self<T>> |
|
268
|
|
|
*/ |
|
269
|
1 |
|
public function groupBy(callable $discriminator): Map |
|
270
|
|
|
{ |
|
271
|
1 |
|
return $this->implementation->groupBy($discriminator); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Return a new set by applying the given function to all elements |
|
276
|
|
|
* |
|
277
|
|
|
* @param callable(T): T $function |
|
278
|
|
|
* |
|
279
|
|
|
* @return self<T> |
|
280
|
|
|
*/ |
|
281
|
2 |
|
public function map(callable $function): self |
|
282
|
|
|
{ |
|
283
|
2 |
|
$self = $this->clear(); |
|
284
|
2 |
|
$self->implementation = $this->implementation->map($function); |
|
285
|
|
|
|
|
286
|
1 |
|
return $self; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Return a sequence of 2 sets partitioned according to the given predicate |
|
291
|
|
|
* |
|
292
|
|
|
* @param callable(T): bool $predicate |
|
293
|
|
|
* |
|
294
|
|
|
* @return Map<bool, self<T>> |
|
295
|
|
|
*/ |
|
296
|
1 |
|
public function partition(callable $predicate): Map |
|
297
|
|
|
{ |
|
298
|
1 |
|
return $this->implementation->partition($predicate); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Concatenate all elements with the given separator |
|
303
|
|
|
*/ |
|
304
|
1 |
|
public function join(string $separator): Str |
|
305
|
|
|
{ |
|
306
|
1 |
|
return $this->implementation->join($separator); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Return a sequence sorted with the given function |
|
311
|
|
|
* |
|
312
|
|
|
* @param callable(T, T): int $function |
|
313
|
|
|
* |
|
314
|
|
|
* @return Sequence<T> |
|
315
|
|
|
*/ |
|
316
|
1 |
|
public function sort(callable $function): Sequence |
|
317
|
|
|
{ |
|
318
|
1 |
|
return $this->implementation->sort($function); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Create a new set with elements of both sets |
|
323
|
|
|
* |
|
324
|
|
|
* @param self<T> $set |
|
325
|
|
|
* |
|
326
|
|
|
* @return self<T> |
|
327
|
|
|
*/ |
|
328
|
2 |
|
public function merge(self $set): self |
|
329
|
|
|
{ |
|
330
|
2 |
|
assertSet($this->type, $set, 1); |
|
331
|
|
|
|
|
332
|
1 |
|
$self = clone $this; |
|
333
|
1 |
|
$self->implementation = $this->implementation->merge( |
|
334
|
1 |
|
$set->implementation |
|
335
|
|
|
); |
|
336
|
|
|
|
|
337
|
1 |
|
return $self; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Reduce the set to a single value |
|
342
|
|
|
* |
|
343
|
|
|
* @param mixed $carry |
|
344
|
|
|
* @param callable(mixed, T) $reducer |
|
345
|
|
|
* |
|
346
|
|
|
* @return mixed |
|
347
|
|
|
*/ |
|
348
|
1 |
|
public function reduce($carry, callable $reducer) |
|
349
|
|
|
{ |
|
350
|
1 |
|
return $this->implementation->reduce($carry, $reducer); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Return a set of the same type but without any value |
|
355
|
|
|
* |
|
356
|
|
|
* @return self<T> |
|
357
|
|
|
*/ |
|
358
|
2 |
|
public function clear(): self |
|
359
|
|
|
{ |
|
360
|
2 |
|
$self = clone $this; |
|
361
|
2 |
|
$self->implementation = $this->implementation->clear(); |
|
362
|
|
|
|
|
363
|
2 |
|
return $self; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
1 |
|
public function empty(): bool |
|
367
|
|
|
{ |
|
368
|
1 |
|
return $this->implementation->empty(); |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|