Matzã«ã£ãã§åãä¸ãããã MurmurHash ã Java ã§æ¸ãã¦ã¿ãã
ãã¨ãã¨ã®ã½ã¼ã¹ã 㨠C ã® unsigned int ã§ããã·ã¥å¤ãè¨ç®ãã¦ããã®ã§ãJava ã§ã¯ long ã§è¨ç®ã
Java ã® int 㯠signed ã ããæ¡ãå¢ããããã ãã©ã
ãã®ãã㧠32 bit ãè¶ ããé¨åã«ã¤ãã¦ã¯ãã¹ã¯ãã¦ããå¿ è¦ãã§ã¦ãã¾ã£ãã
0xffffffffL ã§ãã¹ã¯ããã®ã¯ãªããä¸æ°å¥½ã ãé ãããªãâ¦ã
ããã¡ãã£ã¨ãªãã¨ããã¹ãããã
ä¸å¿ãC ã§è¨ç®ããã®ã¨åãããã·ã¥å¤ãè¿ãäºã¯ç¢ºèªæ¸ã¿ã
ãã¨ãã¨ã®ã½ã¼ã¹ã 㨠C ã® unsigned int ã§ããã·ã¥å¤ãè¨ç®ãã¦ããã®ã§ãJava ã§ã¯ long ã§è¨ç®ã
Java ã® int 㯠signed ã ããæ¡ãå¢ããããã ãã©ã
ãã®ãã㧠32 bit ãè¶ ããé¨åã«ã¤ãã¦ã¯ãã¹ã¯ãã¦ããå¿ è¦ãã§ã¦ãã¾ã£ãã
0xffffffffL ã§ãã¹ã¯ããã®ã¯ãªããä¸æ°å¥½ã ãé ãããªãâ¦ã
ããã¡ãã£ã¨ãªãã¨ããã¹ãããã
ä¸å¿ãC ã§è¨ç®ããã®ã¨åãããã·ã¥å¤ãè¿ãäºã¯ç¢ºèªæ¸ã¿ã
import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.ByteOrder; public class MurmurHash { /** * @param args * @throws UnsupportedEncodingException */ public static void main(String[] args) throws UnsupportedEncodingException { // TODO èªåçæãããã¡ã½ããã»ã¹ã¿ã //byte[] b = { 1, 2, 3, }; byte[] b = "de".getBytes("UTF-8"); System.out.printf("%08xn", computeHash(b, 0)); } public static long computeHash(byte[] b, long h) { final long m = 0x7fd652ad; final int r = 16; h += 0xdeadbeefL; ByteBuffer bb = ByteBuffer.wrap(b); bb.order(ByteOrder.LITTLE_ENDIAN); while (bb.remaining() >= 4) { h += 0xffffffffL & bb.getInt(); h *= m; h &= 0xffffffffL; h ^= h >> r; } switch (bb.remaining()) { case 3: h += (0xff & bb.get(bb.position() + 2)) << 16;
case 2: h += (0xff & bb.get(bb.position() + 1)) << 8;
case 1: h += (0xff & bb.get(bb.position())); h *= m; h &= 0xffffffffL; h ^= h >> r; } h *= m; h &= 0xffffffffL; h ^= h >> 10; h *= m; h &= 0xffffffffL; h ^= h >> 17; return h; } }