| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.hash; |
| 7 |
|
| 8 |
import java.io.ByteArrayOutputStream; |
| 9 |
import java.util.ArrayList; |
| 10 |
import java.util.Collection; |
| 11 |
import java.util.List; |
| 12 |
import java.util.Set; |
| 13 |
import java.util.zip.Deflater; |
| 14 |
import java.util.zip.Inflater; |
| 15 |
|
| 16 |
/** |
| 17 |
* A perfect hash function tool. It needs about 1.4 bits per key, and the |
| 18 |
* resulting hash table is about 79% full. The minimal perfect hash function |
| 19 |
* needs about 2.3 bits per key. |
| 20 |
* <p> |
| 21 |
* Generating the hash function takes about 1 second per million keys |
| 22 |
* for both perfect hash and minimal perfect hash. |
| 23 |
* <p> |
| 24 |
* The algorithm is recursive: sets that contain no or only one entry are not |
| 25 |
* processed as no conflicts are possible. Sets that contain between 2 and 16 |
| 26 |
* entries, up to 16 hash functions are tested to check if they can store the |
| 27 |
* data without conflict. If no function was found, the same is tested on a |
| 28 |
* larger bucket (except for the minimal perfect hash). If no hash function was |
| 29 |
* found, and for larger buckets, the bucket is split into a number of smaller |
| 30 |
* buckets (up to 32). |
| 31 |
* <p> |
| 32 |
* At the end of the generation process, the data is compressed using a general |
| 33 |
* purpose compression tool (Deflate / Huffman coding). The uncompressed data is |
| 34 |
* around 1.52 bits per key (perfect hash) and 3.72 (minimal perfect hash). |
| 35 |
* <p> |
| 36 |
* Please also note the MinimalPerfectHash class, which uses less space per key. |
| 37 |
*/ |
| 38 |
public class PerfectHash { |
| 39 |
|
| 40 |
/** |
| 41 |
* The maximum size of a bucket. |
| 42 |
*/ |
| 43 |
private static final int MAX_SIZE = 16; |
| 44 |
|
| 45 |
/** |
| 46 |
* The maximum number of hash functions to test. |
| 47 |
*/ |
| 48 |
private static final int OFFSETS = 16; |
| 49 |
|
| 50 |
/** |
| 51 |
* The maximum number of buckets to split the set into. |
| 52 |
*/ |
| 53 |
private static final int MAX_SPLIT = 32; |
| 54 |
|
| 55 |
/** |
| 56 |
* The description of the hash function. Used for calculating the hash of a |
| 57 |
* key. |
| 58 |
*/ |
| 59 |
private final byte[] data; |
| 60 |
|
| 61 |
/** |
| 62 |
* The offset of the result of the hash function at the given offset within |
| 63 |
* the data array. Used for calculating the hash of a key. |
| 64 |
*/ |
| 65 |
private final int[] plus; |
| 66 |
|
| 67 |
/** |
| 68 |
* The position of the next bucket in the data array (in case this bucket |
| 69 |
* needs to be skipped). Used for calculating the hash of a key. |
| 70 |
*/ |
| 71 |
private final int[] next; |
| 72 |
|
| 73 |
/** |
| 74 |
* Create a hash object to convert keys to hashes. |
| 75 |
* |
| 76 |
* @param data the data returned by the generate method |
| 77 |
*/ |
| 78 |
public PerfectHash(byte[] data) { |
| 79 |
this.data = data = expand(data); |
| 80 |
plus = new int[data.length]; |
| 81 |
next = new int[data.length]; |
| 82 |
for (int i = 0, p = 0; i < data.length; i++) { |
| 83 |
plus[i] = p; |
| 84 |
int n = data[i] & 255; |
| 85 |
p += n < 2 ? n : n >= MAX_SPLIT ? (n / OFFSETS) : 0; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Calculate the hash from the key. |
| 91 |
* |
| 92 |
* @param x the key |
| 93 |
* @return the hash |
| 94 |
*/ |
| 95 |
public int get(int x) { |
| 96 |
return get(0, x, 0); |
| 97 |
} |
| 98 |
|
| 99 |
private int get(int pos, int x, int level) { |
| 100 |
int n = data[pos] & 255; |
| 101 |
if (n < 2) { |
| 102 |
return plus[pos]; |
| 103 |
} else if (n >= MAX_SPLIT) { |
| 104 |
return plus[pos] + hash(x, level, n % OFFSETS, n / OFFSETS); |
| 105 |
} |
| 106 |
pos++; |
| 107 |
int h = hash(x, level, 0, n); |
| 108 |
for (int i = 0; i < h; i++) { |
| 109 |
pos = read(pos); |
| 110 |
} |
| 111 |
return get(pos, x, level + 1); |
| 112 |
} |
| 113 |
|
| 114 |
private int read(int pos) { |
| 115 |
int p = next[pos]; |
| 116 |
if (p == 0) { |
| 117 |
int n = data[pos] & 255; |
| 118 |
if (n < 2 || n >= MAX_SPLIT) { |
| 119 |
return pos + 1; |
| 120 |
} |
| 121 |
int start = pos++; |
| 122 |
for (int i = 0; i < n; i++) { |
| 123 |
pos = read(pos); |
| 124 |
} |
| 125 |
next[start] = p = pos; |
| 126 |
} |
| 127 |
return p; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Generate the perfect hash function data from the given set of integers. |
| 132 |
* |
| 133 |
* @param list the set |
| 134 |
* @param minimal whether the perfect hash function needs to be minimal |
| 135 |
* @return the data |
| 136 |
*/ |
| 137 |
public static byte[] generate(Set<Integer> list, boolean minimal) { |
| 138 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 139 |
generate(list, 0, minimal, out); |
| 140 |
return compress(out.toByteArray()); |
| 141 |
} |
| 142 |
|
| 143 |
private static void generate(Collection<Integer> set, int level, |
| 144 |
boolean minimal, ByteArrayOutputStream out) { |
| 145 |
int size = set.size(); |
| 146 |
if (size <= 1) { |
| 147 |
out.write(size); |
| 148 |
return; |
| 149 |
} |
| 150 |
if (size < MAX_SIZE) { |
| 151 |
int max = minimal ? size : Math.min(MAX_SIZE - 1, size * 2); |
| 152 |
for (int s = size; s <= max; s++) { |
| 153 |
// Try a few hash functions ("offset" is basically the hash |
| 154 |
// function index). We could try less hash functions, and |
| 155 |
// instead use a larger size and remember the position of the |
| 156 |
// hole (specially for the minimal perfect case), but that's |
| 157 |
// more complicated. |
| 158 |
nextOffset: |
| 159 |
for (int offset = 0; offset < OFFSETS; offset++) { |
| 160 |
int bits = 0; |
| 161 |
for (int x : set) { |
| 162 |
int h = hash(x, level, offset, s); |
| 163 |
if ((bits & (1 << h)) != 0) { |
| 164 |
continue nextOffset; |
| 165 |
} |
| 166 |
bits |= 1 << h; |
| 167 |
} |
| 168 |
out.write(s * OFFSETS + offset); |
| 169 |
return; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
// Split the set into multiple smaller sets. We could try to split more |
| 174 |
// evenly by trying out multiple hash functions, but that's more |
| 175 |
// complicated. |
| 176 |
int split; |
| 177 |
if (minimal) { |
| 178 |
split = size > 150 ? size / 83 : (size + 3) / 4; |
| 179 |
} else { |
| 180 |
split = size > 265 ? size / 142 : (size + 5) / 7; |
| 181 |
} |
| 182 |
split = Math.min(MAX_SPLIT - 1, Math.max(2, split)); |
| 183 |
out.write(split); |
| 184 |
List<List<Integer>> lists = new ArrayList<>(split); |
| 185 |
for (int i = 0; i < split; i++) { |
| 186 |
lists.add(new ArrayList<Integer>(size / split)); |
| 187 |
} |
| 188 |
for (int x : set) { |
| 189 |
lists.get(hash(x, level, 0, split)).add(x); |
| 190 |
} |
| 191 |
for (List<Integer> s2 : lists) { |
| 192 |
generate(s2, level + 1, minimal, out); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Calculate the hash of a key. The result depends on the key, the recursion |
| 198 |
* level, and the offset. |
| 199 |
* |
| 200 |
* @param x the key |
| 201 |
* @param level the recursion level |
| 202 |
* @param offset the index of the hash function |
| 203 |
* @param size the size of the bucket |
| 204 |
* @return the hash (a value between 0, including, and the size, excluding) |
| 205 |
*/ |
| 206 |
private static int hash(int x, int level, int offset, int size) { |
| 207 |
x += level * OFFSETS + offset; |
| 208 |
x = ((x >>> 16) ^ x) * 0x45d9f3b; |
| 209 |
x = ((x >>> 16) ^ x) * 0x45d9f3b; |
| 210 |
x = (x >>> 16) ^ x; |
| 211 |
return Math.abs(x % size); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Compress the hash description using a Huffman coding. |
| 216 |
* |
| 217 |
* @param d the data |
| 218 |
* @return the compressed data |
| 219 |
*/ |
| 220 |
private static byte[] compress(byte[] d) { |
| 221 |
Deflater deflater = new Deflater(); |
| 222 |
deflater.setStrategy(Deflater.HUFFMAN_ONLY); |
| 223 |
deflater.setInput(d); |
| 224 |
deflater.finish(); |
| 225 |
ByteArrayOutputStream out2 = new ByteArrayOutputStream(d.length); |
| 226 |
byte[] buffer = new byte[1024]; |
| 227 |
while (!deflater.finished()) { |
| 228 |
int count = deflater.deflate(buffer); |
| 229 |
out2.write(buffer, 0, count); |
| 230 |
} |
| 231 |
deflater.end(); |
| 232 |
return out2.toByteArray(); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Decompress the hash description using a Huffman coding. |
| 237 |
* |
| 238 |
* @param d the data |
| 239 |
* @return the decompressed data |
| 240 |
*/ |
| 241 |
private static byte[] expand(byte[] d) { |
| 242 |
Inflater inflater = new Inflater(); |
| 243 |
inflater.setInput(d); |
| 244 |
ByteArrayOutputStream out = new ByteArrayOutputStream(d.length); |
| 245 |
byte[] buffer = new byte[1024]; |
| 246 |
try { |
| 247 |
while (!inflater.finished()) { |
| 248 |
int count = inflater.inflate(buffer); |
| 249 |
out.write(buffer, 0, count); |
| 250 |
} |
| 251 |
inflater.end(); |
| 252 |
} catch (Exception e) { |
| 253 |
throw new IllegalArgumentException(e); |
| 254 |
} |
| 255 |
return out.toByteArray(); |
| 256 |
} |
| 257 |
|
| 258 |
} |