| 1 |
# -*- perl -*- |
| 2 |
# |
| 3 |
# Perfect Minimal Hash Generator written in Perl, which produces |
| 4 |
# C output. |
| 5 |
# |
| 6 |
|
| 7 |
require 'random_sv_vectors.ph'; |
| 8 |
require 'crc64.ph'; |
| 9 |
|
| 10 |
# |
| 11 |
# Compute the prehash for a key |
| 12 |
# |
| 13 |
# prehash(key, sv, N) |
| 14 |
# |
| 15 |
sub prehash($$$) { |
| 16 |
my($key, $n, $sv) = @_; |
| 17 |
my @c = crc64($sv, $key); |
| 18 |
my $nmask = ($n << 1) - 2; |
| 19 |
|
| 20 |
# Create a bipartite graph... |
| 21 |
$k1 = ($c[1] & $nmask) + 0; # low word |
| 22 |
$k2 = ($c[0] & $nmask) + 1; # high word |
| 23 |
|
| 24 |
return ($k1, $k2); |
| 25 |
} |
| 26 |
|
| 27 |
# |
| 28 |
# Walk the assignment graph, return true on success |
| 29 |
# |
| 30 |
sub walk_graph($$$$) { |
| 31 |
my($nodeval,$nodeneigh,$n,$v) = @_; |
| 32 |
my $nx; |
| 33 |
|
| 34 |
# print STDERR "Vertex $n value $v\n"; |
| 35 |
$$nodeval[$n] = $v; |
| 36 |
|
| 37 |
foreach $nx (@{$$nodeneigh[$n]}) { |
| 38 |
# $nx -> [neigh, hash] |
| 39 |
my ($o, $e) = @$nx; |
| 40 |
|
| 41 |
# print STDERR "Edge $n,$o value $e: "; |
| 42 |
my $ov; |
| 43 |
if (defined($ov = $$nodeval[$o])) { |
| 44 |
if ($v+$ov != $e) { |
| 45 |
# Cyclic graph with collision |
| 46 |
# print STDERR "error, should be ", $v+$ov, "\n"; |
| 47 |
return 0; |
| 48 |
} else { |
| 49 |
# print STDERR "ok\n"; |
| 50 |
} |
| 51 |
} else { |
| 52 |
return 0 unless (walk_graph($nodeval, $nodeneigh, $o, $e-$v)); |
| 53 |
} |
| 54 |
} |
| 55 |
return 1; |
| 56 |
} |
| 57 |
|
| 58 |
# |
| 59 |
# Generate the function assuming a given N. |
| 60 |
# |
| 61 |
# gen_hash_n(N, sv, \%data, run) |
| 62 |
# |
| 63 |
sub gen_hash_n($$$$) { |
| 64 |
my($n, $sv, $href, $run) = @_; |
| 65 |
my @keys = keys(%{$href}); |
| 66 |
my $i; |
| 67 |
my $gr; |
| 68 |
my ($k, $v); |
| 69 |
my $gsize = 2*$n; |
| 70 |
my @nodeval; |
| 71 |
my @nodeneigh; |
| 72 |
my %edges; |
| 73 |
|
| 74 |
for ($i = 0; $i < $gsize; $i++) { |
| 75 |
$nodeneigh[$i] = []; |
| 76 |
} |
| 77 |
|
| 78 |
%edges = (); |
| 79 |
foreach $k (@keys) { |
| 80 |
my ($pf1, $pf2) = prehash($k, $n, $sv); |
| 81 |
($pf1,$pf2) = ($pf2,$pf1) if ($pf1 > $pf2); # Canonicalize order |
| 82 |
|
| 83 |
my $pf = "$pf1,$pf2"; |
| 84 |
my $e = ${$href}{$k}; |
| 85 |
my $xkey; |
| 86 |
|
| 87 |
if (defined($xkey = $edges{$pf})) { |
| 88 |
next if ($e == ${$href}{$xkey}); # Duplicate hash, safe to ignore |
| 89 |
if (defined($run)) { |
| 90 |
print STDERR "$run: Collision: $pf: $k with $xkey\n"; |
| 91 |
} |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
# print STDERR "Edge $pf value $e from $k\n"; |
| 96 |
|
| 97 |
$edges{$pf} = $k; |
| 98 |
push(@{$nodeneigh[$pf1]}, [$pf2, $e]); |
| 99 |
push(@{$nodeneigh[$pf2]}, [$pf1, $e]); |
| 100 |
} |
| 101 |
|
| 102 |
# Now we need to assign values to each vertex, so that for each |
| 103 |
# edge, the sum of the values for the two vertices give the value |
| 104 |
# for the edge (which is our hash index.) If we find an impossible |
| 105 |
# situation, the graph was cyclic. |
| 106 |
@nodeval = (undef) x $gsize; |
| 107 |
|
| 108 |
for ($i = 0; $i < $gsize; $i++) { |
| 109 |
if (scalar(@{$nodeneigh[$i]})) { |
| 110 |
# This vertex has neighbors (is used) |
| 111 |
if (!defined($nodeval[$i])) { |
| 112 |
# First vertex in a cluster |
| 113 |
unless (walk_graph(\@nodeval, \@nodeneigh, $i, 0)) { |
| 114 |
if (defined($run)) { |
| 115 |
print STDERR "$run: Graph is cyclic\n"; |
| 116 |
} |
| 117 |
return; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
# for ($i = 0; $i < $n; $i++) { |
| 124 |
# print STDERR "Vertex ", $i, ": ", $g[$i], "\n"; |
| 125 |
# } |
| 126 |
|
| 127 |
if (defined($run)) { |
| 128 |
printf STDERR "$run: Done: n = $n, sv = [0x%08x, 0x%08x]\n", |
| 129 |
$$sv[0], $$sv[1]; |
| 130 |
} |
| 131 |
|
| 132 |
return ($n, $sv, \@nodeval); |
| 133 |
} |
| 134 |
|
| 135 |
# |
| 136 |
# Driver for generating the function |
| 137 |
# |
| 138 |
# gen_perfect_hash(\%data) |
| 139 |
# |
| 140 |
sub gen_perfect_hash($) { |
| 141 |
my($href) = @_; |
| 142 |
my @keys = keys(%{$href}); |
| 143 |
my @hashinfo; |
| 144 |
my ($n, $i, $j, $sv, $maxj); |
| 145 |
my $run = 1; |
| 146 |
|
| 147 |
# Minimal power of 2 value for N with enough wiggle room. |
| 148 |
# The scaling constant must be larger than 0.5 in order for the |
| 149 |
# algorithm to ever terminate. The higher the scaling constant, |
| 150 |
# the more space does the hash take up, but the less likely is it |
| 151 |
# that an invalid token will require a string comparison. |
| 152 |
my $room = int(scalar(@keys)*1.6); |
| 153 |
$n = 1; |
| 154 |
while ($n < $room) { |
| 155 |
$n <<= 1; |
| 156 |
} |
| 157 |
|
| 158 |
# Number of times to try... |
| 159 |
$maxj = scalar @random_sv_vectors; |
| 160 |
|
| 161 |
for ($i = 0; $i < 4; $i++) { |
| 162 |
printf STDERR "%d vectors, trying n = %d...\n", |
| 163 |
scalar @keys, $n; |
| 164 |
for ($j = 0; $j < $maxj; $j++) { |
| 165 |
$sv = $random_sv_vectors[$j]; |
| 166 |
@hashinfo = gen_hash_n($n, $sv, $href, $run++); |
| 167 |
return @hashinfo if (@hashinfo); |
| 168 |
} |
| 169 |
$n <<= 1; |
| 170 |
} |
| 171 |
|
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
# |
| 176 |
# Verify that the hash table is actually correct... |
| 177 |
# |
| 178 |
sub verify_hash_table($$) |
| 179 |
{ |
| 180 |
my ($href, $hashinfo) = @_; |
| 181 |
my ($n, $sv, $g) = @{$hashinfo}; |
| 182 |
my $k; |
| 183 |
my $err = 0; |
| 184 |
|
| 185 |
foreach $k (keys(%$href)) { |
| 186 |
my ($pf1, $pf2) = prehash($k, $n, $sv); |
| 187 |
my $g1 = ${$g}[$pf1]; |
| 188 |
my $g2 = ${$g}[$pf2]; |
| 189 |
|
| 190 |
if ($g1+$g2 != ${$href}{$k}) { |
| 191 |
printf STDERR "%s(%d,%d): %d+%d = %d != %d\n", |
| 192 |
$k, $pf1, $pf2, $g1, $g2, $g1+$g2, ${$href}{$k}; |
| 193 |
$err = 1; |
| 194 |
} else { |
| 195 |
# printf STDERR "%s: %d+%d = %d ok\n", |
| 196 |
# $k, $g1, $g2, $g1+$g2; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
die "$0: hash validation error\n" if ($err); |
| 201 |
} |
| 202 |
|
| 203 |
1; |