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

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.