RB.pm

filter: Statementstmt 80.4% 242 / 301 Branchbran 68.3% 123 / 180 Conditioncond 58.0% 29 / 50 MC/DCmcdc 38.7% 12 / 31 Subroutinesub 89.1% 33 / 37 Podpod 100% 11 / 11 totaltotal 73.7% 450 / 610 CC 110 SCAR 27.5
CC
110
Cov
74%
_left_rotate
42.0
_right_rotate
42.0
lookup
30.8
CRAP 15.7
↑ Summary RB/Node.pm »
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'
l!l
120
MC/DC0%0/1
shift() || 'min'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
39 12 ▶ my $next_fn = shift || 'successor';
Condition50%1/2
shift() || 'successor'
l!l
120
MC/DC0%0/1
shift() || 'successor'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
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
BranchTrueFalseTotal
if ($node) { } 45 28 73
46 45 $node = $node->$next_fn;
47 }
48 else {
49 28 ▶ if(defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 16 12 28
50 # seek to $key
51 16 ▶ (undef, $node) = $self->lookup(
Branch: 1 branch
BranchTrueFalseTotal
$next_fn eq 'successor' ? : 9 7 16
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
BranchTrueFalseTotal
defined $arg or croak("Can't seek to an undefined key") 10 0 10
75 10 my %args;
76 10 ▶ if(ref $arg eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 3 7 10
77 3 %args = %$arg;
78 }
79 else {
80 7 $args{-key} = $arg;
81 }
82
83 10 ▶ if(@_ && exists $args{-key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and exists $args{"-key"}) 3 7 10
Condition66%2/3
@_ and exists $args{"-key"}
!ll&&!rl&&r
703
MC/DC50%1/2
@_ and exists $args{"-key"}
@_ exists $args{"-key"}
Truth table66%2/3
A and B
A= @_B= exists $args{"-key"}
ABresult
0X0
100
111
84 3 my $arg = shift;
85 3 ▶ if(ref $arg eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq "HASH") 3 0 3
86 3 %args = (%$arg, %args);
87 }
88 }
89 10 ▶ if(! exists $args{-key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"-key"}) 1 9 10
90 1 ▶ defined $args{'-reverse'} or croak("Expected option '-reverse' is undefined");
Branch: 1 branch
BranchTrueFalseTotal
defined $args{"-reverse"} or croak("Expected option '-reverse' is undefined") 1 0 1
91 }
92 10 $self->[HASH_SEEK_ARG] = \%args;
93 10 ▶ if($self->[HASH_ITER]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3]) 9 1 10
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
BranchTrueFalseTotal
if ($self->[4]) { } 12 8 20
101 12 ▶ my $iter = ($self->[HASH_SEEK_ARG]{'-reverse'} ? 'rev_iter' : 'iter');
Branch: 1 branch
BranchTrueFalseTotal
$self->[4]{'-reverse'} ? : 6 6 12
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
BranchTrueFalseTotal
my $node = $self->[3]->next or return 8 3 11
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
BranchTrueFalseTotal
my $node = $self->[3]->next or return 32 7 39
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
BranchTrueFalseTotal
if ($cmp) 1 6 7
131 1 ▶ ref $cmp eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $cmp eq "CODE" or croak("Invalid arg: codref expected") 1 0 1
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
BranchTrueFalseTotal
if $_[0][0] 4 4 8
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
BranchTrueFalseTotal
if ($self->[0]) 1 3 4
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
BranchTrueFalseTotal
ref $cmp eq "CODE" or croak(sprintf("Arg of type coderef required; got %s", ref $cmp || "undef")) 0 0 0
Condition0%0/2
ref $cmp || 'undef'
l!l
00
MC/DC0%0/1
ref $cmp || 'undef'
ref $cmp
Truth table0%0/2
A
A= ref $cmp
Aresult
11
00
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
BranchTrueFalseTotal
unless $self->[0] 4 14 18
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
BranchTrueFalseTotal
unless $self->[0] 0 10 10
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
BranchTrueFalseTotal
defined $key or croak("Can't use undefined value as key") 51 0 51
191 or croak("Can't use undefined value as key");
192 51 ▶ my $mode = shift || LUEQUAL;
Condition100%2/2
shift() || 0
l!l
2526
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
193 51 my $cmp = $self->[CMP];
194  
195 51 my $y;
196 51 ▶ my $x = $self->[ROOT]
Branch: 1 branch
BranchTrueFalseTotal
my $x = $self->[0] or return 48 3 51
197 or return;
198 48 my $next_child;
199 48 ▶ while($x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x) 116 18 134
200 116 $y = $x;
201 116 ▶ if($cmp ? $cmp->($key, $x->[_KEY]) == 0
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp ? &$cmp($key, $x->[4]) == 0 : $key eq $x->[4]) 30 86 116
$cmp ? : 0 116 116
202 : $key eq $x->[_KEY]) {
203 # found it!
204 30 ▶ if($mode == LUGREAT || $mode == LUNEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode == 3 or $mode == 5) { } 0 30 30
elsif ($mode == 4 or $mode == 6) { } 0 30 30
Condition33%1/3
$mode == 3 or $mode == 5
l!l&&r!l&&!r
0030
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
ABresult
1X1
011
000
205 0 $x = $x->successor;
206 }
207 ▶ elsif($mode == LULESS || $mode == LUPREV) {
Condition33%1/3
$mode == 4 or $mode == 6
l!l&&r!l&&!r
0030
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
ABresult
1X1
011
000
208 0 $x = $x->predecessor;
209 }
210 return wantarray
211 30 ▶ ? ($x->[_VAL], $x)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 18 30
212 : $x->[_VAL];
213 }
214 86 ▶ if($cmp ? $cmp->($key, $x->[_KEY]) < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp ? &$cmp($key, $x->[4]) < 0 : $key lt $x->[4]) { } 41 45 86
$cmp ? : 0 86 86
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
BranchTrueFalseTotal
if ($mode == 1 or $mode == 3) { } 10 8 18
elsif ($mode == 2 or $mode == 4) { } 8 0 8
Condition100%3/3
$mode == 1 or $mode == 3
l!l&&r!l&&!r
828
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
ABresult
1X1
011
000
225 10 ▶ if($next_child == _LEFT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_child == 1) { } 5 5 10
226 5 ▶ return wantarray ? ($y->[_VAL], $y) : $y->[_VAL];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 2 5
227 }
228 else {
229 5 ▶ my $next = $y->successor
Branch: 1 branch
BranchTrueFalseTotal
my $next = $y->successor or return 2 3 5
230 or return;
231 2 ▶ return wantarray ? ($next->[_VAL], $next) : $next->[_VAL];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
232 }
233 }
234 ▶ elsif($mode == LULTEQ || $mode == LULESS) {
Condition33%1/3
$mode == 2 or $mode == 4
l!l&&r!l&&!r
800
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
ABresult
1X1
011
000
235 8 ▶ if($next_child == _RIGHT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_child == 2) { } 3 5 8
236 3 ▶ return wantarray ? ($y->[_VAL], $y) : $y->[_VAL];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
237 }
238 else {
239 5 ▶ my $next = $y->predecessor
Branch: 1 branch
BranchTrueFalseTotal
my $next = $y->predecessor or return 2 3 5
240 or return;
241 2 ▶ return wantarray ? ($next->[_VAL], $next) : $next->[_VAL];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
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
BranchTrueFalseTotal
$i =~ /^-?\d+$/ or croak("Integer index expected") 8 0 8
254 or croak('Integer index expected');
255 8 ▶ if ($i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) 4 4 8
256 4 $i += $self->[SIZE];
257 }
258 8 ▶ if ($i < 0 || $i >= $self->[SIZE]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0 or $i >= $self->[2]) 0 8 8
Condition33%1/3
$i < 0 or $i >= $self->[2]
l!l&&r!l&&!r
008
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]
ABresult
1X1
011
000
259 0 return;
260 }
261  
262 8 my ($node, $next, $moves);
263 8 ▶ if ($i > $self->[SIZE] / 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $self->[2] / 2) { } 4 4 8
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
BranchTrueFalseTotal
if ($count != $moves) 4 8 12
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
BranchTrueFalseTotal
defined $key_or_node or croak("Can't use undefined value as key or node") 41 0 41
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
BranchTrueFalseTotal
ref $key_or_node eq 'Tree::RB::Node' ? : 0 41 41
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
BranchTrueFalseTotal
if ($x) 56 40 96
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
BranchTrueFalseTotal
if ($cmp ? &$cmp($z->[4], $x->[4]) == 0 : $z->[4] eq $x->[4]) 1 55 56
$cmp ? : 8 48 56
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
BranchTrueFalseTotal
if ($cmp ? &$cmp($z->[4], $x->[4]) < 0 : $z->[4] lt $x->[4]) { } 26 29 55
$cmp ? : 8 47 55
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
BranchTrueFalseTotal
if (not defined $y) { } 8 32 40
325 8 $self->[ROOT] = $z;
326 }
327 else {
328 32 ▶ if($cmp ? $cmp->($z->[_KEY], $y->[_KEY]) < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp ? &$cmp($z->[4], $y->[4]) < 0 : $z->[4] lt $y->[4]) { } 17 15 32
$cmp ? : 5 27 32
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
BranchTrueFalseTotal
my $x = shift() or croak("Missing arg: node") 40 0 40
347  
348 40 $x->[_COLOR] = RED;
349 40 ▶ while($x != $self->[ROOT] && $x->[_PARENT][_COLOR] == RED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x != $self->[0] and $x->[0][3] == 0) 7 40 47
Condition100%3/3
$x != $self->[0] and $x->[0][3] == 0
!ll&&!rl&&r
14267
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
ABresult
0X0
100
111
350 7 my ($child, $rotate1, $rotate2);
351 7 ▶ if(($x->[_PARENT] || 0) == ($x->[_PARENT][_PARENT][_LEFT] || 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($x->[0] || 0) == ($x->[0][0][1] || 0)) { } 1 6 7
Condition50%2/4
$x->[0] || 0
l!l
70
$x->[0][0][1] || 0
l!l
70
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]
Aresult
11
00
Truth table50%1/2
A
A= $x->[0][0][1]
Aresult
11
00
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
BranchTrueFalseTotal
if ($y and $y->[3] == 0) { } 7 0 7
Condition33%1/3
$y and $y->[3] == 0
!ll&&!rl&&r
007
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($x == ($x->[0][$child] || 0)) 0 0 0
Condition0%0/2
$x->[0][$child] || 0
l!l
00
MC/DC0%0/1
$x->[0][$child] || 0
$x->[0][$child]
Truth table0%0/2
A
A= $x->[0][$child]
Aresult
11
00
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
BranchTrueFalseTotal
defined $key_or_node or croak("Can't use undefined value as key or node") 7 0 7
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
BranchTrueFalseTotal
ref $key_or_node eq 'Tree::RB::Node' ? : 0 7 7
384 ? $key_or_node
385 : ($self->lookup($key_or_node))[1];
386 7 ▶ return unless $z;
Branch: 1 branch
BranchTrueFalseTotal
unless $z 0 7 7
387  
388 7 my $y;
389 7 ▶ if($z->[_LEFT] && $z->[_RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->[1] and $z->[2]) { } 1 6 7
Condition100%3/3
$z->[1] and $z->[2]
!ll&&!rl&&r
421
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]
ABresult
0X0
100
111
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]
l!l&&r!l&&!r
223
MC/DC100%2/2
$y->[1] || $y->[2]
$y->[1] $y->[2]
Truth table100%3/3
A || B
A= $y->[1]B= $y->[2]
ABresult
1X1
011
000
411 7 ▶ if(defined $x) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $x) { } 4 3 7
elsif (not defined $y->[0]) { } 2 1 3
412 4 $x->[_PARENT] = $y->[_PARENT];
413 4 ▶ if(! defined $y->[_PARENT]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $y->[0]) { } 0 4 4
elsif ($y == $y->[0][1]) { } 2 2 4
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
BranchTrueFalseTotal
if ($y->[3] == 1) 4 0 4
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
BranchTrueFalseTotal
if ($y->[3] == 1) 1 0 1
437 1 $self->_fix_after_deletion($y);
438 }
439 1 ▶ if(defined $y->[_PARENT]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $y->[0]) 1 0 1
440 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
441 1 ▶ if($y == $y->[_PARENT][_LEFT]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y == $y->[0][1]) { } 0 1 1
elsif ($y == $y->[0][2]) { } 1 0 1
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
BranchTrueFalseTotal
my $x = shift() or croak("Missing arg: node") 5 0 5
459  
460 5 ▶ while($x != $self->[ROOT] && color_of($x) == BLACK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x != $self->[0] and color_of($x) == 1) 1 5 6
Condition100%3/3
$x != $self->[0] and color_of($x) == 1
!ll&&!rl&&r
141
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($x == left_of(parent_of($x))) { } 0 1 1
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
BranchTrueFalseTotal
if (color_of($w) == 0) 0 1 1
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
BranchTrueFalseTotal
if (color_of(&$child2($w)) == 1 and color_of(&$child1($w)) == 1) { } 1 0 1
Condition33%1/3
color_of(&$child2($w)) == 1 and color_of(&$child1($w)) == 1
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (color_of(&$child1($w)) == 1) 0 0 0
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
BranchTrueFalseTotal
my $x = shift() or croak("Missing arg: node") 0 0 0
505  
506 0 ▶ my $y = $x->[_RIGHT]
Branch: 1 branch
BranchTrueFalseTotal
my $y = $x->[2] or return 0 0 0
507 or return;
508 0 $x->[_RIGHT] = $y->[_LEFT];
509 0 ▶ if($y->[_LEFT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->[1]) 0 0 0
510 0 $y->[_LEFT]->[_PARENT] = $x;
511 }
512 0 $y->[_PARENT] = $x->[_PARENT];
513 0 ▶ if(not defined $x->[_PARENT]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $x->[0]) { } 0 0 0
514 0 $self->[ROOT] = $y;
515 }
516 else {
517 0 ▶ $x == $x->[_PARENT]->[_LEFT]
Branch: 1 branch
BranchTrueFalseTotal
$x == $x->[0][1] ? : 0 0 0
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
BranchTrueFalseTotal
my $y = shift() or croak("Missing arg: node") 0 0 0
528  
529 0 ▶ my $x = $y->[_LEFT]
Branch: 1 branch
BranchTrueFalseTotal
my $x = $y->[1] or return 0 0 0
530 or return;
531 0 $y->[_LEFT] = $x->[_RIGHT];
532 0 ▶ if($x->[_RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->[2]) 0 0 0
533 0 $x->[_RIGHT]->[_PARENT] = $y
534 }
535 0 $x->[_PARENT] = $y->[_PARENT];
536 0 ▶ if(not defined $y->[_PARENT]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $y->[0]) { } 0 0 0
537 0 $self->[ROOT] = $x;
538 }
539 else {
540 0 ▶ $y == $y->[_PARENT]->[_RIGHT]
Branch: 1 branch
BranchTrueFalseTotal
$y == $y->[0][2] ? : 0 0 0
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__
↑ Summary RB/Node.pm »