提交 1b19a776 authored 作者: Thomas Mueller's avatar Thomas Mueller

A minimal perfect hash function tool: use universal hashing callback (protection…

A minimal perfect hash function tool: use universal hashing callback (protection against hash flooding)
上级 05719a66
...@@ -29,8 +29,8 @@ public class TestPerfectHash extends TestBase { ...@@ -29,8 +29,8 @@ public class TestPerfectHash extends TestBase {
*/ */
public static void main(String... a) throws Exception { public static void main(String... a) throws Exception {
TestPerfectHash test = (TestPerfectHash) TestBase.createCaller().init(); TestPerfectHash test = (TestPerfectHash) TestBase.createCaller().init();
test.measure();
test.test(); test.test();
test.measure();
} }
/** /**
...@@ -38,25 +38,36 @@ public class TestPerfectHash extends TestBase { ...@@ -38,25 +38,36 @@ public class TestPerfectHash extends TestBase {
*/ */
public void measure() { public void measure() {
int size = 1000000; int size = 1000000;
testMinimal(size / 10);
int s; int s;
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
s = testMinimal(size); s = testMinimal(size);
time = System.currentTimeMillis() - time; time = System.currentTimeMillis() - time;
System.out.println((double) s / size + " bits/key (minimal) in " + time + " ms"); System.out.println((double) s / size + " bits/key (minimal) in " +
time + " ms");
time = System.currentTimeMillis();
s = testMinimalWithString(size); time = System.currentTimeMillis();
time = System.currentTimeMillis() - time; s = testMinimalWithString(size);
System.out.println((double) s / size + " bits/key (minimal; String keys) in " + time + " ms"); time = System.currentTimeMillis() - time;
System.out.println((double) s / size +
" bits/key (minimal; String keys) in " +
time + " ms");
time = System.currentTimeMillis();
s = test(size, true); s = test(size, true);
System.out.println((double) s / size + " bits/key (minimal old)"); time = System.currentTimeMillis() - time;
System.out.println((double) s / size + " bits/key (minimal old) in " +
time + " ms");
time = System.currentTimeMillis();
s = test(size, false); s = test(size, false);
System.out.println((double) s / size + " bits/key (not minimal)"); time = System.currentTimeMillis() - time;
System.out.println((double) s / size + " bits/key (not minimal) in " +
time + " ms");
} }
@Override @Override
public void test() { public void test() {
testBrokenHashFunction();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
testMinimal(i); testMinimal(i);
} }
...@@ -72,6 +83,31 @@ public class TestPerfectHash extends TestBase { ...@@ -72,6 +83,31 @@ public class TestPerfectHash extends TestBase {
test(i, false); test(i, false);
} }
} }
private void testBrokenHashFunction() {
int size = 10000;
Random r = new Random(10000);
HashSet<String> set = new HashSet<String>(size);
while (set.size() < size) {
set.add("x " + r.nextDouble());
}
for (int test = 1; test < 10; test++) {
final int badUntilLevel = test;
UniversalHash<String> badHash = new UniversalHash<String>() {
@Override
public int hashCode(String o, int index) {
if (index < badUntilLevel) {
return 0;
}
return StringHash.getFastHash(o, index);
}
};
byte[] desc = MinimalPerfectHash.generate(set, badHash);
testMinimal(desc, set, badHash);
}
}
private int test(int size, boolean minimal) { private int test(int size, boolean minimal) {
Random r = new Random(size); Random r = new Random(size);
...@@ -108,7 +144,7 @@ public class TestPerfectHash extends TestBase { ...@@ -108,7 +144,7 @@ public class TestPerfectHash extends TestBase {
private int testMinimal(int size) { private int testMinimal(int size) {
Random r = new Random(size); Random r = new Random(size);
HashSet<Long> set = new HashSet<Long>(); HashSet<Long> set = new HashSet<Long>(size);
while (set.size() < size) { while (set.size() < size) {
set.add((long) r.nextInt()); set.add((long) r.nextInt());
} }
...@@ -121,7 +157,7 @@ public class TestPerfectHash extends TestBase { ...@@ -121,7 +157,7 @@ public class TestPerfectHash extends TestBase {
private int testMinimalWithString(int size) { private int testMinimalWithString(int size) {
Random r = new Random(size); Random r = new Random(size);
HashSet<String> set = new HashSet<String>(); HashSet<String> set = new HashSet<String>(size);
while (set.size() < size) { while (set.size() < size) {
set.add("x " + r.nextDouble()); set.add("x " + r.nextDouble());
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论