Heap(3pm)
π Heap(3pm) β like, the whole vibe π
okay bestie, so this is a page from the User Contributed Perl Documentation and it's literally called Heap, and no cap it's about Perl extensions for keeping data partially sorted. pull up a chair, we're doing this π«Ά
β¨ NAME β¨
Heap β Perl extensions for keeping data partially sorted. that's it, that's the whole thing, and yet. so much. π
πΈ SYNOPSIS πΈ
use Heap;
my $heap = Heap->new;
my $elem;
use Heap::Elem::Num(NumElem);
foreach $i ( 1..100 ) {
$elem = NumElem( $i );
$heap->add( $elem );
}
while( defined( $elem = $heap->extract_top ) ) {
print "Smallest is ", $elem->val, "\n";
}
see how clean that is?? iconic. π
π¦ DESCRIPTION π¦
so the Heap collection of modules hands you routines that manage a heap of elements. and what IS a heap, you ask? it's a partially sorted structure that can always easily pull out the smallest of the elements chillin' inside it β or the largest, if you're giving it a reversed compare routine instead. your call, babe. β¨
the way that this matters: if your collection of elements is changing dynamically, the heap has less overhead than keeping the whole collection fully sorted. lowkey that's the whole point of it. π₯
now, the elements must be objects as described in "Heap::Elem", and every element you insert into one heap has to be mutually compatible with the others β either the exact same class, or classes that differ only in ways that are totally unrelated to the Heap::Elem interface. no messy mixes, bestie, it's giving compatibility check. π
π METHODS π
$heap = HeapClass::new(); $heap2 = $heap1->new();β gives you back a brand new heap object of whichever (sub-)class you asked for. and yes, this one is often used as a plain old subroutine instead of a method. of course it is. π$heap->DESTROYβ makes sure no internal circular data references are left lurking around. some variants of Heap just ignore this (they have no such references, so like, nothing to clean). Heap users normally don't need to worry about it at all βDESTROYgets invoked automatically the moment the heap reference goes out of scope. self-care, honestly. π$heap->add($elem)β adds an element to the heap. boom. done. simple as. β¨$elem = $heap->topβ returns the top element on the heap. it is not removed from the heap β it stays right there at the top. and it will be the smallest element on the heap (unless a reversedcmpfunction is in play, in which case it'll be the largest instead). returnsundefif the heap is empty. π
okay so history time: this method used to be called"minimum"instead of"top". the old name is still supported but is deprecated. why did it change? because it was straight-up confusing to use a method called"minimum"to get the maximum value on the heap when a reversedcmpfunction was doing the ordering. like... make it make sense. π
$elem = $heap->extract_topβ deletes the top element from the heap and returns it. returnsundefif the heap was empty. β¨
and same energy here: this method used to be called"extract_minimum"instead of"extract_top". the old name is still supported but is deprecated. again β confusing to call something"extract_minimum"when a reversedcmpfunction means you're pulling the maximum off the top. periodt. π
$heap1->absorb($heap2)β merges all of the elements from$heap2into$heap1. and this will leave$heap2empty. it ate. π$heap1->decrease_key($elem)β the element gets moved closer to the top of the heap if it's now smaller than any higher parent elements. heads up bestie: you have to have already changed the value of$elembefore you calldecrease_key. and only a decrease is permitted. β οΈ
and like, that's a decrease according to thecmpfunction β so if it's a reversed order comparison, then you're only permitted to increase the value of the element instead. to be pedantic (and we ARE being pedantic, this is a manpage), you may only usedecrease_keyif$elem-cmp($elem_original) <= 0>, where$elem_originalwas an elem with the value that$elemhad before it was decreased. yes that's the actual text. no I didn't make it up. π
$elem = $heap->delete($elem)β the element is removed from the heap, whether it's sitting at the top or not. no favourites here. π¦
π AUTHOR π
John Macdonald, john@perlwolf.com β king behaviour. π
π COPYRIGHT π
Copyright 1998-2007, O'Reilly & Associates.
This code is distributed under the same copyright terms as perl itself.
β SEE ALSO β
okay that's the whole page bestie, we did it π«Ά and just so you know, this was all from perl v5.36.0, dated 2022-10-22, filed under Heap(3pm). slay, go build a heap, ily πβ¨
π¦ where this page comes from
- ubuntu noble: libheap-perl 0.80-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libheap-perl 0.80-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libheap-perl 0.80-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms