I'm not sure I've captured all of the comments here, but here's my suggestion: Use the value at the hash collision to get the offset for the next hash key. If the hash value is 0 mod $PRIME, use half the prime as an offset, to avoid clustering. In Perl, if you will:
my %hash; # hash of stored values
my $PRIME = some_large_prime(); # random or fixed, your choice
my $PRIME_HALF = int($PRIME/2); # or some other known value or functio
+n
...
my $i = $initial_index; # wherever you get this from
my $failed_tries = $PRIME;
while (defined($hash{$i}) and $failed_tries--) {
my $offset = $hash{$i} % $PRIME;
$offset = $PRIME_HALF if ($offset == 0);
$i = ($i + ($hash{$i}) % $PRIME;
}
if ($failed_tries) {
$hash{$i} = $value;
} else {
warn "Hash full\n";
}
Update: I'm assuming that the hash values, once written, are read only, and never change.
-QM
--
Quantum Mechanics: The dreams stuff is made of
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.