| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->[1] || $self->[2]
MC/DC100%2/2
$self->[1] || $self->[2]
$self->[1]
$self->[2]
Truth table100%3/3
A || B
A= $self->[1]B= $self->[2]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$parent and $parent->[2] and $self == $parent->[2]
$parent and $parent->[2]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$parent and $parent->[1] and $self == $parent->[1]
$parent and $parent->[1]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
shift() || $self
shift()
$self
Truth table0%0/3
A || B
A= shift()B= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $aref; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | push @$aref, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | $node->[_LEFT] | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ? $self->as_lol($node->[_LEFT]) | ||||||||||||||||||||||||||||||||||||||||||
| 107 | : '*'; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | push @$aref, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | $node->[_RIGHT] | ||||||||||||||||||||||||||||||||||||||||||
| 110 | ? $self->as_lol($node->[_RIGHT]) | ||||||||||||||||||||||||||||||||||||||||||
| 111 | : '*'; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $color = ($node->[_COLOR] == RED ? 'R' : 'B'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $callback->($leaf); | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 66 | ▶ | if(!$x->[_LEFT] && !$x->[_RIGHT]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$x->[1] or $x->[2]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 6 | ▶ | $node->[_COLOR] = $color || BLACK; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$color || 1
MC/DC100%1/1
$color || 1
$color
Truth table100%2/2
A
A= $color
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 7 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||||||||||||||||||||||||