in reply to Re: Hash keyed on ranges
in thread Hash keyed on ranges

How would the extent each of the ranges be established?

They come from the source fragments of the composite string. Say the composite string is made up of three fragments "FOO" "QUUX" and "BAR". Each of these fragments is a string-based object, with extra info attached to it that I need to access. My ranged hash (or maybe array - thanks dragonchild) would then look like:

# ... stands for the fragment's metadata { 0 => Fragment->new('FOO', ...), 3 => Fragment->new('QUUX', ...), 7 => Fragment->new('BAR', ...) } # and the composite string looks like "FOOQUUXBAR";

I want to be able to get back to the source fragment object (and its associated data) quickly from a given offset in the composite string.

Do the ranges overlap?

Nope

Are the ranges contiguous?

Yup

Can you reasonably guestimate the minimum and maximium integer values?

Not easily, no - the fragments might be quite short or several thousand characters long, and the total number of fragments is also very variable.

Thanks/

Replies are listed 'Best First'.
Re: Re: Re: Hash keyed on ranges
by BrowserUk (Patriarch) on May 07, 2004 at 14:06 UTC

    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