| 1 | package Net::BART::Art; | ||||||||||
| 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 Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | our $VERSION = '0.01'; | |||||||||
| 8 | |||||||||||
| 9 | 2 | our @EXPORT_OK = qw( | |||||||||
| 10 | pfx_to_idx | ||||||||||
| 11 | octet_to_idx | ||||||||||
| 12 | idx_to_pfx | ||||||||||
| 13 | pfx_bits | ||||||||||
| 14 | prefix_decompose | ||||||||||
| 15 | ); | ||||||||||
| 16 | |||||||||||
| 17 | # Map a prefix (octet + prefix length within stride 1..7) to a base index [1..255]. | ||||||||||
| 18 | 261 | ▶ | sub pfx_to_idx { | ||||||||
|
Subroutine
pfx_to_idx: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 19 | 261 | my ($octet, $pfx_len) = @_; | |||||||||
| 20 | 261 | return ($octet >> (8 - $pfx_len)) + (1 << $pfx_len); | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | # Map a full octet to its /7 index [128..255] for LPM lookup. | ||||||||||
| 24 | 5 | ▶ | sub octet_to_idx { | ||||||||
|
Subroutine
octet_to_idx: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | 5 | my ($octet) = @_; | |||||||||
| 26 | 5 | return ($octet >> 1) + 128; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | # Inverse: from base index [1..255] back to (octet, pfx_len). | ||||||||||
| 30 | 255 | ▶ | sub idx_to_pfx { | ||||||||
|
Subroutine
idx_to_pfx: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 31 | 255 | my ($idx) = @_; | |||||||||
| 32 | 255 | my $pfx_len = _bit_len8($idx) - 1; | |||||||||
| 33 | 255 | my $octet = ($idx - (1 << $pfx_len)) << (8 - $pfx_len); | |||||||||
| 34 | 255 | return ($octet, $pfx_len); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | # Total prefix bits at a given trie depth and base index. | ||||||||||
| 38 | 0 | ▶ | sub pfx_bits { | ||||||||
|
Subroutine
pfx_bits: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 39 | 0 | my ($depth, $idx) = @_; | |||||||||
| 40 | 0 | return $depth * 8 + (_bit_len8($idx) - 1); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | # Decompose a prefix length into: | ||||||||||
| 44 | # $strides - number of full 8-bit octets consumed | ||||||||||
| 45 | # $lastbits - remaining prefix bits (0..7) in the final octet | ||||||||||
| 46 | # | ||||||||||
| 47 | # strides = floor(prefix_len / 8), lastbits = prefix_len % 8 | ||||||||||
| 48 | # | ||||||||||
| 49 | # If lastbits > 0: traverse strides octets, store prefix at depth strides | ||||||||||
| 50 | # If lastbits == 0 and prefix_len > 0: it's a "fringe" (stride-aligned) | ||||||||||
| 51 | # traverse strides-1 octets, store fringe child at depth strides-1 | ||||||||||
| 52 | # If prefix_len == 0: default route, store at root index 1 | ||||||||||
| 53 | 6 | ▶ | sub prefix_decompose { | ||||||||
|
Subroutine
prefix_decompose: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 54 | 6 | my ($prefix_len) = @_; | |||||||||
| 55 | 6 | return (int($prefix_len / 8), $prefix_len % 8); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 255 | ▶ | sub _bit_len8 { | ||||||||
|
Subroutine
_bit_len8: called CC=4 SCAR=13.9
| |||||||||||
| 59 | 255 | my ($x) = @_; | |||||||||
| 60 | 255 | my $n = 0; | |||||||||
| 61 | 255 | ▶ | if ($x & 0xF0) { $n += 4; $x >>= 4; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 255 | ▶ | if ($x & 0x0C) { $n += 2; $x >>= 2; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 255 | ▶ | if ($x & 0x02) { $n += 1; $x >>= 1; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 255 | $n += $x; | |||||||||
| 65 | 255 | return $n; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 2 | 1; | |||||||||