1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MabeEnum; |
6
|
|
|
|
7
|
|
|
use Countable; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Iterator; |
10
|
|
|
use IteratorAggregate; |
11
|
|
|
use OutOfBoundsException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A set of enumerators of the given enumeration (EnumSet<T>) |
15
|
|
|
* based on an integer or binary bitset depending of given enumeration size. |
16
|
|
|
* |
17
|
|
|
* @copyright 2019 Marc Bennewitz |
18
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
19
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
20
|
|
|
*/ |
|
|
|
|
21
|
|
|
class EnumSet implements IteratorAggregate, Countable |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The classname of the Enumeration |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $enumeration; |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Number of enumerators defined in the enumeration |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $enumerationCount; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Integer or binary (little endian) bitset |
37
|
|
|
* @var int|string |
38
|
|
|
*/ |
39
|
|
|
private $bitset = 0; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/**#@+ |
42
|
|
|
* Defines private method names to be called depended of how the bitset type was set too. |
43
|
|
|
* ... Integer or binary bitset. |
44
|
|
|
* ... *Int or *Bin method |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $fnDoCount = 'doCountInt'; |
|
|
|
|
49
|
|
|
private $fnDoGetOrdinals = 'doGetOrdinalsInt'; |
|
|
|
|
50
|
|
|
private $fnDoGetBit = 'doGetBitInt'; |
|
|
|
|
51
|
|
|
private $fnDoSetBit = 'doSetBitInt'; |
|
|
|
|
52
|
|
|
private $fnDoUnsetBit = 'doUnsetBitInt'; |
|
|
|
|
53
|
|
|
private $fnDoGetBinaryBitsetLe = 'doGetBinaryBitsetLeInt'; |
|
|
|
|
54
|
|
|
private $fnDoSetBinaryBitsetLe = 'doSetBinaryBitsetLeInt'; |
|
|
|
|
55
|
|
|
/**#@-*/ |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructor |
59
|
|
|
* |
60
|
|
|
* @param string $enumeration The classname of the enumeration |
|
|
|
|
61
|
|
|
* @throws InvalidArgumentException |
|
|
|
|
62
|
|
|
*/ |
63
|
80 |
|
public function __construct(string $enumeration) |
64
|
|
|
{ |
65
|
80 |
|
if (!\is_subclass_of($enumeration, Enum::class)) { |
66
|
1 |
|
throw new InvalidArgumentException(\sprintf( |
67
|
1 |
|
'%s can handle subclasses of %s only', |
68
|
1 |
|
__METHOD__, |
69
|
1 |
|
Enum::class |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
79 |
|
$this->enumeration = $enumeration; |
74
|
79 |
|
$this->enumerationCount = \count($enumeration::getConstants()); |
75
|
|
|
|
76
|
|
|
// By default the bitset is initialized as integer bitset |
77
|
|
|
// in case the enumeraton has more enumerators then integer bits |
78
|
|
|
// we will switch this into a binary bitset |
79
|
79 |
|
if ($this->enumerationCount > \PHP_INT_SIZE * 8) { |
80
|
|
|
// init binary bitset with zeros |
81
|
21 |
|
$this->bitset = \str_repeat("\0", (int)\ceil($this->enumerationCount / 8)); |
82
|
|
|
|
83
|
|
|
// switch internal binary bitset functions |
84
|
21 |
|
$this->fnDoCount = 'doCountBin'; |
85
|
21 |
|
$this->fnDoGetOrdinals = 'doGetOrdinalsBin'; |
86
|
21 |
|
$this->fnDoGetBit = 'doGetBitBin'; |
87
|
21 |
|
$this->fnDoSetBit = 'doSetBitBin'; |
88
|
21 |
|
$this->fnDoUnsetBit = 'doUnsetBitBin'; |
89
|
21 |
|
$this->fnDoGetBinaryBitsetLe = 'doGetBinaryBitsetLeBin'; |
90
|
21 |
|
$this->fnDoSetBinaryBitsetLe = 'doSetBinaryBitsetLeBin'; |
91
|
|
|
} |
92
|
79 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the classname of the enumeration |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
1 |
|
public function getEnumeration(): string |
99
|
|
|
{ |
100
|
1 |
|
return $this->enumeration; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Attach a new enumerator or overwrite an existing one |
105
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
106
|
|
|
* @return void |
107
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
108
|
|
|
*/ |
109
|
50 |
|
public function attach($enumerator): void |
110
|
|
|
{ |
111
|
50 |
|
$this->{$this->fnDoSetBit}(($this->enumeration)::get($enumerator)->getOrdinal()); |
112
|
50 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Detach the given enumerator |
116
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
117
|
|
|
* @return void |
118
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
119
|
|
|
*/ |
120
|
4 |
|
public function detach($enumerator): void |
121
|
|
|
{ |
122
|
4 |
|
$this->{$this->fnDoUnsetBit}(($this->enumeration)::get($enumerator)->getOrdinal()); |
123
|
4 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Test if the given enumerator was attached |
127
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
128
|
|
|
* @return bool |
|
|
|
|
129
|
|
|
*/ |
130
|
11 |
|
public function contains($enumerator): bool |
131
|
|
|
{ |
132
|
11 |
|
return $this->{$this->fnDoGetBit}(($this->enumeration)::get($enumerator)->getOrdinal()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/* IteratorAggregate */ |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create and return a new iterator |
139
|
|
|
* @return Iterator |
140
|
|
|
*/ |
141
|
|
|
public function getIterator(): Iterator |
142
|
|
|
{ |
143
|
|
|
return new class($this, $this->enumeration, $this->enumerationCount, $this->bitset) implements Iterator { |
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @var EnumSet |
146
|
|
|
*/ |
147
|
|
|
private $enumSet; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @var string |
151
|
|
|
*/ |
152
|
|
|
private $enumeration; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @var int |
156
|
|
|
*/ |
157
|
|
|
private $enumerationCount; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @var int|string |
161
|
|
|
*/ |
162
|
|
|
private $bitset; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @var int |
166
|
|
|
*/ |
167
|
|
|
private $ordinal = -1; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @var string |
171
|
|
|
*/ |
172
|
|
|
private $fnDoRewind = 'doRewindInt'; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Constructor. |
176
|
|
|
* @param EnumSet $enumSet |
|
|
|
|
177
|
|
|
* @param string $enumeration |
|
|
|
|
178
|
|
|
* @param int $enumerationCount |
|
|
|
|
179
|
|
|
* @param int|string $bitset |
|
|
|
|
180
|
|
|
*/ |
181
|
32 |
|
public function __construct(EnumSet $enumSet, $enumeration, $enumerationCount, $bitset) |
|
|
|
|
182
|
|
|
{ |
183
|
32 |
|
$this->enumSet = $enumSet; |
184
|
32 |
|
$this->enumeration = $enumeration; |
185
|
32 |
|
$this->enumerationCount = $enumerationCount; |
186
|
32 |
|
$this->bitset = $bitset; |
187
|
|
|
|
188
|
|
|
// By default the bitset is initialized as integer bitset |
189
|
|
|
// in case the enumeraton has more enumerators then integer bits |
190
|
|
|
// we will switch this into a binary bitset |
191
|
32 |
|
if ($this->enumerationCount > \PHP_INT_SIZE * 8) { |
192
|
11 |
|
$this->fnDoRewind = 'doRewindBin'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// go to the first valid iterator position (if any) |
196
|
32 |
|
$this->next(); |
197
|
32 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the current enumerator |
201
|
|
|
* @return Enum Returns the current enumerator object |
202
|
|
|
* @throws OutOfBoundsException On an invalid iterator position |
|
|
|
|
203
|
|
|
*/ |
204
|
30 |
|
public function current(): Enum |
205
|
|
|
{ |
206
|
30 |
|
if (!$this->valid()) { |
207
|
11 |
|
throw new OutOfBoundsException('Invalid iterator position'); |
208
|
|
|
} |
209
|
|
|
|
210
|
24 |
|
return ($this->enumeration)::byOrdinal($this->ordinal); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the ordinal number of the current iterator position |
215
|
|
|
* @return int The ordinal number of the current enumerator |
|
|
|
|
216
|
|
|
* @throws OutOfBoundsException On an invalid iterator position |
|
|
|
|
217
|
|
|
*/ |
218
|
23 |
|
public function key(): int |
219
|
|
|
{ |
220
|
23 |
|
if (!$this->valid()) { |
221
|
6 |
|
throw new OutOfBoundsException('Invalid iterator position'); |
222
|
|
|
} |
223
|
|
|
|
224
|
22 |
|
return $this->ordinal; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Go to the next valid iterator position. |
229
|
|
|
* If no valid iterator position is found the iterator position will be the last possible + 1. |
|
|
|
|
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
32 |
|
public function next(): void |
233
|
|
|
{ |
234
|
|
|
do { |
235
|
32 |
|
if (++$this->ordinal >= $this->enumerationCount) { |
236
|
31 |
|
$this->ordinal = $this->enumerationCount; |
237
|
31 |
|
return; |
238
|
|
|
} |
239
|
32 |
|
} while (!$this->enumSet->getBit($this->ordinal)); |
240
|
24 |
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Go to the first valid iterator position. |
244
|
|
|
* If no valid iterator position was found the iterator position will be 0. |
245
|
|
|
* @return void |
246
|
|
|
* @uses doRewindBin() |
247
|
|
|
* @uses doRewindInt() |
248
|
|
|
*/ |
249
|
19 |
|
public function rewind(): void |
250
|
|
|
{ |
251
|
19 |
|
$this->{$this->fnDoRewind}(); |
252
|
19 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Go to the first valid iterator position. |
256
|
|
|
* If no valid iterator position was found the iterator position will be 0. |
257
|
|
|
* |
258
|
|
|
* This is the binary bitset implementation. |
259
|
|
|
* |
260
|
|
|
* @return void |
261
|
|
|
* @see rewind() |
262
|
|
|
* @see doRewindInt() |
263
|
|
|
*/ |
264
|
7 |
|
private function doRewindBin(): void |
265
|
|
|
{ |
266
|
7 |
|
if (\ltrim($this->bitset, "\0") !== '') { |
267
|
5 |
|
$this->ordinal = -1; |
268
|
5 |
|
$this->next(); |
269
|
|
|
} else { |
270
|
2 |
|
$this->ordinal = 0; |
271
|
|
|
} |
272
|
7 |
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Go to the first valid iterator position. |
276
|
|
|
* If no valid iterator position was found the iterator position will be 0. |
277
|
|
|
* |
278
|
|
|
* This is the integer bitset implementation. |
279
|
|
|
* |
280
|
|
|
* @return void |
281
|
|
|
* @see rewind() |
282
|
|
|
* @see doRewindBin() |
283
|
|
|
*/ |
284
|
12 |
|
private function doRewindInt(): void |
285
|
|
|
{ |
286
|
12 |
|
if ($this->bitset) { |
287
|
8 |
|
$this->ordinal = -1; |
288
|
8 |
|
$this->next(); |
289
|
|
|
} else { |
290
|
4 |
|
$this->ordinal = 0; |
291
|
|
|
} |
292
|
12 |
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Test if the current iterator position is valid |
296
|
|
|
* @return bool |
|
|
|
|
297
|
|
|
*/ |
298
|
32 |
|
public function valid(): bool |
299
|
|
|
{ |
300
|
32 |
|
return $this->ordinal !== $this->enumerationCount && $this->enumSet->getBit($this->ordinal); |
|
|
|
|
301
|
|
|
} |
302
|
|
|
}; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/* Countable */ |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Count the number of elements |
309
|
|
|
* |
310
|
|
|
* @return int |
|
|
|
|
311
|
|
|
* @uses doCountBin() |
312
|
|
|
* @uses doCountInt() |
313
|
|
|
*/ |
314
|
15 |
|
public function count(): int |
315
|
|
|
{ |
316
|
15 |
|
return $this->{$this->fnDoCount}(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Count the number of elements. |
321
|
|
|
* |
322
|
|
|
* This is the binary bitset implementation. |
323
|
|
|
* |
324
|
|
|
* @return int |
|
|
|
|
325
|
|
|
* @see count() |
326
|
|
|
* @see doCountInt() |
327
|
|
|
*/ |
328
|
7 |
|
private function doCountBin(): int |
329
|
|
|
{ |
330
|
7 |
|
$count = 0; |
331
|
7 |
|
$bitset = $this->bitset; |
332
|
7 |
|
$byteLen = \strlen($bitset); |
333
|
7 |
|
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) { |
334
|
7 |
|
if ($bitset[$bytePos] === "\0") { |
335
|
|
|
// fast skip null byte |
336
|
5 |
|
continue; |
337
|
|
|
} |
338
|
|
|
|
339
|
7 |
|
$ord = \ord($bitset[$bytePos]); |
340
|
7 |
|
if ($ord & 0b00000001) ++$count; |
|
|
|
|
341
|
7 |
|
if ($ord & 0b00000010) ++$count; |
|
|
|
|
342
|
7 |
|
if ($ord & 0b00000100) ++$count; |
|
|
|
|
343
|
7 |
|
if ($ord & 0b00001000) ++$count; |
|
|
|
|
344
|
7 |
|
if ($ord & 0b00010000) ++$count; |
|
|
|
|
345
|
7 |
|
if ($ord & 0b00100000) ++$count; |
|
|
|
|
346
|
7 |
|
if ($ord & 0b01000000) ++$count; |
|
|
|
|
347
|
7 |
|
if ($ord & 0b10000000) ++$count; |
|
|
|
|
348
|
|
|
} |
349
|
7 |
|
return $count; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Count the number of elements. |
354
|
|
|
* |
355
|
|
|
* This is the integer bitset implementation. |
356
|
|
|
* |
357
|
|
|
* @return int |
|
|
|
|
358
|
|
|
* @see count() |
359
|
|
|
* @see doCountBin() |
360
|
|
|
*/ |
361
|
8 |
|
private function doCountInt(): int |
362
|
|
|
{ |
363
|
8 |
|
$count = 0; |
364
|
8 |
|
$bitset = $this->bitset; |
365
|
|
|
|
366
|
|
|
// PHP does not support right shift unsigned |
367
|
8 |
|
if ($bitset < 0) { |
368
|
2 |
|
$count = 1; |
369
|
2 |
|
$bitset = $bitset & \PHP_INT_MAX; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
// iterate byte by byte and count set bits |
373
|
8 |
|
$phpIntBitSize = \PHP_INT_SIZE * 8; |
374
|
8 |
|
for ($bitPos = 0; $bitPos < $phpIntBitSize; $bitPos += 8) { |
375
|
8 |
|
$bitChk = 0xff << $bitPos; |
376
|
8 |
|
$byte = $bitset & $bitChk; |
377
|
8 |
|
if ($byte) { |
378
|
7 |
|
$byte = $byte >> $bitPos; |
379
|
7 |
|
if ($byte & 0b00000001) ++$count; |
|
|
|
|
380
|
7 |
|
if ($byte & 0b00000010) ++$count; |
|
|
|
|
381
|
7 |
|
if ($byte & 0b00000100) ++$count; |
|
|
|
|
382
|
7 |
|
if ($byte & 0b00001000) ++$count; |
|
|
|
|
383
|
7 |
|
if ($byte & 0b00010000) ++$count; |
|
|
|
|
384
|
7 |
|
if ($byte & 0b00100000) ++$count; |
|
|
|
|
385
|
7 |
|
if ($byte & 0b01000000) ++$count; |
|
|
|
|
386
|
7 |
|
if ($byte & 0b10000000) ++$count; |
|
|
|
|
387
|
|
|
} |
388
|
|
|
|
389
|
8 |
|
if ($bitset <= $bitChk) { |
390
|
7 |
|
break; |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
394
|
8 |
|
return $count; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Check if this EnumSet is the same as other |
399
|
|
|
* @param EnumSet $other |
|
|
|
|
400
|
|
|
* @return bool |
|
|
|
|
401
|
|
|
*/ |
402
|
3 |
|
public function isEqual(EnumSet $other): bool |
403
|
|
|
{ |
404
|
3 |
|
return $this->enumeration === $other->enumeration |
405
|
3 |
|
&& $this->bitset === $other->bitset; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Check if this EnumSet is a subset of other |
410
|
|
|
* @param EnumSet $other |
|
|
|
|
411
|
|
|
* @return bool |
|
|
|
|
412
|
|
|
*/ |
413
|
4 |
|
public function isSubset(EnumSet $other): bool |
414
|
|
|
{ |
415
|
4 |
|
return $this->enumeration === $other->enumeration |
416
|
4 |
|
&& ($this->bitset & $other->bitset) === $this->bitset; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Check if this EnumSet is a superset of other |
421
|
|
|
* @param EnumSet $other |
|
|
|
|
422
|
|
|
* @return bool |
|
|
|
|
423
|
|
|
*/ |
424
|
4 |
|
public function isSuperset(EnumSet $other): bool |
425
|
|
|
{ |
426
|
4 |
|
return $this->enumeration === $other->enumeration |
427
|
4 |
|
&& ($this->bitset | $other->bitset) === $this->bitset; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Produce a new set with enumerators from both this and other (this | other) |
432
|
|
|
* |
433
|
|
|
* @param EnumSet $other EnumSet of the same enumeration to produce the union |
|
|
|
|
434
|
|
|
* @return EnumSet |
435
|
|
|
* @throws InvalidArgumentException If $other doesn't match the enumeration |
|
|
|
|
436
|
|
|
*/ |
437
|
2 |
|
public function union(EnumSet $other): EnumSet |
438
|
|
|
{ |
439
|
2 |
|
if ($this->enumeration !== $other->enumeration) { |
440
|
1 |
|
throw new InvalidArgumentException(\sprintf( |
441
|
1 |
|
'Other should be of the same enumeration as this %s', |
442
|
1 |
|
$this->enumeration |
443
|
|
|
)); |
444
|
|
|
} |
445
|
|
|
|
446
|
1 |
|
$clone = clone $this; |
447
|
1 |
|
$clone->bitset = $this->bitset | $other->bitset; |
448
|
1 |
|
return $clone; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Produce a new set with enumerators common to both this and other (this & other) |
453
|
|
|
* |
454
|
|
|
* @param EnumSet $other EnumSet of the same enumeration to produce the intersect |
|
|
|
|
455
|
|
|
* @return EnumSet |
456
|
|
|
* @throws InvalidArgumentException If $other doesn't match the enumeration |
|
|
|
|
457
|
|
|
*/ |
458
|
2 |
|
public function intersect(EnumSet $other): EnumSet |
459
|
|
|
{ |
460
|
2 |
|
if ($this->enumeration !== $other->enumeration) { |
461
|
1 |
|
throw new InvalidArgumentException(\sprintf( |
462
|
1 |
|
'Other should be of the same enumeration as this %s', |
463
|
1 |
|
$this->enumeration |
464
|
|
|
)); |
465
|
|
|
} |
466
|
|
|
|
467
|
1 |
|
$clone = clone $this; |
468
|
1 |
|
$clone->bitset = $this->bitset & $other->bitset; |
469
|
1 |
|
return $clone; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Produce a new set with enumerators in this but not in other (this - other) |
474
|
|
|
* |
475
|
|
|
* @param EnumSet $other EnumSet of the same enumeration to produce the diff |
|
|
|
|
476
|
|
|
* @return EnumSet |
477
|
|
|
* @throws InvalidArgumentException If $other doesn't match the enumeration |
|
|
|
|
478
|
|
|
*/ |
479
|
2 |
|
public function diff(EnumSet $other): EnumSet |
480
|
|
|
{ |
481
|
2 |
|
if ($this->enumeration !== $other->enumeration) { |
482
|
1 |
|
throw new InvalidArgumentException(\sprintf( |
483
|
1 |
|
'Other should be of the same enumeration as this %s', |
484
|
1 |
|
$this->enumeration |
485
|
|
|
)); |
486
|
|
|
} |
487
|
|
|
|
488
|
1 |
|
$clone = clone $this; |
489
|
1 |
|
$clone->bitset = $this->bitset & ~$other->bitset; |
490
|
1 |
|
return $clone; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Produce a new set with enumerators in either this and other but not in both (this ^ other) |
495
|
|
|
* |
496
|
|
|
* @param EnumSet $other EnumSet of the same enumeration to produce the symmetric difference |
|
|
|
|
497
|
|
|
* @return EnumSet |
498
|
|
|
* @throws InvalidArgumentException If $other doesn't match the enumeration |
|
|
|
|
499
|
|
|
*/ |
500
|
2 |
|
public function symDiff(EnumSet $other): EnumSet |
501
|
|
|
{ |
502
|
2 |
|
if ($this->enumeration !== $other->enumeration) { |
503
|
1 |
|
throw new InvalidArgumentException(\sprintf( |
504
|
1 |
|
'Other should be of the same enumeration as this %s', |
505
|
1 |
|
$this->enumeration |
506
|
|
|
)); |
507
|
|
|
} |
508
|
|
|
|
509
|
1 |
|
$clone = clone $this; |
510
|
1 |
|
$clone->bitset = $this->bitset ^ $other->bitset; |
511
|
1 |
|
return $clone; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* Get ordinal numbers of the defined enumerators as array |
516
|
|
|
* @return int[] |
517
|
|
|
* @uses doGetOrdinalsBin() |
518
|
|
|
* @uses doGetOrdinalsInt() |
519
|
|
|
*/ |
520
|
9 |
|
public function getOrdinals(): array |
521
|
|
|
{ |
522
|
9 |
|
return $this->{$this->fnDoGetOrdinals}(); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* Get ordinal numbers of the defined enumerators as array. |
527
|
|
|
* |
528
|
|
|
* This is the binary bitset implementation. |
529
|
|
|
* |
530
|
|
|
* @return int[] |
531
|
|
|
* @see getOrdinals() |
532
|
|
|
* @see goGetOrdinalsInt() |
533
|
|
|
*/ |
534
|
1 |
|
private function doGetOrdinalsBin(): array |
535
|
|
|
{ |
536
|
1 |
|
$ordinals = []; |
537
|
1 |
|
$bitset = $this->bitset; |
538
|
1 |
|
$byteLen = \strlen($bitset); |
539
|
1 |
|
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) { |
540
|
1 |
|
if ($bitset[$bytePos] === "\0") { |
541
|
|
|
// fast skip null byte |
542
|
1 |
|
continue; |
543
|
|
|
} |
544
|
|
|
|
545
|
1 |
|
$ord = \ord($bitset[$bytePos]); |
546
|
1 |
|
for ($bitPos = 0; $bitPos < 8; ++$bitPos) { |
547
|
1 |
|
if ($ord & (1 << $bitPos)) { |
548
|
1 |
|
$ordinals[] = $bytePos * 8 + $bitPos; |
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
} |
552
|
1 |
|
return $ordinals; |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Get ordinal numbers of the defined enumerators as array. |
557
|
|
|
* |
558
|
|
|
* This is the integer bitset implementation. |
559
|
|
|
* |
560
|
|
|
* @return int[] |
561
|
|
|
* @see getOrdinals() |
562
|
|
|
* @see doGetOrdinalsBin() |
563
|
|
|
*/ |
564
|
8 |
|
private function doGetOrdinalsInt(): array |
565
|
|
|
{ |
566
|
8 |
|
$ordinals = []; |
567
|
8 |
|
$ordinalMax = $this->enumerationCount; |
568
|
8 |
|
$bitset = $this->bitset; |
569
|
8 |
|
for ($ord = 0; $ord < $ordinalMax; ++$ord) { |
570
|
8 |
|
if ($bitset & (1 << $ord)) { |
571
|
8 |
|
$ordinals[] = $ord; |
572
|
|
|
} |
573
|
|
|
} |
574
|
8 |
|
return $ordinals; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* Get values of the defined enumerators as array |
579
|
|
|
* @return mixed[] |
580
|
|
|
*/ |
581
|
5 |
|
public function getValues(): array |
582
|
|
|
{ |
583
|
5 |
|
$enumeration = $this->enumeration; |
584
|
5 |
|
$values = []; |
585
|
5 |
|
foreach ($this->getOrdinals() as $ord) { |
586
|
5 |
|
$values[] = $enumeration::byOrdinal($ord)->getValue(); |
587
|
|
|
} |
588
|
5 |
|
return $values; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* Get names of the defined enumerators as array |
593
|
|
|
* @return string[] |
594
|
|
|
*/ |
595
|
1 |
|
public function getNames(): array |
596
|
|
|
{ |
597
|
1 |
|
$enumeration = $this->enumeration; |
598
|
1 |
|
$names = []; |
599
|
1 |
|
foreach ($this->getOrdinals() as $ord) { |
600
|
1 |
|
$names[] = $enumeration::byOrdinal($ord)->getName(); |
601
|
|
|
} |
602
|
1 |
|
return $names; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* Get the defined enumerators as array |
607
|
|
|
* @return Enum[] |
608
|
|
|
*/ |
609
|
1 |
|
public function getEnumerators(): array |
610
|
|
|
{ |
611
|
1 |
|
$enumeration = $this->enumeration; |
612
|
1 |
|
$enumerators = []; |
613
|
1 |
|
foreach ($this->getOrdinals() as $ord) { |
614
|
1 |
|
$enumerators[] = $enumeration::byOrdinal($ord); |
615
|
|
|
} |
616
|
1 |
|
return $enumerators; |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* Get binary bitset in little-endian order |
621
|
|
|
* |
622
|
|
|
* @return string |
623
|
|
|
* @uses doGetBinaryBitsetLeBin() |
624
|
|
|
* @uses doGetBinaryBitsetLeInt() |
625
|
|
|
*/ |
626
|
6 |
|
public function getBinaryBitsetLe(): string |
627
|
|
|
{ |
628
|
6 |
|
return $this->{$this->fnDoGetBinaryBitsetLe}(); |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
/** |
632
|
|
|
* Get binary bitset in little-endian order. |
633
|
|
|
* |
634
|
|
|
* This is the binary bitset implementation. |
635
|
|
|
* |
636
|
|
|
* @return string |
637
|
|
|
* @see getBinaryBitsetLe() |
638
|
|
|
* @see doGetBinaryBitsetLeInt() |
639
|
|
|
*/ |
640
|
4 |
|
private function doGetBinaryBitsetLeBin(): string |
641
|
|
|
{ |
642
|
4 |
|
return $this->bitset; |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Get binary bitset in little-endian order. |
647
|
|
|
* |
648
|
|
|
* This is the integer bitset implementation. |
649
|
|
|
* |
650
|
|
|
* @return string |
651
|
|
|
* @see getBinaryBitsetLe() |
652
|
|
|
* @see doGetBinaryBitsetLeBin() |
653
|
|
|
*/ |
654
|
2 |
|
private function doGetBinaryBitsetLeInt(): string |
655
|
|
|
{ |
656
|
2 |
|
$bin = \pack(\PHP_INT_SIZE === 8 ? 'P' : 'V', $this->bitset); |
657
|
2 |
|
return \substr($bin, 0, (int)\ceil($this->enumerationCount / 8)); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* Set binary bitset in little-endian order |
662
|
|
|
* |
663
|
|
|
* NOTE: It resets the current position of the iterator |
664
|
|
|
* |
665
|
|
|
* @param string $bitset |
|
|
|
|
666
|
|
|
* @return void |
667
|
|
|
* @throws InvalidArgumentException On out-of-range bits given as input bitset |
|
|
|
|
668
|
|
|
* @uses doSetBinaryBitsetLeBin() |
669
|
|
|
* @uses doSetBinaryBitsetLeInt() |
670
|
|
|
*/ |
671
|
12 |
|
public function setBinaryBitsetLe(string $bitset): void |
672
|
|
|
{ |
673
|
12 |
|
$this->{$this->fnDoSetBinaryBitsetLe}($bitset); |
674
|
6 |
|
} |
675
|
|
|
|
676
|
|
|
/** |
677
|
|
|
* Set binary bitset in little-endian order |
678
|
|
|
* |
679
|
|
|
* NOTE: It resets the current position of the iterator |
680
|
|
|
* |
681
|
|
|
* @param string $bitset |
|
|
|
|
682
|
|
|
* @return void |
683
|
|
|
* @throws InvalidArgumentException On out-of-range bits given as input bitset |
|
|
|
|
684
|
|
|
* @see setBinaryBitsetLeBin() |
685
|
|
|
* @see doSetBinaryBitsetLeInt() |
686
|
|
|
*/ |
687
|
7 |
|
private function doSetBinaryBitsetLeBin(string $bitset): void |
688
|
|
|
{ |
689
|
7 |
|
$size = \strlen($this->bitset); |
690
|
7 |
|
$sizeIn = \strlen($bitset); |
691
|
|
|
|
692
|
7 |
|
if ($sizeIn < $size) { |
693
|
|
|
// add "\0" if the given bitset is not long enough |
694
|
1 |
|
$bitset .= \str_repeat("\0", $size - $sizeIn); |
695
|
6 |
|
} elseif ($sizeIn > $size) { |
696
|
2 |
|
if (\ltrim(\substr($bitset, $size), "\0") !== '') { |
697
|
1 |
|
throw new InvalidArgumentException('out-of-range bits detected'); |
698
|
|
|
} |
699
|
1 |
|
$bitset = \substr($bitset, 0, $size); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
// truncate out-of-range bits of last byte |
703
|
6 |
|
$lastByteMaxOrd = $this->enumerationCount % 8; |
704
|
6 |
|
if ($lastByteMaxOrd !== 0) { |
705
|
6 |
|
$lastByte = $bitset[-1]; |
706
|
6 |
|
$lastByteExpected = \chr((1 << $lastByteMaxOrd) - 1) & $lastByte; |
707
|
6 |
|
if ($lastByte !== $lastByteExpected) { |
708
|
2 |
|
throw new InvalidArgumentException('out-of-range bits detected'); |
709
|
|
|
} |
710
|
|
|
|
711
|
4 |
|
$this->bitset = \substr($bitset, 0, -1) . $lastByteExpected; |
712
|
|
|
} |
713
|
|
|
|
714
|
4 |
|
$this->bitset = $bitset; |
715
|
4 |
|
} |
716
|
|
|
|
717
|
|
|
/** |
718
|
|
|
* Set binary bitset in little-endian order |
719
|
|
|
* |
720
|
|
|
* NOTE: It resets the current position of the iterator |
721
|
|
|
* |
722
|
|
|
* @param string $bitset |
|
|
|
|
723
|
|
|
* @return void |
724
|
|
|
* @throws InvalidArgumentException On out-of-range bits given as input bitset |
|
|
|
|
725
|
|
|
* @see setBinaryBitsetLeBin() |
726
|
|
|
* @see doSetBinaryBitsetLeBin() |
727
|
|
|
*/ |
728
|
5 |
|
private function doSetBinaryBitsetLeInt(string $bitset): void |
729
|
|
|
{ |
730
|
5 |
|
$len = \strlen($bitset); |
731
|
5 |
|
$int = 0; |
732
|
5 |
|
for ($i = 0; $i < $len; ++$i) { |
733
|
5 |
|
$ord = \ord($bitset[$i]); |
734
|
|
|
|
735
|
5 |
|
if ($ord && $i > \PHP_INT_SIZE - 1) { |
736
|
1 |
|
throw new InvalidArgumentException('out-of-range bits detected'); |
737
|
|
|
} |
738
|
|
|
|
739
|
5 |
|
$int |= $ord << (8 * $i); |
740
|
|
|
} |
741
|
|
|
|
742
|
4 |
|
if ($int & (~0 << $this->enumerationCount)) { |
743
|
2 |
|
throw new InvalidArgumentException('out-of-range bits detected'); |
744
|
|
|
} |
745
|
|
|
|
746
|
2 |
|
$this->bitset = $int; |
747
|
2 |
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Get binary bitset in big-endian order |
751
|
|
|
* |
752
|
|
|
* @return string |
753
|
|
|
*/ |
754
|
1 |
|
public function getBinaryBitsetBe(): string |
755
|
|
|
{ |
756
|
1 |
|
return \strrev($this->bitset); |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
/** |
760
|
|
|
* Set binary bitset in big-endian order |
761
|
|
|
* |
762
|
|
|
* NOTE: It resets the current position of the iterator |
763
|
|
|
* |
764
|
|
|
* @param string $bitset |
|
|
|
|
765
|
|
|
* @return void |
766
|
|
|
* @throws InvalidArgumentException On out-of-range bits given as input bitset |
|
|
|
|
767
|
|
|
*/ |
768
|
1 |
|
public function setBinaryBitsetBe(string $bitset): void |
769
|
|
|
{ |
770
|
1 |
|
$this->setBinaryBitsetLe(\strrev($bitset)); |
771
|
1 |
|
} |
772
|
|
|
|
773
|
|
|
/** |
774
|
|
|
* Get a bit at the given ordinal number |
775
|
|
|
* |
776
|
|
|
* @param int $ordinal Ordinal number of bit to get |
|
|
|
|
777
|
|
|
* @return bool |
|
|
|
|
778
|
|
|
* @throws InvalidArgumentException If the given ordinal number is out-of-range |
|
|
|
|
779
|
|
|
* @uses doGetBitBin() |
780
|
|
|
* @uses doGetBitInt() |
781
|
|
|
*/ |
782
|
35 |
|
public function getBit(int $ordinal): bool |
783
|
|
|
{ |
784
|
35 |
|
if ($ordinal < 0 || $ordinal > $this->enumerationCount) { |
785
|
1 |
|
throw new InvalidArgumentException("Ordinal number must be between 0 and {$this->enumerationCount}"); |
|
|
|
|
786
|
|
|
} |
787
|
|
|
|
788
|
34 |
|
return $this->{$this->fnDoGetBit}($ordinal); |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
/** |
792
|
|
|
* Get a bit at the given ordinal number. |
793
|
|
|
* |
794
|
|
|
* This is the binary bitset implementation. |
795
|
|
|
* |
796
|
|
|
* @param int $ordinal Ordinal number of bit to get |
|
|
|
|
797
|
|
|
* @return bool |
|
|
|
|
798
|
|
|
* @see getBit() |
799
|
|
|
* @see doGetBitInt() |
800
|
|
|
*/ |
801
|
14 |
|
private function doGetBitBin(int $ordinal): bool |
802
|
|
|
{ |
803
|
14 |
|
return (\ord($this->bitset[(int) ($ordinal / 8)]) & 1 << ($ordinal % 8)) !== 0; |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
/** |
807
|
|
|
* Get a bit at the given ordinal number. |
808
|
|
|
* |
809
|
|
|
* This is the integer bitset implementation. |
810
|
|
|
* |
811
|
|
|
* @param int $ordinal Ordinal number of bit to get |
|
|
|
|
812
|
|
|
* @return bool |
|
|
|
|
813
|
|
|
* @see getBit() |
814
|
|
|
* @see doGetBitBin() |
815
|
|
|
*/ |
816
|
25 |
|
private function doGetBitInt(int $ordinal): bool |
817
|
|
|
{ |
818
|
25 |
|
return (bool)($this->bitset & (1 << $ordinal)); |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
/** |
822
|
|
|
* Set a bit at the given ordinal number |
823
|
|
|
* |
824
|
|
|
* @param int $ordinal Ordinal number of bit to set |
|
|
|
|
825
|
|
|
* @param bool $bit The bit to set |
|
|
|
|
826
|
|
|
* @return void |
827
|
|
|
* @throws InvalidArgumentException If the given ordinal number is out-of-range |
|
|
|
|
828
|
|
|
* @uses doSetBitBin() |
829
|
|
|
* @uses doSetBitInt() |
830
|
|
|
* @uses doUnsetBitBin() |
831
|
|
|
* @uses doUnsetBitInt() |
832
|
|
|
*/ |
833
|
2 |
|
public function setBit(int $ordinal, bool $bit): void |
834
|
|
|
{ |
835
|
2 |
|
if ($ordinal < 0 || $ordinal > $this->enumerationCount) { |
836
|
1 |
|
throw new InvalidArgumentException("Ordinal number must be between 0 and {$this->enumerationCount}"); |
|
|
|
|
837
|
|
|
} |
838
|
|
|
|
839
|
1 |
|
if ($bit) { |
840
|
1 |
|
$this->{$this->fnDoSetBit}($ordinal); |
841
|
|
|
} else { |
842
|
1 |
|
$this->{$this->fnDoUnsetBit}($ordinal); |
843
|
|
|
} |
844
|
1 |
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Set a bit at the given ordinal number. |
848
|
|
|
* |
849
|
|
|
* This is the binary bitset implementation. |
850
|
|
|
* |
851
|
|
|
* @param int $ordinal Ordinal number of bit to set |
|
|
|
|
852
|
|
|
* @return void |
853
|
|
|
* @see setBit() |
854
|
|
|
* @see doSetBitInt() |
855
|
|
|
*/ |
856
|
12 |
|
private function doSetBitBin(int $ordinal): void |
857
|
|
|
{ |
858
|
12 |
|
$byte = (int) ($ordinal / 8); |
859
|
12 |
|
$this->bitset[$byte] = $this->bitset[$byte] | \chr(1 << ($ordinal % 8)); |
860
|
12 |
|
} |
861
|
|
|
|
862
|
|
|
/** |
863
|
|
|
* Set a bit at the given ordinal number. |
864
|
|
|
* |
865
|
|
|
* This is the binary bitset implementation. |
866
|
|
|
* |
867
|
|
|
* @param int $ordinal Ordinal number of bit to set |
|
|
|
|
868
|
|
|
* @return void |
869
|
|
|
* @see setBit() |
870
|
|
|
* @see doSetBitBin() |
871
|
|
|
*/ |
872
|
39 |
|
private function doSetBitInt(int $ordinal): void |
873
|
|
|
{ |
874
|
39 |
|
$this->bitset = $this->bitset | (1 << $ordinal); |
875
|
39 |
|
} |
876
|
|
|
|
877
|
|
|
/** |
878
|
|
|
* Unset a bit at the given ordinal number. |
879
|
|
|
* |
880
|
|
|
* This is the binary bitset implementation. |
881
|
|
|
* |
882
|
|
|
* @param int $ordinal Ordinal number of bit to unset |
|
|
|
|
883
|
|
|
* @return void |
884
|
|
|
* @see setBit() |
885
|
|
|
* @see doUnsetBitInt() |
886
|
|
|
*/ |
887
|
2 |
|
private function doUnsetBitBin(int $ordinal): void |
888
|
|
|
{ |
889
|
2 |
|
$byte = (int) ($ordinal / 8); |
890
|
2 |
|
$this->bitset[$byte] = $this->bitset[$byte] & \chr(~(1 << ($ordinal % 8))); |
891
|
2 |
|
} |
892
|
|
|
|
893
|
|
|
/** |
894
|
|
|
* Unset a bit at the given ordinal number. |
895
|
|
|
* |
896
|
|
|
* This is the integer bitset implementation. |
897
|
|
|
* |
898
|
|
|
* @param int $ordinal Ordinal number of bit to unset |
|
|
|
|
899
|
|
|
* @return void |
900
|
|
|
* @see setBit() |
901
|
|
|
* @see doUnsetBitBin() |
902
|
|
|
*/ |
903
|
3 |
|
private function doUnsetBitInt(int $ordinal): void |
904
|
|
|
{ |
905
|
3 |
|
$this->bitset = $this->bitset & ~(1 << $ordinal); |
906
|
3 |
|
} |
907
|
|
|
} |
908
|
|
|
|