| 1 | package Net::BART::BitSet256; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | our $VERSION = '0.01'; | |||||||||
| 7 | |||||||||||
| 8 | # A 256-bit bitset stored as 4 x 64-bit unsigned integers. | ||||||||||
| 9 | # Bit layout: word 0 = bits 0-63, word 1 = bits 64-127, | ||||||||||
| 10 | # word 2 = bits 128-191, word 3 = bits 192-255. | ||||||||||
| 11 | |||||||||||
| 12 | # Precomputed byte popcount table (0..255) | ||||||||||
| 13 | 3 | my @BYTE_POPCNT; | |||||||||
| 14 | 3 | for my $i (0 .. 255) { | |||||||||
| 15 | 768 | my $c = 0; | |||||||||
| 16 | 768 | my $v = $i; | |||||||||
| 17 | 768 | ▶ | while ($v) { $v &= ($v - 1); $c++ } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 768 | $BYTE_POPCNT[$i] = $c; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | # Precomputed bit_len for bytes (0..255) | ||||||||||
| 22 | 3 | my @BYTE_LEN; | |||||||||
| 23 | 3 | $BYTE_LEN[0] = 0; | |||||||||
| 24 | 3 | for my $i (1 .. 255) { | |||||||||
| 25 | 765 | $BYTE_LEN[$i] = $BYTE_LEN[$i >> 1] + 1; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 348 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 29 | 348 | return bless [0, 0, 0, 0], $_[0]; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub clone { | ||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 33 | 0 | return bless [@{$_[0]}], ref($_[0]); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 2122 | ▶ | sub set { | ||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | 2122 | $_[0]->[$_[1] >> 6] |= (1 << ($_[1] & 63)); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 4 | ▶ | sub clear { | ||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 41 | 4 | $_[0]->[$_[1] >> 6] &= ~(1 << ($_[1] & 63)); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 11 | ▶ | sub test { | ||||||||
|
Subroutine
test: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 45 | 11 | ▶ | return ($_[0]->[$_[1] >> 6] & (1 << ($_[1] & 63))) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 3 | ▶ | sub is_empty { | ||||||||
|
Subroutine
is_empty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 49 | 3 | return !($_[0]->[0] | $_[0]->[1] | $_[0]->[2] | $_[0]->[3]); | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | # Fast popcount using byte lookup table | ||||||||||
| 53 | 8 | ▶ | sub _popcount64 { | ||||||||
|
Subroutine
_popcount64: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 8 | my $x = $_[0]; | |||||||||
| 55 | 8 | return $BYTE_POPCNT[ $x & 0xFF] + | |||||||||
| 56 | $BYTE_POPCNT[($x >> 8) & 0xFF] + | ||||||||||
| 57 | $BYTE_POPCNT[($x >> 16) & 0xFF] + | ||||||||||
| 58 | $BYTE_POPCNT[($x >> 24) & 0xFF] + | ||||||||||
| 59 | $BYTE_POPCNT[($x >> 32) & 0xFF] + | ||||||||||
| 60 | $BYTE_POPCNT[($x >> 40) & 0xFF] + | ||||||||||
| 61 | $BYTE_POPCNT[($x >> 48) & 0xFF] + | ||||||||||
| 62 | $BYTE_POPCNT[($x >> 56) & 0xFF]; | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | # Rank: count of set bits in positions 0..idx (inclusive). | ||||||||||
| 66 | # Fully inlined for speed - this is the hottest function. | ||||||||||
| 67 | 905 | ▶ | sub rank { | ||||||||
|
Subroutine
rank: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 68 | 905 | my $self = $_[0]; | |||||||||
| 69 | 905 | my $idx = $_[1]; | |||||||||
| 70 | 905 | my $word = $idx >> 6; | |||||||||
| 71 | 905 | my $bit = $idx & 63; | |||||||||
| 72 | |||||||||||
| 73 | 905 | my $count = 0; | |||||||||
| 74 | |||||||||||
| 75 | # Unrolled loop for words before target | ||||||||||
| 76 | 905 | ▶ | if ($word > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 207 | my $x = $self->[0]; | |||||||||
| 78 | 207 | $count += $BYTE_POPCNT[ $x & 0xFF] + $BYTE_POPCNT[($x >> 8) & 0xFF] + | |||||||||
| 79 | $BYTE_POPCNT[($x >> 16) & 0xFF] + $BYTE_POPCNT[($x >> 24) & 0xFF] + | ||||||||||
| 80 | $BYTE_POPCNT[($x >> 32) & 0xFF] + $BYTE_POPCNT[($x >> 40) & 0xFF] + | ||||||||||
| 81 | $BYTE_POPCNT[($x >> 48) & 0xFF] + $BYTE_POPCNT[($x >> 56) & 0xFF]; | ||||||||||
| 82 | 207 | ▶ | if ($word > 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 140 | $x = $self->[1]; | |||||||||
| 84 | 140 | $count += $BYTE_POPCNT[ $x & 0xFF] + $BYTE_POPCNT[($x >> 8) & 0xFF] + | |||||||||
| 85 | $BYTE_POPCNT[($x >> 16) & 0xFF] + $BYTE_POPCNT[($x >> 24) & 0xFF] + | ||||||||||
| 86 | $BYTE_POPCNT[($x >> 32) & 0xFF] + $BYTE_POPCNT[($x >> 40) & 0xFF] + | ||||||||||
| 87 | $BYTE_POPCNT[($x >> 48) & 0xFF] + $BYTE_POPCNT[($x >> 56) & 0xFF]; | ||||||||||
| 88 | 140 | ▶ | if ($word > 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 70 | $x = $self->[2]; | |||||||||
| 90 | 70 | $count += $BYTE_POPCNT[ $x & 0xFF] + $BYTE_POPCNT[($x >> 8) & 0xFF] + | |||||||||
| 91 | $BYTE_POPCNT[($x >> 16) & 0xFF] + $BYTE_POPCNT[($x >> 24) & 0xFF] + | ||||||||||
| 92 | $BYTE_POPCNT[($x >> 32) & 0xFF] + $BYTE_POPCNT[($x >> 40) & 0xFF] + | ||||||||||
| 93 | $BYTE_POPCNT[($x >> 48) & 0xFF] + $BYTE_POPCNT[($x >> 56) & 0xFF]; | ||||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | # Masked popcount of the target word | ||||||||||
| 99 | 905 | ▶ | my $masked = ($bit == 63) ? $self->[$word] : ($self->[$word] & ((1 << ($bit + 1)) - 1)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 905 | $count += $BYTE_POPCNT[ $masked & 0xFF] + $BYTE_POPCNT[($masked >> 8) & 0xFF] + | |||||||||
| 101 | $BYTE_POPCNT[($masked >> 16) & 0xFF] + $BYTE_POPCNT[($masked >> 24) & 0xFF] + | ||||||||||
| 102 | $BYTE_POPCNT[($masked >> 32) & 0xFF] + $BYTE_POPCNT[($masked >> 40) & 0xFF] + | ||||||||||
| 103 | $BYTE_POPCNT[($masked >> 48) & 0xFF] + $BYTE_POPCNT[($masked >> 56) & 0xFF]; | ||||||||||
| 104 | |||||||||||
| 105 | 905 | return $count; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | # IntersectionTop: find the highest set bit in (self AND other). | ||||||||||
| 109 | # Returns the bit index, or -1 if the intersection is empty. | ||||||||||
| 110 | 2 | ▶ | sub intersection_top { | ||||||||
|
Subroutine
intersection_top: called CC=5 SCAR=16.9
Pod
undocumented
| |||||||||||
| 111 | 2 | my ($self, $other) = @_; | |||||||||
| 112 | 2 | my $w; | |||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | $w = $self->[3] & $other->[3]; if ($w) { return 192 + _bit_len64($w) - 1 } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | ▶ | $w = $self->[2] & $other->[2]; if ($w) { return 128 + _bit_len64($w) - 1 } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 0 | ▶ | $w = $self->[1] & $other->[1]; if ($w) { return 64 + _bit_len64($w) - 1 } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 2 | ▶ | $w = $self->[0] & $other->[0]; if ($w) { return _bit_len64($w) - 1 } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 1 | return -1; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | # Intersects: returns true if (self AND other) is non-empty | ||||||||||
| 122 | 3 | ▶ | sub intersects { | ||||||||
|
Subroutine
intersects: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 123 | 3 | ▶ | return (($_[0]->[0] & $_[1]->[0]) | | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | ($_[0]->[1] & $_[1]->[1]) | | ||||||||||
| 125 | ($_[0]->[2] & $_[1]->[2]) | | ||||||||||
| 126 | ($_[0]->[3] & $_[1]->[3])) ? 1 : 0; | ||||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | # Number of bits needed to represent x (bits.Len64 equivalent). | ||||||||||
| 130 | # Uses byte lookup table for speed. | ||||||||||
| 131 | 17 | ▶ | sub _bit_len64 { | ||||||||
|
Subroutine
_bit_len64: called CC=9 SCAR=29.5
| |||||||||||
| 132 | 17 | my $x = $_[0]; | |||||||||
| 133 | 17 | ▶ | return 0 unless $x; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 17 | ▶ | if ($x >> 32) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | my $hi = $x >> 32; | |||||||||
| 136 | 0 | ▶ | if ($hi >> 16) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 0 | ▶ | return ($hi >> 24) ? 56 + $BYTE_LEN[$hi >> 24] : 48 + $BYTE_LEN[$hi >> 16]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | } | ||||||||||
| 139 | 0 | ▶ | return ($hi >> 8) ? 40 + $BYTE_LEN[$hi >> 8] : 32 + $BYTE_LEN[$hi]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | } | ||||||||||
| 141 | 17 | ▶ | if ($x >> 16) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | 2 | ▶ | return ($x >> 24) ? 24 + $BYTE_LEN[$x >> 24] : 16 + $BYTE_LEN[($x >> 16) & 0xFF]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | } | ||||||||||
| 144 | 15 | ▶ | return ($x >> 8) ? 8 + $BYTE_LEN[($x >> 8) & 0xFF] : $BYTE_LEN[$x & 0xFF]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | # Iterate over set bits, calling $callback->($bit) for each | ||||||||||
| 148 | 1 | ▶ | sub each_set_bit { | ||||||||
|
Subroutine
each_set_bit: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 149 | 1 | my ($self, $callback) = @_; | |||||||||
| 150 | 1 | for my $i (0, 1, 2, 3) { | |||||||||
| 151 | 4 | my $w = $self->[$i]; | |||||||||
| 152 | 4 | ▶ | next unless $w; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | 3 | my $base = $i << 6; | |||||||||
| 154 | 3 | ▶ | while ($w) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 3 | my $t = $w & (-$w); # isolate lowest set bit | |||||||||
| 156 | 3 | $callback->($base + _bit_len64($t) - 1); | |||||||||
| 157 | 3 | $w &= ($w - 1); | |||||||||
| 158 | } | ||||||||||
| 159 | } | ||||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | # Total number of set bits | ||||||||||
| 163 | 2 | ▶ | sub popcnt { | ||||||||
|
Subroutine
popcnt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 164 | 2 | return _popcount64($_[0]->[0]) + _popcount64($_[0]->[1]) + | |||||||||
| 165 | _popcount64($_[0]->[2]) + _popcount64($_[0]->[3]); | ||||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | 3 | 1; | |||||||||