pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/hash/jchash.go (about)

     1  package hash
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  // JCHash generates Google's Jump Consistent Hash
    11  // More info: http://arxiv.org/abs/1406.2294
    12  func JCHash(key uint64, buckets int) int {
    13  	var b, j int64
    14  
    15  	if buckets <= 0 {
    16  		buckets = 1
    17  	}
    18  
    19  	for j < int64(buckets) {
    20  		b = j
    21  		key = key*2862933555777941757 + 1
    22  		j = int64(float64(b+1) * (float64(int64(1)<<31) / float64((key>>33)+1)))
    23  	}
    24  
    25  	return int(b)
    26  }