Last active
March 1, 2017 15:02
-
-
Save jac18281828/dd30e0718e918ca04f3540961f6d1e78 to your computer and use it in GitHub Desktop.
Murmur2 in Java - Tweaker Edition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Murmur2 { | |
public static final int BYTE_BITMASK = 0xff; | |
public static int MURMUR2_SEED1 = 0xe17a1465; | |
public static int MURMUR2_SEED2 = 0x9747b28c; | |
public static int MURMUR2_SEED3 = MURMUR2_SEED1*7; | |
private int defaultSeed = MURMUR2_SEED1; | |
public Murmur2(final int defaultSeed){ | |
this.defaultSeed = defaultSeed; | |
} | |
public int hash(final byte[] bytes) { | |
return hash(bytes, defaultSeed); | |
} | |
public int hash(byte[] bytes, int startIndex, int endIndex) { | |
return hash(bytes, startIndex, endIndex, defaultSeed); | |
} | |
public int hash(final byte[] bytes, final int seed) { | |
return hash(bytes, 0, bytes.length, seed); | |
} | |
public int hash(final byte[] bytes, final int startIndex, final int endIndex, final int seed) { | |
// hash64 is faster than 32bit on 64bit hardware | |
final long h = hash64(bytes, startIndex, endIndex, seed); | |
return (int)(h ^ (h>>32)); | |
} | |
/** | |
* Generates 64 bit hash from byte array with given start/end indices and seed. | |
* | |
* @param data byte array containing the data to hash | |
* @param startIndex of the portion of data to hash | |
* @param endIndex of the portion of data to hash | |
* @param seed initial seed value | |
* @return 64 bit hash of the given portion of the given data | |
*/ | |
public static long hash64(final byte[] data, final int startIndex, final int endIndex, final int seed) { | |
final long m = 0xc6a4a7935bd1e995L; | |
final int r = 47; | |
final int length = endIndex-startIndex; | |
long h = (seed&0xffffffffl)^(length*m); | |
final int length8 = length>>3; | |
for (int i=0; i<length8; i++) { | |
final int i8 = i<<3; | |
long k = (long)(data[startIndex+i8] & BYTE_BITMASK); | |
for(int j=1; j<8; j++) { | |
k ^= (long)(data[startIndex+i8+j] & BYTE_BITMASK) << j*8; | |
} | |
k *= m; | |
k ^= k >>> r; | |
k *= m; | |
h ^= k; | |
h *= m; | |
} | |
int lDx = (length&~7); | |
int blockIndex = 0; | |
while(lDx < length) { | |
h ^= (long)(data[startIndex+lDx++] & BYTE_BITMASK) << blockIndex++*8; | |
} | |
h*=m; | |
h ^= h >>> r; | |
h *= m; | |
h ^= h >>> r; | |
return h; | |
} | |
/** | |
* Generates 64 bit hash from byte array with given seed value. | |
* | |
* @param data byte array to hash | |
* @param length length of the array to hash | |
* @param seed initial seed value | |
* @return 64 bit hash of the given array | |
*/ | |
public static long hash64(final byte[] data, final int length, final int seed) { | |
return hash64(data, 0, length, seed); | |
} | |
/** | |
* Generates 64 bit hash from byte array with default seed value. | |
* | |
* @param data byte array to hash | |
* @param length length of the array to hash | |
* @return 64 bit hash of the given array | |
*/ | |
public static long hash64(final byte[] data, int length) { | |
return hash64(data, length, MURMUR2_SEED1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment