Heap/Binary.pm

filter: Statementstmt 58.7% 74 / 126 Branchbran 28.1% 18 / 64 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 16 totaltotal 47.2% 113 / 239 CC 37 SCAR 28.5
CC
37
Cov
50%
heapcheck
72.0
hdump
20.0
decrease_key
20.0
CRAP 17.3
« Heap/Fibonacci.pm ↑ Summary Heap/Binomial.pm »
1 package Heap::Binary;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 $VERSION = '0.80';
7  
8 # common names:
9 # h - heap head
10 # i - index of a heap value element
11 # v - user-provided value (to be) stored on the heap
12  
13 ################################################# debugging control
14  
15 2 my $debug = 0;
16 2 my $validate = 0;
17  
18 # enable/disable debugging output
19 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
20 0 ▶ @_ ? ($debug = shift) : $debug;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
21 }
22  
23 # enable/disable validation checks on values
24 0 ▶sub validate {
Subroutine validate: not called CC=2 SCAR=17.9
Pod undocumented
25 0 ▶ @_ ? ($validate = shift) : $validate;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
26 }
27  
28 2 my $width = 3;
29 2 my $bar = ' | ';
30 2 my $corner = ' +-';
31 2 my $vfmt = "%3d";
32  
33 0 ▶sub set_width {
Subroutine set_width: not called CC=2 SCAR=17.9
Pod undocumented
34 0 $width = shift;
35 0 ▶ $width = 2 if $width < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $width < 2 0 0 0
36  
37 0 $vfmt = "%${width}d";
38 0 $bar = $corner = ' ' x $width;
39 0 substr($bar,-2,1) = '|';
40 0 substr($corner,-2,2) = '+-';
41 }
42  
43  
44 0 ▶sub hdump {
Subroutine hdump: not called CC=4 SCAR=30.0
Pod undocumented
45 0 my $h = shift;
46 0 my $i = shift;
47 0 my $p = shift;
48 0 my $ch = $i*2+1;
49  
50 0 ▶ return if $i >= @$h;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @$h 0 0 0
51  
52 0 my $space = ' ' x $width;
53  
54 0 printf( "%${width}d", $h->[$i]->val );
55 0 ▶ if( $ch+1 < @$h ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch + 1 < @$h) 0 0 0
56 0 hdump( $h, $ch, $p . $bar);
57 0 print( $p, $corner );
58 0 ++$ch;
59 }
60 0 ▶ if( $ch < @$h ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch < @$h) { } 0 0 0
61 0 hdump( $h, $ch, $p . $space );
62 } else {
63 0 print "\n";
64 }
65 }
66  
67 0 ▶sub heapdump {
Subroutine heapdump: not called CC=2 SCAR=17.9
Pod undocumented
68 0 my $h;
69  
70 0 ▶ while( $h = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h = shift()) 0 0 0
71 0 hdump $h, 0, '';
72 0 print "\n";
73 }
74 }
75  
76 0 ▶sub heapcheck {
Subroutine heapcheck: not called CC=8 SCAR=42.8
Pod undocumented
77 0 my $h;
78 0 ▶ while( $h = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h = shift()) 0 0 0
79 0 my $i;
80 my $p;
81 0 ▶ next unless @$h;
Branch: 1 branch
BranchTrueFalseTotal
unless @$h 0 0 0
82 0 ▶ for( $p = 0, $i = 1; $i < @$h; ++$p, ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$h) 0 0 0
83 0 ▶ $h->[$p]->cmp($h->[$i]) <= 0 or die "not in heap order";
Branch: 1 branch
BranchTrueFalseTotal
$h->[$p]->cmp($h->[$i]) <= 0 or die "not in heap order" 0 0 0
84 0 ▶ last unless ++$i < @$h;
Branch: 1 branch
BranchTrueFalseTotal
unless ++$i < @$h 0 0 0
85 0 ▶ $h->[$p]->cmp($h->[$i]) <= 0 or die "not in heap order";
Branch: 1 branch
BranchTrueFalseTotal
$h->[$p]->cmp($h->[$i]) <= 0 or die "not in heap order" 0 0 0
86 }
87 0 ▶ heapdump $h if $validate >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $validate >= 2 0 0 0
88 }
89 }
90  
91 ################################################# forward declarations
92  
93 sub moveto;
94 sub heapup;
95 sub heapdown;
96  
97 ################################################# heap methods
98  
99 # new() usually Heap::Binary->new()
100 # return a new empty heap
101 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
102 2 my $self = shift;
103 2 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
104 2 return bless [], $class;
105 }
106  
107 # add($h,$v) usually $h->add($v)
108 # insert value $v into the heap
109 200 ▶sub add {
Subroutine add: called CC=4 SCAR=19.7
Pod undocumented
110 200 my $h = shift;
111 200 my $v = shift;
112 200 ▶ $validate && do {
Branch: 1 branch
BranchTrueFalseTotal
$validate and do { die "Method 'heap' required for element on heap" unless $v->can("heap"); die "Method 'cmp' required for element on heap" unless $v->can("cmp") } 0 200 200
113 0 ▶ die "Method 'heap' required for element on heap"
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("heap") 0 0 0
114 unless $v->can('heap');
115 0 ▶ die "Method 'cmp' required for element on heap"
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("cmp") 0 0 0
116 unless $v->can('cmp');
117 };
118 200 heapup $h, scalar(@$h), $v;
119 }
120  
121 # top($h) usually $h->top
122 # the smallest value is returned, but it is still left on the heap
123 102 ▶sub top {
Subroutine top: called CC=1 SCAR=0.0
Pod undocumented
124 102 my $h = shift;
125 102 $h->[0];
126 }
127  
128 2 *minimum = \&top;
129  
130 # extract_top($h) usually $h->extract_top
131 # the smallest value is returned after removing it from the heap
132 102 ▶sub extract_top {
Subroutine extract_top: called CC=3 SCAR=11.0
Pod undocumented
133 102 my $h = shift;
134 102 my $top = $h->[0];
135 102 ▶ if( @$h ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$h) 100 2 102
136 # there was at least one item, must decrease the heap
137 100 $top->heap(undef);
138 100 my $last = pop(@$h);
139 100 ▶ if( @$h ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$h) 100 0 100
140 # $top was not the only thing left, so re-heap the
141 # remainder by over-writing position zero (where
142 # $top was) using the value popped from the end
143 100 heapdown $h, 0, $last;
144 }
145 }
146 102 $top;
147 }
148  
149 2 *extract_minimum = \&extract_top;
150  
151 # absorb($h,$h2) usually $h->absorb($h2)
152 # all of the values in $h2 are inserted into $h instead, $h2 is left
153 # empty.
154 0 ▶sub absorb {
Subroutine absorb: not called CC=2 SCAR=17.9
Pod undocumented
155 0 my $h = shift;
156 0 my $h2 = shift;
157 0 my $v;
158  
159 0 foreach $v (splice @$h2, 0) {
160 0 $h->add($v);
161 }
162 0 $h;
163 }
164  
165 # decrease_key($h,$v) usually $h->decrease_key($v)
166 # the key value of $v has just been decreased and so it may need to
167 # be percolated to a higher position in the heap
168 0 ▶sub decrease_key {
Subroutine decrease_key: not called CC=4 SCAR=30.0
Pod undocumented
169 0 my $h = shift;
170 0 my $v = shift;
171 0 ▶ $validate && do {
Branch: 1 branch
BranchTrueFalseTotal
$validate and do { die "Method 'heap' required for element on heap" unless $v->can("heap"); die "Method 'cmp' required for element on heap" unless $v->can("cmp") } 0 0 0
172 0 ▶ die "Method 'heap' required for element on heap"
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("heap") 0 0 0
173 unless $v->can('heap');
174 0 ▶ die "Method 'cmp' required for element on heap"
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("cmp") 0 0 0
175 unless $v->can('cmp');
176 };
177 0 my $i = $v->heap;
178  
179 0 heapup $h, $i, $v;
180 }
181  
182 # delete($h,$v) usually: $h->delete($v)
183 # delete value $v from heap $h. It must have previously been
184 # add'ed to $h.
185 100 ▶sub delete {
Subroutine delete: called CC=7 SCAR=21.1
Pod undocumented
186 100 my $h = shift;
187 100 my $v = shift;
188 100 ▶ $validate && do {
Branch: 1 branch
BranchTrueFalseTotal
$validate and do { die "Method 'heap' required for element on heap" unless $v->can("heap"); die "Method 'cmp' required for element on heap" unless $v->can("cmp") } 0 100 100
189 0 ▶ die "Method 'heap' required for element on heap"
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("heap") 0 0 0
190 unless $v->can('heap');
191 0 ▶ die "Method 'cmp' required for element on heap"
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("cmp") 0 0 0
192 unless $v->can('cmp');
193 };
194 100 my $i = $v->heap;
195  
196 100 ▶ return $v unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 100 100
197  
198 100 ▶ if( $i == $#$h ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $h->$#*) { } 2 98 100
199 2 pop @$h;
200 } else {
201 98 my $v2 = pop @$h;
202 98 ▶ if( $v2->cmp($v) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v2->cmp($v) < 0) { } 22 76 98
203 22 heapup $h, $i, $v2;
204 } else {
205 76 heapdown $h, $i, $v2;
206 }
207 }
208 100 $v->heap(undef);
209 100 return $v;
210 }
211  
212  
213 ################################################# internal utility functions
214  
215 # moveto($h,$i,$v)
216 # place value $v at index $i in the heap $h, and update it record
217 # of where it is located
218 1274 ▶sub moveto {
Subroutine moveto: called CC=1 SCAR=0.0
Pod undocumented
219 1274 my $h = shift;
220 1274 my $i = shift;
221 1274 my $v = shift;
222  
223 1274 $h->[$i] = $v;
224 1274 $v->heap($i);
225 }
226  
227 # heapup($h,$i,$v)
228 # value $v is to be placed at index $i in heap $h, but it might
229 # be smaller than some of its parents. Keep pushing parents down
230 # until a smaller parent is found or the top of the heap is reached,
231 # and then place $v there.
232 222 ▶sub heapup {
Subroutine heapup: called CC=3 SCAR=11.0
Pod undocumented
233 222 my $h = shift;
234 222 my $i = shift;
235 222 my $v = shift;
236 222 my $pi; # parent index
237  
238 222 ▶ while( $i && $v->cmp($h->[$pi = int( ($i-1)/2 )]) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i and $v->cmp($$h[$pi = int(($i - 1) / 2)]) < 0) 322 222 544
Condition100%3/3
$i and $v->cmp($$h[$pi = int(($i - 1) / 2)]) < 0
!ll&&!rl&&r
4218322
MC/DC100%2/2
$i and $v->cmp($$h[$pi = int(($i - 1) / 2)]) < 0
$i $v->cmp($$h[$pi = int(($i - 1) / 2)]) < 0
Truth table100%3/3
A and B
A= $iB= $v->cmp($$h[$pi = int(($i - 1) / 2)]) < 0
ABresult
0X0
100
111
239 322 moveto $h, $i, $h->[$pi];
240 322 $i = $pi;
241 }
242  
243 222 moveto $h, $i, $v;
244 222 $v;
245 }
246  
247 # heapdown($h,$i,$v)
248 # value $v is to be placed at index $i in heap $h, but it might
249 # have children that are smaller than it is. Keep popping the smallest
250 # child up until a pair of larger children is found or a leaf node is
251 # reached, and then place $v there.
252 176 ▶sub heapdown {
Subroutine heapdown: called CC=5 SCAR=16.1
Pod undocumented
253 176 my $h = shift;
254 176 my $i = shift;
255 176 my $v = shift;
256 176 my $leaf = int(@$h/2);
257  
258 176 ▶ while( $i < $leaf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $leaf) 588 142 730
259 588 my $j = $i*2+1;
260 588 my $k = $j+1;
261  
262 588 ▶ $j = $k if $k < @$h && $h->[$k]->cmp($h->[$j]) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $k < @$h and $h->[$k]->cmp($h->[$j]) < 0 300 288 588
Condition100%3/3
$k < @$h and $h->[$k]->cmp($h->[$j]) < 0
!ll&&!rl&&r
6282300
MC/DC100%2/2
$k < @$h and $h->[$k]->cmp($h->[$j]) < 0
$k < @$h $h->[$k]->cmp($h->[$j]) < 0
Truth table100%3/3
A and B
A= $k < @$hB= $h->[$k]->cmp($h->[$j]) < 0
ABresult
0X0
100
111
263 588 ▶ if( $v->cmp($h->[$j]) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->cmp($h->[$j]) > 0) 554 34 588
264 554 moveto $h, $i, $h->[$j];
265 554 $i = $j;
266 554 next;
267 }
268 34 last;
269 }
270 176 moveto $h, $i, $v;
271 }
272  
273  
274 2 1;
275  
276 __END__
« Heap/Fibonacci.pm ↑ Summary Heap/Binomial.pm »