File tree 1 file changed +13
-6
lines changed
1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -3,26 +3,33 @@ package bloom_filter_code
3
3
import (
4
4
"github.com/spaolacci/murmur3"
5
5
"github.com/willf/bitset"
6
+ "math"
6
7
)
7
8
8
- //计算最佳的配置
9
- //https://hur.st/bloomfilter/
10
- //n代表元素的个数
11
- //p代表假阳率
12
- //m代表位图长度
13
- //k代表hash函数的个数
14
9
type BloomFilter struct {
15
10
m uint64 //数组集合大小
16
11
k uint32 //hash函数个数
17
12
b * bitset.BitSet
18
13
}
19
14
15
+ //m代表位图长度
16
+ //k代表hash函数的个数
20
17
func New (m uint64 , k uint32 ) * BloomFilter {
21
18
return & BloomFilter {
22
19
m , k , bitset .New (uint (m )),
23
20
}
24
21
}
25
22
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
+
26
33
func (f * BloomFilter ) Add (data []byte ) {
27
34
for i := uint32 (0 ); i < f .k ; i ++ {
28
35
f .b .Set (f .locate (data , i ))
You can’t perform that action at this time.
0 commit comments