Heap::...RefRev(3pm)

πŸ’– Heap::...RefRev(3pm) β€” the reversed-reference heap element glow-up πŸ’–

okay bestie, welcome to the User Contributed Perl Documentation section of the manual, this one is Heap::...RefRev and she is serving largest-element-first energy and honestly? obsessed. πŸ¦‹


✨ NAME ✨

Heap::Elem::RefRev β€” Reversed Object Reverence Heap Elements πŸ’… (yes, "Reverence", that's literally the name, don't @ me, that's the vibe the author went with and we respect it πŸ‘‘)


πŸŽ€ SYNOPSIS πŸŽ€

here's the whole thing, copy it exactly bestie, no typos, we are not doing that today:

  use Heap::Elem::RefRev( RefRElem );
  use Heap::Fibonacci;

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

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

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

like. look at her go. one hundred objects, all wrapped up, all heaped up, and then we just keep pulling the Largest one off the top until there's nothing left. iconic behaviour fr fr πŸ’…


🌸 DESCRIPTION 🌸

so Heap::Elem::RefRev is the little wrapper bestie that takes your object references and turns them into elements that a heap can actually manage. it's giving "let me handle the paperwork, you just be an object" 🫢

the way it works: every object you reference must have a method called cmp β€” that's the method that lets it compare itself with any of the other objects that have references sitting on the same heap. and ngl those comparisons have to be consistant with normal arithmetic (that's the spelling from the original, keeping it, we don't fix, we preserve ✨). no chaos, no vibes-based ordering, real arithmetic-consistent ordering only.

and because this is the Rev version, the top of the heap has the largest element still remaining β€” largest as determined by cmp. so every time you extract_top, you get the biggest one. main character selection. periodt. πŸ’…

πŸ’€ not me about to tell you the plot twist: if you actually want the heap to always hand you the smallest element instead, you're in the wrong page bestie β€” go read Heap::Elem::Ref. that's the non-reversed one. this one is flipped, that's the whole point. 🀸

if you want the full tea on the Elem interface itself, that's all described in Heap::Elem.

and if you want to know how to actually use a Heap interface, that lives in Heap.

that's it, that's the whole description, short and slay 🌈


πŸ¦‹ AUTHOR πŸ¦‹

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


Copyright 1998-2007, O'Reilly & Associates.

this code is distributed under the same copyright terms as perl itself. so like, it's perl-flavoured free, iykyk. ✨


⭐ SEE ALSO ⭐

go read them, they're the friend group, they all reference each other, it's very 🫢


🌟 bestie reminder: that cmp method is non-negotiable and its comparisons have to be consistant with normal arithmetic. skip that and your heap is just a chaos pile. no cap. πŸ’€

that's the whole page! this was pulled from perl v5.36.0, dated 2022-10-22, from the Heap::...RefRev(3pm) manual. go forth and heap responsibly, love u, bye πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from