RB/Node.pm

filter: Statementstmt 90.9% 90 / 99 Branchbran 72.7% 32 / 44 Conditioncond 78.2% 18 / 23 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 95.6% 22 / 23 Podpod 61.5% 8 / 13 totaltotal 83.2% 179 / 215 CC 30 SCAR 15.6
CC
30
Cov
84%
as_lol
20.4
strip
6.0
predecessor
5.0
CRAP 4.8
« RB.pm ↑ Summary RB/Node/_Constants.pm »
1 package Tree::RB::Node;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Tree::RB::Node::_Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use vars qw( $VERSION @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 require Exporter;
9 7 *import = \&Exporter::import;
10 7 @EXPORT_OK = qw[set_color color_of parent_of left_of right_of];
11  
12 7 $VERSION = '0.500006';
13  
14 7 my %attribute = (
15 key => _KEY,
16 val => _VAL,
17 color => _COLOR,
18 parent => _PARENT,
19 left => _LEFT,
20 right => _RIGHT,
21 );
22  
23 42 ▶sub _accessor {
Subroutine _accessor: called CC=1 SCAR=0.0
24 42 my $index = shift;
25 76 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
26 76 my $self = shift;
27 76 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 65 76
28 11 $self->[$index] = shift;
29 }
30 76 return $self->[$index];
31 42 };
32 }
33  
34 7 ▶while(my($at, $idx) = each %attribute) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %attribute) 42 7 49
35 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 42 *$at = _accessor($idx);
37 }
38  
39 46 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
40 46 my $class = shift;
41 46 my $obj = [];
42  
43 46 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 46 0 46
44 46 $obj->[_KEY] = shift;
45 46 $obj->[_VAL] = shift;
46 }
47 46 return bless $obj, $class;
48 }
49  
50 29 ▶sub min {
Subroutine min: called CC=2 SCAR=6.9
Pod documented
51 29 my $self = shift;
52 29 ▶ while ($self->[_LEFT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]) 32 29 61
53 32 $self = $self->[_LEFT];
54 }
55 29 return $self;
56 }
57  
58 16 ▶sub max {
Subroutine max: called CC=2 SCAR=6.9
Pod documented
59 16 my $self = shift;
60 16 ▶ while ($self->[_RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2]) 18 16 34
61 18 $self = $self->[_RIGHT];
62 }
63 16 return $self;
64 }
65  
66 68 ▶sub leaf {
Subroutine leaf: called CC=3 SCAR=11.0
Pod documented
67 68 my $self = shift;
68 68 ▶ while (my $any_child = $self->[_LEFT] || $self->[_RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $any_child = $self->[1] || $self->[2]) 40 68 108
Condition100%3/3
$self->[1] || $self->[2]
l!l&&r!l&&!r
221868
MC/DC100%2/2
$self->[1] || $self->[2]
$self->[1] $self->[2]
Truth table100%3/3
A || B
A= $self->[1]B= $self->[2]
ABresult
1X1
011
000
69 40 $self = $any_child;
70 }
71 68 return $self;
72 }
73  
74 46 ▶sub successor {
Subroutine successor: called CC=5 SCAR=16.1
Pod documented
75 46 my $self = shift;
76 46 ▶ if ($self->[_RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2]) 14 32 46
77 14 return $self->[_RIGHT]->min;
78 }
79 32 my $parent = $self->[_PARENT];
80 32 ▶ while ($parent && $parent->[_RIGHT] && $self == $parent->[_RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and $parent->[2] and $self == $parent->[2]) 20 32 52
Condition100%6/6
$parent and $parent->[2] and $self == $parent->[2]
!ll&&!rl&&r
171520
$parent and $parent->[2]
!ll&&!rl&&r
9835
MC/DC100%3/3
$parent and $parent->[2] and $self == $parent->[2]
$parent $parent->[2] $self == $parent->[2]
Truth table100%4/4
A and B and C
A= $parentB= $parent->[2]C= $self == $parent->[2]
ABCresult
0XX0
10X0
1100
1111
81 20 $self = $parent;
82 20 $parent = $parent->[_PARENT];
83 }
84 32 return $parent;
85 }
86  
87 16 ▶sub predecessor {
Subroutine predecessor: called CC=5 SCAR=16.1
Pod documented
88 16 my $self = shift;
89 16 ▶ if ($self->[_LEFT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]) 5 11 16
90 5 return $self->[_LEFT]->max;
91 }
92 11 my $parent = $self->[_PARENT];
93 11 ▶ while ($parent && $parent->[_LEFT] && $self == $parent->[_LEFT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and $parent->[1] and $self == $parent->[1]) 9 11 20
Condition83%5/6
$parent and $parent->[1] and $self == $parent->[1]
!ll&&!rl&&r
479
$parent and $parent->[1]
!ll&&!rl&&r
4016
MC/DC66%2/3
$parent and $parent->[1] and $self == $parent->[1]
$parent $parent->[1] $self == $parent->[1]
Truth table75%3/4
A and B and C
A= $parentB= $parent->[1]C= $self == $parent->[1]
ABCresult
0XX0
10X0
1100
1111
94 9 $self = $parent;
95 9 $parent = $parent->[_PARENT];
96 }
97 11 return $parent;
98 }
99  
100 0 ▶sub as_lol {
Subroutine as_lol: not called CC=5 SCAR=30.1
Pod documented
101 0 my $self = shift;
102 0 ▶ my $node = shift || $self;
Condition0%0/3
shift() || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self
shift() $self
Truth table0%0/3
A || B
A= shift()B= $self
ABresult
1X1
011
000
103 0 my $aref;
104 0 ▶ push @$aref,
Branch: 1 branch
BranchTrueFalseTotal
$node->[1] ? : 0 0 0
105 $node->[_LEFT]
106 ? $self->as_lol($node->[_LEFT])
107 : '*';
108 0 ▶ push @$aref,
Branch: 1 branch
BranchTrueFalseTotal
$node->[2] ? : 0 0 0
109 $node->[_RIGHT]
110 ? $self->as_lol($node->[_RIGHT])
111 : '*';
112 0 ▶ my $color = ($node->[_COLOR] == RED ? 'R' : 'B');
Branch: 1 branch
BranchTrueFalseTotal
$node->[3] == 0 ? : 0 0 0
113 7 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 0 push @$aref, "$color:$node->[_KEY]";
115 0 return $aref;
116 }
117  
118 46 ▶sub strip {
Subroutine strip: called CC=6 SCAR=18.0
Pod documented
119 46 my $self = shift;
120 46 my $callback = shift;
121  
122 46 my $x = $self;
123 46 ▶ while($x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x) 66 46 112
124 66 my $leaf = $x->leaf;
125 66 $x = $leaf->[_PARENT];
126  
127 # detach $leaf from the (sub)tree
128 7 ▶ no warnings "uninitialized";
Subroutine BEGIN: called CC=1 SCAR=0.0
129 66 ▶ if($leaf == $x->[_LEFT]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leaf == $x->[1]) { } 14 52 66
130 14 undef $x->[_LEFT];
131 }
132 else {
133 52 undef $x->[_RIGHT];
134 }
135 66 undef $leaf->[_PARENT];
136 66 ▶ if($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) 0 66 66
137 0 $callback->($leaf);
138 }
139  
140 66 ▶ if(!$x->[_LEFT] && !$x->[_RIGHT]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->[1] or $x->[2]) 57 9 66
Condition66%2/3
$x->[1] or $x->[2]
l!l&&r!l&&!r
0957
MC/DC50%1/2
$x->[1] or $x->[2]
$x->[1] $x->[2]
Truth table66%2/3
A or B
A= $x->[1]B= $x->[2]
ABresult
1X1
011
000
141 57 $x = $x->[_PARENT];
142 }
143 }
144 }
145  
146 45 ▶sub DESTROY { $_[0]->strip; }
Subroutine DESTROY: called CC=1 SCAR=0.0
147  
148 # Null aware accessors to assist with rebalancings during insertion and deletion
149 #
150 # A weird case of Java to the rescue!
151 # These are inspired by http://www.javaresearch.org/source/jdk142/java/util/TreeMap.java.html
152 # which was found via http://en.wikipedia.org/wiki/Red-black_tree#Implementations
153  
154 6 ▶sub set_color {
Subroutine set_color: called CC=3 SCAR=11.1
Pod undocumented
155 6 my ($node, $color) = @_;
156 6 ▶ if($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 6 0 6
157 6 ▶ $node->[_COLOR] = $color || BLACK;
Condition100%2/2
$color || 1
l!l
51
MC/DC100%1/1
$color || 1
$color
Truth table100%2/2
A
A= $color
Aresult
11
00
158 }
159 }
160  
161 8 ▶sub color_of {
Subroutine color_of: called CC=2 SCAR=6.9
Pod undocumented
162 8 ▶ $_[0] ? $_[0]->[_COLOR] : BLACK;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 6 2 8
163 }
164  
165 3 ▶sub parent_of {
Subroutine parent_of: called CC=2 SCAR=7.6
Pod undocumented
166 3 ▶ $_[0] ? $_[0]->[_PARENT] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 3 0 3
167 }
168  
169 3 ▶sub left_of {
Subroutine left_of: called CC=2 SCAR=7.6
Pod undocumented
170 3 ▶ $_[0] ? $_[0]->[_LEFT] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 3 0 3
171 }
172  
173 1 ▶sub right_of {
Subroutine right_of: called CC=2 SCAR=7.6
Pod undocumented
174 1 ▶ $_[0] ? $_[0]->[_RIGHT] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 0 1
175 }
176  
177  
178 7 1; # Magic true value required at end of module
179 __END__
« RB.pm ↑ Summary RB/Node/_Constants.pm »