-
Notifications
You must be signed in to change notification settings - Fork 3
/
hash.go
66 lines (51 loc) · 1.19 KB
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package shardmap
type HashFn[K comparable] func(k K) uint64
func HashBytes(b []byte) uint64 {
var hash uint64 = 5381 //magic constant, apparently this hash fewest collisions possible.
for _, chr := range b {
hash = ((hash << 5) + hash) + uint64(chr)
}
return hash
}
func HashString(s string) uint64 {
var hash uint64 = 5381 //magic constant, apparently this hash fewest collisions possible.
for _, chr := range s {
hash = ((hash << 5) + hash) + uint64(chr)
}
return hash
}
func HashUint32(u uint32) uint64 {
return HashUint64(uint64(u))
}
func HashUint16(u uint16) uint64 {
return HashUint64(uint64(u))
}
func HashUint8(u uint8) uint64 {
return HashUint64(uint64(u))
}
func HashInt64(i int64) uint64 {
return HashUint64(uint64(i))
}
func HashInt32(i int32) uint64 {
return HashUint64(uint64(i))
}
func HashInt16(i int16) uint64 {
return HashUint64(uint64(i))
}
func HashInt8(i int8) uint64 {
return HashUint64(uint64(i))
}
func HashInt(i int) uint64 {
return HashUint64(uint64(i))
}
func HashUint(i uint) uint64 {
return HashUint64(uint64(i))
}
func HashUint64(u uint64) uint64 {
u ^= u >> 33
u *= 0xff51afd7ed558ccd
u ^= u >> 33
u *= 0xc4ceb9fe1a85ec53
u ^= u >> 33
return u
}