forked from seiflotfy/cuckoofilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
36 lines (31 loc) · 762 Bytes
/
util.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
package cuckoo
import (
metro "github.com/dgryski/go-metro"
)
func getAltIndex(fp byte, i uint, numBuckets uint) uint {
hash := uint(metro.Hash64([]byte{fp}, 1337))
return (i ^ hash) % numBuckets
}
func getFingerprint(data []byte) byte {
fp := byte(metro.Hash64(data, 1335)%255 + 1)
return fp
}
// getIndicesAndFingerprint returns the 2 bucket indices and fingerprint to be used
func getIndicesAndFingerprint(data []byte, numBuckets uint) (uint, uint, byte) {
hash := metro.Hash64(data, 1337)
f := getFingerprint(data)
i1 := uint(hash) % numBuckets
i2 := getAltIndex(f, i1, numBuckets)
return i1, i2, f
}
func getNextPow2(n uint64) uint {
n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n |= n >> 32
n++
return uint(n)
}