Heap::Elem::Ref(3pm)

πŸ’– Heap::Elem::Ref β€” the perl manual page, but make it cute ✨

okay bestie, gather round, because this is Heap::Elem::Ref(3pm) from the User Contributed Perl Documentation and it's genuinely so much simpler than it sounds 🫢


✨ NAME ✨

Heap::Elem::Ref β€” Object Reference Heap Elements πŸ‘‘

like. that's it. that's the name. object references, but they get to live on a heap.


🌸 SYNOPSIS 🌸

use Heap::Elem::Ref( RefElem );
use Heap::Fibonacci;

my $heap = Heap::Fibonacci->new;
my $elem;

foreach $i ( 1..100 ) {
    $obj = myObject->new( $i );
    $elem = RefElem( $obj );
    $heap->add( $elem );
}

while( defined( $elem = $heap->extract_top ) ) {
    # assume that myObject object have a method I<printable>
    print "Smallest is ", $elem->val->printable, "\n";
}

see how we're just yeeting a hundred objects into a Fibonacci heap and popping them out smallest-first? iconic behaviour honestly πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so, Heap::Elem::Ref exists to wrap object reference values into an element that a heap can actually manage for you. that's the whole job. it's giving personal assistant for your objects πŸŽ€

here's the deal, no cap: every referenced object must have a method called cmp that can compare itself against any of the other objects that have references sitting on the same heap. those comparisons have to be consistent with normal arithmetic β€” like, don't be weird about it, the ordering has to actually make sense ✨

the top of the heap will have the smallest element (according to cmp) that's still remaining. so the littlest one comes out first, every time.

and if you're out here wanting the heap to always hand you the largest element instead? go look at Heap::Elem::RefRev, that's literally what it's for πŸ’€

the details of the Elem interface are described in Heap::Elem.

the details of using a Heap interface are described in Heap.


πŸ’œ AUTHOR πŸ’œ

John Macdonald, john@perlwolf.com β€” king behaviour, thank you for your service πŸ‘‘


Copyright 1998-2007, O'Reilly & Associates.

This code is distributed under the same copyright terms as perl itself.


🌈 SEE ALSO 🌈


and that's the whole page bestie, you ate it πŸ“ this was perl v5.36.0, dated 2022-10-22, and the page itself is Heap::Elem::Ref(3pm) β€” now go wrap some objects and sort them, you've got this 🫢✨

πŸ“¦ where this page comes from