-
Notifications
You must be signed in to change notification settings - Fork 5
/
hashfunc.go
76 lines (67 loc) · 1.72 KB
/
hashfunc.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
package hashmap
import (
"reflect"
"strconv"
"bytes"
"fmt"
)
func writeValue(buf *bytes.Buffer, val reflect.Value) {
switch val.Kind() {
case reflect.String:
buf.WriteByte('"')
buf.WriteString(val.String())
buf.WriteByte('"')
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
buf.WriteString(strconv.FormatInt(val.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
buf.WriteString(strconv.FormatUint(val.Uint(), 10))
case reflect.Float32, reflect.Float64:
buf.WriteString(strconv.FormatFloat(val.Float(), 'E', -1, 64))
case reflect.Bool:
if val.Bool() {
buf.WriteByte('t')
} else {
buf.WriteByte('f')
}
case reflect.Ptr:
if !val.IsNil() || val.Type().Elem().Kind() == reflect.Struct {
writeValue(buf, reflect.Indirect(val))
} else {
writeValue(buf, reflect.Zero(val.Type().Elem()))
}
case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct, reflect.Interface:
buf.WriteString(fmt.Sprintf("%#v", val))
default:
_, err := buf.WriteString(val.String())
if err != nil {
panic(fmt.Errorf("unsupported type %T", val))
}
}
}
// Hash function, return bucket index
func hashFunc(blockSize int, key Key) (hashKey uint, bucketIdx uint) {
var buf bytes.Buffer
writeValue(&buf, reflect.ValueOf(key))
h := djb2Hash(&buf)
//h := jenkinsHash(&buf)
return h, (h % uint(blockSize))
}
func djb2Hash(buf *bytes.Buffer) uint {
var h uint = 5381
for _, r := range buf.Bytes() {
h = (h << 5) + h + uint(r)
}
return h
}
func jenkinsHash(buf *bytes.Buffer) uint {
var h uint
for _, c := range buf.Bytes() {
h += uint(c)
h += (h << 10)
h ^= (h >> 6)
}
h += (h << 3)
h ^= (h >> 11)
h += (h << 15)
return h
}