Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
It's too bad there's no easy mechanism for creating a hash value pointing to a hash key, instead of another hash value.

Does it buy anything to create a hash with keys of some known hash function, and values as indexes pointing into an array? There's one more lookup, but into an array, which should be much cheaper than another hash lookup.

#!/usr/bin/env perl use strict; use warnings; use Devel::Size qw(size total_size); my $num = 0; my %hashed_keys; my @values = (); for my $word ('aaaa'..'zzzz') { # Store $word -> $num my $hash_word = some_hash($word); my $hash_word_index = scalar @values; $hashed_keys{$hash_word} = $hash_word_index; push @values, $num; # Store $num -> $word my $hash_num = some_hash($num); my $hash_num_index = scalar @values; $hashed_keys{$hash_num} = $hash_num_index; push @values, $word; ++$num; } sub some_hash { return shift; } my $total_size; for my $var (\%hashed_keys, \@values) { my $tsize = total_size($var); printf "%s\n", total_size($var); $total_size += $tsize; } print "total: $total_size\n"; exit;

Without a real hash function, this gives:

43383174 29785044 total: 73168218

...which is only slightly smaller than your original 2-hash result. Mind you, I'm on a 32-bit perl, so that could be the difference.

(Caveats: I've not used a real hash function. I've not accounted for hash collisions.)

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: Bidirectional lookup algorithm? by QM
in thread Bidirectional lookup algorithm? (Updated: further info.) by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found