/* * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (https://h2database.com/html/license.html). * Initial Developer: H2 Group */ package org.h2.dev.hash; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; import java.util.zip.Deflater; import java.util.zip.Inflater; /** * A perfect hash function tool. It needs about 1.4 bits per key, and the * resulting hash table is about 79% full. The minimal perfect hash function * needs about 2.3 bits per key. *
* Generating the hash function takes about 1 second per million keys * for both perfect hash and minimal perfect hash. *
* The algorithm is recursive: sets that contain no or only one entry are not * processed as no conflicts are possible. Sets that contain between 2 and 16 * entries, up to 16 hash functions are tested to check if they can store the * data without conflict. If no function was found, the same is tested on a * larger bucket (except for the minimal perfect hash). If no hash function was * found, and for larger buckets, the bucket is split into a number of smaller * buckets (up to 32). *
* At the end of the generation process, the data is compressed using a general * purpose compression tool (Deflate / Huffman coding). The uncompressed data is * around 1.52 bits per key (perfect hash) and 3.72 (minimal perfect hash). *
* Please also note the MinimalPerfectHash class, which uses less space per key.
*/
public class PerfectHash {
/**
* The maximum size of a bucket.
*/
private static final int MAX_SIZE = 16;
/**
* The maximum number of hash functions to test.
*/
private static final int OFFSETS = 16;
/**
* The maximum number of buckets to split the set into.
*/
private static final int MAX_SPLIT = 32;
/**
* The description of the hash function. Used for calculating the hash of a
* key.
*/
private final byte[] data;
/**
* The offset of the result of the hash function at the given offset within
* the data array. Used for calculating the hash of a key.
*/
private final int[] plus;
/**
* The position of the next bucket in the data array (in case this bucket
* needs to be skipped). Used for calculating the hash of a key.
*/
private final int[] next;
/**
* Create a hash object to convert keys to hashes.
*
* @param data the data returned by the generate method
*/
public PerfectHash(byte[] data) {
this.data = data = expand(data);
plus = new int[data.length];
next = new int[data.length];
for (int i = 0, p = 0; i < data.length; i++) {
plus[i] = p;
int n = data[i] & 255;
p += n < 2 ? n : n >= MAX_SPLIT ? (n / OFFSETS) : 0;
}
}
/**
* Calculate the hash from the key.
*
* @param x the key
* @return the hash
*/
public int get(int x) {
return get(0, x, 0);
}
private int get(int pos, int x, int level) {
int n = data[pos] & 255;
if (n < 2) {
return plus[pos];
} else if (n >= MAX_SPLIT) {
return plus[pos] + hash(x, level, n % OFFSETS, n / OFFSETS);
}
pos++;
int h = hash(x, level, 0, n);
for (int i = 0; i < h; i++) {
pos = read(pos);
}
return get(pos, x, level + 1);
}
private int read(int pos) {
int p = next[pos];
if (p == 0) {
int n = data[pos] & 255;
if (n < 2 || n >= MAX_SPLIT) {
return pos + 1;
}
int start = pos++;
for (int i = 0; i < n; i++) {
pos = read(pos);
}
next[start] = p = pos;
}
return p;
}
/**
* Generate the perfect hash function data from the given set of integers.
*
* @param list the set
* @param minimal whether the perfect hash function needs to be minimal
* @return the data
*/
public static byte[] generate(Set> lists = new ArrayList<>(split);
for (int i = 0; i < split; i++) {
lists.add(new ArrayList