in reply to Re^3: Re-orderable keyed access structure?
in thread Re-orderable keyed access structure?
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:
Which could be done by:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Re-orderable keyed access structure?
by Aristotle (Chancellor) on Aug 15, 2004 at 03:54 UTC | |
by BrowserUk (Patriarch) on Aug 15, 2004 at 04:24 UTC | |
by Aristotle (Chancellor) on Aug 15, 2004 at 04:46 UTC | |
by tye (Sage) on Aug 15, 2004 at 05:08 UTC | |
by Aristotle (Chancellor) on Aug 15, 2004 at 06:34 UTC | |
| |
by BrowserUk (Patriarch) on Aug 15, 2004 at 13:35 UTC | |
by Aristotle (Chancellor) on Aug 15, 2004 at 19:05 UTC | |
|