Skip to content

Commit

Permalink
Merge pull request #47 from zumba/optimization
Browse files Browse the repository at this point in the history
Optimized conversion to bitmask
  • Loading branch information
jrbasso authored Apr 3, 2024
2 parents 76e11b1 + 8ac60a9 commit 229a424
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 229a424

Please sign in to comment.