-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set.php
339 lines (293 loc) · 6.99 KB
/
Set.php
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
<?php
namespace Altair\Structure;
use Altair\Structure\Contracts\CapacityInterface;
use Altair\Structure\Contracts\MapInterface;
use Altair\Structure\Contracts\SetInterface;
use ArrayAccess;
use Error;
use IteratorAggregate;
use OutOfBoundsException;
/**
* 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
*/
class Set implements IteratorAggregate, ArrayAccess, SetInterface, CapacityInterface
{
use Traits\CollectionTrait;
/**
* 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->internal = new Map();
if (func_num_args()) {
$this->add(...$values);
}
}
/**
* {@inheritdoc}
*/
public function add(...$values): SetInterface
{
foreach ($values as $value) {
$this->internal->put($value, null);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function allocate(int $capacity): SetInterface
{
$this->internal->allocate($capacity);
return $this;
}
/**
* {@inheritdoc}
*/
public function diff(SetInterface $set): SetInterface
{
return $this->internal->diff($set->getMap())->keys();
}
/**
* {@inheritdoc}
*/
public function xor(SetInterface $set): SetInterface
{
return $this->internal->xor($set->getMap())->keys();
}
/**
* {@inheritdoc}
*/
public function capacity(): int
{
return $this->internal->capacity();
}
/**
* {@inheritdoc}
*/
public function contains(...$values): bool
{
foreach ($values as $value) {
if (!$this->internal->hasKey($value)) {
return false;
}
}
return true;
}
/**
* {@inheritdoc}
*/
public function filter(callable $callback = null): SetInterface
{
return new static(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->internal->first()->key;
}
/**
* Returns the last value in the set.
*
* @return mixed the last value in the set.
*/
public function last()
{
return $this->internal->last()->key;
}
/**
* {@inheritdoc}
*/
public function get(int $position)
{
return $this->internal->skip($position)->key;
}
/**
* {@inheritdoc}
*/
public function intersect(SetInterface $set): SetInterface
{
return $this->internal->intersect($set->getMap())->keys();
}
/**
* {@inheritdoc}
*/
public function join(string $glue = null): string
{
return implode($glue, $this->toArray());
}
/**
* {@inheritdoc}
*/
public function reduce(callable $callback, $initial = null)
{
$carry = $initial;
foreach ($this as $value) {
$carry = $callback($carry, $value);
}
return $carry;
}
/**
* {@inheritdoc}
*/
public function remove(...$values)
{
foreach ($values as $value) {
$this->internal->remove($value, null);
}
}
/**
* Returns a reversed copy of the set.
*
* @return SetInterface
*/
public function reverse(): SetInterface
{
$values = array_reverse($this->internal->keys()->toArray());
return new static($values);
}
/**
* {@inheritdoc}
*/
public function slice(int $offset, int $length = null): SetInterface
{
return new static($this->internal->slice($offset, $length)->keys());
}
/**
* {@inheritdoc}
*/
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|\Traversable $values
*
* @return SetInterface
*/
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.
*/
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 SetInterface $set
*
* @return SetInterface
*/
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}
*/
public function getMap(): MapInterface
{
return $this->internal;
}
/**
* {@inheritdoc}
*/
public function isEmpty(): bool
{
return $this->internal->isEmpty();
}
/**
* {@inheritdoc}
*/
public function toArray(): array
{
return iterator_to_array($this);
}
/**
* Get iterator.
*/
public function getIterator()
{
foreach ($this->internal 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->internal->skip($offset)->key;
}
/**
* {@inheritdoc}
*
* @throws Error
*/
public function offsetExists($offset)
{
throw new Error();
}
/**
* {@inheritdoc}
*
* @throws Error
*/
public function offsetUnset($offset)
{
throw new Error();
}
}