Bloom Filter 实验室
24 bit · 3个哈希函数 · 允许误判2 个元素选择要插入的字符串
位数组状态蓝色表示被已有元素置1;带强调边框的格子是当前查询检查的位置。
查询哪个元素?
h1 = 13h2 = 4h3 = 7
过滤器:肯定不存在真实集合:没有插入,正确排除。
python
import math
import hashlib
class BloomFilter:
def __init__(self, capacity, error_rate=0.01):
if capacity <= 0:
raise ValueError("capacity must be positive")
if not 0 < error_rate < 1:
raise ValueError("invalid error rate")
self.capacity = capacity
self.error_rate = error_rate
self.m = math.ceil(
-capacity * math.log(error_rate)
/ math.log(2) ** 2
)
self.k = max(
1,
round(self.m / capacity * math.log(2))
)
self.bits = bytearray((self.m + 7) // 8)
self.count = 0
def _hashes(self, key):
data = key.encode("utf-8")
digest = hashlib.sha256(data).digest()
h1 = int.from_bytes(digest[:8], "big")
h2 = int.from_bytes(digest[8:16], "big")
h2 = h2 or 1
for i in range(self.k):
yield (h1 + i * h2) % self.m
def add(self, key):
for pos in self._hashes(key):
byte_index = pos // 8
bit_index = pos % 8
self.bits[byte_index] |= (1 << bit_index)
self.count += 1
def might_contain(self, key):
for pos in self._hashes(key):
byte_index = pos // 8
bit_index = pos % 8
if not (
self.bits[byte_index] & (1 << bit_index)
):
return False
return True