I appreciate your having stuck with me through this. Please understand that this is a gut reaction after first reading of your post (and having previously looked at a CPAN Heap implementation), rather than a fully thought through one. I will pursue this further.

Using a weighted heap, moving an item to the top of the heap requires either:

  1. Giving that item a weight higher than every other item in the heap.

    Which could be done by:

    1. Giving the item being promoted the highest possible weight. No good if you need to do it twice.
    2. Iterating the heap to find the highest weight and adding one. One recursive operation that will then trigger another. (In Perl not C)
    3. Remembering the highest value yet allocated and incrementing it. (Possible, but it still triggers a recursive operation at the perl level.)
  2. Or: Adjusting the weights of every item between the item being promoted to the top, and the current top item.
  3. Then there is the question of how you raise an items weight by 1 or 2 places?
  4. And the complexity (and performance) of recursive reference chasing in Perl code.

Contrast this with the simplicity of the splice operation.

Then contrast it with the efficiency of doing the linear operation in C, to recursively dereferencing linked lists implemented using perl hashes or arrays.

O(N) -v- O(log N) doesn't tell the complete story when the the former is performed in C code and the latter in Perl code. Especially, heavily structured OO Perl code.

Then take a look at the complexity (and the weight) of something like Heap::Priority.

The order does matter, because it is my cheap mechanism for retaining and propagating the weightings of the elements. One operation takes care of adjusting all the weights of all the affected items in the stack, because their weights are their positions.

For now, my "string list" mechanism looks like it will be easy to implement:

<pre> $order = "ABCDE"; %valByKey { A => \A_value } { B => \B_value } { C => \C_value } { D => \D_value } { E => \E_value } </pre>

Accessing C via key calls for C to become top item getByKey( 'C' ); does:

sub getByKey { my $key = shift; ## to top. $order =~ s[(^.*)($key)][$2$1]; ## Needs quotemetaing and adjusting for 3-char keys ## may also require C<no warnings 'undefined'> ## to prevent that when the item being moved is at ## the beginning or the end of the string. ## to bottom ## $order =~ s[($key)(.*$)][$2$1]; ## up one ## $order =~ s[(.?)($key)][$2$1]; ## down one ## $order =~ s[($key)(.?)][$2$1]; ## up two ## $order =~ s[(..)?($key)][$2$1]; ## down two ## $order =~ s[($Key)(..)?][$1$2]; return $valByKey{ $key };; }

Afterwards:

$order = "CABDE"; %valByKey { A => \A_value } { B => \B_value } { C => \C_value } { D => \D_value } { E => \E_value }

This satisfies all my criteria, it's lightweight, pretty damn fast and easy to program.

It's only limitation (that I can see?) is the fixed length keys, but generalising that can be an exercise for another time.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re^4: Re-orderable keyed access structure? by BrowserUk
in thread Re-orderable keyed access structure? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.