| 1 | package Tree::RB; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Tree::RB::Node qw[set_color color_of parent_of left_of right_of]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Tree::RB::Node::_Constants; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use vars qw( $VERSION @EXPORT_OK ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 6 | $VERSION = '0.500006'; | |||||||||||||||||||||||||||||||
| 10 | 6 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 6 | require Exporter; | |||||||||||||||||||||||||||||||
| 13 | 6 | *import = \&Exporter::import; | |||||||||||||||||||||||||||||||
| 14 | 6 | @EXPORT_OK = qw[LUEQUAL LUGTEQ LULTEQ LUGREAT LULESS LUNEXT LUPREV]; | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use enum qw{ | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | LUEQUAL | ||||||||||||||||||||||||||||||||
| 18 | LUGTEQ | ||||||||||||||||||||||||||||||||
| 19 | LULTEQ | ||||||||||||||||||||||||||||||||
| 20 | LUGREAT | ||||||||||||||||||||||||||||||||
| 21 | LULESS | ||||||||||||||||||||||||||||||||
| 22 | LUNEXT | ||||||||||||||||||||||||||||||||
| 23 | LUPREV | ||||||||||||||||||||||||||||||||
| 24 | 6 | }; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # object slots | ||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | use enum qw{ | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | ROOT | ||||||||||||||||||||||||||||||||
| 29 | CMP | ||||||||||||||||||||||||||||||||
| 30 | SIZE | ||||||||||||||||||||||||||||||||
| 31 | HASH_ITER | ||||||||||||||||||||||||||||||||
| 32 | HASH_SEEK_ARG | ||||||||||||||||||||||||||||||||
| 33 | 6 | }; | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | # Node and hash Iteration | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 12 | ▶ | sub _mk_iter { | ||||||||||||||||||||||||||||||
|
Subroutine
_mk_iter: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 38 | 12 | ▶ | my $start_fn = shift || 'min'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'min'
MC/DC0%0/1
shift() || 'min'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 39 | 12 | ▶ | my $next_fn = shift || 'successor'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'successor'
MC/DC0%0/1
shift() || 'successor'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 40 | 30 | ▶ | return sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 41 | 30 | my $self = shift; | |||||||||||||||||||||||||||||||
| 42 | 30 | my $key = shift; | |||||||||||||||||||||||||||||||
| 43 | 30 | my $node; | |||||||||||||||||||||||||||||||
| 44 | 73 | ▶ | my $iter = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 45 | 73 | ▶ | if($node) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 45 | $node = $node->$next_fn; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||||||
| 49 | 28 | ▶ | if(defined $key) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | # seek to $key | ||||||||||||||||||||||||||||||||
| 51 | 16 | ▶ | (undef, $node) = $self->lookup( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | $key, | ||||||||||||||||||||||||||||||||
| 53 | $next_fn eq 'successor' ? LUGTEQ : LULTEQ | ||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||||||
| 57 | 12 | $node = $self->$start_fn; | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | 73 | return $node; | |||||||||||||||||||||||||||||||
| 61 | 30 | }; | |||||||||||||||||||||||||||||||
| 62 | 30 | return bless($iter => 'Tree::RB::Iterator'); | |||||||||||||||||||||||||||||||
| 63 | 12 | }; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 73 | ▶ | *Tree::RB::Iterator::next = sub { $_[0]->() }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 6 | *iter = _mk_iter(qw/min successor/); | |||||||||||||||||||||||||||||||
| 69 | 6 | *rev_iter = _mk_iter(qw/max predecessor/); | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | sub hseek { | ||||||||||||||||||||||||||||||
|
Subroutine
hseek: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 72 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||
| 73 | 10 | my $arg = shift; | |||||||||||||||||||||||||||||||
| 74 | 10 | ▶ | defined $arg or croak("Can't seek to an undefined key"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 10 | my %args; | |||||||||||||||||||||||||||||||
| 76 | 10 | ▶ | if(ref $arg eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 3 | %args = %$arg; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||||||||||||||
| 80 | 7 | $args{-key} = $arg; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 10 | ▶ | if(@_ && exists $args{-key}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ and exists $args{"-key"}
MC/DC50%1/2
@_ and exists $args{"-key"}
@_
exists $args{"-key"}
Truth table66%2/3
A and B
A= @_B= exists $args{"-key"}
| |||||||||||||||||||||||||||||||||
| 84 | 3 | my $arg = shift; | |||||||||||||||||||||||||||||||
| 85 | 3 | ▶ | if(ref $arg eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 3 | %args = (%$arg, %args); | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | 10 | ▶ | if(! exists $args{-key}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | defined $args{'-reverse'} or croak("Expected option '-reverse' is undefined"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | 10 | $self->[HASH_SEEK_ARG] = \%args; | |||||||||||||||||||||||||||||||
| 93 | 10 | ▶ | if($self->[HASH_ITER]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 9 | $self->_reset_hash_iter; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 20 | ▶ | sub _reset_hash_iter { | ||||||||||||||||||||||||||||||
|
Subroutine
_reset_hash_iter: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 99 | 20 | my $self = shift; | |||||||||||||||||||||||||||||||
| 100 | 20 | ▶ | if($self->[HASH_SEEK_ARG]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | 12 | ▶ | my $iter = ($self->[HASH_SEEK_ARG]{'-reverse'} ? 'rev_iter' : 'iter'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 102 | 12 | $self->[HASH_ITER] = $self->$iter($self->[HASH_SEEK_ARG]{'-key'}); | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||||||
| 105 | 8 | $self->[HASH_ITER] = $self->iter; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 11 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 110 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||
| 111 | 11 | $self->_reset_hash_iter; | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 11 | ▶ | my $node = $self->[HASH_ITER]->next | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | or return; | ||||||||||||||||||||||||||||||||
| 115 | 8 | return $node->[_KEY]; | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 39 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 119 | 39 | my $self = shift; | |||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 39 | ▶ | my $node = $self->[HASH_ITER]->next | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | or return; | ||||||||||||||||||||||||||||||||
| 123 | 32 | return $node->[_KEY]; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 127 | 7 | my ($class, $cmp) = @_; | |||||||||||||||||||||||||||||||
| 128 | 7 | my $obj = []; | |||||||||||||||||||||||||||||||
| 129 | 7 | $obj->[SIZE] = 0; | |||||||||||||||||||||||||||||||
| 130 | 7 | ▶ | if($cmp) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | ref $cmp eq 'CODE' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | or croak('Invalid arg: codref expected'); | ||||||||||||||||||||||||||||||||
| 133 | 1 | $obj->[CMP] = $cmp; | |||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | 7 | return bless $obj => $class; | |||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 6 | *TIEHASH = \&new; | |||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 8 | ▶ | sub DESTROY { $_[0]->[ROOT]->DESTROY if $_[0]->[ROOT] } | ||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 4 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 143 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 144 | 4 | ▶ | if($self->[ROOT]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | 1 | $self->[ROOT]->DESTROY; | |||||||||||||||||||||||||||||||
| 146 | 1 | undef $self->[ROOT]; | |||||||||||||||||||||||||||||||
| 147 | 1 | undef $self->[HASH_ITER]; | |||||||||||||||||||||||||||||||
| 148 | 1 | $self->[SIZE] = 0; | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | sub UNTIE { | ||||||||||||||||||||||||||||||
|
Subroutine
UNTIE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 153 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 154 | 2 | $self->DESTROY; | |||||||||||||||||||||||||||||||
| 155 | 2 | undef @$self; | |||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub resort { | ||||||||||||||||||||||||||||||
|
Subroutine
resort: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 159 | 0 | my $self = $_[0]; | |||||||||||||||||||||||||||||||
| 160 | 0 | my $cmp = $_[1]; | |||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | ref $cmp eq 'CODE' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $cmp || 'undef'
MC/DC0%0/1
ref $cmp || 'undef'
ref $cmp
Truth table0%0/2
A
A= ref $cmp
| |||||||||||||||||||||||||||||||||
| 162 | or croak sprintf(q[Arg of type coderef required; got %s], ref $cmp || 'undef'); | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 0 | my $new_tree = __PACKAGE__->new($cmp); | |||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | $self->[ROOT]->strip(sub { $new_tree->put($_[0]) }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 166 | 0 | $new_tree->put(delete $self->[ROOT]); | |||||||||||||||||||||||||||||||
| 167 | 0 | $_[0] = $new_tree; | |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 1 | ▶ | sub root { $_[0]->[ROOT] } | ||||||||||||||||||||||||||||||
|
Subroutine
root: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 171 | 6 | ▶ | sub size { $_[0]->[SIZE] } | ||||||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 6 | *SCALAR = \&size; | |||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | 18 | ▶ | sub min { | ||||||||||||||||||||||||||||||
|
Subroutine
min: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 176 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||
| 177 | 18 | ▶ | return undef unless $self->[ROOT]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 14 | return $self->[ROOT]->min; | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 10 | ▶ | sub max { | ||||||||||||||||||||||||||||||
|
Subroutine
max: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 182 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||
| 183 | 10 | ▶ | return undef unless $self->[ROOT]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 184 | 10 | return $self->[ROOT]->max; | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 51 | ▶ | sub lookup { | ||||||||||||||||||||||||||||||
|
Subroutine
lookup: called CC=26 SCAR=34.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 188 | 51 | my $self = shift; | |||||||||||||||||||||||||||||||
| 189 | 51 | my $key = shift; | |||||||||||||||||||||||||||||||
| 190 | 51 | ▶ | defined $key | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | or croak("Can't use undefined value as key"); | ||||||||||||||||||||||||||||||||
| 192 | 51 | ▶ | my $mode = shift || LUEQUAL; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 193 | 51 | my $cmp = $self->[CMP]; | |||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 51 | my $y; | |||||||||||||||||||||||||||||||
| 196 | 51 | ▶ | my $x = $self->[ROOT] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 197 | or return; | ||||||||||||||||||||||||||||||||
| 198 | 48 | my $next_child; | |||||||||||||||||||||||||||||||
| 199 | 48 | ▶ | while($x) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 200 | 116 | $y = $x; | |||||||||||||||||||||||||||||||
| 201 | 116 | ▶ | if($cmp ? $cmp->($key, $x->[_KEY]) == 0 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 202 | : $key eq $x->[_KEY]) { | ||||||||||||||||||||||||||||||||
| 203 | # found it! | ||||||||||||||||||||||||||||||||
| 204 | 30 | ▶ | if($mode == LUGREAT || $mode == LUNEXT) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$mode == 3 or $mode == 5
MC/DC0%0/2
$mode == 3 or $mode == 5
$mode == 3
$mode == 5
Truth table33%1/3
A or B
A= $mode == 3B= $mode == 5
| |||||||||||||||||||||||||||||||||
| 205 | 0 | $x = $x->successor; | |||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | ▶ | elsif($mode == LULESS || $mode == LUPREV) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$mode == 4 or $mode == 6
MC/DC0%0/2
$mode == 4 or $mode == 6
$mode == 4
$mode == 6
Truth table33%1/3
A or B
A= $mode == 4B= $mode == 6
| |||||||||||||||||||||||||||||||||
| 208 | 0 | $x = $x->predecessor; | |||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||
| 210 | return wantarray | ||||||||||||||||||||||||||||||||
| 211 | 30 | ▶ | ? ($x->[_VAL], $x) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 212 | : $x->[_VAL]; | ||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | 86 | ▶ | if($cmp ? $cmp->($key, $x->[_KEY]) < 0 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 215 | : $key lt $x->[_KEY]) { | ||||||||||||||||||||||||||||||||
| 216 | 41 | $next_child = _LEFT; | |||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | else { | ||||||||||||||||||||||||||||||||
| 219 | 45 | $next_child = _RIGHT; | |||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | 86 | $x = $x->[$next_child]; | |||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | # Didn't find it :( | ||||||||||||||||||||||||||||||||
| 224 | 18 | ▶ | if($mode == LUGTEQ || $mode == LUGREAT) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$mode == 1 or $mode == 3
MC/DC100%2/2
$mode == 1 or $mode == 3
$mode == 1
$mode == 3
Truth table100%3/3
A or B
A= $mode == 1B= $mode == 3
| |||||||||||||||||||||||||||||||||
| 225 | 10 | ▶ | if($next_child == _LEFT) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | 5 | ▶ | return wantarray ? ($y->[_VAL], $y) : $y->[_VAL]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | else { | ||||||||||||||||||||||||||||||||
| 229 | 5 | ▶ | my $next = $y->successor | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 230 | or return; | ||||||||||||||||||||||||||||||||
| 231 | 2 | ▶ | return wantarray ? ($next->[_VAL], $next) : $next->[_VAL]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||
| 234 | ▶ | elsif($mode == LULTEQ || $mode == LULESS) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$mode == 2 or $mode == 4
MC/DC0%0/2
$mode == 2 or $mode == 4
$mode == 2
$mode == 4
Truth table33%1/3
A or B
A= $mode == 2B= $mode == 4
| |||||||||||||||||||||||||||||||||
| 235 | 8 | ▶ | if($next_child == _RIGHT) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 236 | 3 | ▶ | return wantarray ? ($y->[_VAL], $y) : $y->[_VAL]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||
| 238 | else { | ||||||||||||||||||||||||||||||||
| 239 | 5 | ▶ | my $next = $y->predecessor | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 240 | or return; | ||||||||||||||||||||||||||||||||
| 241 | 2 | ▶ | return wantarray ? ($next->[_VAL], $next) : $next->[_VAL]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||
| 244 | 0 | return; | |||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | 6 | *FETCH = \&lookup; | |||||||||||||||||||||||||||||||
| 248 | 6 | *get = \&lookup; | |||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | 8 | ▶ | sub nth { | ||||||||||||||||||||||||||||||
|
Subroutine
nth: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 251 | 8 | my ($self, $i) = @_; | |||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | 8 | ▶ | $i =~ /^-?\d+$/ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 254 | or croak('Integer index expected'); | ||||||||||||||||||||||||||||||||
| 255 | 8 | ▶ | if ($i < 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 256 | 4 | $i += $self->[SIZE]; | |||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||
| 258 | 8 | ▶ | if ($i < 0 || $i >= $self->[SIZE]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$i < 0 or $i >= $self->[2]
MC/DC0%0/2
$i < 0 or $i >= $self->[2]
$i < 0
$i >= $self->[2]
Truth table33%1/3
A or B
A= $i < 0B= $i >= $self->[2]
| |||||||||||||||||||||||||||||||||
| 259 | 0 | return; | |||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | 8 | my ($node, $next, $moves); | |||||||||||||||||||||||||||||||
| 263 | 8 | ▶ | if ($i > $self->[SIZE] / 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 264 | 4 | $node = $self->max; | |||||||||||||||||||||||||||||||
| 265 | 4 | $next = 'predecessor'; | |||||||||||||||||||||||||||||||
| 266 | 4 | $moves = $self->[SIZE] - $i - 1; | |||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||
| 268 | else { | ||||||||||||||||||||||||||||||||
| 269 | 4 | $node = $self->min; | |||||||||||||||||||||||||||||||
| 270 | 4 | $next = 'successor'; | |||||||||||||||||||||||||||||||
| 271 | 4 | $moves = $i; | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | 8 | my $count = 0; | |||||||||||||||||||||||||||||||
| 275 | 8 | ▶ | while ($count != $moves) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 276 | 4 | $node = $node->$next; | |||||||||||||||||||||||||||||||
| 277 | 4 | ++$count; | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | 8 | return $node; | |||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | 2 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 283 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 284 | 2 | my $key = shift; | |||||||||||||||||||||||||||||||
| 285 | 2 | return defined $self->lookup($key); | |||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | 41 | ▶ | sub put { | ||||||||||||||||||||||||||||||
|
Subroutine
put: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 289 | 41 | my $self = shift; | |||||||||||||||||||||||||||||||
| 290 | 41 | my $key_or_node = shift; | |||||||||||||||||||||||||||||||
| 291 | 41 | ▶ | defined $key_or_node | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 292 | or croak("Can't use undefined value as key or node"); | ||||||||||||||||||||||||||||||||
| 293 | 41 | my $val = shift; | |||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 41 | my $cmp = $self->[CMP]; | |||||||||||||||||||||||||||||||
| 296 | 41 | ▶ | my $z = (ref $key_or_node eq 'Tree::RB::Node') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 297 | ? $key_or_node | ||||||||||||||||||||||||||||||||
| 298 | : Tree::RB::Node->new($key_or_node => $val); | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | 41 | my $y; | |||||||||||||||||||||||||||||||
| 301 | 41 | my $x = $self->[ROOT]; | |||||||||||||||||||||||||||||||
| 302 | 41 | ▶ | while($x) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 303 | 56 | $y = $x; | |||||||||||||||||||||||||||||||
| 304 | # Handle case of inserting node with duplicate key. | ||||||||||||||||||||||||||||||||
| 305 | 56 | ▶ | if($cmp ? $cmp->($z->[_KEY], $x->[_KEY]) == 0 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 306 | : $z->[_KEY] eq $x->[_KEY]) | ||||||||||||||||||||||||||||||||
| 307 | { | ||||||||||||||||||||||||||||||||
| 308 | 1 | my $old_val = $x->[_VAL]; | |||||||||||||||||||||||||||||||
| 309 | 1 | $x->[_VAL] = $z->[_VAL]; | |||||||||||||||||||||||||||||||
| 310 | 1 | return $old_val; | |||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | 55 | ▶ | if($cmp ? $cmp->($z->[_KEY], $x->[_KEY]) < 0 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 314 | : $z->[_KEY] lt $x->[_KEY]) | ||||||||||||||||||||||||||||||||
| 315 | { | ||||||||||||||||||||||||||||||||
| 316 | 26 | $x = $x->[_LEFT]; | |||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | else { | ||||||||||||||||||||||||||||||||
| 319 | 29 | $x = $x->[_RIGHT]; | |||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||
| 322 | # insert new node | ||||||||||||||||||||||||||||||||
| 323 | 40 | $z->[_PARENT] = $y; | |||||||||||||||||||||||||||||||
| 324 | 40 | ▶ | if(not defined $y) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 325 | 8 | $self->[ROOT] = $z; | |||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | else { | ||||||||||||||||||||||||||||||||
| 328 | 32 | ▶ | if($cmp ? $cmp->($z->[_KEY], $y->[_KEY]) < 0 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 329 | : $z->[_KEY] lt $y->[_KEY]) | ||||||||||||||||||||||||||||||||
| 330 | { | ||||||||||||||||||||||||||||||||
| 331 | 17 | $y->[_LEFT] = $z; | |||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||
| 333 | else { | ||||||||||||||||||||||||||||||||
| 334 | 15 | $y->[_RIGHT] = $z; | |||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||
| 337 | 40 | $self->_fix_after_insertion($z); | |||||||||||||||||||||||||||||||
| 338 | 40 | $self->[SIZE]++; | |||||||||||||||||||||||||||||||
| 339 | 40 | return; | |||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | 6 | *STORE = \&put; | |||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | 40 | ▶ | sub _fix_after_insertion { | ||||||||||||||||||||||||||||||
|
Subroutine
_fix_after_insertion: called CC=11 SCAR=28.5
| |||||||||||||||||||||||||||||||||
| 345 | 40 | my $self = shift; | |||||||||||||||||||||||||||||||
| 346 | 40 | ▶ | my $x = shift or croak('Missing arg: node'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | 40 | $x->[_COLOR] = RED; | |||||||||||||||||||||||||||||||
| 349 | 40 | ▶ | while($x != $self->[ROOT] && $x->[_PARENT][_COLOR] == RED) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$x != $self->[0] and $x->[0][3] == 0
MC/DC100%2/2
$x != $self->[0] and $x->[0][3] == 0
$x != $self->[0]
$x->[0][3] == 0
Truth table100%3/3
A and B
A= $x != $self->[0]B= $x->[0][3] == 0
| |||||||||||||||||||||||||||||||||
| 350 | 7 | my ($child, $rotate1, $rotate2); | |||||||||||||||||||||||||||||||
| 351 | 7 | ▶ | if(($x->[_PARENT] || 0) == ($x->[_PARENT][_PARENT][_LEFT] || 0)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$x->[0] || 0
$x->[0][0][1] || 0
MC/DC0%0/1
$x->[0] || 0
$x->[0]
MC/DC0%0/1
$x->[0][0][1] || 0
$x->[0][0][1]
Truth table50%1/2
A
A= $x->[0]
Truth table50%1/2
A
A= $x->[0][0][1]
| |||||||||||||||||||||||||||||||||
| 352 | 1 | ($child, $rotate1, $rotate2) = (_RIGHT, '_left_rotate', '_right_rotate'); | |||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||
| 354 | else { | ||||||||||||||||||||||||||||||||
| 355 | 6 | ($child, $rotate1, $rotate2) = (_LEFT, '_right_rotate', '_left_rotate'); | |||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||
| 357 | 7 | my $y = $x->[_PARENT][_PARENT][$child]; | |||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||
| 359 | 7 | ▶ | if($y && $y->[_COLOR] == RED) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$y and $y->[3] == 0
MC/DC0%0/2
$y and $y->[3] == 0
$y
$y->[3] == 0
Truth table33%1/3
A and B
A= $yB= $y->[3] == 0
| |||||||||||||||||||||||||||||||||
| 360 | 7 | $x->[_PARENT][_COLOR] = BLACK; | |||||||||||||||||||||||||||||||
| 361 | 7 | $y->[_COLOR] = BLACK; | |||||||||||||||||||||||||||||||
| 362 | 7 | $x->[_PARENT][_PARENT][_COLOR] = RED; | |||||||||||||||||||||||||||||||
| 363 | 7 | $x = $x->[_PARENT][_PARENT]; | |||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||
| 365 | else { | ||||||||||||||||||||||||||||||||
| 366 | 0 | ▶ | if($x == ($x->[_PARENT][$child] || 0)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$x->[0][$child] || 0
MC/DC0%0/1
$x->[0][$child] || 0
$x->[0][$child]
Truth table0%0/2
A
A= $x->[0][$child]
| |||||||||||||||||||||||||||||||||
| 367 | 0 | $x = $x->[_PARENT]; | |||||||||||||||||||||||||||||||
| 368 | 0 | $self->$rotate1($x); | |||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||
| 370 | 0 | $x->[_PARENT][_COLOR] = BLACK; | |||||||||||||||||||||||||||||||
| 371 | 0 | $x->[_PARENT][_PARENT][_COLOR] = RED; | |||||||||||||||||||||||||||||||
| 372 | 0 | $self->$rotate2($x->[_PARENT][_PARENT]); | |||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||
| 375 | 40 | $self->[ROOT][_COLOR] = BLACK; | |||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | 7 | ▶ | sub delete { | ||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=16 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 379 | 7 | my ($self, $key_or_node) = @_; | |||||||||||||||||||||||||||||||
| 380 | 7 | ▶ | defined $key_or_node | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 381 | or croak("Can't use undefined value as key or node"); | ||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||
| 383 | 7 | ▶ | my $z = (ref $key_or_node eq 'Tree::RB::Node') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 384 | ? $key_or_node | ||||||||||||||||||||||||||||||||
| 385 | : ($self->lookup($key_or_node))[1]; | ||||||||||||||||||||||||||||||||
| 386 | 7 | ▶ | return unless $z; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||
| 388 | 7 | my $y; | |||||||||||||||||||||||||||||||
| 389 | 7 | ▶ | if($z->[_LEFT] && $z->[_RIGHT]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$z->[1] and $z->[2]
MC/DC100%2/2
$z->[1] and $z->[2]
$z->[1]
$z->[2]
Truth table100%3/3
A and B
A= $z->[1]B= $z->[2]
| |||||||||||||||||||||||||||||||||
| 390 | # (Notes kindly provided by Christopher Gurnee) | ||||||||||||||||||||||||||||||||
| 391 | # When deleting a node 'z' which has two children from a binary search tree, the | ||||||||||||||||||||||||||||||||
| 392 | # typical algorithm is to delete the successor node 'y' instead (which is | ||||||||||||||||||||||||||||||||
| 393 | # guaranteed to have at most one child), and then to overwrite the key/values of | ||||||||||||||||||||||||||||||||
| 394 | # node 'z' (which is still in the tree) with the key/values (which we don't want | ||||||||||||||||||||||||||||||||
| 395 | # to lose) from the now-deleted successor node 'y'. | ||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | # Since we need to return the deleted item, it's not good enough to overwrite the | ||||||||||||||||||||||||||||||||
| 398 | # key/values of node 'z' with those of node 'y'. Instead we swap them so we can | ||||||||||||||||||||||||||||||||
| 399 | # return the deleted values. | ||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | 1 | $y = $z->successor; | |||||||||||||||||||||||||||||||
| 402 | 1 | ($z->[_KEY], $y->[_KEY]) = ($y->[_KEY], $z->[_KEY]); | |||||||||||||||||||||||||||||||
| 403 | 1 | ($z->[_VAL], $y->[_VAL]) = ($y->[_VAL], $z->[_VAL]); | |||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||
| 405 | else { | ||||||||||||||||||||||||||||||||
| 406 | 6 | $y = $z; | |||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||
| 409 | # splice out $y | ||||||||||||||||||||||||||||||||
| 410 | 7 | ▶ | my $x = $y->[_LEFT] || $y->[_RIGHT]; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$y->[1] || $y->[2]
MC/DC100%2/2
$y->[1] || $y->[2]
$y->[1]
$y->[2]
Truth table100%3/3
A || B
A= $y->[1]B= $y->[2]
| |||||||||||||||||||||||||||||||||
| 411 | 7 | ▶ | if(defined $x) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 412 | 4 | $x->[_PARENT] = $y->[_PARENT]; | |||||||||||||||||||||||||||||||
| 413 | 4 | ▶ | if(! defined $y->[_PARENT]) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 414 | 0 | $self->[ROOT] = $x; | |||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||
| 416 | elsif($y == $y->[_PARENT][_LEFT]) { | ||||||||||||||||||||||||||||||||
| 417 | 2 | $y->[_PARENT][_LEFT] = $x; | |||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||
| 419 | else { | ||||||||||||||||||||||||||||||||
| 420 | 2 | $y->[_PARENT][_RIGHT] = $x; | |||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||
| 422 | # Null out links so they are OK to use by _fix_after_deletion | ||||||||||||||||||||||||||||||||
| 423 | 4 | delete @{$y}[_PARENT, _LEFT, _RIGHT]; | |||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | # Fix replacement | ||||||||||||||||||||||||||||||||
| 426 | 4 | ▶ | if($y->[_COLOR] == BLACK) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 427 | 4 | $self->_fix_after_deletion($x); | |||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||
| 430 | elsif(! defined $y->[_PARENT]) { | ||||||||||||||||||||||||||||||||
| 431 | # return if we are the only node | ||||||||||||||||||||||||||||||||
| 432 | 2 | delete $self->[ROOT]; | |||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||
| 434 | else { | ||||||||||||||||||||||||||||||||
| 435 | # No children. Use self as phantom replacement and unlink | ||||||||||||||||||||||||||||||||
| 436 | 1 | ▶ | if($y->[_COLOR] == BLACK) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 437 | 1 | $self->_fix_after_deletion($y); | |||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||
| 439 | 1 | ▶ | if(defined $y->[_PARENT]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 440 | 6 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 441 | 1 | ▶ | if($y == $y->[_PARENT][_LEFT]) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 442 | 0 | delete $y->[_PARENT][_LEFT]; | |||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||
| 444 | elsif($y == $y->[_PARENT][_RIGHT]) { | ||||||||||||||||||||||||||||||||
| 445 | 1 | delete $y->[_PARENT][_RIGHT]; | |||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||
| 447 | 1 | delete $y->[_PARENT]; | |||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||
| 450 | 7 | $self->[SIZE]--; | |||||||||||||||||||||||||||||||
| 451 | 7 | return $y; | |||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||
| 454 | 6 | *DELETE = \&delete; | |||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | 5 | ▶ | sub _fix_after_deletion { | ||||||||||||||||||||||||||||||
|
Subroutine
_fix_after_deletion: called CC=9 SCAR=28.0
| |||||||||||||||||||||||||||||||||
| 457 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||
| 458 | 5 | ▶ | my $x = shift or croak('Missing arg: node'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||
| 460 | 5 | ▶ | while($x != $self->[ROOT] && color_of($x) == BLACK) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$x != $self->[0] and color_of($x) == 1
MC/DC100%2/2
$x != $self->[0] and color_of($x) == 1
$x != $self->[0]
color_of($x) == 1
Truth table100%3/3
A and B
A= $x != $self->[0]B= color_of($x) == 1
| |||||||||||||||||||||||||||||||||
| 461 | 1 | my ($child1, $child2, $rotate1, $rotate2); | |||||||||||||||||||||||||||||||
| 462 | 6 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 463 | 1 | ▶ | if($x == left_of(parent_of($x))) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 464 | 0 | ($child1, $child2, $rotate1, $rotate2) = | |||||||||||||||||||||||||||||||
| 465 | (\&right_of, \&left_of, '_left_rotate', '_right_rotate'); | ||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||
| 467 | else { | ||||||||||||||||||||||||||||||||
| 468 | 1 | ($child1, $child2, $rotate1, $rotate2) = | |||||||||||||||||||||||||||||||
| 469 | (\&left_of, \&right_of, '_right_rotate', '_left_rotate'); | ||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||
| 471 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||
| 473 | 1 | my $w = $child1->(parent_of($x)); | |||||||||||||||||||||||||||||||
| 474 | 1 | ▶ | if(color_of($w) == RED) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 475 | 0 | set_color($w, BLACK); | |||||||||||||||||||||||||||||||
| 476 | 0 | set_color(parent_of($x), RED); | |||||||||||||||||||||||||||||||
| 477 | 0 | $self->$rotate1(parent_of($x)); | |||||||||||||||||||||||||||||||
| 478 | 0 | $w = right_of(parent_of($x)); | |||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||
| 480 | 1 | ▶ | if(color_of($child2->($w)) == BLACK && | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
color_of(&$child2($w)) == 1 and color_of(&$child1($w)) == 1
MC/DC0%0/2
color_of(&$child2($w)) == 1 and color_of(&$child1($w)) == 1
color_of(&$child2($w)) == 1
color_of(&$child1($w)) == 1
Truth table33%1/3
A and B
A= color_of(&$child2($w)) == 1B= color_of(&$child1($w)) == 1
| |||||||||||||||||||||||||||||||||
| 481 | color_of($child1->($w)) == BLACK) { | ||||||||||||||||||||||||||||||||
| 482 | 1 | set_color($w, RED); | |||||||||||||||||||||||||||||||
| 483 | 1 | $x = parent_of($x); | |||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||
| 485 | else { | ||||||||||||||||||||||||||||||||
| 486 | 0 | ▶ | if(color_of($child1->($w)) == BLACK) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 487 | 0 | set_color($child2->($w), BLACK); | |||||||||||||||||||||||||||||||
| 488 | 0 | set_color($w, RED); | |||||||||||||||||||||||||||||||
| 489 | 0 | $self->$rotate2($w); | |||||||||||||||||||||||||||||||
| 490 | 0 | $w = $child1->(parent_of($x)); | |||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||
| 492 | 0 | set_color($w, color_of(parent_of($x))); | |||||||||||||||||||||||||||||||
| 493 | 0 | set_color(parent_of($x), BLACK); | |||||||||||||||||||||||||||||||
| 494 | 0 | set_color($child1->($w), BLACK); | |||||||||||||||||||||||||||||||
| 495 | 0 | $self->$rotate1(parent_of($x)); | |||||||||||||||||||||||||||||||
| 496 | 0 | $x = $self->[ROOT]; | |||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||
| 499 | 5 | set_color($x, BLACK); | |||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||
| 502 | 0 | ▶ | sub _left_rotate { | ||||||||||||||||||||||||||||||
|
Subroutine
_left_rotate: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||
| 503 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 504 | 0 | ▶ | my $x = shift or croak('Missing arg: node'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||
| 506 | 0 | ▶ | my $y = $x->[_RIGHT] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 507 | or return; | ||||||||||||||||||||||||||||||||
| 508 | 0 | $x->[_RIGHT] = $y->[_LEFT]; | |||||||||||||||||||||||||||||||
| 509 | 0 | ▶ | if($y->[_LEFT]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 510 | 0 | $y->[_LEFT]->[_PARENT] = $x; | |||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||
| 512 | 0 | $y->[_PARENT] = $x->[_PARENT]; | |||||||||||||||||||||||||||||||
| 513 | 0 | ▶ | if(not defined $x->[_PARENT]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 514 | 0 | $self->[ROOT] = $y; | |||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||
| 516 | else { | ||||||||||||||||||||||||||||||||
| 517 | 0 | ▶ | $x == $x->[_PARENT]->[_LEFT] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 518 | ? $x->[_PARENT]->[_LEFT] = $y | ||||||||||||||||||||||||||||||||
| 519 | : $x->[_PARENT]->[_RIGHT] = $y; | ||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||
| 521 | 0 | $y->[_LEFT] = $x; | |||||||||||||||||||||||||||||||
| 522 | 0 | $x->[_PARENT] = $y; | |||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||
| 525 | 0 | ▶ | sub _right_rotate { | ||||||||||||||||||||||||||||||
|
Subroutine
_right_rotate: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||
| 526 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 527 | 0 | ▶ | my $y = shift or croak('Missing arg: node'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||
| 529 | 0 | ▶ | my $x = $y->[_LEFT] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 530 | or return; | ||||||||||||||||||||||||||||||||
| 531 | 0 | $y->[_LEFT] = $x->[_RIGHT]; | |||||||||||||||||||||||||||||||
| 532 | 0 | ▶ | if($x->[_RIGHT]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 533 | 0 | $x->[_RIGHT]->[_PARENT] = $y | |||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||
| 535 | 0 | $x->[_PARENT] = $y->[_PARENT]; | |||||||||||||||||||||||||||||||
| 536 | 0 | ▶ | if(not defined $y->[_PARENT]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 537 | 0 | $self->[ROOT] = $x; | |||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||
| 539 | else { | ||||||||||||||||||||||||||||||||
| 540 | 0 | ▶ | $y == $y->[_PARENT]->[_RIGHT] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 541 | ? $y->[_PARENT]->[_RIGHT] = $x | ||||||||||||||||||||||||||||||||
| 542 | : $y->[_PARENT]->[_LEFT] = $x; | ||||||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||||||
| 544 | 0 | $x->[_RIGHT] = $y; | |||||||||||||||||||||||||||||||
| 545 | 0 | $y->[_PARENT] = $x; | |||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||
| 548 | 6 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||||
| 549 | __END__ | ||||||||||||||||||||||||||||||||