algorithm-in-python/search/bloomFilter.py

40 lines
1.0 KiB
Python
Raw Normal View History

2018-12-11 15:57:58 +08:00
''' mbinary
#########################################################################
# File : bloomFilter.py
# Author: mbinary
# Mail: zhuheqin1@gmail.com
2019-01-31 12:09:46 +08:00
# Blog: https://mbinary.xyz
2018-12-11 15:57:58 +08:00
# Github: https://github.com/mbinary
# Created Time: 2018-10-17 11:19
# Description:
#########################################################################
'''
from bitarray import bitarray
import mmh3
2020-04-15 12:28:20 +08:00
class bloomFilter(set):
2020-04-15 12:28:20 +08:00
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
2020-04-15 12:28:20 +08:00
def __len__(self):
return self.size
2020-04-15 12:28:20 +08:00
def __iter__(self):
return iter(self.bits)
2020-04-15 12:28:20 +08:00
def add(self, item):
for i in range(self.hash_count):
2020-04-15 12:28:20 +08:00
idx = mmh3.hash(item, i) % self.size
self.bits[idx] = 1
return self
2020-04-15 12:28:20 +08:00
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])