File Coverage

lib/Net/BART/LPM.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package Net::BART::LPM;
2              
3 1     1   7 use strict;
  1         1  
  1         64  
4 1     1   7 use warnings;
  1         1  
  1         57  
5 1     1   7 use Net::BART::BitSet256;
  1         2  
  1         53  
6 1     1   7 use Exporter 'import';
  1         1  
  1         271  
7              
8             our $VERSION = '0.01';
9              
10             our @EXPORT_OK = qw(@LOOKUP_TBL);
11              
12             # Precomputed lookup table: for each base index [0..255], LOOKUP_TBL[idx]
13             # is a BitSet256 with idx and all its ancestors in the complete binary tree set.
14             #
15             # Ancestors are found by repeatedly right-shifting the index:
16             # idx -> idx>>1 -> idx>>2 -> ... -> 1
17             #
18             # Used for O(1) longest-prefix-match at each trie node:
19             # intersection_top(node.prefixes.bitset, LOOKUP_TBL[octet_to_idx(octet)])
20              
21             our @LOOKUP_TBL;
22              
23             sub _generate_lookup_tbl {
24 1     1   4 for my $idx (0 .. 255) {
25 256         446 my $bs = Net::BART::BitSet256->new;
26 256         280 my $i = $idx;
27 256         378 while ($i > 0) {
28 1793         2907 $bs->set($i);
29 1793         2549 $i >>= 1;
30             }
31 256         429 $LOOKUP_TBL[$idx] = $bs;
32             }
33             }
34              
35             _generate_lookup_tbl();
36              
37             1;