-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIBF.h
More file actions
80 lines (65 loc) · 1.47 KB
/
IBF.h
File metadata and controls
80 lines (65 loc) · 1.47 KB
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
70
71
72
73
74
75
76
77
78
79
80
#ifndef IBF_H
#define IBF_H
#include "BloomFilter.h"
#include "tools.h"
class IBF{
BloomFilter *bf;
int m; // number of bits in bf
int k; // number of hash functions
int setNum; // number of sets
int codeLen; // number of bits in a code
public:
int mc_ins, mc_que;
IBF();
IBF(int _m, int _k, int s);
~IBF();
void insert(ELEMENT *elem);
int query(char *elem);
};
IBF::IBF(){
mc_ins = 0;
mc_que = 0;
}
IBF::IBF(int _m, int _k, int s){
mc_ins = 0;
mc_que = 0;
m = _m;
k = _k;
setNum = s;
codeLen = NearestLarger2Power(setNum);
bf = new BloomFilter(m);
}
IBF::~IBF(){
delete bf;
}
void IBF::insert(ELEMENT *elem){
unsigned int val = elem->category;
for(int i = 0; i < k; ++i)
{
int pos = hash_k(elem->val, i) % m;
unsigned char* tmpStr = bf->query(pos, codeLen);
for(int j = 0; j < codeLen; ++j)
if((val & ((unsigned int)1 << (codeLen - 1 - j))) != 0)
tmpStr[j / 8] |= 1 << (7 - (j % 8));
bf->set(pos, codeLen, tmpStr);
free(tmpStr);
mc_ins += numOfMemoryAccess(pos, codeLen);
}
}
int IBF::query(char *elem){
unsigned int ans = 0;
for(int i = 0; i < codeLen; ++i)
ans |= (unsigned int)1 << i;
for(int i = 0; i < k; ++i)
{
int pos = hash_k(elem, i) % m;
unsigned char* tmpStr = bf->query(pos, codeLen);
for(int j = 0; j < codeLen; ++j)
if((tmpStr[j / 8] & (1 << (7 - (j % 8)))) == 0)
ans &= ~((unsigned int)1 << (codeLen - 1 - j));
free(tmpStr);
mc_que += numOfMemoryAccess(pos, codeLen);
}
return (int)ans;
}
#endif