jump.go 586 B

1234567891011121314151617181920212223
  1. // Package jump implements Google's Jump Consistent Hash
  2. /*
  3. From the paper "A Fast, Minimal Memory, Consistent Hash Algorithm" by John Lamping, Eric Veach (2014).
  4. http://arxiv.org/abs/1406.2294
  5. */
  6. package jump
  7. // Hash consistently chooses a hash bucket number in the range [0, numBuckets) for the given key. numBuckets must be >= 1.
  8. func Hash(key uint64, numBuckets int) int32 {
  9. var b int64 = -1
  10. var j int64
  11. for j < int64(numBuckets) {
  12. b = j
  13. key = key*2862933555777941757 + 1
  14. j = int64(float64(b+1) * (float64(int64(1)<<31) / float64((key>>33)+1)))
  15. }
  16. return int32(b)
  17. }