-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.fnv.go
74 lines (61 loc) · 1.33 KB
/
util.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package elio
import (
"fmt"
"unsafe"
)
// Fnv32 fnv32 type definition
type Fnv32 uint32
// FromUint32 from uint32
func (f *Fnv32) FromUint32(v uint32) {
*f = Fnv32(v)
}
// ToUint32 to uint32
func (f *Fnv32) ToUint32() uint32 {
return uint32(*f)
}
// FromString from string
func (f *Fnv32) FromString(k string) {
hash := uint32(2166136261)
const prime32 = uint32(16777619)
for i := 0; i < len(k); i++ {
hash *= prime32
hash ^= uint32(k[i])
}
f.FromUint32(hash)
}
// // Fnv32 fnv32
// func Fnv32(key string) uint32 {
// hash := uint32(2166136261)
// const prime32 = uint32(16777619)
// for i := 0; i < len(key); i++ {
// hash *= prime32
// hash ^= uint32(key[i])
// }
// return hash
// }
// Fnv64 fnv64 type definition
type Fnv64 uint64
// FromUint64 from uint64
func (f *Fnv64) FromUint64(v uint64) {
*f = Fnv64(v)
}
// ToUint64 to uint64
func (f *Fnv64) ToUint64() uint64 {
return uint64(*f)
}
// FromString from string
func (f *Fnv64) FromString(k string) {
hash := uint64(2166136261)
const prime64 = uint64(16777619)
for i := 0; i < len(k); i++ {
hash *= prime64
hash ^= uint64(k[i])
}
f.FromUint64(hash)
}
// FromPointer from pointer
func (f *Fnv64) FromPointer(p unsafe.Pointer) {
// TODO: pointer 가 짝수만 나와 문자열로 수정
//f.FromUint64(uint64(uintptr(p)))
f.FromString(fmt.Sprintf("%p", p))
}