-
Notifications
You must be signed in to change notification settings - Fork 8
/
key.go
90 lines (72 loc) · 1.7 KB
/
key.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
78
79
80
81
82
83
84
85
86
87
88
89
90
package ttlcache
import (
"fmt"
"hash/crc64"
"sync"
)
var hashPool = sync.Pool{
New: func() interface{} {
return crc64.MakeTable(crc64.ISO)
},
}
// IntKey creates key from int value.
func IntKey(k int) uint64 {
return uint64(k)
}
// ByteKey creates key from byte value.
func ByteKey(k byte) uint64 {
return uint64(k)
}
// Int8Key creates key from int8 value.
func Int8Key(k int8) uint64 {
return uint64(k)
}
// Uint8Key creates key from uint8 value.
func Uint8Key(k uint8) uint64 {
return uint64(k)
}
// Int16Key creates key from int16 value.
func Int16Key(k int16) uint64 {
return uint64(k)
}
// Uint16Key creates key from uint16 value.
func Uint16Key(k uint16) uint64 {
return uint64(k)
}
// Int32Key creates key from int32 value.
func Int32Key(k int32) uint64 {
return uint64(k)
}
// Uint32Key creates key from uint32 value.
func Uint32Key(k uint32) uint64 {
return uint64(k)
}
// Int64Key creates key from int64 value.
func Int64Key(k int64) uint64 {
return uint64(k)
}
// Uint64Key creates key from uint64 value.
func Uint64Key(k uint64) uint64 {
return uint64(k)
}
// BytesKey creates key from slice of bytes value.
func BytesKey(k []byte) uint64 {
return newKeyFromBytes(k)
}
// StringKey creates key from string value.
func StringKey(k string) uint64 {
return newKeyFromBytes([]byte(k))
}
// AnyKey creates key from anything.
// Should not be used for large datasets.
// For complex keys you can write your own hashing implementation.
func AnyKey(k interface{}) uint64 {
s := fmt.Sprintf("%#v", k)
return newKeyFromBytes([]byte(s))
}
func newKeyFromBytes(k []byte) uint64 {
hashTable := hashPool.Get().(*crc64.Table)
hash := crc64.Checksum(k, hashTable)
hashPool.Put(hashTable)
return hash
}