/
util_hash.go
77 lines (67 loc) · 1.8 KB
/
util_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
67
68
69
70
71
72
73
74
75
76
77
package hashmap
import (
"reflect"
"unsafe"
"github.com/cespare/xxhash"
)
func (m *HashMap[Key, Value]) stringHasher(key Key) uintptr {
sh := (*reflect.StringHeader)(unsafe.Pointer(&key))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
}
buf := *(*[]byte)(unsafe.Pointer(&bh))
return uintptr(xxhash.Sum64(buf))
}
func (m *HashMap[Key, Value]) uintptrHasher(key Key) uintptr {
bh := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: intSizeBytes,
}
buf := *(*[]byte)(unsafe.Pointer(&bh))
return uintptr(xxhash.Sum64(buf))
}
func (m *HashMap[Key, Value]) byteHasher(key Key) uintptr {
bh := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: 1,
}
buf := *(*[]byte)(unsafe.Pointer(&bh))
return uintptr(xxhash.Sum64(buf))
}
func (m *HashMap[Key, Value]) wordHasher(key Key) uintptr {
bh := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: 2,
}
buf := *(*[]byte)(unsafe.Pointer(&bh))
return uintptr(xxhash.Sum64(buf))
}
func (m *HashMap[Key, Value]) dwordHasher(key Key) uintptr {
bh := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: 4,
}
buf := *(*[]byte)(unsafe.Pointer(&bh))
return uintptr(xxhash.Sum64(buf))
}
func (m *HashMap[Key, Value]) qwordHasher(key Key) uintptr {
bh := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: 8,
}
buf := *(*[]byte)(unsafe.Pointer(&bh))
return uintptr(xxhash.Sum64(buf))
}
func (m *HashMap[Key, Value]) owordHasher(key Key) uintptr {
bh := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: 16,
}
buf := *(*[]byte)(unsafe.Pointer(&bh))
return uintptr(xxhash.Sum64(buf))
}
// used in unit test to test collision support.
func (m *HashMap[Key, Value]) staticHasher(key Key) uintptr {
return 4 // chosen by fair dice roll. guaranteed to be random.
}