2007-08-31 04:15:25 +08:00
|
|
|
# -*- perl -*-
|
|
|
|
#
|
|
|
|
# Perfect Minimal Hash Generator written in Perl, which produces
|
|
|
|
# C output.
|
|
|
|
#
|
2007-08-31 07:42:39 +08:00
|
|
|
# Requires the CPAN Graph module (tested against 0.81, 0.83, 0.84)
|
2007-09-01 02:10:23 +08:00
|
|
|
#
|
2007-08-31 04:15:25 +08:00
|
|
|
|
|
|
|
use Graph::Undirected;
|
2007-09-01 02:10:23 +08:00
|
|
|
require 'random_sv_vectors.ph';
|
2007-10-03 08:40:00 +08:00
|
|
|
require 'crc64.ph';
|
2007-08-31 04:15:25 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# Compute the prehash for a key
|
|
|
|
#
|
|
|
|
# prehash(key, sv, N)
|
|
|
|
#
|
|
|
|
sub prehash($$$) {
|
|
|
|
my($key, $n, $sv) = @_;
|
2007-10-03 08:40:00 +08:00
|
|
|
my @c = crc64($sv, $key);
|
2007-08-31 04:15:25 +08:00
|
|
|
|
2007-09-01 02:10:23 +08:00
|
|
|
# Create a bipartite graph...
|
2007-10-03 08:40:00 +08:00
|
|
|
$k1 = (($c[1] & ($n-1)) << 1) + 0; # low word
|
|
|
|
$k2 = (($c[0] & ($n-1)) << 1) + 1; # high word
|
2007-08-31 04:15:25 +08:00
|
|
|
|
2007-09-01 02:10:23 +08:00
|
|
|
return ($k1, $k2);
|
2007-08-31 04:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Walk the assignment graph
|
|
|
|
#
|
|
|
|
sub walk_graph($$$) {
|
|
|
|
my($gr,$n,$v) = @_;
|
|
|
|
my $nx;
|
|
|
|
|
|
|
|
# print STDERR "Vertex $n value $v\n";
|
|
|
|
$gr->set_vertex_attribute($n,"val",$v);
|
|
|
|
|
|
|
|
foreach $nx ($gr->neighbors($n)) {
|
|
|
|
die unless ($gr->has_edge_attribute($n, $nx, "hash"));
|
|
|
|
my $e = $gr->get_edge_attribute($n, $nx, "hash");
|
|
|
|
|
|
|
|
# print STDERR "Edge $n=$nx value $e: ";
|
|
|
|
|
|
|
|
if ($gr->has_vertex_attribute($nx, "val")) {
|
|
|
|
die if ($v+$gr->get_vertex_attribute($nx, "val") != $e);
|
|
|
|
# print STDERR "ok\n";
|
|
|
|
} else {
|
|
|
|
walk_graph($gr, $nx, $e-$v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generate the function assuming a given N.
|
|
|
|
#
|
2007-10-03 08:40:00 +08:00
|
|
|
# gen_hash_n(N, sv, \%data, run)
|
2007-08-31 04:15:25 +08:00
|
|
|
#
|
2007-10-03 08:40:00 +08:00
|
|
|
sub gen_hash_n($$$$) {
|
|
|
|
my($n, $sv, $href, $run) = @_;
|
2007-08-31 04:15:25 +08:00
|
|
|
my @keys = keys(%{$href});
|
2007-09-01 02:10:23 +08:00
|
|
|
my $i, $sv, @g;
|
2007-08-31 04:15:25 +08:00
|
|
|
my $gr;
|
|
|
|
my $k, $v;
|
2007-08-31 07:42:39 +08:00
|
|
|
my $gsize = 2*$n;
|
2007-08-31 04:15:25 +08:00
|
|
|
|
|
|
|
$gr = Graph::Undirected->new;
|
2007-08-31 07:42:39 +08:00
|
|
|
for ($i = 0; $i < $gsize; $i++) {
|
2007-08-31 04:15:25 +08:00
|
|
|
$gr->add_vertex($i);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach $k (@keys) {
|
2007-09-01 02:10:23 +08:00
|
|
|
my ($pf1, $pf2) = prehash($k, $n, $sv);
|
2007-08-31 04:15:25 +08:00
|
|
|
my $e = ${$href}{$k};
|
|
|
|
|
|
|
|
if ($gr->has_edge($pf1, $pf2)) {
|
|
|
|
my $xkey = $gr->get_edge_attribute($pf1, $pf2, "key");
|
|
|
|
my ($xp1, $xp2) = prehash($xkey, $n, $sv);
|
2007-10-03 08:40:00 +08:00
|
|
|
if (defined($run)) {
|
|
|
|
print STDERR "$run: Collision: $pf1=$pf2 $k with ";
|
|
|
|
print STDERR "$xkey ($xp1,$xp2)\n";
|
|
|
|
}
|
2007-08-31 04:15:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-01 02:10:23 +08:00
|
|
|
# print STDERR "Edge $pf1=$pf2 value $e from $k\n";
|
2007-08-31 04:15:25 +08:00
|
|
|
|
|
|
|
$gr->add_edge($pf1, $pf2);
|
|
|
|
$gr->set_edge_attribute($pf1, $pf2, "hash", $e);
|
|
|
|
$gr->set_edge_attribute($pf1, $pf2, "key", $k);
|
|
|
|
}
|
|
|
|
|
|
|
|
# At this point, we're good if the graph is acyclic.
|
|
|
|
if ($gr->is_cyclic) {
|
2007-10-03 08:40:00 +08:00
|
|
|
if (defined($run)) {
|
|
|
|
print STDERR "$run: Graph is cyclic\n";
|
|
|
|
}
|
2007-08-31 04:15:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2007-09-13 00:55:57 +08:00
|
|
|
|
2007-10-03 08:40:00 +08:00
|
|
|
if (defined($run)) {
|
|
|
|
print STDERR "$run: Graph OK, computing vertices...\n";
|
|
|
|
}
|
2007-08-31 04:15:25 +08:00
|
|
|
|
|
|
|
# Now we need to assign values to each vertex, so that for each
|
|
|
|
# edge, the sum of the values for the two vertices give the value
|
|
|
|
# for the edge (which is our hash index.) Since the graph is
|
|
|
|
# acyclic, this is always doable.
|
2007-08-31 07:42:39 +08:00
|
|
|
for ($i = 0; $i < $gsize; $i++) {
|
2007-08-31 15:23:31 +08:00
|
|
|
if ($gr->degree($i)) {
|
|
|
|
# This vertex has neighbors (is used)
|
|
|
|
if (!$gr->has_vertex_attribute($i, "val")) {
|
|
|
|
walk_graph($gr,$i,0); # First vertex in a cluster
|
|
|
|
}
|
|
|
|
push(@g, $gr->get_vertex_attribute($i, "val"));
|
|
|
|
} else {
|
|
|
|
# Unused vertex
|
|
|
|
push(@g, undef);
|
2007-08-31 04:15:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# for ($i = 0; $i < $n; $i++) {
|
|
|
|
# print STDERR "Vertex ", $i, ": ", $g[$i], "\n";
|
|
|
|
# }
|
|
|
|
|
2007-10-03 08:40:00 +08:00
|
|
|
if (defined($run)) {
|
|
|
|
printf STDERR "$run: Done: n = $n, sv = [0x%08x, 0x%08x]\n",
|
|
|
|
$$sv[0], $$sv[1];
|
|
|
|
}
|
2007-08-31 04:15:25 +08:00
|
|
|
|
2007-09-01 02:10:23 +08:00
|
|
|
return ($n, $sv, \@g);
|
2007-08-31 04:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Driver for generating the function
|
|
|
|
#
|
|
|
|
# gen_perfect_hash(\%data)
|
|
|
|
#
|
|
|
|
sub gen_perfect_hash($) {
|
|
|
|
my($href) = @_;
|
|
|
|
my @keys = keys(%{$href});
|
|
|
|
my @hashinfo;
|
|
|
|
my $n, $i, $j, $sv, $maxj;
|
2007-10-03 08:40:00 +08:00
|
|
|
my $run = 1;
|
2007-08-31 04:15:25 +08:00
|
|
|
|
2007-08-31 07:42:39 +08:00
|
|
|
# Minimal power of 2 value for N with enough wiggle room.
|
|
|
|
# The scaling constant must be larger than 0.5 in order for the
|
|
|
|
# algorithm to ever terminate.
|
2008-05-21 07:46:36 +08:00
|
|
|
my $room = scalar(@keys)*0.8;
|
2007-08-31 04:15:25 +08:00
|
|
|
$n = 1;
|
2007-08-31 05:39:37 +08:00
|
|
|
while ($n < $room) {
|
2007-08-31 04:15:25 +08:00
|
|
|
$n <<= 1;
|
|
|
|
}
|
|
|
|
|
2007-09-01 02:10:23 +08:00
|
|
|
# Number of times to try...
|
|
|
|
$maxj = scalar @random_sv_vectors;
|
2007-08-31 04:15:25 +08:00
|
|
|
|
|
|
|
for ($i = 0; $i < 4; $i++) {
|
2008-05-21 07:46:36 +08:00
|
|
|
printf STDERR "%d vectors, trying n = %d...\n",
|
|
|
|
scalar @keys, $n;
|
2007-08-31 04:15:25 +08:00
|
|
|
for ($j = 0; $j < $maxj; $j++) {
|
2007-09-01 02:10:23 +08:00
|
|
|
$sv = $random_sv_vectors[$j];
|
2007-10-03 08:40:00 +08:00
|
|
|
@hashinfo = gen_hash_n($n, $sv, $href, $run++);
|
2007-08-31 05:39:37 +08:00
|
|
|
return @hashinfo if (defined(@hashinfo));
|
2007-08-31 04:15:25 +08:00
|
|
|
}
|
|
|
|
$n <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Read input file
|
|
|
|
#
|
|
|
|
sub read_input() {
|
|
|
|
my $key,$val;
|
|
|
|
my %out;
|
|
|
|
my $x = 0;
|
|
|
|
|
|
|
|
while (defined($l = <STDIN>)) {
|
|
|
|
chomp $l;
|
|
|
|
$l =~ s/\s*(\#.*|)$//;
|
|
|
|
|
|
|
|
next if ($l eq '');
|
|
|
|
|
|
|
|
if ($l =~ /^([^=]+)\=([^=]+)$/) {
|
|
|
|
$out{$1} = $2;
|
|
|
|
$x = $2;
|
|
|
|
} else {
|
|
|
|
$out{$l} = $x;
|
|
|
|
}
|
|
|
|
$x++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return %out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Verify that the hash table is actually correct...
|
|
|
|
#
|
|
|
|
sub verify_hash_table($$)
|
|
|
|
{
|
|
|
|
my ($href, $hashinfo) = @_;
|
2007-09-01 02:10:23 +08:00
|
|
|
my ($n, $sv, $g) = @{$hashinfo};
|
2007-08-31 04:15:25 +08:00
|
|
|
my $k;
|
|
|
|
my $err = 0;
|
|
|
|
|
|
|
|
foreach $k (keys(%$href)) {
|
2007-09-01 02:10:23 +08:00
|
|
|
my ($pf1, $pf2) = prehash($k, $n, $sv);
|
2007-08-31 04:15:25 +08:00
|
|
|
my $g1 = ${$g}[$pf1];
|
|
|
|
my $g2 = ${$g}[$pf2];
|
|
|
|
|
|
|
|
if ($g1+$g2 != ${$href}{$k}) {
|
2007-09-01 02:10:23 +08:00
|
|
|
printf STDERR "%s(%d,%d): %d+%d = %d != %d\n",
|
|
|
|
$k, $pf1, $pf2, $g1, $g2, $g1+$g2, ${$href}{$k};
|
2007-08-31 04:15:25 +08:00
|
|
|
$err = 1;
|
|
|
|
} else {
|
2007-08-31 05:39:37 +08:00
|
|
|
# printf STDERR "%s: %d+%d = %d ok\n",
|
|
|
|
# $k, $g1, $g2, $g1+$g2;
|
2007-08-31 04:15:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
die "$0: hash validation error\n" if ($err);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|