-
Notifications
You must be signed in to change notification settings - Fork 88
/
bloomFilter.py
39 lines (32 loc) · 1.04 KB
/
bloomFilter.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
38
39
''' mbinary
#########################################################################
# File : bloomFilter.py
# Author: mbinary
# Mail: [email protected]
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-10-17 11:19
# Description:
#########################################################################
'''
from bitarray import bitarray
import mmh3
class bloomFilter(set):
def __init__(self, size, hash_count):
super(bloomFilter, self).__init__()
self.bits = bitarray(size)
self.bits.setall(0)
self.size = size
self.hash_count = hash_count
def __len__(self):
return self.size
def __iter__(self):
return iter(self.bits)
def add(self, item):
for i in range(self.hash_count):
idx = mmh3.hash(item, i) % self.size
self.bits[idx] = 1
return self
def __contains__(self, item):
idxs = [mmh3.hash(item, i) % self.size for i in range(self.hash_count)]
return all([self.bits[i] == 1 for i in idxs])