added types

This commit is contained in:
Isidro Arias 2023-04-07 17:49:15 +02:00
parent 2fd71965f1
commit 314237d94e

View File

@ -72,14 +72,14 @@ class Bloom:
self.size = size
def add(self, value: str) -> None:
h = self.hash_(value)
h = self.hash(value)
self.bitarray |= h
def exists(self, value: str) -> bool:
h = self.hash_(value)
h = self.hash(value)
return (h & self.bitarray) == h
def __contains__(self, other):
def __contains__(self, other: str) -> bool:
return self.exists(other)
def format_bin(self, bitarray: int) -> str:
@ -87,10 +87,10 @@ class Bloom:
return res.zfill(self.size)
@property
def bitstring(self):
def bitstring(self) -> None:
return self.format_bin(self.bitarray)
def hash_(self, value: str) -> int:
def hash(self, value: str) -> int:
res = 0b0
for func in HASH_FUNCTIONS:
b = func(value.encode()).digest()
@ -99,9 +99,9 @@ class Bloom:
return res
def format_hash(self, value: str) -> str:
return self.format_bin(self.hash_(value))
return self.format_bin(self.hash(value))
def estimated_error_rate(self):
def estimated_error_rate(self) -> float:
n_ones = bin(self.bitarray).count("1")
k = len(HASH_FUNCTIONS)
return (n_ones / self.size) ** k