Skip to content

Commit

Permalink
Optimized conversion to bitmask
Browse files Browse the repository at this point in the history
New method is about 60% faster than before
  • Loading branch information
jrbasso committed Apr 3, 2024
1 parent 76e11b1 commit 8ac60a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,17 @@ protected function reduceToBitmask($list)
{
$this->logger->debug('Swivel - reducing to bitmask.', compact('list'));

return !is_array($list) ? $list : array_reduce($list, function ($mask, $index) {
if ((int)$index == 0) {
return $mask;
if (!is_array($list)) {
return $list;
}

$mask = 0;
foreach ($list as $value) {
if ($value > 0) {
$mask |= (1 << ((int)$value - 1));
}
return $mask | (1 << ($index - 1));
}, 0);
}
return $mask;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test/Tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public function parseProvider()
['a' => [6, 7], 'a.b' => [7]],
['a' => Bucket::SIXTH | Bucket::SEVENTH, 'a.b' => Bucket::SEVENTH],
],
[
['a' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]],
['a' => Bucket::ALL],
],
[
['a' => Bucket::FIRST],
['a' => Bucket::FIRST],
Expand Down

0 comments on commit 8ac60a9

Please sign in to comment.