| 1 | # Copyright 1998-2007, O'Reilly & Associates | ||
| 2 | # | ||
| 3 | # This program is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
| 4 | # Lesser Public License as published by the Free Software Foundation, either version 3 of the | ||
| 5 | # License, or (at your option) any later version. | ||
| 6 | # | ||
| 7 | # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | ||
| 8 | # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 9 | # Lesser Public License for more details. | ||
| 10 | # | ||
| 11 | # You should have received a copy of the GNU Lesser Public License along with this program. If not, | ||
| 12 | # see <http://www.gnu.org/licenses/>. | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # Games::Axmud::Obj::Heap, a copy of Heap::Binomial by John Macdonald | ||
| 16 | # (Packaged along with Axmud, and renamed, so that MS Windows users can use it, and so that CPAN | ||
| 17 | # doesn't complain about unauthorised packages) | ||
| 18 | # (Commented out $VERSION as it causes Kwalitee errors) | ||
| 19 | |||
| 20 | package Games::Axmud::Obj::Heap; | ||
| 21 | |||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | #use vars qw($VERSION); | ||
| 25 | |||
| 26 | #$VERSION = '0.80'; | ||
| 27 | |||
| 28 | # common names | ||
| 29 | # h - heap head | ||
| 30 | # el - linkable element, contains user-provided value | ||
| 31 | # v - user-provided value | ||
| 32 | |||
| 33 | ################################################# debugging control | ||
| 34 | |||
| 35 | my $debug = 0; | ||
| 36 | my $validate = 0; | ||
| 37 | |||
| 38 | # enable/disable debugging output | ||
| 39 | sub debug { | ||
| 40 | @_ ? ($debug = shift) : $debug; | ||
| 41 | } | ||
| 42 | |||
| 43 | # enable/disable validation checks on values | ||
| 44 | sub validate { | ||
| 45 | @_ ? ($validate = shift) : $validate; | ||
| 46 | } | ||
| 47 | |||
| 48 | my $width = 3; | ||
| 49 | my $bar = ' | '; | ||
| 50 | my $corner = ' +-'; | ||
| 51 | my $vfmt = "%3d"; | ||
| 52 | |||
| 53 | sub set_width { | ||
| 54 | $width = shift; | ||
| 55 | $width = 2 if $width < 2; | ||
| 56 | |||
| 57 | $vfmt = "%${width}d"; | ||
| 58 | $bar = $corner = ' ' x $width; | ||
| 59 | substr($bar,-2,1) = '|'; | ||
| 60 | substr($corner,-2,2) = '+-'; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub hdump { | ||
| 64 | my $el = shift; | ||
| 65 | my $l1 = shift; | ||
| 66 | my $b = shift; | ||
| 67 | |||
| 68 | my $ch; | ||
| 69 | |||
| 70 | unless( $el ) { | ||
| 71 | print $l1, "\n"; | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | hdump( $ch = $el->{child}, | ||
| 76 | $l1 . sprintf( $vfmt, $el->{val}->val), | ||
| 77 | $b . $bar ); | ||
| 78 | |||
| 79 | while( $ch = $ch->{sib} ) { | ||
| 80 | hdump( $ch, $b . $corner, $b . $bar ); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | sub heapdump { | ||
| 85 | my $h; | ||
| 86 | |||
| 87 | while( $h = shift ) { | ||
| 88 | my $el; | ||
| 89 | |||
| 90 | for( $el = $$h; $el; $el = $el->{sib} ) { | ||
| 91 | hdump( $el, sprintf( "%02d: ", $el->{degree}), ' ' ); | ||
| 92 | } | ||
| 93 | print "\n"; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub bhcheck { | ||
| 98 | |||
| 99 | my $pel = shift; | ||
| 100 | my $pdeg = $pel->{degree}; | ||
| 101 | my $pv = $pel->{val}; | ||
| 102 | my $cel; | ||
| 103 | for( $cel = $pel->{child}; $cel; $cel = $cel->{sib} ) { | ||
| 104 | die "degree not decreasing in heap" | ||
| 105 | unless --$pdeg == $cel->{degree}; | ||
| 106 | die "heap order not preserved" | ||
| 107 | unless $pv->cmp($cel->{val}) <= 0; | ||
| 108 | bhcheck($cel); | ||
| 109 | } | ||
| 110 | die "degree did not decrease to zero" | ||
| 111 | unless $pdeg == 0; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub heapcheck { | ||
| 116 | my $h; | ||
| 117 | while( $h = shift ) { | ||
| 118 | heapdump $h if $validate >= 2; | ||
| 119 | my $el = $$h or next; | ||
| 120 | my $pdeg = -1; | ||
| 121 | for( ; $el; $el = $el->{sib} ) { | ||
| 122 | $el->{degree} > $pdeg | ||
| 123 | or die "degree not increasing in list"; | ||
| 124 | $pdeg = $el->{degree}; | ||
| 125 | bhcheck($el); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | ################################################# forward declarations | ||
| 132 | |||
| 133 | sub elem; | ||
| 134 | sub elem_DESTROY; | ||
| 135 | sub link_to; | ||
| 136 | sub moveto; | ||
| 137 | |||
| 138 | ################################################# heap methods | ||
| 139 | |||
| 140 | |||
| 141 | sub new { | ||
| 142 | my $self = shift; | ||
| 143 | my $class = ref($self) || $self; | ||
| 144 | my $h = undef; | ||
| 145 | bless \$h, $class; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub DESTROY { | ||
| 149 | my $h = shift; | ||
| 150 | |||
| 151 | elem_DESTROY $$h; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub add { | ||
| 155 | my $h = shift; | ||
| 156 | my $v = shift; | ||
| 157 | $validate && do { | ||
| 158 | die "Method 'heap' required for element on heap" | ||
| 159 | unless $v->can('heap'); | ||
| 160 | die "Method 'cmp' required for element on heap" | ||
| 161 | unless $v->can('cmp'); | ||
| 162 | }; | ||
| 163 | $$h = elem $v, $$h; | ||
| 164 | $h->self_union_once; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub top { | ||
| 168 | my $h = shift; | ||
| 169 | my $el = $$h or return undef; | ||
| 170 | my $top = $el->{val}; | ||
| 171 | while( $el = $el->{sib} ) { | ||
| 172 | $top = $el->{val} | ||
| 173 | if $top->cmp($el->{val}) > 0; | ||
| 174 | } | ||
| 175 | $top; | ||
| 176 | } | ||
| 177 | |||
| 178 | *minimum = \⊤ | ||
| 179 | |||
| 180 | sub extract_top { | ||
| 181 | my $h = shift; | ||
| 182 | my $mel = $$h or return undef; | ||
| 183 | my $top = $mel->{val}; | ||
| 184 | my $mpred = $h; | ||
| 185 | my $el = $mel; | ||
| 186 | my $pred = $h; | ||
| 187 | |||
| 188 | # find the heap with the lowest value on it | ||
| 189 | while( $pred = \$el->{sib}, $el = $$pred ) { | ||
| 190 | if( $top->cmp($el->{val}) > 0 ) { | ||
| 191 | $top = $el->{val}; | ||
| 192 | $mel = $el; | ||
| 193 | $mpred = $pred; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | # found it, $mpred points to it, $mel is its container, $val is it | ||
| 198 | # unlink it from the chain | ||
| 199 | $$mpred = $mel->{sib}; | ||
| 200 | |||
| 201 | # we're going to return the value from $mel, but all of its children | ||
| 202 | # must be retained in the heap. Make a second heap with the children | ||
| 203 | # and then merge the heaps. | ||
| 204 | $h->absorb_children($mel); | ||
| 205 | |||
| 206 | # finally break all of its pointers, so that we won't leave any | ||
| 207 | # memory loops when we forget about the pointer to $mel | ||
| 208 | $mel->{p} = $mel->{child} = $mel->{sib} = $mel->{val} = undef; | ||
| 209 | |||
| 210 | # break the back link | ||
| 211 | $top->heap(undef); | ||
| 212 | |||
| 213 | # and return the value | ||
| 214 | $top; | ||
| 215 | } | ||
| 216 | |||
| 217 | *extract_minimum = \&extract_top; | ||
| 218 | |||
| 219 | sub absorb { | ||
| 220 | my $h = shift; | ||
| 221 | my $h2 = shift; | ||
| 222 | |||
| 223 | my $dest_link = $h; | ||
| 224 | my $el1 = $$h; | ||
| 225 | my $el2 = $$h2; | ||
| 226 | my $anymerge = $el1 && $el2; | ||
| 227 | while( $el1 && $el2 ) { | ||
| 228 | if( $el1->{degree} <= $el2->{degree} ) { | ||
| 229 | # advance on h's list, it's already linked | ||
| 230 | $dest_link = \$el1->{sib}; | ||
| 231 | $el1 = $$dest_link; | ||
| 232 | } else { | ||
| 233 | # move next h2 elem to head of h list | ||
| 234 | $$dest_link = $el2; | ||
| 235 | $dest_link = \$el2->{sib}; | ||
| 236 | $el2 = $$dest_link; | ||
| 237 | $$dest_link = $el1; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | # if h ran out first, move rest of h2 onto end | ||
| 242 | if( $el2 ) { | ||
| 243 | $$dest_link = $el2; | ||
| 244 | } | ||
| 245 | |||
| 246 | # clean out h2, all of its elements have been move to h | ||
| 247 | $$h2 = undef; | ||
| 248 | |||
| 249 | # fix up h - it can have multiple items at the same degree if we | ||
| 250 | # actually merged two non-empty lists | ||
| 251 | $anymerge ? $h->self_union: $h; | ||
| 252 | } | ||
| 253 | |||
| 254 | # a key has been decreased, it may have to percolate up in its heap | ||
| 255 | sub decrease_key { | ||
| 256 | my $h = shift; | ||
| 257 | my $v = shift; | ||
| 258 | my $el = $v->heap or return undef; | ||
| 259 | my $p; | ||
| 260 | |||
| 261 | while( $p = $el->{p} ) { | ||
| 262 | last if $v->cmp($p->{val}) >= 0; | ||
| 263 | moveto $el, $p->{val}; | ||
| 264 | $el = $p; | ||
| 265 | } | ||
| 266 | |||
| 267 | moveto $el, $v; | ||
| 268 | |||
| 269 | $v; | ||
| 270 | } | ||
| 271 | |||
| 272 | # to delete an item, we bubble it to the top of its heap (as if its key | ||
| 273 | # had been decreased to -infinity), and then remove it (as in extract_top) | ||
| 274 | sub delete { | ||
| 275 | my $h = shift; | ||
| 276 | my $v = shift; | ||
| 277 | my $el = $v->heap or return undef; | ||
| 278 | |||
| 279 | # bubble it to the top of its heap | ||
| 280 | my $p; | ||
| 281 | while( $p = $el->{p} ) { | ||
| 282 | moveto $el, $p->{val}; | ||
| 283 | $el = $p; | ||
| 284 | } | ||
| 285 | |||
| 286 | # find it on the main list, to remove it and split up the children | ||
| 287 | my $n; | ||
| 288 | for( $p = $h; ($n = $$p) && $n != $el; $p = \$n->{sib} ) { | ||
| 289 | ; | ||
| 290 | } | ||
| 291 | |||
| 292 | # remove it from the main list | ||
| 293 | $$p = $el->{sib}; | ||
| 294 | |||
| 295 | # put any children back onto the main list | ||
| 296 | $h->absorb_children($el); | ||
| 297 | |||
| 298 | # remove the link to $el | ||
| 299 | $v->heap(undef); | ||
| 300 | |||
| 301 | return $v; | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | ################################################# internal utility functions | ||
| 306 | |||
| 307 | sub elem { | ||
| 308 | my $v = shift; | ||
| 309 | my $sib = shift; | ||
| 310 | my $el = { | ||
| 311 | p => undef, | ||
| 312 | degree => 0, | ||
| 313 | child => undef, | ||
| 314 | val => $v, | ||
| 315 | sib => $sib, | ||
| 316 | }; | ||
| 317 | $v->heap($el); | ||
| 318 | $el; | ||
| 319 | } | ||
| 320 | |||
| 321 | sub elem_DESTROY { | ||
| 322 | my $el = shift; | ||
| 323 | my $ch; | ||
| 324 | my $next; | ||
| 325 | |||
| 326 | while( $el ) { | ||
| 327 | $ch = $el->{child} and elem_DESTROY $ch; | ||
| 328 | $next = $el->{sib}; | ||
| 329 | |||
| 330 | $el->{val}->heap(undef); | ||
| 331 | $el->{child} = $el->{sib} = $el->{p} = $el->{val} = undef; | ||
| 332 | $el = $next; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | sub link_to { | ||
| 337 | my $el = shift; | ||
| 338 | my $p = shift; | ||
| 339 | |||
| 340 | $el->{p} = $p; | ||
| 341 | $el->{sib} = $p->{child}; | ||
| 342 | $p->{child} = $el; | ||
| 343 | $p->{degree}++; | ||
| 344 | } | ||
| 345 | |||
| 346 | sub moveto { | ||
| 347 | my $el = shift; | ||
| 348 | my $v = shift; | ||
| 349 | |||
| 350 | $el->{val} = $v; | ||
| 351 | $v->heap($el); | ||
| 352 | } | ||
| 353 | |||
| 354 | # we've merged two lists in degree order. Traverse the list and link | ||
| 355 | # together any pairs (adding 1 + 1 to get 10 in binary) to the next | ||
| 356 | # higher degree. After such a merge, there may be a triple at the | ||
| 357 | # next degree - skip one and merge the others (adding 1 + 1 + carry | ||
| 358 | # of 1 to get 11 in binary). | ||
| 359 | sub self_union { | ||
| 360 | my $h = shift; | ||
| 361 | my $prev = $h; | ||
| 362 | my $cur = $$h; | ||
| 363 | my $next; | ||
| 364 | my $n2; | ||
| 365 | |||
| 366 | while( $next = $cur->{sib} ) { | ||
| 367 | if( $cur->{degree} != $next->{degree} ) { | ||
| 368 | $prev = \$cur->{sib}; | ||
| 369 | $cur = $next; | ||
| 370 | next; | ||
| 371 | } | ||
| 372 | |||
| 373 | # two or three of same degree, need to do a merge. First though, | ||
| 374 | # skip over the leading one of there are three (it is the result | ||
| 375 | # [carry] from the previous merge) | ||
| 376 | if( ($n2 = $next->{sib}) && $n2->{degree} == $cur->{degree} ) { | ||
| 377 | $prev = \$cur->{sib}; | ||
| 378 | $cur = $next; | ||
| 379 | $next = $n2; | ||
| 380 | } | ||
| 381 | |||
| 382 | # and now the merge | ||
| 383 | if( $cur->{val}->cmp($next->{val}) <= 0 ) { | ||
| 384 | $cur->{sib} = $next->{sib}; | ||
| 385 | link_to $next, $cur; | ||
| 386 | } else { | ||
| 387 | $$prev = $next; | ||
| 388 | link_to $cur, $next; | ||
| 389 | $cur = $next; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | $h; | ||
| 393 | } | ||
| 394 | |||
| 395 | # we've added one element at the front, keep merging pairs until there isn't | ||
| 396 | # one of the same degree (change all the low order one bits to zero and the | ||
| 397 | # lowest order zero bit to one) | ||
| 398 | sub self_union_once { | ||
| 399 | my $h = shift; | ||
| 400 | my $cur = $$h; | ||
| 401 | my $next; | ||
| 402 | |||
| 403 | while( $next = $cur->{sib} ) { | ||
| 404 | return if $cur->{degree} != $next->{degree}; | ||
| 405 | |||
| 406 | # merge | ||
| 407 | if( $cur->{val}->cmp($next->{val}) <= 0 ) { | ||
| 408 | $cur->{sib} = $next->{sib}; | ||
| 409 | link_to $next, $cur; | ||
| 410 | } else { | ||
| 411 | $$h = $next; | ||
| 412 | link_to $cur, $next; | ||
| 413 | $cur = $next; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | $h; | ||
| 417 | } | ||
| 418 | |||
| 419 | # absorb all the children of an element into a heap | ||
| 420 | sub absorb_children { | ||
| 421 | my $h = shift; | ||
| 422 | my $el = shift; | ||
| 423 | |||
| 424 | my $h2 = $h->new; | ||
| 425 | my $child = $el->{child}; | ||
| 426 | while( $child ) { | ||
| 427 | my $sib = $child->{sib}; | ||
| 428 | $child->{sib} = $$h2; | ||
| 429 | $child->{p} = undef; | ||
| 430 | $$h2 = $child; | ||
| 431 | $child = $sib; | ||
| 432 | } | ||
| 433 | |||
| 434 | # merge them all in | ||
| 435 | $h->absorb($h2); | ||
| 436 | } | ||
| 437 | |||
| 438 | |||
| 439 | 1; |