Skip to content

Commit 5a8feea

Browse files
authored
Merge pull request #67 from marc-mabe/enumset_optimize_count
optimized EnumSet::count()
2 parents 3090b04 + 3f67cbc commit 5a8feea

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/EnumSet.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,21 @@ public function valid()
187187
*/
188188
public function count()
189189
{
190-
$cnt = 0;
191-
$ord = 0;
190+
$count = 0;
191+
$byteLen = strlen($this->bitset);
192+
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
193+
if ($this->bitset[$bytePos] === "\0") {
194+
continue; // fast skip null byte
195+
}
192196

193-
while ($ord !== $this->ordinalMax) {
194-
if ($this->getBit($ord++)) {
195-
++$cnt;
197+
for ($bitPos = 0; $bitPos < 8; ++$bitPos) {
198+
if ((ord($this->bitset[$bytePos]) & (1 << $bitPos)) !== 0) {
199+
++$count;
200+
}
196201
}
197202
}
198203

199-
return $cnt;
204+
return $count;
200205
}
201206

202207
/**
@@ -249,7 +254,7 @@ public function getOrdinals()
249254
$ordinals = array();
250255
$byteLen = strlen($this->bitset);
251256

252-
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
257+
for ($bytePos = 0; $bytePos < $byteLen; ++$bytePos) {
253258
if ($this->bitset[$bytePos] === "\0") {
254259
continue; // fast skip null byte
255260
}

0 commit comments

Comments
 (0)