Heap/Binomial.pm

filter: Statementstmt 70.5% 141 / 200 Branchbran 47.7% 43 / 90 Conditioncond 86.6% 13 / 15 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 66.6% 16 / 24 Podpod 0.0% 0 / 21 totaltotal 61.1% 220 / 360 CC 51 SCAR 23.9
CC
51
Cov
65%
heapcheck
42.0
bhcheck
30.0
decrease_key
20.0
CRAP 10.9
« Heap/Binary.pm ↑ Summary Heap/Elem.pm »
1 package Heap::Binomial;
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 # el - linkable element, contains user-provided value
11 # v - user-provided value
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 0 ▶sub hdump {
Subroutine hdump: not called CC=3 SCAR=24.8
Pod undocumented
44 0 my $el = shift;
45 0 my $l1 = shift;
46 0 my $b = shift;
47  
48 0 my $ch;
49  
50 0 ▶ unless( $el ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($el) 0 0 0
51 0 print $l1, "\n";
52 0 return;
53 }
54  
55 hdump( $ch = $el->{child},
56 0 $l1 . sprintf( $vfmt, $el->{val}->val),
57 $b . $bar );
58  
59 0 ▶ while( $ch = $ch->{sib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch = $ch->{"sib"}) 0 0 0
60 0 hdump( $ch, $b . $corner, $b . $bar );
61 }
62 }
63  
64 0 ▶sub heapdump {
Subroutine heapdump: not called CC=3 SCAR=24.8
Pod undocumented
65 0 my $h;
66  
67 0 ▶ while( $h = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h = shift()) 0 0 0
68 0 my $el;
69  
70 0 ▶ for( $el = $$h; $el; $el = $el->{sib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el) 0 0 0
71 0 hdump( $el, sprintf( "%02d: ", $el->{degree}), ' ' );
72 }
73 0 print "\n";
74 }
75 }
76  
77 0 ▶sub bhcheck {
Subroutine bhcheck: not called CC=5 SCAR=34.0
Pod undocumented
78  
79 0 my $pel = shift;
80 0 my $pdeg = $pel->{degree};
81 0 my $pv = $pel->{val};
82 0 my $cel;
83 0 ▶ for( $cel = $pel->{child}; $cel; $cel = $cel->{sib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cel) 0 0 0
84 die "degree not decreasing in heap"
85 0 ▶ unless --$pdeg == $cel->{degree};
Branch: 1 branch
BranchTrueFalseTotal
unless --$pdeg == $cel->{"degree"} 0 0 0
86 die "heap order not preserved"
87 0 ▶ unless $pv->cmp($cel->{val}) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $pv->cmp($cel->{"val"}) <= 0 0 0 0
88 0 bhcheck($cel);
89 }
90 0 ▶ die "degree did not decrease to zero"
Branch: 1 branch
BranchTrueFalseTotal
unless $pdeg == 0 0 0 0
91 unless $pdeg == 0;
92 }
93  
94  
95 0 ▶sub heapcheck {
Subroutine heapcheck: not called CC=6 SCAR=37.4
Pod undocumented
96 0 my $h;
97 0 ▶ while( $h = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h = shift()) 0 0 0
98 0 ▶ heapdump $h if $validate >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $validate >= 2 0 0 0
99 0 ▶ my $el = $$h or next;
Branch: 1 branch
BranchTrueFalseTotal
my $el = $$h or next 0 0 0
100 0 my $pdeg = -1;
101 0 ▶ for( ; $el; $el = $el->{sib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el) 0 0 0
102 0 ▶ $el->{degree} > $pdeg
Branch: 1 branch
BranchTrueFalseTotal
$el->{"degree"} > $pdeg or die "degree not increasing in list" 0 0 0
103 or die "degree not increasing in list";
104 0 $pdeg = $el->{degree};
105 0 bhcheck($el);
106 }
107 }
108 }
109  
110  
111 ################################################# forward declarations
112  
113 sub elem;
114 sub elem_DESTROY;
115 sub link_to;
116 sub moveto;
117  
118 ################################################# heap methods
119  
120  
121 202 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
122 202 my $self = shift;
123 202 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
20020
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
124 202 my $h = undef;
125 202 bless \$h, $class;
126 }
127  
128 202 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
129 202 my $h = shift;
130  
131 202 elem_DESTROY $$h;
132 }
133  
134 200 ▶sub add {
Subroutine add: called CC=4 SCAR=18.7
Pod undocumented
135 200 my $h = shift;
136 200 my $v = shift;
137 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
138 0 ▶ die "Method 'heap' required for element on heap"
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("heap") 0 0 0
139 unless $v->can('heap');
140 0 ▶ die "Method 'cmp' required for element on heap"
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("cmp") 0 0 0
141 unless $v->can('cmp');
142 };
143 200 $$h = elem $v, $$h;
144 200 $h->self_union_once;
145 }
146  
147 102 ▶sub top {
Subroutine top: called CC=4 SCAR=13.9
Pod undocumented
148 102 my $h = shift;
149 102 ▶ my $el = $$h or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $el = $$h or return undef 100 2 102
150 100 my $top = $el->{val};
151 100 ▶ while( $el = $el->{sib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el = $el->{"sib"}) 219 100 319
152 $top = $el->{val}
153 219 ▶ if $top->cmp($el->{val}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $top->cmp($el->{"val"}) > 0 51 168 219
154 }
155 100 $top;
156 }
157  
158 2 *minimum = \&top;
159  
160 102 ▶sub extract_top {
Subroutine extract_top: called CC=4 SCAR=13.9
Pod undocumented
161 102 my $h = shift;
162 102 ▶ my $mel = $$h or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $mel = $$h or return undef 100 2 102
163 100 my $top = $mel->{val};
164 100 my $mpred = $h;
165 100 my $el = $mel;
166 100 my $pred = $h;
167  
168 # find the heap with the lowest value on it
169 100 ▶ while( $pred = \$el->{sib}, $el = $$pred ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pred = \$el->{"sib"}, $el = $$pred) 219 100 319
170 219 ▶ if( $top->cmp($el->{val}) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top->cmp($el->{"val"}) > 0) 51 168 219
171 51 $top = $el->{val};
172 51 $mel = $el;
173 51 $mpred = $pred;
174 }
175 }
176  
177 # found it, $mpred points to it, $mel is its container, $val is it
178 # unlink it from the chain
179 100 $$mpred = $mel->{sib};
180  
181 # we're going to return the value from $mel, but all of its children
182 # must be retained in the heap. Make a second heap with the children
183 # and then merge the heaps.
184 100 $h->absorb_children($mel);
185  
186 # finally break all of its pointers, so that we won't leave any
187 # memory loops when we forget about the pointer to $mel
188 100 $mel->{p} = $mel->{child} = $mel->{sib} = $mel->{val} = undef;
189  
190 # break the back link
191 100 $top->heap(undef);
192  
193 # and return the value
194 100 $top;
195 }
196  
197 2 *extract_minimum = \&extract_top;
198  
199 200 ▶sub absorb {
Subroutine absorb: called CC=7 SCAR=19.5
Pod undocumented
200 200 my $h = shift;
201 200 my $h2 = shift;
202  
203 200 my $dest_link = $h;
204 200 my $el1 = $$h;
205 200 my $el2 = $$h2;
206 200 ▶ my $anymerge = $el1 && $el2;
Condition100%3/3
$el1 && $el2
!ll&&!rl&&r
87185
MC/DC100%2/2
$el1 && $el2
$el1 $el2
Truth table100%3/3
A && B
A= $el1B= $el2
ABresult
0X0
100
111
207 200 ▶ while( $el1 && $el2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el1 and $el2) 763 200 963
Condition100%3/3
$el1 and $el2
!ll&&!rl&&r
59141763
MC/DC100%2/2
$el1 and $el2
$el1 $el2
Truth table100%3/3
A and B
A= $el1B= $el2
ABresult
0X0
100
111
208 763 ▶ if( $el1->{degree} <= $el2->{degree} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el1->{'degree'} <= $el2->{'degree'}) { } 249 514 763
209 # advance on h's list, it's already linked
210 249 $dest_link = \$el1->{sib};
211 249 $el1 = $$dest_link;
212 } else {
213 # move next h2 elem to head of h list
214 514 $$dest_link = $el2;
215 514 $dest_link = \$el2->{sib};
216 514 $el2 = $$dest_link;
217 514 $$dest_link = $el1;
218 }
219 }
220  
221 # if h ran out first, move rest of h2 onto end
222 200 ▶ if( $el2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el2) 58 142 200
223 58 $$dest_link = $el2;
224 }
225  
226 # clean out h2, all of its elements have been move to h
227 200 $$h2 = undef;
228  
229 # fix up h - it can have multiple items at the same degree if we
230 # actually merged two non-empty lists
231 200 ▶ $anymerge ? $h->self_union: $h;
Branch: 1 branch
BranchTrueFalseTotal
$anymerge ? : 185 15 200
232 }
233  
234 # a key has been decreased, it may have to percolate up in its heap
235 0 ▶sub decrease_key {
Subroutine decrease_key: not called CC=4 SCAR=30.0
Pod undocumented
236 0 my $h = shift;
237 0 my $v = shift;
238 0 ▶ my $el = $v->heap or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $el = $v->heap or return undef 0 0 0
239 0 my $p;
240  
241 0 ▶ while( $p = $el->{p} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p = $el->{"p"}) 0 0 0
242 0 ▶ last if $v->cmp($p->{val}) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v->cmp($p->{"val"}) >= 0 0 0 0
243 0 moveto $el, $p->{val};
244 0 $el = $p;
245 }
246  
247 0 moveto $el, $v;
248  
249 0 $v;
250 }
251  
252 # to delete an item, we bubble it to the top of its heap (as if its key
253 # had been decreased to -infinity), and then remove it (as in extract_top)
254 100 ▶sub delete {
Subroutine delete: called CC=5 SCAR=16.1
Pod undocumented
255 100 my $h = shift;
256 100 my $v = shift;
257 100 ▶ my $el = $v->heap or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $el = $v->heap or return undef 100 0 100
258  
259 # bubble it to the top of its heap
260 100 my $p;
261 100 ▶ while( $p = $el->{p} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p = $el->{"p"}) 113 100 213
262 113 moveto $el, $p->{val};
263 113 $el = $p;
264 }
265  
266 # find it on the main list, to remove it and split up the children
267 100 my $n;
268 100 ▶ for( $p = $h; ($n = $$p) && $n != $el; $p = \$n->{sib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n = $$p and $n != $el) 181 100 281
Condition66%2/3
$n = $$p and $n != $el
!ll&&!rl&&r
0100181
MC/DC50%1/2
$n = $$p and $n != $el
$n = $$p $n != $el
Truth table66%2/3
A and B
A= $n = $$pB= $n != $el
ABresult
0X0
100
111
269 ;
270 }
271  
272 # remove it from the main list
273 100 $$p = $el->{sib};
274  
275 # put any children back onto the main list
276 100 $h->absorb_children($el);
277  
278 # remove the link to $el
279 100 $v->heap(undef);
280  
281 100 return $v;
282 }
283  
284  
285 ################################################# internal utility functions
286  
287 200 ▶sub elem {
Subroutine elem: called CC=1 SCAR=0.0
Pod undocumented
288 200 my $v = shift;
289 200 my $sib = shift;
290 200 my $el = {
291 p => undef,
292 degree => 0,
293 child => undef,
294 val => $v,
295 sib => $sib,
296 };
297 200 $v->heap($el);
298 200 $el;
299 }
300  
301 202 ▶sub elem_DESTROY {
Subroutine elem_DESTROY: called CC=3 SCAR=17.3
Pod undocumented
302 202 my $el = shift;
303 202 my $ch;
304 my $next;
305  
306 202 ▶ while( $el ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el) 0 202 202
307 0 ▶ $ch = $el->{child} and elem_DESTROY $ch;
Branch: 1 branch
BranchTrueFalseTotal
$ch = $el->{"child"} and elem_DESTROY($ch) 0 0 0
308 0 $next = $el->{sib};
309  
310 0 $el->{val}->heap(undef);
311 0 $el->{child} = $el->{sib} = $el->{p} = $el->{val} = undef;
312 0 $el = $next;
313 }
314 }
315  
316 679 ▶sub link_to {
Subroutine link_to: called CC=1 SCAR=0.0
Pod undocumented
317 679 my $el = shift;
318 679 my $p = shift;
319  
320 679 $el->{p} = $p;
321 679 $el->{sib} = $p->{child};
322 679 $p->{child} = $el;
323 679 $p->{degree}++;
324 }
325  
326 113 ▶sub moveto {
Subroutine moveto: called CC=1 SCAR=0.0
Pod undocumented
327 113 my $el = shift;
328 113 my $v = shift;
329  
330 113 $el->{val} = $v;
331 113 $v->heap($el);
332 }
333  
334 # we've merged two lists in degree order. Traverse the list and link
335 # together any pairs (adding 1 + 1 to get 10 in binary) to the next
336 # higher degree. After such a merge, there may be a triple at the
337 # next degree - skip one and merge the others (adding 1 + 1 + carry
338 # of 1 to get 11 in binary).
339 185 ▶sub self_union {
Subroutine self_union: called CC=6 SCAR=17.9
Pod undocumented
340 185 my $h = shift;
341 185 my $prev = $h;
342 185 my $cur = $$h;
343 185 my $next;
344 my $n2;
345  
346 185 ▶ while( $next = $cur->{sib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next = $cur->{"sib"}) 874 185 1059
347 874 ▶ if( $cur->{degree} != $next->{degree} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur->{"degree"} != $next->{"degree"}) 392 482 874
348 392 $prev = \$cur->{sib};
349 392 $cur = $next;
350 392 next;
351 }
352  
353 # two or three of same degree, need to do a merge. First though,
354 # skip over the leading one of there are three (it is the result
355 # [carry] from the previous merge)
356 482 ▶ if( ($n2 = $next->{sib}) && $n2->{degree} == $cur->{degree} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n2 = $next->{"sib"} and $n2->{"degree"} == $cur->{"degree"}) 112 370 482
Condition100%3/3
$n2 = $next->{"sib"} and $n2->{"degree"} == $cur->{"degree"}
!ll&&!rl&&r
42328112
MC/DC100%2/2
$n2 = $next->{"sib"} and $n2->{"degree"} == $cur->{"degree"}
$n2 = $next->{"sib"} $n2->{"degree"} == $cur->{"degree"}
Truth table100%3/3
A and B
A= $n2 = $next->{"sib"}B= $n2->{"degree"} == $cur->{"degree"}
ABresult
0X0
100
111
357 112 $prev = \$cur->{sib};
358 112 $cur = $next;
359 112 $next = $n2;
360 }
361  
362 # and now the merge
363 482 ▶ if( $cur->{val}->cmp($next->{val}) <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur->{'val'}->cmp($next->{'val'}) <= 0) { } 352 130 482
364 352 $cur->{sib} = $next->{sib};
365 352 link_to $next, $cur;
366 } else {
367 130 $$prev = $next;
368 130 link_to $cur, $next;
369 130 $cur = $next;
370 }
371 }
372 185 $h;
373 }
374  
375 # we've added one element at the front, keep merging pairs until there isn't
376 # one of the same degree (change all the low order one bits to zero and the
377 # lowest order zero bit to one)
378 200 ▶sub self_union_once {
Subroutine self_union_once: called CC=4 SCAR=13.9
Pod undocumented
379 200 my $h = shift;
380 200 my $cur = $$h;
381 200 my $next;
382  
383 200 ▶ while( $next = $cur->{sib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next = $cur->{"sib"}) 389 8 397
384 389 ▶ return if $cur->{degree} != $next->{degree};
Branch: 1 branch
BranchTrueFalseTotal
if $cur->{"degree"} != $next->{"degree"} 192 197 389
385  
386 # merge
387 197 ▶ if( $cur->{val}->cmp($next->{val}) <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur->{'val'}->cmp($next->{'val'}) <= 0) { } 9 188 197
388 9 $cur->{sib} = $next->{sib};
389 9 link_to $next, $cur;
390 } else {
391 188 $$h = $next;
392 188 link_to $cur, $next;
393 188 $cur = $next;
394 }
395 }
396 8 $h;
397 }
398  
399 # absorb all the children of an element into a heap
400 200 ▶sub absorb_children {
Subroutine absorb_children: called CC=2 SCAR=6.9
Pod undocumented
401 200 my $h = shift;
402 200 my $el = shift;
403  
404 200 my $h2 = $h->new;
405 200 my $child = $el->{child};
406 200 ▶ while( $child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 679 200 879
407 679 my $sib = $child->{sib};
408 679 $child->{sib} = $$h2;
409 679 $child->{p} = undef;
410 679 $$h2 = $child;
411 679 $child = $sib;
412 }
413  
414 # merge them all in
415 200 $h->absorb($h2);
416 }
417  
418  
419 2 1;
420  
421 __END__
« Heap/Binary.pm ↑ Summary Heap/Elem.pm »