github.com/fufuok/balancer@v1.0.0/internal/go-jump/jump.go (about)

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