-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbloom_filter.py
37 lines (29 loc) · 1011 Bytes
/
bloom_filter.py
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
from bitarray import *
class bloom_filter:
def __init__(self, size):
self.size = size
self.first_filter, self.second_filter = bitarray(size), bitarray(size)
self.first_filter.setall(0)
self.second_filter.setall(0)
def set_bit(self, filter_no, indices):
for idx in indices:
if filter_no == 1:
self.first_filter[int(idx)] = 1
else:
self.second_filter[int(idx)] = 1
def look_up(self, filter_no, indices):
for idx in indices:
if filter_no == 1 and not self.first_filter[int(idx)]:
return False
if filter_no == 2 and not self.second_filter[int(idx)]:
return False
return True
def display(self):
print(self.first_filter)
print(self.second_filter)
if __name__ == "__main__":
bf = bloom_filter(20)
bf.display()
bf.set_bit(1, [1,5,6,7,3,15])
bf.display()
print(bf.look_up(1, [1,5,6,7,3,15]))