1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| public class MurmurHash {
private static final int CHUNK_SIZE = 4;
private static final int C1 = 0xcc9e2d51; private static final int C2 = 0x1b873593;
private static int getIntLittleEndian(byte[] input, int offset) { return input[offset + 3]& 0xff << 24 | input[offset + 2]& 0xff << 16 | input[offset + 1] & 0xff << 8 |input[offset] & 0xff; }
private static int mixK(int k) { k *= C1; k = Integer.rotateLeft(k, 15); k *= C2; return k; }
private static int mixHash(int h, int k) { h ^= k; h = Integer.rotateLeft(h, 13); h = h * 5 + 0xe6546b64; return h; }
private static int toInt(byte value){ return value & 0xff; }
private static int fmix(int hash, int length) { hash ^= length; hash ^= hash >>> 16; hash *= 0x85ebca6b; hash ^= hash >>> 13; hash *= 0xc2b2ae35; hash ^= hash >>> 16; return hash; }
public static int murmurhash3_x86_32(byte[] key,int offset, int len,int seed){ int hash= seed; int i; for (i = 0; i + CHUNK_SIZE <= len; i += CHUNK_SIZE) { int k= mixK(getIntLittleEndian(key,offset+i)); hash = mixHash(hash, k); } int k1 = 0; for (int shift = 0; i < len; i++, shift += 8) { k1 ^= toInt(key[offset + i]) << shift; } hash ^= mixK(k1); return fmix(hash, len); }
public static int murmurhash3_x86_32_Str(int seed, String key, Charset charset){ byte[] data = key.getBytes(charset); return murmurhash3_x86_32(data,0,data.length,seed); } }
|