|
| 1 | +package dev.faststats.core; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.Contract; |
| 4 | + |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | + |
| 7 | +/** |
| 8 | + * Implementation of the MurmurHash3 128-bit hash algorithm. |
| 9 | + * <p> |
| 10 | + * MurmurHash is a non-cryptographic hash function suitable for general hash-based lookup. |
| 11 | + * It provides excellent distribution and performance while minimizing collisions. |
| 12 | + * </p> |
| 13 | + * <p> |
| 14 | + * This implementation follows the MurmurHash3_x64_128 variant as described at: |
| 15 | + * <a href="https://en.wikipedia.org/wiki/MurmurHash">https://en.wikipedia.org/wiki/MurmurHash</a> |
| 16 | + * </p> |
| 17 | + * <p> |
| 18 | + * Original algorithm by Austin Appleby. The name comes from the two elementary operations |
| 19 | + * it uses: multiply (MU) and rotate (R). |
| 20 | + * </p> |
| 21 | + */ |
| 22 | +final class MurmurHash3 { |
| 23 | + /** |
| 24 | + * Computes the 128-bit MurmurHash3 hash of the input string. |
| 25 | + * <p> |
| 26 | + * The string is encoded to UTF-8 bytes before hashing. The result is returned |
| 27 | + * as an array of two long values (64 bits each), combined they form a 128-bit hash. |
| 28 | + * </p> |
| 29 | + * |
| 30 | + * @param data the input string to hash |
| 31 | + * @return a 2-element array containing the lower 64 bits at index 0 and upper 64 bits at index 1 |
| 32 | + * @see <a href="https://en.wikipedia.org/wiki/MurmurHash">MurmurHash on Wikipedia</a> |
| 33 | + */ |
| 34 | + @Contract(value = "_ -> new", pure = true) |
| 35 | + public static long[] hash(String data) { |
| 36 | + var bytes = data.getBytes(StandardCharsets.UTF_8); |
| 37 | + var h1 = 0L; |
| 38 | + var h2 = 0L; |
| 39 | + final var c1 = 0x87c37b91114253d5L; |
| 40 | + final var c2 = 0x4cf5ad432745937fL; |
| 41 | + var length = bytes.length; |
| 42 | + var blocks = length / 16; |
| 43 | + |
| 44 | + // Process 128-bit blocks |
| 45 | + for (int i = 0; i < blocks; i++) { |
| 46 | + var k1 = getInt(bytes, i * 16); |
| 47 | + var k2 = getInt(bytes, i * 16 + 4); |
| 48 | + var k3 = getInt(bytes, i * 16 + 8); |
| 49 | + var k4 = getInt(bytes, i * 16 + 12); |
| 50 | + |
| 51 | + k1 *= (int) c1; |
| 52 | + k1 = Integer.rotateLeft(k1, 31); |
| 53 | + k1 *= (int) c2; |
| 54 | + h1 ^= k1; |
| 55 | + |
| 56 | + h1 = Long.rotateLeft(h1, 27); |
| 57 | + h1 += h2; |
| 58 | + h1 = h1 * 5 + 0x52dce729; |
| 59 | + |
| 60 | + k2 *= (int) c2; |
| 61 | + k2 = Integer.rotateLeft(k2, 33); |
| 62 | + k2 *= (int) c1; |
| 63 | + h2 ^= k2; |
| 64 | + |
| 65 | + h2 = Long.rotateLeft(h2, 31); |
| 66 | + h2 += h1; |
| 67 | + h2 = h2 * 5 + 0x38495ab5; |
| 68 | + } |
| 69 | + |
| 70 | + // Tail |
| 71 | + var k1 = 0; |
| 72 | + var k2 = 0; |
| 73 | + var k3 = 0; |
| 74 | + var k4 = 0; |
| 75 | + var tail = blocks * 16; |
| 76 | + |
| 77 | + switch (length & 15) { |
| 78 | + case 15: |
| 79 | + k4 ^= (bytes[tail + 14] & 0xff) << 16; |
| 80 | + case 14: |
| 81 | + k4 ^= (bytes[tail + 13] & 0xff) << 8; |
| 82 | + case 13: |
| 83 | + k4 ^= (bytes[tail + 12] & 0xff); |
| 84 | + k4 *= (int) c2; |
| 85 | + k4 = Integer.rotateLeft(k4, 33); |
| 86 | + k4 *= (int) c1; |
| 87 | + h2 ^= k4; |
| 88 | + case 12: |
| 89 | + k3 ^= (bytes[tail + 11] & 0xff) << 24; |
| 90 | + case 11: |
| 91 | + k3 ^= (bytes[tail + 10] & 0xff) << 16; |
| 92 | + case 10: |
| 93 | + k3 ^= (bytes[tail + 9] & 0xff) << 8; |
| 94 | + case 9: |
| 95 | + k3 ^= (bytes[tail + 8] & 0xff); |
| 96 | + k3 *= (int) c1; |
| 97 | + k3 = Integer.rotateLeft(k3, 31); |
| 98 | + k3 *= (int) c2; |
| 99 | + h1 ^= k3; |
| 100 | + case 8: |
| 101 | + k2 ^= (bytes[tail + 7] & 0xff) << 24; |
| 102 | + case 7: |
| 103 | + k2 ^= (bytes[tail + 6] & 0xff) << 16; |
| 104 | + case 6: |
| 105 | + k2 ^= (bytes[tail + 5] & 0xff) << 8; |
| 106 | + case 5: |
| 107 | + k2 ^= (bytes[tail + 4] & 0xff); |
| 108 | + k2 *= (int) c2; |
| 109 | + k2 = Integer.rotateLeft(k2, 33); |
| 110 | + k2 *= (int) c1; |
| 111 | + h2 ^= k2; |
| 112 | + case 4: |
| 113 | + k1 ^= (bytes[tail + 3] & 0xff) << 24; |
| 114 | + case 3: |
| 115 | + k1 ^= (bytes[tail + 2] & 0xff) << 16; |
| 116 | + case 2: |
| 117 | + k1 ^= (bytes[tail + 1] & 0xff) << 8; |
| 118 | + case 1: |
| 119 | + k1 ^= (bytes[tail] & 0xff); |
| 120 | + k1 *= (int) c1; |
| 121 | + k1 = Integer.rotateLeft(k1, 31); |
| 122 | + k1 *= (int) c2; |
| 123 | + h1 ^= k1; |
| 124 | + } |
| 125 | + |
| 126 | + // Finalization |
| 127 | + h1 ^= length; |
| 128 | + h2 ^= length; |
| 129 | + |
| 130 | + h1 += h2; |
| 131 | + h2 += h1; |
| 132 | + |
| 133 | + h1 = fmix64(h1); |
| 134 | + h2 = fmix64(h2); |
| 135 | + |
| 136 | + h1 += h2; |
| 137 | + h2 += h1; |
| 138 | + |
| 139 | + return new long[]{h1, h2}; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Finalization mix function to avalanche the bits in the hash. |
| 144 | + * <p> |
| 145 | + * This function improves the distribution of the hash by XORing and multiplying |
| 146 | + * with carefully chosen constants, ensuring that similar inputs produce very |
| 147 | + * different outputs (avalanche effect). |
| 148 | + * </p> |
| 149 | + * |
| 150 | + * @param k the 64-bit value to mix |
| 151 | + * @return the mixed 64-bit value |
| 152 | + * @see <a href="https://en.wikipedia.org/wiki/MurmurHash#Algorithm">MurmurHash Algorithm on Wikipedia</a> |
| 153 | + */ |
| 154 | + @Contract(pure = true) |
| 155 | + private static long fmix64(long k) { |
| 156 | + k ^= k >>> 33; |
| 157 | + k *= 0xff51afd7ed558ccdL; |
| 158 | + k ^= k >>> 33; |
| 159 | + k *= 0xc4ceb9fe1a85ec53L; |
| 160 | + k ^= k >>> 33; |
| 161 | + return k; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Reads a 32-bit little-endian integer from the byte array at the specified offset. |
| 166 | + * <p> |
| 167 | + * This helper method extracts four consecutive bytes and combines them into a |
| 168 | + * single integer using little-endian byte order. |
| 169 | + * </p> |
| 170 | + * |
| 171 | + * @param bytes the byte array to read from |
| 172 | + * @param offset the starting index in the byte array (must have at least 4 bytes from offset) |
| 173 | + * @return the 32-bit integer value read in little-endian order |
| 174 | + */ |
| 175 | + @Contract(pure = true) |
| 176 | + private static int getInt(byte[] bytes, int offset) { |
| 177 | + return (bytes[offset] & 0xff) | |
| 178 | + ((bytes[offset + 1] & 0xff) << 8) | |
| 179 | + ((bytes[offset + 2] & 0xff) << 16) | |
| 180 | + ((bytes[offset + 3] & 0xff) << 24); |
| 181 | + } |
| 182 | +} |
0 commit comments