kappa has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Ordering objects using external index
by fergal (Chaplain) on Sep 06, 2004 at 19:04 UTC | |
by kappa (Chaplain) on Sep 06, 2004 at 21:41 UTC | |
by fergal (Chaplain) on Sep 07, 2004 at 15:30 UTC | |
|
Re: Ordering objects using external index
by saintmike (Vicar) on Sep 06, 2004 at 17:45 UTC | |
by kappa (Chaplain) on Sep 07, 2004 at 11:34 UTC | |
|
Re: Ordering objects using external index
by Anonymous Monk on Sep 06, 2004 at 18:29 UTC | |
|
Re: Ordering objects using external index
by BrowserUk (Patriarch) on Sep 06, 2004 at 19:23 UTC | |
by kappa (Chaplain) on Sep 06, 2004 at 21:12 UTC | |
by BrowserUk (Patriarch) on Sep 06, 2004 at 21:36 UTC | |
by kappa (Chaplain) on Sep 07, 2004 at 11:14 UTC |