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