-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmurmurhash.go
More file actions
49 lines (43 loc) · 812 Bytes
/
Copy pathmurmurhash.go
File metadata and controls
49 lines (43 loc) · 812 Bytes
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
package stf
import (
"bytes"
"encoding/binary"
"log"
)
// Imeplementation of murmurHash (ver 1) in go
func MurmurHash(data []byte) uint32 {
const m uint32 = 0x5bd1e995
const r uint8 = 16
var length uint32 = uint32(len(data))
var h uint32 = length * m
nblocks := int(length / 4)
buf := bytes.NewBuffer(data)
for i := 0; i < nblocks; i++ {
var x uint32
err := binary.Read(buf, binary.LittleEndian, &x)
if err != nil {
log.Fatal("Failed to read from buffer")
}
h += x
h *= m
h ^= h >> r
}
tailIndex := nblocks * 4
switch length & 3 {
case 3:
h += uint32(data[tailIndex+2]) << 16
fallthrough
case 2:
h += uint32(data[tailIndex+1]) << 8
fallthrough
case 1:
h += uint32(data[tailIndex])
h *= m
h ^= h >> r
}
h *= m
h ^= h >> 10
h *= m
h ^= h >> 17
return h
}