| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | #=============================================================================== | ||||||||||||||||||||||||||||
| 3 | # PODNAME: Net::IP::Identifier::Binode | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: A node in the binary tree | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # AUTHOR: Reid Augustin (REID) | ||||||||||||||||||||||||||||
| 7 | # EMAIL: reid@hellosix.com | ||||||||||||||||||||||||||||
| 8 | # CREATED: Mon Oct 6 10:20:33 PDT 2014 | ||||||||||||||||||||||||||||
| 9 | #=============================================================================== | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use 5.002; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | $DB::deep = 150; # IPv6 needs 128 plus some headroom | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | package Net::IP::Identifier::Binode; | ||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | our $VERSION = '0.111'; # VERSION | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | has zero => ( | |||||||||||||||||||||||||||
| 26 | is => 'rw', | ||||||||||||||||||||||||||||
| 27 | isa => \&isnode, | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | 4 | has one => ( | |||||||||||||||||||||||||||
| 30 | is => 'rw', | ||||||||||||||||||||||||||||
| 31 | isa => \&isnode, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | 4 | has payload => ( | |||||||||||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 12589 | ▶ | sub isnode { | ||||||||||||||||||||||||||
|
Subroutine
isnode: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 12589 | ▶ | die if defined $_[0] and ref $_[0] ne __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $_[0] and ref $_[0] ne __PACKAGE__
MC/DC0%0/2
defined $_[0] and ref $_[0] ne __PACKAGE__
defined $_[0]
ref $_[0] ne __PACKAGE__
Truth table33%1/3
A and B
A= defined $_[0]B= ref $_[0] ne __PACKAGE__
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub bin_to_ip { | ||||||||||||||||||||||||||
|
Subroutine
bin_to_ip: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 0 | my ($bin) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | my @results; | |||||||||||||||||||||||||||
| 45 | 0 | my $max = length $bin; | |||||||||||||||||||||||||||
| 46 | 0 | for (my $ii = 0; ; $ii++) { | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($ii % 8 == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | last if ($ii >= $max); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | push @results, 0; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $b = $ii < $max ? substr($bin, $ii, 1) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | $results[-1] <<= 1; | |||||||||||||||||||||||||||
| 53 | 0 | $results[-1] |= $b; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 0 | return join '.', @results; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 4 | our $path = Math::BigInt->new(0); | |||||||||||||||||||||||||||
| 59 | 1519 | ▶ | sub construct { | ||||||||||||||||||||||||||
|
Subroutine
construct: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 1519 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1519 | my $node; | |||||||||||||||||||||||||||
| 63 | $self->_follow( | ||||||||||||||||||||||||||||
| 64 | $path, | ||||||||||||||||||||||||||||
| 65 | 0, | ||||||||||||||||||||||||||||
| 66 | 34498 | ▶ | sub { # construction callback | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 67 | 34498 | my ($self, $path, $level) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 34498 | ▶ | if ($level < length $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 32979 | ▶ | if (substr($path, $level, 1)) { # next step | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 13766 | ▶ | $self->one($self->new) if (not $self->one); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||
| 74 | 19213 | ▶ | $self->zero($self->new) if (not $self->zero); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 1519 | $node = $self; # when we reach the end | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 34498 | return 0; # always continue | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 1519 | ); | |||||||||||||||||||||||||||
| 83 | 1519 | return $node; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 50 | ▶ | sub follow { | ||||||||||||||||||||||||||
|
Subroutine
follow: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 50 | my ($self, $path, $callback, @extra) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 50 | ▶ | croak "Need a code ref\n" if (not $callback or ref $callback ne 'CODE'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $callback or ref $callback ne "CODE"
MC/DC0%0/2
not $callback or ref $callback ne "CODE"
not $callback
ref $callback ne "CODE"
Truth table33%1/3
not(A) or B
A= not $callbackB= ref $callback ne "CODE"
| |||||||||||||||||||||||||||||
| 90 | 50 | return $self->_follow($path, 0, $callback, @extra); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 36645 | ▶ | sub _follow { | ||||||||||||||||||||||||||
|
Subroutine
_follow: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 94 | 4 | ▶ | no warnings 'recursion'; # IPv6 requires at least 128 levels | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 36645 | my ($self, $path, $level, $callback, @extra) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 36645 | ▶ | return if $callback->($self, $path, $level, @extra); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 36645 | ▶ | return $self if ($level >= length $path); # end of the line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 35118 | ▶ | if (substr($path, $level, 1)) { # next step | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 13989 | ▶ | return $self->one->_follow($path, $level + 1, $callback, @extra) if ($self->one); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||
| 105 | 21129 | ▶ | return $self->zero->_follow($path, $level + 1, $callback, @extra) if ($self->zero); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 42 | return; # no node at $path | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 21 | ▶ | sub traverse_width_first { | ||||||||||||||||||||||||||
|
Subroutine
traverse_width_first: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 21 | my ($self, $callback, @extra) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 21 | ▶ | croak "Need a code ref\n" if (not $callback or ref $callback ne 'CODE'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $callback or ref $callback ne "CODE"
MC/DC0%0/2
not $callback or ref $callback ne "CODE"
not $callback
ref $callback ne "CODE"
Truth table33%1/3
not(A) or B
A= not $callbackB= ref $callback ne "CODE"
| |||||||||||||||||||||||||||||
| 114 | 21 | return $self->_traverse_width_first(0, $callback, @extra); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1317 | ▶ | sub _traverse_width_first { | ||||||||||||||||||||||||||
|
Subroutine
_traverse_width_first: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 118 | 4 | ▶ | no warnings 'recursion'; # IPv6 requires at least 128 levels | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | 1317 | my ($self, $level, $callback, @extra) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1317 | ▶ | return if $callback->($self, $level, @extra); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | #$path <<= 1; | ||||||||||||||||||||||||||||
| 124 | 1307 | ▶ | $self->zero->_traverse_width_first($level + 1, $callback, @extra) if ($self->zero); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | #$path |= 1; | ||||||||||||||||||||||||||||
| 126 | 1307 | ▶ | $self->one ->_traverse_width_first($level + 1, $callback, @extra) if ($self->one); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | #$path >>= 1; | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub traverse_depth_first { | ||||||||||||||||||||||||||
|
Subroutine
traverse_depth_first: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 0 | my ($self, $callback, @extra) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | croak "Need a code ref\n" if (not $callback or ref $callback ne 'CODE'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $callback or ref $callback ne "CODE"
MC/DC0%0/2
not $callback or ref $callback ne "CODE"
not $callback
ref $callback ne "CODE"
Truth table0%0/3
not(A) or B
A= not $callbackB= ref $callback ne "CODE"
| |||||||||||||||||||||||||||||
| 134 | 0 | $self->_traverse_depth_first(0, $callback, @extra); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub _traverse_depth_first { | ||||||||||||||||||||||||||
|
Subroutine
_traverse_depth_first: not called CC=5 SCAR=29.4
| |||||||||||||||||||||||||||||
| 138 | 4 | ▶ | no warnings 'recursion'; # IPv6 requires at least 128 levels | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | 0 | my ($self, $level, $callback, @extra) = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | my $stop; | |||||||||||||||||||||||||||
| 142 | #print "path=", $path->as_hex, "\n"; | ||||||||||||||||||||||||||||
| 143 | #$path <<= 1; | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | $stop = $self->zero->_traverse_depth_first($level + 1, $callback, @extra) if ($self->zero); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | #$path |= 1; | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | $stop = $self->one ->_traverse_depth_first($level + 1, $callback, @extra) if (not $stop and $self->one); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $stop and $self->one
MC/DC0%0/2
not $stop and $self->one
not $stop
$self->one
Truth table0%0/3
not(A) and B
A= not $stopB= $self->one
| |||||||||||||||||||||||||||||
| 147 | #$path >>= 1; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | $callback->($self, $level, @extra) if (not $stop); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 4 | 1; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||