/
fnv.go
57 lines (48 loc) · 1.4 KB
/
fnv.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/prometheus/common/model/fnv.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Prometheus Authors.
package mimirpb
// Inline and byte-free variant of hash/fnv's fnv64a.
const (
offset32 = 2166136261
prime32 = 16777619
)
// HashNew32 initializes a new fnv32 hash value.
func HashNew32() uint32 {
return offset32
}
// HashAdd32 adds a string to an fnv32 hash value, returning the updated hash.
// Note this is the same algorithm as Go stdlib `sum32.Write()`
func HashAdd32(h uint32, s string) uint32 {
for i := 0; i < len(s); i++ {
h *= prime32
h ^= uint32(s[i])
}
return h
}
// HashAddByte32 adds a byte to an fnv32 hash value, returning the updated hash.
func HashAddByte32(h uint32, b byte) uint32 {
h *= prime32
h ^= uint32(b)
return h
}
// HashNew32a initializies a new fnv32a hash value.
func HashNew32a() uint32 {
return offset32
}
// HashAdd32a adds a string to an fnv32a hash value, returning the updated hash.
// Note this is the same algorithm as Go stdlib `sum32.Write()`
func HashAdd32a(h uint32, s string) uint32 {
for i := 0; i < len(s); i++ {
h ^= uint32(s[i])
h *= prime32
}
return h
}
// HashAddByte32a adds a byte to an fnv32a hash value, returning the updated hash.
func HashAddByte32a(h uint32, b byte) uint32 {
h ^= uint32(b)
h *= prime32
return h
}