-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.php
More file actions
444 lines (395 loc) · 9.58 KB
/
Copy pathSet.php
File metadata and controls
444 lines (395 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
declare(strict_types=1);
/*
* This file is part of the univeros/framework
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Altair\Structure;
use Altair\Structure\Contracts\CapacityInterface;
use Altair\Structure\Contracts\MapInterface;
use Altair\Structure\Contracts\SetInterface;
use Altair\Structure\Traits\CollectionTrait;
use ArrayAccess;
use Error;
use Generator;
use IteratorAggregate;
use OutOfBoundsException;
use Override;
use ReturnTypeWillChange;
use Traversable;
/**
* Set.
*
* A Set is a collection of unique values. The textbook definition of a set will say that values are unordered unless an
* implementation specifies otherwise. Using Java as an example, java.util.Set is an interface with two primary
* implementations: HashSet and TreeSet. HashSet provides O(1) add and remove, where TreeSet ensures a sorted set but
* O(log n) add and remove.
*
* Set uses the same internal structure as a Map, which is based on the same structure as an array. This means that a
* Set can be sorted in O(n * log(n)) time whenever it needs to be, just like a Map and an array.
*
* @link https://medium.com/@rtheunissen/efficient-data-structures-for-php-7-9dda7af674cd#.gl62k1xqr
*
* @template TValue
*
* @implements SetInterface<TValue>
* @implements IteratorAggregate<int, TValue>
* @implements ArrayAccess<int, TValue>
*
* @phpstan-consistent-constructor
*/
class Set implements IteratorAggregate, ArrayAccess, SetInterface, CapacityInterface
{
/** @use CollectionTrait<int, TValue> */
use CollectionTrait;
/**
* Set values are stored as the keys of an internal Map; the associated
* value is always null. Typed against the concrete Map so its capacity API
* and pair access resolve. The empty-array default is required for trait
* property compatibility (CollectionTrait declares `$internal = []`) and is
* replaced with a Map in the constructor before any method is invoked.
*
* @var Map<TValue, null>
*/
protected $internal = [];
/**
* Creates a new set using the values of an array or Traversable object.
* The keys of either will not be preserved.
*
* @param array<array-key, TValue>|Traversable<array-key, TValue>|null $values
*/
public function __construct($values = null)
{
$this->internal = new Map();
if (\func_num_args() !== 0) {
$this->add(...$this->normalizeItems($values ?? []));
}
}
/**
* {@inheritDoc}
*
* @param TValue ...$values
*
* @return SetInterface<TValue>
*/
#[Override]
public function add(...$values): SetInterface
{
foreach ($values as $value) {
$this->internal->put($value, null);
}
return $this;
}
/**
* {@inheritDoc}
*
* @return SetInterface<TValue>
*/
#[Override]
public function allocate(int $capacity): SetInterface
{
$this->internal->allocate($capacity);
return $this;
}
/**
* {@inheritDoc}
*
* @param SetInterface<TValue> $set
*
* @return SetInterface<TValue>
*/
#[Override]
public function diff(SetInterface $set): SetInterface
{
return $this->internal->diff($set->getMap())->keys();
}
/**
* {@inheritDoc}
*
* @param SetInterface<TValue> $set
*
* @return SetInterface<TValue>
*/
#[Override]
public function xor(SetInterface $set): SetInterface
{
return $this->internal->xor($set->getMap())->keys();
}
/**
* {@inheritDoc}
*/
#[Override]
public function capacity(): int
{
return $this->internal->capacity();
}
/**
* {@inheritDoc}
*
* @param TValue ...$values
*/
#[Override]
public function contains(...$values): bool
{
foreach ($values as $value) {
if (!$this->internal->hasKey($value)) {
return false;
}
}
return true;
}
/**
* {@inheritDoc}
*
* @return SetInterface<TValue>
*/
#[Override]
public function filter(?callable $callback = null): SetInterface
{
return new static(array_filter($this->toArray(), $callback ?: 'boolval'));
}
/**
* Returns the first value in the set.
*
* @return TValue the first value in the set.
*/
#[Override]
public function first()
{
return $this->internal->first()->key;
}
/**
* Returns the last value in the set.
*
* @return TValue the last value in the set.
*/
#[Override]
public function last()
{
return $this->internal->last()->key;
}
/**
* {@inheritDoc}
*
* @return TValue
*/
#[Override]
public function get(int $position)
{
return $this->internal->skip($position)->key;
}
/**
* {@inheritDoc}
*
* @param SetInterface<TValue> $set
*
* @return SetInterface<TValue>
*/
#[Override]
public function intersect(SetInterface $set): SetInterface
{
return $this->internal->intersect($set->getMap())->keys();
}
/**
* {@inheritDoc}
*/
#[Override]
public function join(?string $glue = null): string
{
return implode($glue ?? '', $this->toArray());
}
/**
* {@inheritDoc}
*/
#[Override]
public function reduce(callable $callback, $initial = null)
{
$carry = $initial;
foreach ($this as $value) {
$carry = $callback($carry, $value);
}
return $carry;
}
/**
* {@inheritDoc}
*
* @param TValue ...$values
*/
#[Override]
public function remove(...$values): void
{
foreach ($values as $value) {
$this->internal->remove($value);
}
}
/**
* Returns a reversed copy of the set.
*
* @return SetInterface<TValue>
*/
#[Override]
public function reverse(): SetInterface
{
$values = array_reverse($this->internal->keys()->toArray());
return new static($values);
}
/**
* {@inheritDoc}
*
* @return SetInterface<TValue>
*/
#[Override]
public function slice(int $offset, ?int $length = null): SetInterface
{
return new static($this->internal->slice($offset, $length)->keys());
}
/**
* {@inheritDoc}
*
* @return SetInterface<TValue>
*/
#[Override]
public function sort(?callable $comparator = null): SetInterface
{
return new static($this->getMap()->ksort($comparator)->keys());
}
/**
* Returns the result of adding all given values to the set.
*
* @param array<array-key, TValue>|Traversable<array-key, TValue> $values
*
* @return SetInterface<TValue>
*/
#[Override]
public function merge($values): SetInterface
{
$merged = $this->copy();
foreach ($values as $value) {
$merged->add($value);
}
return $merged;
}
/**
* Returns the sum of all values in the set.
*
* @return int|float The sum of all the values in the set.
*/
#[Override]
public function sum(): float|int
{
return array_sum($this->toArray());
}
/**
* Creates a new set that contains the values of this set as well as the
* values of another set.
*
* Formally: A ∪ B = {x: x ∈ A ∨ x ∈ B}
*
* @param SetInterface<TValue> $set
*
* @return SetInterface<TValue>
*/
#[Override]
public function union(SetInterface $set): SetInterface
{
$union = new static();
foreach ($this as $value) {
$union->add($value);
}
foreach ($set as $value) {
$union->add($value);
}
return $union;
}
/**
* {@inheritDoc}
*
* @return MapInterface<TValue, null>
*/
#[Override]
public function getMap(): MapInterface
{
return $this->internal;
}
/**
* {@inheritDoc}
*/
#[Override]
public function isEmpty(): bool
{
return $this->internal->isEmpty();
}
/**
* {@inheritDoc}
*
* @return array<array-key, TValue>
*/
#[Override]
public function toArray(): array
{
return iterator_to_array($this);
}
/**
* Get iterator.
*
* @return Generator<int, TValue>
*/
#[ReturnTypeWillChange]
#[Override]
public function getIterator()
{
foreach ($this->internal as $key => $value) {
yield $key;
}
}
/**
* {@inheritDoc}
*
* @param TValue $value
*
* @throws OutOfBoundsException
*/
#[ReturnTypeWillChange]
#[Override]
public function offsetSet($offset, mixed $value): void
{
if ($offset === null) {
$this->add($value);
return;
}
throw new OutOfBoundsException('Out of bounds');
}
/**
* {@inheritDoc}
*/
#[ReturnTypeWillChange]
#[Override]
public function offsetGet($offset)
{
return $this->internal->skip($offset)->key;
}
/**
* {@inheritDoc}
*
* @throws Error
*/
#[ReturnTypeWillChange]
#[Override]
public function offsetExists($offset): bool
{
throw new Error('Not supported');
}
/**
* {@inheritDoc}
*
* @throws Error
*/
#[ReturnTypeWillChange]
#[Override]
public function offsetUnset($offset): void
{
throw new Error('Not supported');
}
}