-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathSet.php
More file actions
460 lines (410 loc) · 10.6 KB
/
Copy pathSet.php
File metadata and controls
460 lines (410 loc) · 10.6 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
namespace Ds;
use Error;
use OutOfBoundsException;
use OutOfRangeException;
/**
* A sequence of unique values.
*
* @package Ds
*/
final class Set implements \IteratorAggregate, \ArrayAccess, Collection
{
use Traits\GenericCollection;
const MIN_CAPACITY = Map::MIN_CAPACITY;
/**
* @var Map internal map to store the values.
*/
private $table;
/**
* Creates a new set using the values of an array or Traversable object.
* The keys of either will not be preserved.
*
* @param array|\Traversable|null $values
*/
public function __construct($values = null)
{
$this->table = new Map();
if (func_num_args()) {
$this->add(...$values);
}
}
/**
* Adds zero or more values to the set.
*
* @param mixed ...$values
*/
public function add(...$values)
{
foreach ($values as $value) {
$this->table->put($value, null);
}
}
/**
* Ensures that enough memory is allocated for a specified capacity. This
* potentially reduces the number of reallocations as the size increases.
*
* @param int $capacity The number of values for which capacity should be
* allocated. Capacity will stay the same if this value
* is less than or equal to the current capacity.
*/
public function allocate(int $capacity)
{
$this->table->allocate($capacity);
}
/**
* Returns the current capacity of the set.
*
* @return int
*/
public function capacity(): int
{
return $this->table->capacity();
}
/**
* Clear all elements in the Set
*/
public function clear()
{
$this->table->clear();
}
/**
* Determines whether the set contains all of zero or more values.
*
* @param mixed ...$values
*
* @return bool true if at least one value was provided and the set
* contains all given values, false otherwise.
*/
public function contains(...$values): bool
{
foreach ($values as $value) {
if ( ! $this->table->hasKey($value)) {
return false;
}
}
return true;
}
/**
* @inheritDoc
*/
public function copy(): Collection
{
return new self($this);
}
/**
* Returns the number of elements in the Stack
*
* @return int
*/
public function count(): int
{
return count($this->table);
}
/**
* Creates a new set using values from this set that aren't in another set.
*
* Formally: A \ B = {x ∈ A | x ∉ B}
*
* @param Set $set
*
* @return Set
*/
public function diff(Set $set): Set
{
return $this->table->diff($set->table)->keys();
}
/**
* Creates a new set using values in either this set or in another set,
* but not in both.
*
* Formally: A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)}
*
* @param Set $set
*
* @return Set
*/
public function xor(Set $set): Set
{
return $this->table->xor($set->table)->keys();
}
/**
* Returns a new set containing only the values for which a callback
* returns true. A boolean test will be used if a callback is not provided.
*
* @param callable|null $callback Accepts a value, returns a boolean:
* true : include the value,
* false: skip the value.
*
* @return Set
*/
public function filter(callable $callback = null): Set
{
return new self(array_filter($this->toArray(), $callback ?: 'boolval'));
}
/**
* Returns the first value in the set.
*
* @return mixed the first value in the set.
*/
public function first()
{
return $this->table->first()->key;
}
/**
* Returns the value at a specified position in the set.
*
* @param int $position
*
* @return mixed|null
*
* @throws OutOfRangeException
*/
public function get(int $position)
{
return $this->table->skip($position)->key;
}
/**
* Creates a new set using values common to both this set and another set.
*
* In other words, returns a copy of this set with all values removed that
* aren't in the other set.
*
* Formally: A ∩ B = {x : x ∈ A ∧ x ∈ B}
*
* @param Set $set
*
* @return Set
*/
public function intersect(Set $set): Set
{
return $this->table->intersect($set->table)->keys();
}
/**
* @inheritDoc
*/
public function isEmpty(): bool
{
return $this->table->isEmpty();
}
/**
* Joins all values of the set into a string, adding an optional 'glue'
* between them. Returns an empty string if the set is empty.
*
* @param string $glue
*
* @return string
*/
public function join(string $glue = null): string
{
return implode($glue, $this->toArray());
}
/**
* Returns the last value in the set.
*
* @return mixed the last value in the set.
*/
public function last()
{
return $this->table->last()->key;
}
/**
* Iteratively reduces the set to a single value using a callback.
*
* @param callable $callback Accepts the carry and current value, and
* returns an updated carry value.
*
* @param mixed|null $initial Optional initial carry value.
*
* @return mixed The carry value of the final iteration, or the initial
* value if the set was empty.
*/
public function reduce(callable $callback, $initial = null)
{
$carry = $initial;
foreach ($this as $value) {
$carry = $callback($carry, $value);
}
return $carry;
}
/**
* Removes zero or more values from the set.
*
* @param mixed ...$values
*/
public function remove(...$values)
{
foreach ($values as $value) {
$this->table->remove($value, null);
}
}
/**
* Reverses the set in-place.
*/
public function reverse()
{
$this->table->reverse();
}
/**
* Returns a reversed copy of the set.
*
* @return Set
*/
public function reversed(): Set
{
$reversed = $this->copy();
$reversed->table->reverse();
return $reversed;
}
/**
* Returns a subset of a given length starting at a specified offset.
*
* @param int $offset If the offset is non-negative, the set will start
* at that offset in the set. If offset is negative,
* the set will start that far from the end.
*
* @param int $length If a length is given and is positive, the resulting
* set will have up to that many values in it.
* If the requested length results in an overflow, only
* values up to the end of the set will be included.
*
* If a length is given and is negative, the set
* will stop that many values from the end.
*
* If a length is not provided, the resulting set
* will contains all values between the offset and the
* end of the set.
*
* @return Set
*/
public function slice(int $offset, int $length = null): Set
{
$sliced = new self();
$sliced->table = $this->table->slice($offset, $length);
return $sliced;
}
/**
* Sorts the set in-place, based on an optional callable comparator.
*
* @param callable|null $comparator Accepts two values to be compared.
* Should return the result of a <=> b.
*/
public function sort(callable $comparator = null)
{
$this->table->ksort($comparator);
}
/**
* Returns a sorted copy of the set, based on an optional callable
* comparator. Natural ordering will be used if a comparator is not given.
*
* @param callable|null $comparator Accepts two values to be compared.
* Should return the result of a <=> b.
*
* @return Set
*/
public function sorted(callable $comparator = null): Set
{
$sorted = $this->copy();
$sorted->table->ksort($comparator);
return $sorted;
}
/**
* Returns the result of adding all given values to the set.
*
* @param array|\Traversable $values
*
* @return \Ds\Set
*/
public function merge($values): Set
{
$merged = $this->copy();
foreach ($values as $value) {
$merged->add($value);
}
return $merged;
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return iterator_to_array($this);
}
/**
* Returns the sum of all values in the set.
*
* @return int|float The sum of all the values in the set.
*/
public function sum()
{
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 Set $set
*
* @return Set
*/
public function union(Set $set): Set
{
$union = new self();
foreach ($this as $value) {
$union->add($value);
}
foreach ($set as $value) {
$union->add($value);
}
return $union;
}
/**
* Get iterator
*/
public function getIterator()
{
foreach ($this->table as $key => $value) {
yield $key;
}
}
/**
* @inheritdoc
*
* @throws OutOfBoundsException
*/
public function offsetSet($offset, $value)
{
if ($offset === null) {
$this->add($value);
return;
}
throw new OutOfBoundsException();
}
/**
* @inheritdoc
*/
public function offsetGet($offset)
{
return $this->table->skip($offset)->key;
}
/**
* @inheritdoc
*
* @throws Error
*/
public function offsetExists($offset)
{
throw new Error();
}
/**
* @inheritdoc
*
* @throws Error
*/
public function offsetUnset($offset)
{
throw new Error();
}
}