/usr/share/php/MabeEnum/EnumSet.php is in php-enum 3.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 | <?php
namespace MabeEnum;
use Countable;
use Iterator;
use InvalidArgumentException;
/**
* A set of enumerators of the given enumeration (EnumSet<T>)
* based on an integer or binary bitset depending of given enumeration size.
*
* @link http://github.com/marc-mabe/php-enum for the canonical source repository
* @copyright Copyright (c) 2017 Marc Bennewitz
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
*/
class EnumSet implements Iterator, Countable
{
/**
* The classname of the Enumeration
* @var string
*/
private $enumeration;
/**
* Ordinal number of current iterator position
* @var int
*/
private $ordinal = 0;
/**
* Highest possible ordinal number
* @var int
*/
private $ordinalMax;
/**
* Integer or binary (little endian) bitset
* @var int|string
*/
private $bitset = 0;
/**#@+
* Defines private method names to be called depended of how the bitset type was set too.
* ... Integer or binary bitset.
* ... *Int or *Bin method
*
* @var string
*/
private $fnDoRewind = 'doRewindInt';
private $fnDoCount = 'doCountInt';
private $fnDoGetOrdinals = 'doGetOrdinalsInt';
private $fnDoGetBit = 'doGetBitInt';
private $fnDoSetBit = 'doSetBitInt';
private $fnDoUnsetBit = 'doUnsetBitInt';
private $fnDoGetBinaryBitsetLe = 'doGetBinaryBitsetLeInt';
private $fnDoSetBinaryBitsetLe = 'doSetBinaryBitsetLeInt';
/**#@-*/
/**
* Constructor
*
* @param string $enumeration The classname of the enumeration
* @throws InvalidArgumentException
*/
public function __construct($enumeration)
{
if (!\is_subclass_of($enumeration, Enum::class)) {
throw new InvalidArgumentException(\sprintf(
"%s can handle subclasses of '%s' only",
static::class,
Enum::class
));
}
$this->enumeration = $enumeration;
$this->ordinalMax = count($enumeration::getConstants());
// By default the bitset is initialized as integer bitset
// in case the enumeraton has more enumerators then integer bits
// we will switch this into a binary bitset
if ($this->ordinalMax > \PHP_INT_SIZE * 8) {
// init binary bitset with zeros
$this->bitset = \str_repeat("\0", (int)\ceil($this->ordinalMax / 8));
// switch internal binary bitset functions
$this->fnDoRewind = 'doRewindBin';
$this->fnDoCount = 'doCountBin';
$this->fnDoGetOrdinals = 'doGetOrdinalsBin';
$this->fnDoGetBit = 'doGetBitBin';
$this->fnDoSetBit = 'doSetBitBin';
$this->fnDoUnsetBit = 'doUnsetBitBin';
$this->fnDoGetBinaryBitsetLe = 'doGetBinaryBitsetLeBin';
$this->fnDoSetBinaryBitsetLe = 'doSetBinaryBitsetLeBin';
}
}
/**
* Get the classname of the enumeration
* @return string
*/
public function getEnumeration()
{
return $this->enumeration;
}
/**
* Attach a new enumerator or overwrite an existing one
* @param Enum|null|boolean|int|float|string $enumerator
* @return void
* @throws InvalidArgumentException On an invalid given enumerator
*/
public function attach($enumerator)
{
$enumeration = $this->enumeration;
$this->{$this->fnDoSetBit}($enumeration::get($enumerator)->getOrdinal());
}
/**
* Detach the given enumerator
* @param Enum|null|boolean|int|float|string $enumerator
* @return void
* @throws InvalidArgumentException On an invalid given enumerator
*/
public function detach($enumerator)
{
$enumeration = $this->enumeration;
$this->{$this->fnDoUnsetBit}($enumeration::get($enumerator)->getOrdinal());
}
/**
* Test if the given enumerator was attached
* @param Enum|null|boolean|int|float|string $enumerator
* @return boolean
*/
public function contains($enumerator)
{
$enumeration = $this->enumeration;
return $this->{$this->fnDoGetBit}($enumeration::get($enumerator)->getOrdinal());
}
/* Iterator */
/**
* Get the current enumerator
* @return Enum|null Returns the current enumerator or NULL on an invalid iterator position
*/
public function current()
{
if ($this->valid()) {
$enumeration = $this->enumeration;
return $enumeration::byOrdinal($this->ordinal);
}
return null;
}
/**
* Get the ordinal number of the current iterator position
* @return int
*/
public function key()
{
return $this->ordinal;
}
/**
* Go to the next valid iterator position.
* If no valid iterator position is found the iterator position will be the last possible + 1.
* @return void
*/
public function next()
{
do {
if (++$this->ordinal >= $this->ordinalMax) {
$this->ordinal = $this->ordinalMax;
return;
}
} while (!$this->{$this->fnDoGetBit}($this->ordinal));
}
/**
* Go to the first valid iterator position.
* If no valid iterator position was found the iterator position will be 0.
* @return void
* @see doRewindBin
* @see doRewindInt
*/
public function rewind()
{
$this->{$this->fnDoRewind}();
}
/**
* Go to the first valid iterator position.
* If no valid iterator position was found the iterator position will be 0.
*
* This is the binary bitset implementation.
*
* @return void
* @see rewind
* @see doRewindInt
*/
private function doRewindBin()
{
if (\ltrim($this->bitset, "\0") !== '') {
$this->ordinal = -1;
$this->next();
} else {
$this->ordinal = 0;
}
}
/**
* Go to the first valid iterator position.
* If no valid iterator position was found the iterator position will be 0.
*
* This is the binary bitset implementation.
*
* @return void
* @see rewind
* @see doRewindBin
*/
private function doRewindInt()
{
if ($this->bitset) {
$this->ordinal = -1;
$this->next();
} else {
$this->ordinal = 0;
}
}
/**
* Test if the iterator is in a valid state
* @return boolean
*/
public function valid()
{
return $this->ordinal !== $this->ordinalMax && $this->{$this->fnDoGetBit}($this->ordinal);
}
/* Countable */
/**
* Count the number of elements
*
* @return int
* @see doCountBin
* @see doCountInt
*/
public function count()
{
return $this->{$this->fnDoCount}();
}
/**
* Count the number of elements.
*
* This is the binary bitset implementation.
*
* @return int
* @see count
* @see doCountInt
*/
private function doCountBin()
{
$count = 0;
$bitset = $this->bitset;
$byteLen = \strlen($bitset);
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
if ($bitset[$bytePos] === "\0") {
// fast skip null byte
continue;
}
$ord = \ord($bitset[$bytePos]);
if ($ord & 0b00000001) ++$count;
if ($ord & 0b00000010) ++$count;
if ($ord & 0b00000100) ++$count;
if ($ord & 0b00001000) ++$count;
if ($ord & 0b00010000) ++$count;
if ($ord & 0b00100000) ++$count;
if ($ord & 0b01000000) ++$count;
if ($ord & 0b10000000) ++$count;
}
return $count;
}
/**
* Count the number of elements.
*
* This is the integer bitset implementation.
*
* @return int
* @see count
* @see doCountBin
*/
private function doCountInt()
{
$count = 0;
$bitset = $this->bitset;
// PHP does not support right shift unsigned
if ($bitset < 0) {
$count = 1;
$bitset = $bitset & \PHP_INT_MAX;
}
// iterate byte by byte and count set bits
$phpIntBitSize = \PHP_INT_SIZE * 8;
for ($bitPos = 0; $bitPos < $phpIntBitSize; $bitPos += 8) {
$bitChk = 0xff << $bitPos;
$byte = $bitset & $bitChk;
if ($byte) {
$byte = $byte >> $bitPos;
if ($byte & 0b00000001) ++$count;
if ($byte & 0b00000010) ++$count;
if ($byte & 0b00000100) ++$count;
if ($byte & 0b00001000) ++$count;
if ($byte & 0b00010000) ++$count;
if ($byte & 0b00100000) ++$count;
if ($byte & 0b01000000) ++$count;
if ($byte & 0b10000000) ++$count;
}
if ($bitset <= $bitChk) {
break;
}
}
return $count;
}
/**
* Check if this EnumSet is the same as other
* @param EnumSet $other
* @return bool
*/
public function isEqual(EnumSet $other)
{
return $this->enumeration === $other->enumeration
&& $this->bitset === $other->bitset;
}
/**
* Check if this EnumSet is a subset of other
* @param EnumSet $other
* @return bool
*/
public function isSubset(EnumSet $other)
{
if ($this->enumeration !== $other->enumeration) {
return false;
}
return ($this->bitset & $other->bitset) === $this->bitset;
}
/**
* Check if this EnumSet is a superset of other
* @param EnumSet $other
* @return bool
*/
public function isSuperset(EnumSet $other)
{
if ($this->enumeration !== $other->enumeration) {
return false;
}
return ($this->bitset | $other->bitset) === $this->bitset;
}
/**
* Produce a new set with enumerators from both this and other (this | other)
*
* @param EnumSet $other EnumSet of the same enumeration to produce the union
* @return EnumSet
*/
public function union(EnumSet $other)
{
if ($this->enumeration !== $other->enumeration) {
throw new InvalidArgumentException(\sprintf(
'Other should be of the same enumeration as this %s',
$this->enumeration
));
}
$clone = clone $this;
$clone->bitset = $this->bitset | $other->bitset;
return $clone;
}
/**
* Produce a new set with enumerators common to both this and other (this & other)
*
* @param EnumSet $other EnumSet of the same enumeration to produce the intersect
* @return EnumSet
*/
public function intersect(EnumSet $other)
{
if ($this->enumeration !== $other->enumeration) {
throw new InvalidArgumentException(\sprintf(
'Other should be of the same enumeration as this %s',
$this->enumeration
));
}
$clone = clone $this;
$clone->bitset = $this->bitset & $other->bitset;
return $clone;
}
/**
* Produce a new set with enumerators in this but not in other (this - other)
*
* @param EnumSet $other EnumSet of the same enumeration to produce the diff
* @return EnumSet
*/
public function diff(EnumSet $other)
{
if ($this->enumeration !== $other->enumeration) {
throw new InvalidArgumentException(\sprintf(
'Other should be of the same enumeration as this %s',
$this->enumeration
));
}
$clone = clone $this;
$clone->bitset = $this->bitset & ~$other->bitset;
return $clone;
}
/**
* Produce a new set with enumerators in either this and other but not in both (this ^ other)
*
* @param EnumSet $other EnumSet of the same enumeration to produce the symmetric difference
* @return EnumSet
*/
public function symDiff(EnumSet $other)
{
if ($this->enumeration !== $other->enumeration) {
throw new InvalidArgumentException(\sprintf(
'Other should be of the same enumeration as this %s',
$this->enumeration
));
}
$clone = clone $this;
$clone->bitset = $this->bitset ^ $other->bitset;
return $clone;
}
/**
* Get ordinal numbers of the defined enumerators as array
* @return int[]
*/
public function getOrdinals()
{
return $this->{$this->fnDoGetOrdinals}();
}
/**
* Get ordinal numbers of the defined enumerators as array.
*
* This is the binary bitset implementation.
*
* @return int[]
* @see getOrdinals
* @see goGetOrdinalsInt
*/
private function doGetOrdinalsBin()
{
$ordinals = [];
$bitset = $this->bitset;
$byteLen = \strlen($bitset);
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
if ($bitset[$bytePos] === "\0") {
// fast skip null byte
continue;
}
$ord = \ord($bitset[$bytePos]);
for ($bitPos = 0; $bitPos < 8; ++$bitPos) {
if ($ord & (1 << $bitPos)) {
$ordinals[] = $bytePos * 8 + $bitPos;
}
}
}
return $ordinals;
}
/**
* Get ordinal numbers of the defined enumerators as array.
*
* This is the integer bitset implementation.
*
* @return int[]
* @see getOrdinals
* @see doGetOrdinalsBin
*/
private function doGetOrdinalsInt()
{
$ordinals = [];
$ordinalMax = $this->ordinalMax;
$bitset = $this->bitset;
for ($ord = 0; $ord < $ordinalMax; ++$ord) {
if ($bitset & (1 << $ord)) {
$ordinals[] = $ord;
}
}
return $ordinals;
}
/**
* Get values of the defined enumerators as array
* @return null[]|bool[]|int[]|float[]|string[]
*/
public function getValues()
{
$enumeration = $this->enumeration;
$values = [];
foreach ($this->getOrdinals() as $ord) {
$values[] = $enumeration::byOrdinal($ord)->getValue();
}
return $values;
}
/**
* Get names of the defined enumerators as array
* @return string[]
*/
public function getNames()
{
$enumeration = $this->enumeration;
$names = [];
foreach ($this->getOrdinals() as $ord) {
$names[] = $enumeration::byOrdinal($ord)->getName();
}
return $names;
}
/**
* Get the defined enumerators as array
* @return Enum[]
*/
public function getEnumerators()
{
$enumeration = $this->enumeration;
$enumerators = [];
foreach ($this->getOrdinals() as $ord) {
$enumerators[] = $enumeration::byOrdinal($ord);
}
return $enumerators;
}
/**
* Get binary bitset in little-endian order
*
* @return string
*/
public function getBinaryBitsetLe()
{
return $this->{$this->fnDoGetBinaryBitsetLe}();
}
/**
* Get binary bitset in little-endian order.
*
* This is the binary bitset implementation.
*
* @return string
*/
private function doGetBinaryBitsetLeBin()
{
return $this->bitset;
}
/**
* Get binary bitset in little-endian order.
*
* This is the integer bitset implementation.
*
* @return string
*/
private function doGetBinaryBitsetLeInt()
{
$bin = \pack(\PHP_INT_SIZE === 8 ? 'P' : 'V', $this->bitset);
return \substr($bin, 0, (int)\ceil($this->ordinalMax / 8));
}
/**
* Set binary bitset in little-endian order
*
* NOTE: It resets the current position of the iterator
*
* @param string $bitset
* @return void
* @throws InvalidArgumentException On a non string is given as Parameter
*/
public function setBinaryBitsetLe($bitset)
{
if (!\is_string($bitset)) {
throw new InvalidArgumentException('Bitset must be a string');
}
$this->{$this->fnDoSetBinaryBitsetLe}($bitset);
// reset the iterator position
$this->rewind();
}
/**
* Set binary bitset in little-endian order
*
* NOTE: It resets the current position of the iterator
*
* @param string $bitset
* @return void
* @throws InvalidArgumentException On a non string is given as Parameter
*/
private function doSetBinaryBitsetLeBin($bitset)
{
$size = \strlen($this->bitset);
$sizeIn = \strlen($bitset);
if ($sizeIn < $size) {
// add "\0" if the given bitset is not long enough
$bitset .= \str_repeat("\0", $size - $sizeIn);
} elseif ($sizeIn > $size) {
if (\ltrim(\substr($bitset, $size), "\0") !== '') {
throw new InvalidArgumentException('Out-Of-Range bits detected');
}
$bitset = \substr($bitset, 0, $size);
}
// truncate out-of-range bits of last byte
$lastByteMaxOrd = $this->ordinalMax % 8;
if ($lastByteMaxOrd !== 0) {
$lastByte = $bitset[$size - 1];
$lastByteExpected = \chr((1 << $lastByteMaxOrd) - 1) & $lastByte;
if ($lastByte !== $lastByteExpected) {
throw new InvalidArgumentException('Out-Of-Range bits detected');
}
$this->bitset = \substr($bitset, 0, -1) . $lastByteExpected;
}
$this->bitset = $bitset;
}
/**
* Set binary bitset in little-endian order
*
* NOTE: It resets the current position of the iterator
*
* @param string $bitset
* @return void
* @throws InvalidArgumentException On a non string is given as Parameter
*/
private function doSetBinaryBitsetLeInt($bitset)
{
$len = \strlen($bitset);
$int = 0;
for ($i = 0; $i < $len; ++$i) {
$ord = \ord($bitset[$i]);
if ($ord && $i > \PHP_INT_SIZE - 1) {
throw new InvalidArgumentException('Out-Of-Range bits detected');
}
$int |= $ord << (8 * $i);
}
if ($int & (~0 << $this->ordinalMax)) {
throw new InvalidArgumentException('Out-Of-Range bits detected');
}
$this->bitset = $int;
}
/**
* Get binary bitset in big-endian order
*
* @return string
*/
public function getBinaryBitsetBe()
{
return \strrev($this->bitset);
}
/**
* Set binary bitset in big-endian order
*
* NOTE: It resets the current position of the iterator
*
* @param string $bitset
* @return void
* @throws InvalidArgumentException On a non string is given as Parameter
*/
public function setBinaryBitsetBe($bitset)
{
if (!\is_string($bitset)) {
throw new InvalidArgumentException('Bitset must be a string');
}
$this->setBinaryBitsetLe(\strrev($bitset));
}
/**
* Get a bit at the given ordinal number
*
* @param int $ordinal Ordinal number of bit to get
* @return boolean
*/
public function getBit($ordinal)
{
if ($ordinal < 0 || $ordinal > $this->ordinalMax) {
throw new InvalidArgumentException("Ordinal number must be between 0 and {$this->ordinalMax}");
}
return $this->{$this->fnDoGetBit}($ordinal);
}
/**
* Get a bit at the given ordinal number.
*
* This is the binary bitset implementation.
*
* @param int $ordinal Ordinal number of bit to get
* @return boolean
* @see getBit
* @see doGetBitInt
*/
private function doGetBitBin($ordinal)
{
return (\ord($this->bitset[(int) ($ordinal / 8)]) & 1 << ($ordinal % 8)) !== 0;
}
/**
* Get a bit at the given ordinal number.
*
* This is the integer bitset implementation.
*
* @param int $ordinal Ordinal number of bit to get
* @return boolean
* @see getBit
* @see doGetBitBin
*/
private function doGetBitInt($ordinal)
{
return (bool)($this->bitset & (1 << $ordinal));
}
/**
* Set a bit at the given ordinal number
*
* @param int $ordinal Ordnal number of bit to set
* @param bool $bit The bit to set
* @return void
* @see doSetBitBin
* @see doSetBitInt
* @see doUnsetBin
* @see doUnsetInt
*/
public function setBit($ordinal, $bit)
{
if ($ordinal < 0 || $ordinal > $this->ordinalMax) {
throw new InvalidArgumentException("Ordinal number must be between 0 and {$this->ordinalMax}");
}
if ($bit) {
$this->{$this->fnDoSetBit}($ordinal);
} else {
$this->{$this->fnDoUnsetBit}($ordinal);
}
}
/**
* Set a bit at the given ordinal number.
*
* This is the binary bitset implementation.
*
* @param int $ordinal Ordnal number of bit to set
* @return void
* @see setBit
* @see doSetBitInt
*/
private function doSetBitBin($ordinal)
{
$byte = (int) ($ordinal / 8);
$this->bitset[$byte] = $this->bitset[$byte] | \chr(1 << ($ordinal % 8));
}
/**
* Set a bit at the given ordinal number.
*
* This is the binary bitset implementation.
*
* @param int $ordinal Ordnal number of bit to set
* @return void
* @see setBit
* @see doSetBitBin
*/
private function doSetBitInt($ordinal)
{
$this->bitset = $this->bitset | (1 << $ordinal);
}
/**
* Unset a bit at the given ordinal number.
*
* This is the binary bitset implementation.
*
* @param int $ordinal Ordinal number of bit to unset
* @return void
* @see setBit
* @see doUnsetBitInt
*/
private function doUnsetBitBin($ordinal)
{
$byte = (int) ($ordinal / 8);
$this->bitset[$byte] = $this->bitset[$byte] & \chr(~(1 << ($ordinal % 8)));
}
/**
* Unset a bit at the given ordinal number.
*
* This is the integer bitset implementation.
*
* @param int $ordinal Ordinal number of bit to unset
* @return void
* @see setBit
* @see doUnsetBitBin
*/
private function doUnsetBitInt($ordinal)
{
$this->bitset = $this->bitset & ~(1 << $ordinal);
}
}
|