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