Debian Codegraph @

util.go

raw
1 package cuckoo
2
3 import (
4 metro "github.com/dgryski/go-metro"
5 )
6
7 var (
8 altHash = [256]uint{}
9 masks = [65]uint{}
10 )
11
12 func init() {
13 for i := 0; i < 256; i++ {
14 altHash[i] = (uint(metro.Hash64([]byte{byte(i)}, 1337)))
15 }
16 for i := uint(0); i <= 64; i++ {
17 masks[i] = (1 << i) - 1
18 }
19 }
20
21 func getAltIndex(fp fingerprint, i uint, bucketPow uint) uint {
22 mask := masks[bucketPow]
23 hash := altHash[fp] & mask
24 return (i & mask) ^ hash
25 }
26
27 func getFingerprint(hash uint64) byte {
28 // Use least significant bits for fingerprint.
29 fp := byte(hash%255 + 1)
30 return fp
31 }
32
33 // getIndicesAndFingerprint returns the 2 bucket indices and fingerprint to be used
34 func getIndexAndFingerprint(data []byte, bucketPow uint) (uint, fingerprint) {
35 hash := metro.Hash64(data, 1337)
36 fp := getFingerprint(hash)
37 // Use most significant bits for deriving index.
38 i1 := uint(hash>>32) & masks[bucketPow]
39 return i1, fingerprint(fp)
40 }
41
42 func getNextPow2(n uint64) uint {
43 n--
44 n |= n >> 1
45 n |= n >> 2
46 n |= n >> 4
47 n |= n >> 8
48 n |= n >> 16
49 n |= n >> 32
50 n++
51 return uint(n)
52 }