| 1 | package Net::BART::SparseArray256; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Net::BART::BitSet256; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | our $VERSION = '0.01'; | |||||||||
| 8 | |||||||||||
| 9 | # A popcount-compressed sparse array with 256 possible indices. | ||||||||||
| 10 | # Uses a BitSet256 to track which indices are occupied, | ||||||||||
| 11 | # and a compact array holding only the occupied values. | ||||||||||
| 12 | # | ||||||||||
| 13 | # Structure: [$bitset, $items_arrayref] | ||||||||||
| 14 | # Using array-based object for speed over hash. | ||||||||||
| 15 | |||||||||||
| 16 | 80 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 17 | 80 | return bless [Net::BART::BitSet256->new, []], $_[0]; | |||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | sub clone { | ||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 21 | 0 | return bless [$_[0]->[0]->clone, [@{$_[0]->[1]}]], ref($_[0]); | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | # Get value at index $i. Returns (value, 1) if present, (undef, 0) if not. | ||||||||||
| 25 | 829 | ▶ | sub get { | ||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 26 | 829 | my $bs = $_[0]->[0]; | |||||||||
| 27 | 829 | ▶ | if ($bs->[$_[1] >> 6] & (1 << ($_[1] & 63))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 545 | return ($_[0]->[1][$bs->rank($_[1]) - 1], 1); | |||||||||
| 29 | } | ||||||||||
| 30 | 284 | return (undef, 0); | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | # Insert or update value at index $i. Returns 1 if new, 0 if updated. | ||||||||||
| 34 | 311 | ▶ | sub insert_at { | ||||||||
|
Subroutine
insert_at: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 35 | 311 | my ($self, $i, $value) = @_; | |||||||||
| 36 | 311 | my $bs = $self->[0]; | |||||||||
| 37 | 311 | ▶ | if ($bs->[$i >> 6] & (1 << ($i & 63))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | # Update existing | ||||||||||
| 39 | 12 | $self->[1][$bs->rank($i) - 1] = $value; | |||||||||
| 40 | 12 | return 0; | |||||||||
| 41 | } | ||||||||||
| 42 | # Insert new | ||||||||||
| 43 | 299 | $bs->set($i); | |||||||||
| 44 | 299 | my $rank = $bs->rank($i); | |||||||||
| 45 | 299 | splice(@{$self->[1]}, $rank - 1, 0, $value); | |||||||||
| 46 | 299 | return 1; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # Delete value at index $i. Returns (old_value, 1) or (undef, 0). | ||||||||||
| 50 | 3 | ▶ | sub delete_at { | ||||||||
|
Subroutine
delete_at: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 51 | 3 | my ($self, $i) = @_; | |||||||||
| 52 | 3 | my $bs = $self->[0]; | |||||||||
| 53 | 3 | ▶ | if (!($bs->[$i >> 6] & (1 << ($i & 63)))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 1 | return (undef, 0); | |||||||||
| 55 | } | ||||||||||
| 56 | 2 | my $rank = $bs->rank($i); | |||||||||
| 57 | 2 | my $old = splice(@{$self->[1]}, $rank - 1, 1); | |||||||||
| 58 | 2 | $bs->clear($i); | |||||||||
| 59 | 2 | return ($old, 1); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | # Test if index $i is occupied (inlined bitset test). | ||||||||||
| 63 | 0 | ▶ | sub test { | ||||||||
|
Subroutine
test: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 64 | 0 | ▶ | return ($_[0]->[0]->[$_[1] >> 6] & (1 << ($_[1] & 63))) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 4 | ▶ | sub len { | ||||||||
|
Subroutine
len: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 68 | 4 | return scalar @{$_[0]->[1]}; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | sub is_empty { | ||||||||
|
Subroutine
is_empty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 72 | 1 | return !@{$_[0]->[1]}; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | # Iterate over occupied (index, value) pairs in ascending order. | ||||||||||
| 76 | 5 | ▶ | sub each_pair { | ||||||||
|
Subroutine
each_pair: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 77 | 5 | my ($self, $callback) = @_; | |||||||||
| 78 | 5 | my $items = $self->[1]; | |||||||||
| 79 | 5 | my $pos = 0; | |||||||||
| 80 | 5 | my $bs = $self->[0]; | |||||||||
| 81 | 5 | for my $i (0, 1, 2, 3) { | |||||||||
| 82 | 20 | my $w = $bs->[$i]; | |||||||||
| 83 | 20 | ▶ | next unless $w; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 5 | my $base = $i << 6; | |||||||||
| 85 | 5 | ▶ | while ($w) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 5 | my $t = $w & (-$w); | |||||||||
| 87 | 5 | $callback->($base + Net::BART::BitSet256::_bit_len64($t) - 1, $items->[$pos]); | |||||||||
| 88 | 5 | $pos++; | |||||||||
| 89 | 5 | $w &= ($w - 1); | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # Direct access to the bitset (for LPM operations). | ||||||||||
| 95 | 0 | ▶ | sub bitset { return $_[0]->[0] } | ||||||||
|
Subroutine
bitset: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 96 | |||||||||||
| 97 | # Direct access to items array. | ||||||||||
| 98 | 0 | ▶ | sub items { return $_[0]->[1] } | ||||||||
|
Subroutine
items: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 99 | |||||||||||
| 100 | 2 | 1; | |||||||||