/usr/share/perl5/Heap/Binary.pm is in libheap-perl 0.80-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | package Heap::Binary;
use strict;
use vars qw($VERSION);
$VERSION = '0.80';
# common names:
# h - heap head
# i - index of a heap value element
# v - user-provided value (to be) stored on the heap
################################################# debugging control
my $debug = 0;
my $validate = 0;
# enable/disable debugging output
sub debug {
@_ ? ($debug = shift) : $debug;
}
# enable/disable validation checks on values
sub validate {
@_ ? ($validate = shift) : $validate;
}
my $width = 3;
my $bar = ' | ';
my $corner = ' +-';
my $vfmt = "%3d";
sub set_width {
$width = shift;
$width = 2 if $width < 2;
$vfmt = "%${width}d";
$bar = $corner = ' ' x $width;
substr($bar,-2,1) = '|';
substr($corner,-2,2) = '+-';
}
sub hdump {
my $h = shift;
my $i = shift;
my $p = shift;
my $ch = $i*2+1;
return if $i >= @$h;
my $space = ' ' x $width;
printf( "%${width}d", $h->[$i]->val );
if( $ch+1 < @$h ) {
hdump( $h, $ch, $p . $bar);
print( $p, $corner );
++$ch;
}
if( $ch < @$h ) {
hdump( $h, $ch, $p . $space );
} else {
print "\n";
}
}
sub heapdump {
my $h;
while( $h = shift ) {
hdump $h, 0, '';
print "\n";
}
}
sub heapcheck {
my $h;
while( $h = shift ) {
my $i;
my $p;
next unless @$h;
for( $p = 0, $i = 1; $i < @$h; ++$p, ++$i ) {
$h->[$p]->cmp($h->[$i]) <= 0 or die "not in heap order";
last unless ++$i < @$h;
$h->[$p]->cmp($h->[$i]) <= 0 or die "not in heap order";
}
heapdump $h if $validate >= 2;
}
}
################################################# forward declarations
sub moveto;
sub heapup;
sub heapdown;
################################################# heap methods
# new() usually Heap::Binary->new()
# return a new empty heap
sub new {
my $self = shift;
my $class = ref($self) || $self;
return bless [], $class;
}
# add($h,$v) usually $h->add($v)
# insert value $v into the heap
sub add {
my $h = shift;
my $v = shift;
$validate && 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');
};
heapup $h, scalar(@$h), $v;
}
# top($h) usually $h->top
# the smallest value is returned, but it is still left on the heap
sub top {
my $h = shift;
$h->[0];
}
*minimum = \⊤
# extract_top($h) usually $h->extract_top
# the smallest value is returned after removing it from the heap
sub extract_top {
my $h = shift;
my $top = $h->[0];
if( @$h ) {
# there was at least one item, must decrease the heap
$top->heap(undef);
my $last = pop(@$h);
if( @$h ) {
# $top was not the only thing left, so re-heap the
# remainder by over-writing position zero (where
# $top was) using the value popped from the end
heapdown $h, 0, $last;
}
}
$top;
}
*extract_minimum = \&extract_top;
# absorb($h,$h2) usually $h->absorb($h2)
# all of the values in $h2 are inserted into $h instead, $h2 is left
# empty.
sub absorb {
my $h = shift;
my $h2 = shift;
my $v;
foreach $v (splice @$h2, 0) {
$h->add($v);
}
$h;
}
# decrease_key($h,$v) usually $h->decrease_key($v)
# the key value of $v has just been decreased and so it may need to
# be percolated to a higher position in the heap
sub decrease_key {
my $h = shift;
my $v = shift;
$validate && 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');
};
my $i = $v->heap;
heapup $h, $i, $v;
}
# delete($h,$v) usually: $h->delete($v)
# delete value $v from heap $h. It must have previously been
# add'ed to $h.
sub delete {
my $h = shift;
my $v = shift;
$validate && 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');
};
my $i = $v->heap;
return $v unless defined $i;
if( $i == $#$h ) {
pop @$h;
} else {
my $v2 = pop @$h;
if( $v2->cmp($v) < 0 ) {
heapup $h, $i, $v2;
} else {
heapdown $h, $i, $v2;
}
}
$v->heap(undef);
return $v;
}
################################################# internal utility functions
# moveto($h,$i,$v)
# place value $v at index $i in the heap $h, and update it record
# of where it is located
sub moveto {
my $h = shift;
my $i = shift;
my $v = shift;
$h->[$i] = $v;
$v->heap($i);
}
# heapup($h,$i,$v)
# value $v is to be placed at index $i in heap $h, but it might
# be smaller than some of its parents. Keep pushing parents down
# until a smaller parent is found or the top of the heap is reached,
# and then place $v there.
sub heapup {
my $h = shift;
my $i = shift;
my $v = shift;
my $pi; # parent index
while( $i && $v->cmp($h->[$pi = int( ($i-1)/2 )]) < 0 ) {
moveto $h, $i, $h->[$pi];
$i = $pi;
}
moveto $h, $i, $v;
$v;
}
# heapdown($h,$i,$v)
# value $v is to be placed at index $i in heap $h, but it might
# have children that are smaller than it is. Keep popping the smallest
# child up until a pair of larger children is found or a leaf node is
# reached, and then place $v there.
sub heapdown {
my $h = shift;
my $i = shift;
my $v = shift;
my $leaf = int(@$h/2);
while( $i < $leaf ) {
my $j = $i*2+1;
my $k = $j+1;
$j = $k if $k < @$h && $h->[$k]->cmp($h->[$j]) < 0;
if( $v->cmp($h->[$j]) > 0 ) {
moveto $h, $i, $h->[$j];
$i = $j;
next;
}
last;
}
moveto $h, $i, $v;
}
1;
__END__
=head1 NAME
Heap::Binary - a binary heap to keep data partially sorted
=head1 SYNOPSIS
use Heap::Binary;
$heap = Heap::Binary->new;
# see Heap(3) for usage
=head1 DESCRIPTION
Keeps an array of elements in heap order. The I<heap> method
of an element is used to store the index into the array that
refers to the element.
See L<Heap> for details on using this module.
=head1 AUTHOR
John Macdonald, john@perlwolf.com
=head1 COPYRIGHT
Copyright 1998-2007, O'Reilly & Associates.
This code is distributed under the same copyright terms as perl itself.
=head1 SEE ALSO
Heap(3), Heap::Elem(3).
=cut
|