github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/hash/go_hash.go (about)

     1  package hash
     2  
     3  // References:
     4  // https://colobu.com/2022/12/21/use-the-builtin-map-hasher/
     5  
     6  import (
     7  	"math"
     8  )
     9  
    10  func HashInt64(i int64) uint64 {
    11  	return uint64(i)
    12  }
    13  
    14  func HashFloat64(f float64) uint64 {
    15  	return math.Float64bits(f)
    16  }
    17  
    18  // HashString Using FNV-1a hash algorithm
    19  func HashString(str string) uint64 {
    20  	var hash uint64 = 14695981039346656037 // offset
    21  	for i := 0; i < len(str); i++ {
    22  		hash ^= uint64(str[i])
    23  		hash *= 1099511628211
    24  	}
    25  	return hash
    26  }