Assuming "a few thousand" is less than say low tens of thousands, then using an array, and storing the (same)object in each element of the range would certainly be the fastest method. The limitation being the memory consumption if your array is likely to get very large.

{ my array; sub set{ my( $frag, $start, @meta ) = @_; @array[ $start .. ( $start + length( $freg ) -1 ) ] = ( Fragment->new( $frag, @meta ) ) x length( $frag ); return $array[ $start ]; } sub find{ my( $offset ) = @_; return $array[ $offset ]; } } ... set( 'FOO', 0, ... ); set( 'QUUX', 3, ... ); set( 'BAR', 7, ... ); ... my $found = find( 5 ); print $found->value; # prints 'QUUX';

If that consumes too much space, then you could use a scalar to represent the array (in less space) and an array to store the objects.

{ my $pseudo; my @frags; sub set{ my( $frag, $offset, @meta ) = @_; push @frags, Fragment->new( $frag, @meta ); $pseudo .= pack( 'v', $#frags ) x length( $frag ); return $frags[ -1 ]; } sub find{ my $offset = @_; my $index = unpack( 'v', substr( $pseudo, $offset, 2 ) ); return $frags[ $index ]; } }

With this, you push the objects onto an array once for each fragment, and then store the index at which you pushed it, converted to it's binary representation, once for each position it represents.

This way is also very fast but reduces the memory consumption. A fragment that contains 100 characters requires about 200 + 24 bytes of storage, rather than 100 * 24 bytes. The limitation is that using a 16-bit integer to hold the indexes limits you to 64K fragments. You could move to 32-bits whch would probably cover mosts needs, with the obvious increase in memory usage.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Re: Re: Re: Hash keyed on ranges by BrowserUk
in thread Hash keyed on ranges by ViceRaid

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.