jenkins.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Straight copy from:
  3. * https://stackoverflow.com/questions/23436358/bob-jenkins-hash-getting-bad-performance
  4. *
  5. * @license undefined
  6. */
  7. package bloom
  8. type jenkinsHash struct {
  9. }
  10. // adapted from http://bretmulvey.com/hash/7.html
  11. func (_ jenkinsHash) ComputeHash(data []byte) (uint64, error) {
  12. var a, b, c uint64
  13. a, b = 0x9e3779b9, 0x9e3779b9
  14. c = 0
  15. i := 0
  16. for i = 0; i < len(data)-12; {
  17. a += uint64(data[i]) | uint64(data[i+1]<<8) | uint64(data[i+2]<<16) | uint64(data[i+3]<<24)
  18. i += 4
  19. b += uint64(data[i]) | uint64(data[i+1]<<8) | uint64(data[i+2]<<16) | uint64(data[i+3]<<24)
  20. i += 4
  21. c += uint64(data[i]) | uint64(data[i+1]<<8) | uint64(data[i+2]<<16) | uint64(data[i+3]<<24)
  22. a, b, c = mix(a, b, c)
  23. }
  24. c += uint64(len(data))
  25. if i < len(data) {
  26. a += uint64(data[i])
  27. i++
  28. }
  29. if i < len(data) {
  30. a += uint64(data[i]) << 8
  31. i++
  32. }
  33. if i < len(data) {
  34. a += uint64(data[i]) << 16
  35. i++
  36. }
  37. if i < len(data) {
  38. a += uint64(data[i]) << 24
  39. i++
  40. }
  41. if i < len(data) {
  42. b += uint64(data[i])
  43. i++
  44. }
  45. if i < len(data) {
  46. b += uint64(data[i]) << 8
  47. i++
  48. }
  49. if i < len(data) {
  50. b += uint64(data[i]) << 16
  51. i++
  52. }
  53. if i < len(data) {
  54. b += uint64(data[i]) << 24
  55. i++
  56. }
  57. if i < len(data) {
  58. c += uint64(data[i]) << 8
  59. i++
  60. }
  61. if i < len(data) {
  62. c += uint64(data[i]) << 16
  63. i++
  64. }
  65. if i < len(data) {
  66. c += uint64(data[i]) << 24
  67. i++
  68. }
  69. a, b, c = mix(a, b, c)
  70. return c, nil
  71. }
  72. func mix(a, b, c uint64) (uint64, uint64, uint64) {
  73. a -= b
  74. a -= c
  75. a ^= (c >> 13)
  76. b -= c
  77. b -= a
  78. b ^= (a << 8)
  79. c -= a
  80. c -= b
  81. c ^= (b >> 13)
  82. a -= b
  83. a -= c
  84. a ^= (c >> 12)
  85. b -= c
  86. b -= a
  87. b ^= (a << 16)
  88. c -= a
  89. c -= b
  90. c ^= (b >> 5)
  91. a -= b
  92. a -= c
  93. a ^= (c >> 3)
  94. b -= c
  95. b -= a
  96. b ^= (a << 10)
  97. c -= a
  98. c -= b
  99. c ^= (b >> 15)
  100. return a, b, c
  101. }