Hello, fellow monks.
I have a kinda algorithmic question. Probably my Knuth-fu skills need a refresh or something.
What I've got is a large unordered array of opaque objects (refs, actually, so most operations are cheap). Each of them has a unique precalculated numeric key. Also, I have an ordered array of those keys (this is index). Naturally, I'd like to end up with an array of original objects ordered according to the index.
I do it in this way now:
my $uids = $self->sorted_uids; my $msgs = $self->unsorted_messages; my %uid2msg = map { $_->uid => $_ } @$msgs; return [ map { $uid2msg{$_} } @$uids ];
It seems to me rather awkward and probably slow. I actually didn't do any benchmarks as there's nothing to compare it with. Directly sorting messages is an alternative but that makes other operations extremely... eh.. different. Although I'll probably try it later.
I'd like to add that creating a temp array $uid2msg[$_->uid] = $_ foreach @$msgs is not an option as those uids could be very large (and more important, both large and small in one message set).
Looks like this is the very kind of operation each sql server implementation performs when queried with a SELECT * FROM t ORDER BY column and there's an index on column. I failed to find anything about relevant algorithms on google, though.
In reply to Ordering objects using external index by kappa
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |