forked from seiflotfy/cuckoofilter
-
Notifications
You must be signed in to change notification settings - Fork 13
/
bucket.go
69 lines (60 loc) · 1.35 KB
/
bucket.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
package cuckoo
import (
"bytes"
"fmt"
)
// fingerprint represents a single entry in a bucket.
type fingerprint uint16
// bucket keeps track of fingerprints hashing to the same index.
type bucket [bucketSize]fingerprint
const (
nullFp = 0
bucketSize = 4
fingerprintSizeBits = 16
maxFingerprint = (1 << fingerprintSizeBits) - 1
)
// insert a fingerprint into a bucket. Returns true if there was enough space and insertion succeeded.
// Note it allows inserting the same fingerprint multiple times.
func (b *bucket) insert(fp fingerprint) bool {
for i, tfp := range b {
if tfp == nullFp {
b[i] = fp
return true
}
}
return false
}
// delete a fingerprint from a bucket.
// Returns true if the fingerprint was present and successfully removed.
func (b *bucket) delete(fp fingerprint) bool {
for i, tfp := range b {
if tfp == fp {
b[i] = nullFp
return true
}
}
return false
}
func (b *bucket) contains(needle fingerprint) bool {
for _, fp := range b {
if fp == needle {
return true
}
}
return false
}
// reset deletes all fingerprints in the bucket.
func (b *bucket) reset() {
for i := range b {
b[i] = nullFp
}
}
func (b *bucket) String() string {
var buf bytes.Buffer
buf.WriteString("[")
for _, by := range b {
buf.WriteString(fmt.Sprintf("%5d ", by))
}
buf.WriteString("]")
return buf.String()
}