| 1 | package Net::IPAM::Tree::Private; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use List::MoreUtils qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Net::IPAM::Tree::Private - private implementation for Net::IPAM::Tree | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | This module is not useful standalone, it's just needed for Net::IPAM::Tree. | ||||||||||||||||||||||||||||
| 16 | The implementation details are hidden by the public API in Net::IPAM::Tree. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head2 _buildIndexTree($tree, $parent, $child) | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Building the tree with just the array indices, the sorted items itself are not moved. | ||||||||||||||||||||||||||||
| 23 | create the {parent}->[childs] map, rec-descent algo. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub _build_index_tree { | ||||||||||||||||||||||||||||
| 28 | 52 | ▶ | my ( $t, $parent, $child ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_index_tree: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # if parent has no childs yet, just append the child idx | ||||||||||||||||||||||||||||
| 31 | 52 | ▶ | if ( not defined $t->{_tree}{$parent} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 14 | push @{ $t->{_tree}{$parent} }, $child; | |||||||||||||||||||||||||||
| 33 | 14 | return; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # everything is sorted, just look for previous child for coverage | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # get prev child idx for this parent | ||||||||||||||||||||||||||||
| 39 | 38 | my $prev = $t->{_tree}{$parent}[-1]; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # item is covered by previous child, it's an ancestot, not a sibling | ||||||||||||||||||||||||||||
| 42 | 38 | ▶ | if ( $t->{_items}[$prev]->contains( $t->{_items}[$child] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # rec-descent | ||||||||||||||||||||||||||||
| 45 | 16 | _build_index_tree( $t, $prev, $child ); | |||||||||||||||||||||||||||
| 46 | 16 | return; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # not covered by previous child, append as sibling | ||||||||||||||||||||||||||||
| 50 | 22 | push @{ $t->{_tree}{$parent} }, $child; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 22 | return; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #### | ||||||||||||||||||||||||||||
| 56 | # _superset($block) | ||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||
| 58 | # returns the outermost containing block or undef | ||||||||||||||||||||||||||||
| 59 | sub _superset { | ||||||||||||||||||||||||||||
| 60 | 8 | ▶ | my ( $t, $thing ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_superset: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # find first item in root-level equal to or superset of block | ||||||||||||||||||||||||||||
| 63 | 8 | for my $i ( @{ $t->{_tree}{_ROOT} } ) { | |||||||||||||||||||||||||||
| 64 | 21 | ▶ | if ( $t->{_items}[$i]->cmp($thing) == 0 || $t->{_items}[$i]->contains($thing) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$t->{"_items"}[$i]->cmp($thing) == 0 or $t->{"_items"}[$i]->contains($thing)
MC/DC100%2/2
$t->{"_items"}[$i]->cmp($thing) == 0 or $t->{"_items"}[$i]->contains($thing)
$t->{"_items"}[$i]->cmp($thing) == 0
$t->{"_items"}[$i]->contains($thing)
Truth table100%3/3
A or B
A= $t->{"_items"}[$i]->cmp($thing) == 0B= $t->{"_items"}[$i]->contains($thing)
| |||||||||||||||||||||||||||||
| 65 | 3 | return $t->{_items}[$i]; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | return; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | #### | ||||||||||||||||||||||||||||
| 73 | # _lookup($block) | ||||||||||||||||||||||||||||
| 74 | # | ||||||||||||||||||||||||||||
| 75 | # Returns item in tree with longest-prefix-match for $block, returns undef if not found. | ||||||||||||||||||||||||||||
| 76 | # | ||||||||||||||||||||||||||||
| 77 | # thing is a Net::IPAM::Block or a subclass of them | ||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||
| 79 | # returns the lpm block | ||||||||||||||||||||||||||||
| 80 | # | ||||||||||||||||||||||||||||
| 81 | # rec-descent algo | ||||||||||||||||||||||||||||
| 82 | sub _lookup { | ||||||||||||||||||||||||||||
| 83 | 14 | ▶ | my ( $t, $parent, $block ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_lookup: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # derefernce child idxs array | ||||||||||||||||||||||||||||
| 86 | 14 | my $c_idxs = $t->{_tree}{$parent}; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # find first index where child->{block} >= block | ||||||||||||||||||||||||||||
| 89 | my $idx = | ||||||||||||||||||||||||||||
| 90 | 14 | ▶ | List::MoreUtils::lower_bound { $t->{_items}[$_]->cmp($block) } @$c_idxs; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # found by exact match? | ||||||||||||||||||||||||||||
| 93 | # search index may be -1 or at end, take care for index panics | ||||||||||||||||||||||||||||
| 94 | 14 | ▶ | if ( $idx >= 0 and $idx < @$c_idxs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$idx >= 0 and $idx < @$c_idxs
MC/DC100%2/2
$idx >= 0 and $idx < @$c_idxs
$idx >= 0
$idx < @$c_idxs
Truth table100%3/3
A and B
A= $idx >= 0B= $idx < @$c_idxs
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # deref for better reading and debugging | ||||||||||||||||||||||||||||
| 97 | 7 | my $i = $c_idxs->[$idx]; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 7 | ▶ | if ( $t->{_items}[$i]->cmp($block) == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 2 | return $t->{_items}[$i]; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # look if child before idx contains block | ||||||||||||||||||||||||||||
| 105 | # search index may be 0, take care for index panics | ||||||||||||||||||||||||||||
| 106 | 12 | ▶ | if ( $idx > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # deref for better reading and debugging | ||||||||||||||||||||||||||||
| 109 | 7 | my $i = $c_idxs->[ $idx - 1 ]; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 7 | ▶ | if ( $t->{_items}[$i]->contains($block) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # rec-descent | ||||||||||||||||||||||||||||
| 114 | 3 | return _lookup( $t, $i, $block ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # return parent at this level, if root returns undef | ||||||||||||||||||||||||||||
| 119 | 9 | ▶ | if ( $parent eq '_ROOT' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 7 | return; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | return $t->{_items}[$parent]; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # recdescent to string | ||||||||||||||||||||||||||||
| 127 | sub _to_string { | ||||||||||||||||||||||||||||
| 128 | 24 | ▶ | my ( $t, $cb, $parent, $buf, $prefix ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_to_string: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 24 | my $c_idxs = $t->{_tree}{$parent}; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # STOP condition, no more childs | ||||||||||||||||||||||||||||
| 133 | 24 | ▶ | unless ( defined $c_idxs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 17 | return $buf; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 7 | my $len_c = @$c_idxs; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # stop before last child | ||||||||||||||||||||||||||||
| 140 | 7 | for my $i ( @{$c_idxs}[ 0 .. $len_c - 2 ] ) { | |||||||||||||||||||||||||||
| 141 | 13 | $buf .= $prefix . "ââ " . $cb->( $t->{_items}[$i] ) . "\n"; | |||||||||||||||||||||||||||
| 142 | 13 | $buf = _to_string( $t, $cb, $i, $buf, $prefix . "â " ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # last child | ||||||||||||||||||||||||||||
| 146 | 7 | my $i = $c_idxs->[-1]; | |||||||||||||||||||||||||||
| 147 | 7 | $buf .= $prefix . "ââ " . $cb->( $t->{_items}[$i] ) . "\n"; | |||||||||||||||||||||||||||
| 148 | 7 | $buf = _to_string( $t, $cb, $i, $buf, $prefix . " " ); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 7 | return $buf; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # walk the tree, call the cb for every item with: | ||||||||||||||||||||||||||||
| 154 | # my $err = $cb->( | ||||||||||||||||||||||||||||
| 155 | # { | ||||||||||||||||||||||||||||
| 156 | # depth => $depth, | ||||||||||||||||||||||||||||
| 157 | # item => $item, | ||||||||||||||||||||||||||||
| 158 | # parent => $parent, | ||||||||||||||||||||||||||||
| 159 | # childs => [@childs], | ||||||||||||||||||||||||||||
| 160 | # } | ||||||||||||||||||||||||||||
| 161 | # ); | ||||||||||||||||||||||||||||
| 162 | # | ||||||||||||||||||||||||||||
| 163 | sub _walk { | ||||||||||||||||||||||||||||
| 164 | 12 | ▶ | my ( $t, $cb, $depth, $p, $i ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_walk: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 12 | my $parent; | |||||||||||||||||||||||||||
| 167 | 12 | ▶ | if ( defined $p ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 9 | $parent = $t->{_items}[$p]; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 12 | my $item = $t->{_items}[$i]; | |||||||||||||||||||||||||||
| 172 | 12 | my $c_idxs = $t->{_tree}{$i}; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 12 | my @childs; | |||||||||||||||||||||||||||
| 175 | 12 | foreach my $c (@$c_idxs) { | |||||||||||||||||||||||||||
| 176 | 9 | push @childs, $t->{_items}[$c]; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 12 | my $err = $cb->( | |||||||||||||||||||||||||||
| 180 | { | ||||||||||||||||||||||||||||
| 181 | depth => $depth, | ||||||||||||||||||||||||||||
| 182 | item => $item, | ||||||||||||||||||||||||||||
| 183 | parent => $parent, | ||||||||||||||||||||||||||||
| 184 | childs => [@childs], | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | ); | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 12 | ▶ | return $err if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 11 | foreach my $c (@$c_idxs) { | |||||||||||||||||||||||||||
| 191 | 9 | my $err = _walk( $t, $cb, $depth + 1, $i, $c ); | |||||||||||||||||||||||||||
| 192 | 9 | ▶ | return $err if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 9 | return; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Karl Gaissmaier, C<< <karl.gaissmaier(at)uni-ulm.de> >> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | perldoc Net::IPAM::Tree::Private | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | You can also look for information at: | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =over 4 | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =item * on github | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | TODO | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =back | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | This software is copyright (c) 2020-2021 by Karl Gaissmaier. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 223 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =encoding utf8 | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =cut | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 6 | 1; | |||||||||||||||||||||||||||