-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.go
48 lines (42 loc) · 799 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
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (C) linvon
* Date 2021/2/18 10:29
*/
package cuckoo
import "fmt"
const (
bitsPerByte = 8
bytesPerUint64 = 8
bytesPerUint32 = 4
)
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)
}
func maxLoadFactor(tagsPerBucket uint) float64 {
switch tagsPerBucket {
case 2:
return 0.85
case 4:
return 0.96
default:
return 0.99
}
}
func getBucketsFromHint(initialBucketsHint []byte, expectedLength uint) ([]byte, error) {
result := initialBucketsHint
if len(result) == 0 {
result = make([]byte, expectedLength)
}
if uint(len(result)) != expectedLength {
return nil, fmt.Errorf("buckets length should be %d but got %d", expectedLength, len(result))
}
return result, nil
}