DataStructure/BTree.pm

filter: Statementstmt 28.8% 28 / 97 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 50.0% 9 / 18 Podpod 0.0% 0 / 7 totaltotal 19.3% 37 / 191 CC 28 SCAR 40.7
CC
28
Cov
18%
delete
156.0
insert
72.0
find
20.0
CRAP 58.4
↑ Summary DataStructure/RedBlackTree.pm »
1 # A binary tree data-structure.
2 #
3 # Mostly from the Cormen-Leiserson-Rivest book.
4  
5 package DataStructure::BTree;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use feature ':5.24';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use DataStructure::BTree::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use parent qw(DataStructure::OrderedSet);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =pod
19  
20 =head1 NAME
21  
22 DataStructure::BTree
23  
24 =head1 SYNOPSIS
25  
26 A binary tree data-structure, written in pure Perl.
27  
28 =head1 DESCRIPTION
29  
30 =head2 CONSTRUCTOR
31  
32 C<< DataStructure::BTree->new(%options) >>
33  
34 Creates an empty binary tree.
35  
36 Available options are:
37  
38 =over 4
39  
40 =item cmp
41  
42 The comparator used for the elements of the tree. Should be a reference to a
43 sub using C<$a> and C<$b>. Defaults to using C<cmp> if not specified.
44  
45 =item multiset
46  
47 Whether several identical values can be stored.
48  
49 =back
50  
51 =cut
52  
53 0 ▶sub new ($class, %options) {
Subroutine new: not called CC=4 SCAR=25.0
Pod undocumented
54 0 ▶ my $calling_pkg = caller($options{_context_skip} // 0);
Condition0%0/2
$options{"_context_skip"} // 0
l!l
00
MC/DC0%0/1
$options{"_context_skip"} // 0
$options{"_context_skip"}
Truth table0%0/2
A
A= $options{"_context_skip"}
Aresult
11
00
55 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 return bless {
57 size => 0,
58 root => undef,
59 0 ▶ compare => $options{cmp} // sub { $a cmp $b},
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 multi => $options{multiset} // 0,
61 0 a => \*{ $calling_pkg . '::a' },
62 0 ▶ b => \*{ $calling_pkg . '::b' },
Condition0%0/5
$options{"cmp"} // sub { $a cmp $b; }
l!l&&r!l&&!r
000
$options{"multiset"} // 0
l!l
00
MC/DC0%0/2
$options{"cmp"} // sub { $a cmp $b; }
$options{"cmp"} sub { $a cmp $b; }
MC/DC0%0/1
$options{"multiset"} // 0
$options{"multiset"}
Truth table0%0/3
A // B
A= $options{"cmp"}B= sub { $a cmp $b; }
ABresult
1X1
011
000
Truth table0%0/2
A
A= $options{"multiset"}
Aresult
11
00
63 }, $class;
64 }
65  
66 # Returns a node with the given value or undef.
67 0 ▶sub find ($self, $value) {
Subroutine find: not called CC=4 SCAR=30.0
Pod undocumented
68 0 my $current = $self->{root};
69 0 local *{$self->{a}} = $value;
70 0 ▶ while (defined $current && $current->{value} != $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current and $current->{"value"} != $value) 0 0 0
Condition0%0/3
defined $current and $current->{"value"} != $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $current and $current->{"value"} != $value
defined $current $current->{"value"} != $value
Truth table0%0/3
A and B
A= defined $currentB= $current->{"value"} != $value
ABresult
0X0
100
111
71 0 local *{$self->{b}} = $current->{value};
72 0 ▶ if ($self->{compare}->() < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'compare'}->() < 0) { } 0 0 0
73 0 $current = $current->{left};
74 } else {
75 0 $current = $current->{right};
76 }
77 }
78 0 return $current;
79 }
80  
81 # Returns the newly inserted node or undef if the value is already present and
82 # multiset was not passed in the options.
83 0 ▶sub insert ($self, $value, $hint = undef) {
Subroutine insert: not called CC=8 SCAR=42.8
Pod undocumented
84 0 my $new_node = DataStructure::BTree::Node->new($self, $value);
85 0 my $current = $self->{root};
86 0 my $parent = undef;
87 0 local *{$self->{a}} = $value;
88 0 my $c;
89 0 ▶ while (defined $current) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current) 0 0 0
90 0 $parent = $current;
91 0 local *{$self->{b}} = $current->{value};
92 0 $c = $self->{compare}->();
93 0 ▶ if ($c < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c < 0) { } 0 0 0
elsif ($c > 0 or $self->{'multi'}) { } 0 0 0
94 0 $current = $current->{left};
95 ▶ } elsif ($c >0 || $self->{multi}) {
Condition0%0/3
$c > 0 or $self->{"multi"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c > 0 or $self->{"multi"}
$c > 0 $self->{"multi"}
Truth table0%0/3
A or B
A= $c > 0B= $self->{"multi"}
ABresult
1X1
011
000
96 0 $current = $current->{right};
97 } else {
98 0 return;
99 }
100 }
101 0 $self->{size}++;
102 0 $new_node->{parent} = $parent;
103 0 ▶ if (!defined $parent) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $parent) { } 0 0 0
elsif ($c < 0) { } 0 0 0
104 0 $self->{root} = $new_node;
105 } elsif ($c < 0) {
106 0 $parent->{left} = $new_node;
107 } else {
108 0 $parent->{right} = $new_node;
109 }
110 0 return $new_node;
111 }
112  
113 # Deletes the given node or one node with that value.
114 # Returns a true value on success and undef if the value is not found.
115 # Invalidates all node of the tree.
116 0 ▶sub delete ($self, $value_or_node) {
Subroutine delete: not called CC=12 SCAR=50.5
Pod undocumented
117 0 my $node;
118 0 ▶ if (ref($value_or_node) && $value_or_node->isa('DataStructure::BTree::Node')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value_or_node and $value_or_node->isa('DataStructure::BTree::Node')) { } 0 0 0
Condition0%0/3
ref $value_or_node and $value_or_node->isa("DataStructure::BTree::Node")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value_or_node and $value_or_node->isa("DataStructure::BTree::Node")
ref $value_or_node $value_or_node->isa("DataStructure::BTree::Node")
Truth table0%0/3
A and B
A= ref $value_or_nodeB= $value_or_node->isa("DataStructure::BTree::Node")
ABresult
0X0
100
111
119 0 $node = $value_or_node;
120 0 ▶ return unless $self == $node->{tree};
Branch: 1 branch
BranchTrueFalseTotal
unless $self == $node->{"tree"} 0 0 0
121 } else {
122 0 $node = $self->find($value_or_node);
123 0 ▶ return unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
124 }
125 0 my $replacement;
126 0 ▶ if (defined $node->{left} && defined $node->{right}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{'left'} and defined $node->{'right'}) { } 0 0 0
Condition0%0/3
defined $node->{"left"} and defined $node->{"right"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $node->{"left"} and defined $node->{"right"}
defined $node->{"left"} defined $node->{"right"}
Truth table0%0/3
A and B
A= defined $node->{"left"}B= defined $node->{"right"}
ABresult
0X0
100
111
127 0 $replacement = $node->_succ(); # cannot be null because we have a right child.
128 } else {
129 0 $replacement = $node;
130 }
131 0 my $new_child;
132 0 ▶ if (defined $replacement->{left}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replacement->{'left'}) { } 0 0 0
133 0 $new_child = $replacement->{left};
134 } else {
135 0 $new_child = $replacement->{right};
136 }
137 0 ▶ $new_child->{parent} = $replacement->{parent} if defined $new_child;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_child 0 0 0
138 0 my $parent = $replacement->{parent};
139 0 ▶ if ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) { } 0 0 0
140 0 ▶ if ($replacement == $parent->{left}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replacement == $parent->{'left'}) { } 0 0 0
141 0 $parent->{left} = $new_child;
142 } else {
143 0 $parent->{right} = $new_child;
144 }
145 } else {
146 0 $self->{root} = $new_child;
147 }
148 0 ▶ $node->{value} = $replacement->{value} if $node != $replacement;
Branch: 1 branch
BranchTrueFalseTotal
if $node != $replacement 0 0 0
149 0 $self->{size}--;
150 0 return 1;
151 }
152  
153 # Returns the smallest node in the tree
154 0 ▶sub min ($self) {
Subroutine min: not called CC=2 SCAR=17.9
Pod undocumented
155 0 ▶ return unless defined $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"root"} 0 0 0
156 0 return $self->{root}->_min_child();
157 }
158  
159 # Returns the biggest node in the tree
160 0 ▶sub max ($self) {
Subroutine max: not called CC=2 SCAR=17.9
Pod undocumented
161 0 ▶ return unless defined $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"root"} 0 0 0
162 0 return $self->{root}->_max_child();
163 }
164  
165 0 ▶sub _values ($self) {
Subroutine _values: not called CC=1 SCAR=6.9
166 0 ▶ sub node_value ($node) {
Subroutine node_value: not called CC=2 SCAR=17.9
Pod undocumented
167 0 ▶ return (undef) unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
168 0 return [$node->value(), $node->left(), $node->right()];
169 }
170 0 return node_value($self->{root});
171 }
172  
173 1 1;
↑ Summary DataStructure/RedBlackTree.pm »