| 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.util.ArrayList; |
| 9 |
import java.util.Arrays; |
| 10 |
|
| 11 |
/** |
| 12 |
* A minimum perfect hash function tool. It needs about 2.2 bits per key. |
| 13 |
*/ |
| 14 |
public class IntPerfectHash { |
| 15 |
|
| 16 |
/** |
| 17 |
* Large buckets are typically divided into buckets of this size. |
| 18 |
*/ |
| 19 |
private static final int DIVIDE = 6; |
| 20 |
|
| 21 |
/** |
| 22 |
* The maximum size of a small bucket (one that is not further split if |
| 23 |
* possible). |
| 24 |
*/ |
| 25 |
private static final int MAX_SIZE = 12; |
| 26 |
|
| 27 |
/** |
| 28 |
* The maximum offset for hash functions of small buckets. At most that many |
| 29 |
* hash functions are tried for the given size. |
| 30 |
*/ |
| 31 |
private static final int[] MAX_OFFSETS = { 0, 0, 8, 18, 47, 123, 319, 831, 2162, |
| 32 |
5622, 14617, 38006, 98815 }; |
| 33 |
|
| 34 |
/** |
| 35 |
* The output value to split the bucket into many (more than 2) smaller |
| 36 |
* buckets. |
| 37 |
*/ |
| 38 |
private static final int SPLIT_MANY = 3; |
| 39 |
|
| 40 |
/** |
| 41 |
* The minimum output value for a small bucket of a given size. |
| 42 |
*/ |
| 43 |
private static final int[] SIZE_OFFSETS = new int[MAX_OFFSETS.length + 1]; |
| 44 |
|
| 45 |
static { |
| 46 |
int last = SPLIT_MANY + 1; |
| 47 |
for (int i = 0; i < MAX_OFFSETS.length; i++) { |
| 48 |
SIZE_OFFSETS[i] = last; |
| 49 |
last += MAX_OFFSETS[i]; |
| 50 |
} |
| 51 |
SIZE_OFFSETS[SIZE_OFFSETS.length - 1] = last; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* The description of the hash function. Used for calculating the hash of a |
| 56 |
* key. |
| 57 |
*/ |
| 58 |
private final byte[] data; |
| 59 |
|
| 60 |
/** |
| 61 |
* Create a hash object to convert keys to hashes. |
| 62 |
* |
| 63 |
* @param data the data returned by the generate method |
| 64 |
*/ |
| 65 |
public IntPerfectHash(byte[] data) { |
| 66 |
this.data = data; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get the hash function description. |
| 71 |
* |
| 72 |
* @return the data |
| 73 |
*/ |
| 74 |
public byte[] getData() { |
| 75 |
return data; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Calculate the hash value for the given key. |
| 80 |
* |
| 81 |
* @param x the key |
| 82 |
* @return the hash value |
| 83 |
*/ |
| 84 |
public int get(int x) { |
| 85 |
return get(0, x, 0); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get the hash value for the given key, starting at a certain position and |
| 90 |
* level. |
| 91 |
* |
| 92 |
* @param pos the start position |
| 93 |
* @param x the key |
| 94 |
* @param level the level |
| 95 |
* @return the hash value |
| 96 |
*/ |
| 97 |
private int get(int pos, int x, int level) { |
| 98 |
int n = readVarInt(data, pos); |
| 99 |
if (n < 2) { |
| 100 |
return 0; |
| 101 |
} else if (n > SPLIT_MANY) { |
| 102 |
int size = getSize(n); |
| 103 |
int offset = getOffset(n, size); |
| 104 |
return hash(x, level, offset, size); |
| 105 |
} |
| 106 |
pos++; |
| 107 |
int split; |
| 108 |
if (n == SPLIT_MANY) { |
| 109 |
split = readVarInt(data, pos); |
| 110 |
pos += getVarIntLength(data, pos); |
| 111 |
} else { |
| 112 |
split = n; |
| 113 |
} |
| 114 |
int h = hash(x, level, 0, split); |
| 115 |
int s; |
| 116 |
int start = pos; |
| 117 |
for (int i = 0; i < h; i++) { |
| 118 |
pos = getNextPos(pos); |
| 119 |
} |
| 120 |
s = getSizeSum(start, pos); |
| 121 |
return s + get(pos, x, level + 1); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get the position of the next sibling. |
| 126 |
* |
| 127 |
* @param pos the position of this branch |
| 128 |
* @return the position of the next sibling |
| 129 |
*/ |
| 130 |
private int getNextPos(int pos) { |
| 131 |
int n = readVarInt(data, pos); |
| 132 |
pos += getVarIntLength(data, pos); |
| 133 |
if (n < 2 || n > SPLIT_MANY) { |
| 134 |
return pos; |
| 135 |
} |
| 136 |
int split; |
| 137 |
if (n == SPLIT_MANY) { |
| 138 |
split = readVarInt(data, pos); |
| 139 |
pos += getVarIntLength(data, pos); |
| 140 |
} else { |
| 141 |
split = n; |
| 142 |
} |
| 143 |
for (int i = 0; i < split; i++) { |
| 144 |
pos = getNextPos(pos); |
| 145 |
} |
| 146 |
return pos; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* The sum of the sizes between the start and end position. |
| 151 |
* |
| 152 |
* @param start the start position |
| 153 |
* @param end the end position (excluding) |
| 154 |
* @return the sizes |
| 155 |
*/ |
| 156 |
private int getSizeSum(int start, int end) { |
| 157 |
int s = 0; |
| 158 |
for (int pos = start; pos < end;) { |
| 159 |
int n = readVarInt(data, pos); |
| 160 |
pos += getVarIntLength(data, pos); |
| 161 |
if (n < 2) { |
| 162 |
s += n; |
| 163 |
} else if (n > SPLIT_MANY) { |
| 164 |
s += getSize(n); |
| 165 |
} else if (n == SPLIT_MANY) { |
| 166 |
pos += getVarIntLength(data, pos); |
| 167 |
} |
| 168 |
} |
| 169 |
return s; |
| 170 |
} |
| 171 |
|
| 172 |
private static void writeSizeOffset(ByteStream out, int size, |
| 173 |
int offset) { |
| 174 |
writeVarInt(out, SIZE_OFFSETS[size] + offset); |
| 175 |
} |
| 176 |
|
| 177 |
private static int getOffset(int n, int size) { |
| 178 |
return n - SIZE_OFFSETS[size]; |
| 179 |
} |
| 180 |
|
| 181 |
private static int getSize(int n) { |
| 182 |
for (int i = 0; i < SIZE_OFFSETS.length; i++) { |
| 183 |
if (n < SIZE_OFFSETS[i]) { |
| 184 |
return i - 1; |
| 185 |
} |
| 186 |
} |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Generate the minimal perfect hash function data from the given list. |
| 192 |
* |
| 193 |
* @param list the data |
| 194 |
* @return the hash function description |
| 195 |
*/ |
| 196 |
public static byte[] generate(ArrayList<Integer> list) { |
| 197 |
ByteStream out = new ByteStream(); |
| 198 |
generate(list, 0, out); |
| 199 |
return out.toByteArray(); |
| 200 |
} |
| 201 |
|
| 202 |
private static void generate(ArrayList<Integer> list, int level, ByteStream out) { |
| 203 |
int size = list.size(); |
| 204 |
if (size <= 1) { |
| 205 |
out.write((byte) size); |
| 206 |
return; |
| 207 |
} |
| 208 |
if (level > 32) { |
| 209 |
throw new IllegalStateException("Too many recursions; " + |
| 210 |
" incorrect universal hash function?"); |
| 211 |
} |
| 212 |
if (size <= MAX_SIZE) { |
| 213 |
int maxOffset = MAX_OFFSETS[size]; |
| 214 |
int testSize = size; |
| 215 |
nextOffset: |
| 216 |
for (int offset = 0; offset < maxOffset; offset++) { |
| 217 |
int bits = 0; |
| 218 |
for (int i = 0; i < size; i++) { |
| 219 |
int x = list.get(i); |
| 220 |
int h = hash(x, level, offset, testSize); |
| 221 |
if ((bits & (1 << h)) != 0) { |
| 222 |
continue nextOffset; |
| 223 |
} |
| 224 |
bits |= 1 << h; |
| 225 |
} |
| 226 |
writeSizeOffset(out, size, offset); |
| 227 |
return; |
| 228 |
} |
| 229 |
} |
| 230 |
int split; |
| 231 |
if (size > 57 * DIVIDE) { |
| 232 |
split = size / (36 * DIVIDE); |
| 233 |
} else { |
| 234 |
split = (size - 47) / DIVIDE; |
| 235 |
} |
| 236 |
split = Math.max(2, split); |
| 237 |
ArrayList<ArrayList<Integer>> lists = new ArrayList<>(split); |
| 238 |
for (int i = 0; i < split; i++) { |
| 239 |
lists.add(new ArrayList<Integer>(size / split)); |
| 240 |
} |
| 241 |
for (int x : list) { |
| 242 |
ArrayList<Integer> l = lists.get(hash(x, level, 0, split)); |
| 243 |
l.add(x); |
| 244 |
} |
| 245 |
if (split >= SPLIT_MANY) { |
| 246 |
out.write((byte) SPLIT_MANY); |
| 247 |
} |
| 248 |
writeVarInt(out, split); |
| 249 |
list.clear(); |
| 250 |
list.trimToSize(); |
| 251 |
for (ArrayList<Integer> s2 : lists) { |
| 252 |
generate(s2, level + 1, out); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
private static int hash(int x, int level, int offset, int size) { |
| 257 |
x += level + offset * 32; |
| 258 |
x = ((x >>> 16) ^ x) * 0x45d9f3b; |
| 259 |
x = ((x >>> 16) ^ x) * 0x45d9f3b; |
| 260 |
x = (x >>> 16) ^ x; |
| 261 |
return Math.abs(x % size); |
| 262 |
} |
| 263 |
|
| 264 |
private static int writeVarInt(ByteStream out, int x) { |
| 265 |
int len = 0; |
| 266 |
while ((x & ~0x7f) != 0) { |
| 267 |
out.write((byte) (0x80 | (x & 0x7f))); |
| 268 |
x >>>= 7; |
| 269 |
len++; |
| 270 |
} |
| 271 |
out.write((byte) x); |
| 272 |
return ++len; |
| 273 |
} |
| 274 |
|
| 275 |
private static int readVarInt(byte[] d, int pos) { |
| 276 |
int x = d[pos++]; |
| 277 |
if (x >= 0) { |
| 278 |
return x; |
| 279 |
} |
| 280 |
x &= 0x7f; |
| 281 |
for (int s = 7; s < 64; s += 7) { |
| 282 |
int b = d[pos++]; |
| 283 |
x |= (b & 0x7f) << s; |
| 284 |
if (b >= 0) { |
| 285 |
break; |
| 286 |
} |
| 287 |
} |
| 288 |
return x; |
| 289 |
} |
| 290 |
|
| 291 |
private static int getVarIntLength(byte[] d, int pos) { |
| 292 |
int x = d[pos++]; |
| 293 |
if (x >= 0) { |
| 294 |
return 1; |
| 295 |
} |
| 296 |
int len = 2; |
| 297 |
for (int s = 7; s < 64; s += 7) { |
| 298 |
int b = d[pos++]; |
| 299 |
if (b >= 0) { |
| 300 |
break; |
| 301 |
} |
| 302 |
len++; |
| 303 |
} |
| 304 |
return len; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* A stream of bytes. |
| 309 |
*/ |
| 310 |
static class ByteStream { |
| 311 |
|
| 312 |
private byte[] data; |
| 313 |
private int pos; |
| 314 |
|
| 315 |
ByteStream() { |
| 316 |
this.data = new byte[16]; |
| 317 |
} |
| 318 |
|
| 319 |
ByteStream(byte[] data) { |
| 320 |
this.data = data; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Read a byte. |
| 325 |
* |
| 326 |
* @return the byte, or -1. |
| 327 |
*/ |
| 328 |
int read() { |
| 329 |
return pos < data.length ? (data[pos++] & 255) : -1; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Write a byte. |
| 334 |
* |
| 335 |
* @param value the byte |
| 336 |
*/ |
| 337 |
void write(byte value) { |
| 338 |
if (pos >= data.length) { |
| 339 |
data = Arrays.copyOf(data, data.length * 2); |
| 340 |
} |
| 341 |
data[pos++] = value; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Get the byte array. |
| 346 |
* |
| 347 |
* @return the byte array |
| 348 |
*/ |
| 349 |
byte[] toByteArray() { |
| 350 |
return Arrays.copyOf(data, pos); |
| 351 |
} |
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* A helper class for bit arrays. |
| 357 |
*/ |
| 358 |
public static class BitArray { |
| 359 |
|
| 360 |
/** |
| 361 |
* Set a bit in the array. |
| 362 |
* |
| 363 |
* @param data the array |
| 364 |
* @param x the bit index |
| 365 |
* @param value the new value |
| 366 |
* @return the bit array (if the passed one was too small) |
| 367 |
*/ |
| 368 |
public static byte[] setBit(byte[] data, int x, boolean value) { |
| 369 |
int pos = x / 8; |
| 370 |
if (pos >= data.length) { |
| 371 |
data = Arrays.copyOf(data, pos + 1); |
| 372 |
} |
| 373 |
if (value) { |
| 374 |
data[pos] |= 1 << (x & 7); |
| 375 |
} else { |
| 376 |
data[pos] &= 255 - (1 << (x & 7)); |
| 377 |
} |
| 378 |
return data; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Get a bit in a bit array. |
| 383 |
* |
| 384 |
* @param data the array |
| 385 |
* @param x the bit index |
| 386 |
* @return the value |
| 387 |
*/ |
| 388 |
public static boolean getBit(byte[] data, int x) { |
| 389 |
return (data[x / 8] & (1 << (x & 7))) != 0; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Count the number of set bits. |
| 394 |
* |
| 395 |
* @param data the array |
| 396 |
* @return the number of set bits |
| 397 |
*/ |
| 398 |
public static int countBits(byte[] data) { |
| 399 |
int count = 0; |
| 400 |
for (byte x : data) { |
| 401 |
count += Integer.bitCount(x & 255); |
| 402 |
} |
| 403 |
return count; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
} |
| 408 |
|
| 409 |
} |