Search in sources :

Example 1 with UniversalHash

use of org.h2.dev.hash.MinimalPerfectHash.UniversalHash in project h2database by h2database.

the class TestPerfectHash method testBrokenHashFunction.

private void testBrokenHashFunction() {
    int size = 10000;
    Random r = new Random(10000);
    HashSet<String> set = new HashSet<>(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, int seed) {
                if (index < badUntilLevel) {
                    return 0;
                }
                return StringHash.getFastHash(o, index, seed);
            }
        };
        byte[] desc = MinimalPerfectHash.generate(set, badHash);
        testMinimal(desc, set, badHash);
    }
}
Also used : UniversalHash(org.h2.dev.hash.MinimalPerfectHash.UniversalHash) Random(java.util.Random) HashSet(java.util.HashSet)

Example 2 with UniversalHash

use of org.h2.dev.hash.MinimalPerfectHash.UniversalHash in project h2database by h2database.

the class TestPerfectHash method testMinimal.

private <K> int testMinimal(byte[] desc, Set<K> set, UniversalHash<K> hf) {
    int max = -1;
    BitSet test = new BitSet();
    MinimalPerfectHash<K> hash = new MinimalPerfectHash<>(desc, hf);
    for (K x : set) {
        int h = hash.get(x);
        assertTrue(h >= 0);
        assertTrue(h <= set.size() * 3);
        max = Math.max(max, h);
        assertFalse(test.get(h));
        test.set(h);
    }
    return max;
}
Also used : MinimalPerfectHash(org.h2.dev.hash.MinimalPerfectHash) BitSet(java.util.BitSet)

Example 3 with UniversalHash

use of org.h2.dev.hash.MinimalPerfectHash.UniversalHash in project h2database by h2database.

the class TestPerfectHash method largeFile.

private static void largeFile(String s) throws IOException {
    String fileName = System.getProperty("user.home") + "/temp/" + s;
    if (!new File(fileName).exists()) {
        System.out.println("not found: " + fileName);
        return;
    }
    RandomAccessFile f = new RandomAccessFile(fileName, "r");
    byte[] data = new byte[(int) f.length()];
    f.readFully(data);
    UniversalHash<Text> hf = new UniversalHash<Text>() {

        @Override
        public int hashCode(Text o, int index, int seed) {
            return o.hashCode(index, seed);
        }
    };
    f.close();
    HashSet<Text> set = new HashSet<>();
    Text t = new Text(data, 0);
    while (true) {
        set.add(t);
        int end = t.getEnd();
        if (end >= data.length - 1) {
            break;
        }
        t = new Text(data, end + 1);
        if (set.size() % 1000000 == 0) {
            System.out.println("size: " + set.size());
        }
    }
    System.out.println("file: " + s);
    System.out.println("size: " + set.size());
    long time = System.nanoTime();
    byte[] desc = MinimalPerfectHash.generate(set, hf);
    time = System.nanoTime() - time;
    System.out.println("millis: " + TimeUnit.NANOSECONDS.toMillis(time));
    System.out.println("len: " + desc.length);
    int bits = desc.length * 8;
    System.out.println(((double) bits / set.size()) + " bits/key");
}
Also used : UniversalHash(org.h2.dev.hash.MinimalPerfectHash.UniversalHash) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)2 UniversalHash (org.h2.dev.hash.MinimalPerfectHash.UniversalHash)2 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 BitSet (java.util.BitSet)1 Random (java.util.Random)1 MinimalPerfectHash (org.h2.dev.hash.MinimalPerfectHash)1