Skip to content

Commit 41d2331

Browse files
committed
添加自动计算最佳m和k的配置
1 parent 3b99b05 commit 41d2331

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

bloom_filter_code/bloom.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,33 @@ package bloom_filter_code
33
import (
44
"github.com/spaolacci/murmur3"
55
"github.com/willf/bitset"
6+
"math"
67
)
78

8-
//计算最佳的配置
9-
//https://hur.st/bloomfilter/
10-
//n代表元素的个数
11-
//p代表假阳率
12-
//m代表位图长度
13-
//k代表hash函数的个数
149
type BloomFilter struct {
1510
m uint64 //数组集合大小
1611
k uint32 //hash函数个数
1712
b *bitset.BitSet
1813
}
1914

15+
//m代表位图长度
16+
//k代表hash函数的个数
2017
func New(m uint64, k uint32) *BloomFilter {
2118
return &BloomFilter{
2219
m, k, bitset.New(uint(m)),
2320
}
2421
}
2522

23+
//计算最佳的配置
24+
//https://hur.st/bloomfilter/
25+
//n代表最多个不同元素
26+
//p代表假阳率
27+
//m代表位图长度
28+
//k代表hash函数的个数
29+
func NewWithExpected(n uint, p float64) *BloomFilter {
30+
return New(uint64(math.Ceil(-1*float64(n)*math.Log(p)/math.Pow(math.Log(2), 2))), uint32(math.Ceil(math.Log(2)*float64(n)/float64(n))))
31+
}
32+
2633
func (f *BloomFilter) Add(data []byte) {
2734
for i := uint32(0); i < f.k; i++ {
2835
f.b.Set(f.locate(data, i))

0 commit comments

Comments
 (0)