lib/Net/BART/Node.pm

filter: Statementstmt 86.2% 69 / 80 Branchbran 58.3% 14 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 18 / 20 Podpod n/a totaltotal 79.0% 102 / 129 CC 16 SCAR 12.7
CC
16
Cov
79%
matches_prefix
6.1
lpm_test
6.0
contains_ip
5.6
CRAP 3.6
« lib/Net/BART/BitSet256.pm ↑ Summary lib/Net/BART/SparseArray256.pm »
1 package Net::BART::Node;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Net::BART::SparseArray256;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.01';
8  
9 1 ▶use Net::BART::Art qw(pfx_to_idx octet_to_idx idx_to_pfx pfx_bits prefix_decompose);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # --- LeafNode: path-compressed leaf storing a full prefix + value ---
12 # Structure: bless [$addr, $prefix_len, $value], 'Net::BART::Node::Leaf'
13 # Constants for field access:
14 1 ▶use constant { LEAF_ADDR => 0, LEAF_PFXLEN => 1, LEAF_VALUE => 2 };
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 package Net::BART::Node::Leaf {
17 10 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
18 10 my ($class, %args) = @_;
19 10 return bless [$args{addr}, $args{prefix_len}, $args{value}], $class;
20 }
21  
22 5 ▶ sub contains_ip {
Subroutine contains_ip: called CC=5 SCAR=17.3
23 5 my ($self, $ip_bytes) = @_;
24 5 my $pfx = $self->[0]; # addr
25 5 my $pfx_len = $self->[1];
26 5 my $full_bytes = $pfx_len >> 3; # int($pfx_len / 8)
27 5 for my $i (0 .. $full_bytes - 1) {
28 24 ▶ return 0 if $pfx->[$i] != $ip_bytes->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $pfx->[$i] != $ip_bytes->[$i] 2 22 24
29 }
30 3 my $remaining = $pfx_len & 7;
31 3 ▶ if ($remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining) 0 3 3
32 0 my $mask = (0xFF << (8 - $remaining)) & 0xFF;
33 0 ▶ return 0 if ($pfx->[$full_bytes] & $mask) != ($ip_bytes->[$full_bytes] & $mask);
Branch: 1 branch
BranchTrueFalseTotal
if ($pfx->[$full_bytes] & $mask) != ($ip_bytes->[$full_bytes] & $mask) 0 0 0
34 }
35 3 return 1;
36 }
37  
38 6 ▶ sub matches_prefix {
Subroutine matches_prefix: called CC=6 SCAR=18.2
39 6 my ($self, $addr, $prefix_len) = @_;
40 6 ▶ return 0 if $self->[1] != $prefix_len;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[1] != $prefix_len 3 3 6
41 3 my $pfx = $self->[0];
42 3 my $full_bytes = $prefix_len >> 3;
43 3 for my $i (0 .. $full_bytes - 1) {
44 7 ▶ return 0 if $pfx->[$i] != $addr->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $pfx->[$i] != $addr->[$i] 2 5 7
45 }
46 1 my $remaining = $prefix_len & 7;
47 1 ▶ if ($remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining) 1 0 1
48 1 my $mask = (0xFF << (8 - $remaining)) & 0xFF;
49 1 ▶ return 0 if ($pfx->[$full_bytes] & $mask) != ($addr->[$full_bytes] & $mask);
Branch: 1 branch
BranchTrueFalseTotal
if ($pfx->[$full_bytes] & $mask) != ($addr->[$full_bytes] & $mask) 1 0 1
50 }
51 0 return 1;
52 }
53 }
54  
55 # --- FringeNode: stride-aligned prefix, value only ---
56 # Structure: bless [$value], 'Net::BART::Node::Fringe'
57  
58 1 package Net::BART::Node::Fringe {
59 270 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
60 270 my ($class, %args) = @_;
61 270 return bless [$args{value}], $class;
62 }
63 }
64  
65 # --- BartNode: internal trie node ---
66 # Structure: bless [$prefixes_sparse_array, $children_sparse_array], 'Net::BART::Node::Bart'
67 1 ▶use constant { BART_PFX => 0, BART_CHD => 1 };
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 1 package Net::BART::Node::Bart {
70 37 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
71 37 return bless [Net::BART::SparseArray256->new, Net::BART::SparseArray256->new], $_[0];
72 }
73  
74 # Prefix operations - delegate to sparse array
75 8 ▶ sub insert_prefix { return $_[0]->[0]->insert_at($_[1], $_[2]) }
Subroutine insert_prefix: called CC=1 SCAR=0.0
76 0 ▶ sub delete_prefix { return $_[0]->[0]->delete_at($_[1]) }
Subroutine delete_prefix: not called CC=1 SCAR=6.9
77 1 ▶ sub get_prefix { return $_[0]->[0]->get($_[1]) }
Subroutine get_prefix: called CC=1 SCAR=0.0
78  
79 # Child operations
80 811 ▶ sub get_child { return $_[0]->[1]->get($_[1]) }
Subroutine get_child: called CC=1 SCAR=0.0
81 291 ▶ sub set_child { return $_[0]->[1]->insert_at($_[1], $_[2]) }
Subroutine set_child: called CC=1 SCAR=0.0
82 1 ▶ sub delete_child { return $_[0]->[1]->delete_at($_[1]) }
Subroutine delete_child: called CC=1 SCAR=0.0
83  
84 # Longest-prefix-match at this node for the given octet.
85 # Returns (value, 1) if found, (undef, 0) if not.
86 16 ▶ sub lpm {
Subroutine lpm: called CC=5 SCAR=16.7
87 16 my $self = $_[0];
88 16 my $idx = ($_[1] >> 1) + 128; # octet_to_idx inlined
89 16 my $pfx_bs = $self->[0][0]; # prefixes sparse array -> bitset
90 16 my $lut = $Net::BART::LPM::LOOKUP_TBL[$idx];
91  
92 # intersection_top inlined
93 16 my $w;
94 0 ▶ $w = $pfx_bs->[3] & $lut->[3]; if ($w) { return $self->[0]->get(192 + Net::BART::BitSet256::_bit_len64($w) - 1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($w) 0 16 16
95 0 ▶ $w = $pfx_bs->[2] & $lut->[2]; if ($w) { return $self->[0]->get(128 + Net::BART::BitSet256::_bit_len64($w) - 1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($w) 0 16 16
96 0 ▶ $w = $pfx_bs->[1] & $lut->[1]; if ($w) { return $self->[0]->get( 64 + Net::BART::BitSet256::_bit_len64($w) - 1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($w) 0 16 16
97 16 ▶ $w = $pfx_bs->[0] & $lut->[0]; if ($w) { return $self->[0]->get( Net::BART::BitSet256::_bit_len64($w) - 1) }
Branch: 1 branch
BranchTrueFalseTotal
if ($w) 8 8 16
98 8 return (undef, 0);
99 }
100  
101 # Check if any prefix at this node matches the given octet.
102 0 ▶ sub lpm_test {
Subroutine lpm_test: not called CC=2 SCAR=17.9
103 0 my $pfx_bs = $_[0]->[0][0]; # prefixes -> bitset
104 0 my $lut = $Net::BART::LPM::LOOKUP_TBL[($_[1] >> 1) + 128];
105 0 ▶ return (($pfx_bs->[0] & $lut->[0]) |
Branch: 1 branch
BranchTrueFalseTotal
$pfx_bs->[0] & $lut->[0] | $pfx_bs->[1] & $lut->[1] | $pfx_bs->[2] & $lut->[2] | $pfx_bs->[3] & $lut->[3] ? : 0 0 0
106 ($pfx_bs->[1] & $lut->[1]) |
107 ($pfx_bs->[2] & $lut->[2]) |
108 ($pfx_bs->[3] & $lut->[3])) ? 1 : 0;
109 }
110  
111 1 ▶ sub is_empty {
Subroutine is_empty: called CC=2 SCAR=9.2
112 0 ▶ return !@{$_[0]->[0][1]} && !@{$_[0]->[1][1]};
Condition33%1/3
@{$_[0][0][1];} || @{$_[0][1][1];}
l!l&&r!l&&!r
100
MC/DC0%0/2
@{$_[0][0][1];} || @{$_[0][1][1];}
@{$_[0][0][1];} @{$_[0][1][1];}
Truth table33%1/3
A || B
A= @{$_[0][0][1];}B= @{$_[0][1][1];}
ABresult
1X1
011
000
113 }
114 }
115  
116 1 1;
« lib/Net/BART/BitSet256.pm ↑ Summary lib/Net/BART/SparseArray256.pm »