1<?php 2 3declare(strict_types=1); 4 5namespace Antlr\Antlr4\Runtime\Utils; 6 7use Antlr\Antlr4\Runtime\Comparison\Hasher; 8 9final class BitSet 10{ 11 /** @var array<mixed> */ 12 private $data = []; 13 14 public function add(int $value) : void 15 { 16 $this->data[$value] = true; 17 } 18 19 public function or(BitSet $set) : void 20 { 21 $this->data += $set->data; 22 } 23 24 public function remove(int $value) : void 25 { 26 unset($this->data[$value]); 27 } 28 29 public function contains(int $value) : bool 30 { 31 return \array_key_exists($value, $this->data); 32 } 33 34 /** 35 * @return array<mixed> 36 */ 37 public function values() : array 38 { 39 return \array_keys($this->data); 40 } 41 42 /** 43 * @return mixed 44 */ 45 public function minValue() 46 { 47 return \min($this->values()); 48 } 49 50 public function hashCode() : int 51 { 52 return Hasher::hash(...$this->values()); 53 } 54 55 public function equals(object $other) : bool 56 { 57 if ($this === $other) { 58 return true; 59 } 60 61 return $other instanceof self 62 && $this->data === $other->data; 63 } 64 65 public function length() : int 66 { 67 return \count($this->data); 68 } 69 70 public function __toString() : string 71 { 72 return \sprintf('{%s}', \implode(', ', $this->values())); 73 } 74} 75