| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | 0 | my $el; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | for( $el = $$h; $el; $el = $el->{sib} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 84 | die "degree not decreasing in heap" | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | unless --$pdeg == $cel->{degree}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | die "heap order not preserved" | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | unless $pv->cmp($cel->{val}) <= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | bhcheck($cel); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | die "degree did not decrease to zero" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | heapdump $h if $validate >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | my $el = $$h or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | my $pdeg = -1; | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | for( ; $el; $el = $el->{sib} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $el->{degree} > $pdeg | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | die "Method 'heap' required for element on heap" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | unless $v->can('heap'); | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | die "Method 'cmp' required for element on heap" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 150 | 100 | my $top = $el->{val}; | |||||||||||||||||||||||||||
| 151 | 100 | ▶ | while( $el = $el->{sib} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | $top = $el->{val} | ||||||||||||||||||||||||||||
| 153 | 219 | ▶ | if $top->cmp($el->{val}) > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 100 | $top; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 2 | *minimum = \⊤ | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 170 | 219 | ▶ | if( $top->cmp($el->{val}) > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC100%2/2
$el1 && $el2
$el1
$el2
Truth table100%3/3
A && B
A= $el1B= $el2
| |||||||||||||||||||||||||||||
| 207 | 200 | ▶ | while( $el1 && $el2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$el1 and $el2
MC/DC100%2/2
$el1 and $el2
$el1
$el2
Truth table100%3/3
A and B
A= $el1B= $el2
| |||||||||||||||||||||||||||||
| 208 | 763 | ▶ | if( $el1->{degree} <= $el2->{degree} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 239 | 0 | my $p; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 0 | ▶ | while( $p = $el->{p} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 0 | ▶ | last if $v->cmp($p->{val}) >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # bubble it to the top of its heap | ||||||||||||||||||||||||||||
| 260 | 100 | my $p; | |||||||||||||||||||||||||||
| 261 | 100 | ▶ | while( $p = $el->{p} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$n = $$p and $n != $el
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 307 | 0 | ▶ | $ch = $el->{child} and elem_DESTROY $ch; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 347 | 874 | ▶ | if( $cur->{degree} != $next->{degree} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$n2 = $next->{"sib"} and $n2->{"degree"} == $cur->{"degree"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 384 | 389 | ▶ | return if $cur->{degree} != $next->{degree}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | # merge | ||||||||||||||||||||||||||||
| 387 | 197 | ▶ | if( $cur->{val}->cmp($next->{val}) <= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||