当前位置:   article > 正文

最快的摘要算法XXHash算法的JAVA实现

xxhash

xxHash是一个非常快的哈希算法,能在限制速度的RAM上运行。 它成功完成了SMHasher测试套件,它可以评估散列函数的冲突,色散和随机性。 代码非常便于携带,所有平台上的散列都相同(小/大)。测试对比参考:https://blog.csdn.net/tianshan2010/article/details/115775292

  1. public class XXHash {
  2. private static final int PRIME1 = (int) 2654435761L;
  3. private static final int PRIME2 = (int) 2246822519L;
  4. private static final int PRIME3 = (int) 3266489917L;
  5. private static final int PRIME4 = 668265263;
  6. private static final int PRIME5 = 0x165667b1;
  7. public static int digestSmall(byte[] data, int seed, boolean bigendian) {
  8. final EndianReader er = bigendian ? EncodeUtils.BEReader : EncodeUtils.LEReader;
  9. final int len = data.length;
  10. final int bEnd = len;
  11. final int limit = bEnd - 4;
  12. int idx = seed + PRIME1;
  13. int crc = PRIME5;
  14. int i = 0;
  15. while (i < limit) {
  16. crc += er.toInt(data, i) + (idx++);
  17. crc += Integer.rotateLeft(crc, 17) * PRIME4;
  18. crc *= PRIME1;
  19. i += 4;
  20. }
  21. while (i < bEnd) {
  22. crc += (data[i] & 0xFF) + (idx++);
  23. crc *= PRIME1;
  24. i++;
  25. }
  26. crc += len;
  27. crc ^= crc >>> 15;
  28. crc *= PRIME2;
  29. crc ^= crc >>> 13;
  30. crc *= PRIME3;
  31. crc ^= crc >>> 16;
  32. return crc;
  33. }
  34. public static int digestFast32(byte[] data, int seed, boolean bigendian) {
  35. final int len = data.length;
  36. if (len < 16) {
  37. return digestSmall(data, seed, bigendian);
  38. }
  39. final EndianReader er = bigendian ? EncodeUtils.BEReader : EncodeUtils.LEReader;
  40. final int bEnd = len;
  41. final int limit = bEnd - 16;
  42. int v1 = seed + PRIME1;
  43. int v2 &
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/891033
推荐阅读
相关标签
  

闽ICP备14008679号