-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbloom_filter.cpp
61 lines (49 loc) · 1.01 KB
/
bloom_filter.cpp
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
#include "bloom_filter.hpp"
//获取字符串布隆结果
bool BloomFilter::get(std::string name)
{
//字符串哈希
ULL index = hash(name) % BLOOM_SIZE;
//如果布隆过滤器中存在
if(!m_bloom_filter[index])
{
return false;
}
//不存在则不必访问数据库
return true;
}
//添加字符串至布隆
bool BloomFilter::add(std::string name)
{
//字符串哈希
ULL index = hash(name) % BLOOM_SIZE;
//添加到布隆过滤器
m_bloom_filter[index] = 1;
return true;
}
//哈希函数
BloomFilter::ULL BloomFilter::hash(std::string name)
{
ULL res = 0;
//字符串P进制求哈希值
for(auto c : name)
{
res = res * P + c;
}
return res;
}
//构造函数
BloomFilter::BloomFilter(){}
//析构函数
BloomFilter::~BloomFilter(){}
//初始化
void BloomFilter::init()
{
//每一个位置都为0
m_bloom_filter.reset();
}
//布隆位图
std::bitset<BloomFilter::BLOOM_SIZE>& BloomFilter::get()
{
return m_bloom_filter;
}