Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Bidirectional lookup algorithm?

by QM (Parson)
on Jan 08, 2015 at 13:31 UTC ( [id://1112641]=note: print w/replies, xml ) Need Help??


in reply to Bidirectional lookup algorithm? (Updated: further info.)

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

Replies are listed 'Best First'.
Re^2: Bidirectional lookup algorithm?
by BrowserUk (Patriarch) on Jan 08, 2015 at 15:26 UTC

    Essentially, that is what I am doing.

    One array of structs contains the paired data. This is ordered by hashing one of the values.

    A parallel array of ints contains indexes into the first array and are ordered by hashing the second value,

    Once I've fixed a couple of edge cases I know about, tested it some more, and probably moved it from linear to hopscotch probing, I'll post the code in this thread.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1112641]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-28 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found