1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHP-Yacc package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace PhpYacc\Lalr; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class StringBitset. |
14
|
|
|
*/ |
15
|
|
|
class StringBitSet implements BitSet |
16
|
|
|
{ |
17
|
|
|
const BITS = 8; |
18
|
|
|
|
19
|
|
|
const MASKS = [ |
20
|
|
|
"\x01", |
21
|
|
|
"\x02", |
22
|
|
|
"\x04", |
23
|
|
|
"\x08", |
24
|
|
|
"\x10", |
25
|
|
|
"\x20", |
26
|
|
|
"\x40", |
27
|
|
|
"\x80", |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
public $numBits; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $str; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* StringBitSet constructor. |
42
|
|
|
* |
43
|
|
|
* @param int $numBits |
44
|
|
|
*/ |
45
|
|
|
public function __construct(int $numBits) |
46
|
|
|
{ |
47
|
|
|
$this->numBits = $numBits; |
48
|
|
|
$this->str = \str_repeat("\0", \intdiv($numBits + self::BITS - 1, self::BITS)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param int $i |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function testBit(int $i): bool |
57
|
|
|
{ |
58
|
|
|
$offset = \intdiv($i, self::BITS); |
59
|
|
|
|
60
|
|
|
return ((\ord($this->str[$offset]) >> ($i % self::BITS)) & 1) !== 0; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param int $i |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function setBit(int $i) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$offset = \intdiv($i, self::BITS); |
69
|
|
|
$char = $this->str[$offset]; |
70
|
|
|
$char |= self::MASKS[$i % self::BITS]; |
71
|
|
|
$this->str[$offset] = $char; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int $i |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function clearBit(int $i) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$offset = \intdiv($i, self::BITS); |
80
|
|
|
$char = $this->str[$offset]; |
81
|
|
|
$char &= ~self::MASKS[$i % self::BITS]; |
82
|
|
|
$this->str[$offset] = $char; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param BitSet $other |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function or(BitSet $other): bool |
91
|
|
|
{ |
92
|
|
|
/* @var StringBitSet $other */ |
93
|
|
|
\assert($this->numBits === $other->numBits); |
94
|
|
|
|
95
|
|
|
$changed = false; |
96
|
|
|
for ($i = 0; $i < $this->numBits; $i += self::BITS) { |
97
|
|
|
$offset = $i / self::BITS; |
98
|
|
|
if ("\0" !== ($other->str[$offset] & ~$this->str[$offset])) { |
99
|
|
|
$changed = true; |
100
|
|
|
$this->str[$offset] = $this->str[$offset] | $other->str[$offset]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $changed; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return \Generator |
109
|
|
|
*/ |
110
|
|
|
public function getIterator(): \Generator |
111
|
|
|
{ |
112
|
|
|
for ($i = 0; $i < $this->numBits; $i++) { |
113
|
|
|
if ($this->testBit($i)) { |
114
|
|
|
yield $i; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.