mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-07 10:00:55 +00:00
added types
This commit is contained in:
parent
2fd71965f1
commit
314237d94e
|
@ -72,14 +72,14 @@ class Bloom:
|
||||||
self.size = size
|
self.size = size
|
||||||
|
|
||||||
def add(self, value: str) -> None:
|
def add(self, value: str) -> None:
|
||||||
h = self.hash_(value)
|
h = self.hash(value)
|
||||||
self.bitarray |= h
|
self.bitarray |= h
|
||||||
|
|
||||||
def exists(self, value: str) -> bool:
|
def exists(self, value: str) -> bool:
|
||||||
h = self.hash_(value)
|
h = self.hash(value)
|
||||||
return (h & self.bitarray) == h
|
return (h & self.bitarray) == h
|
||||||
|
|
||||||
def __contains__(self, other):
|
def __contains__(self, other: str) -> bool:
|
||||||
return self.exists(other)
|
return self.exists(other)
|
||||||
|
|
||||||
def format_bin(self, bitarray: int) -> str:
|
def format_bin(self, bitarray: int) -> str:
|
||||||
|
@ -87,10 +87,10 @@ class Bloom:
|
||||||
return res.zfill(self.size)
|
return res.zfill(self.size)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def bitstring(self):
|
def bitstring(self) -> None:
|
||||||
return self.format_bin(self.bitarray)
|
return self.format_bin(self.bitarray)
|
||||||
|
|
||||||
def hash_(self, value: str) -> int:
|
def hash(self, value: str) -> int:
|
||||||
res = 0b0
|
res = 0b0
|
||||||
for func in HASH_FUNCTIONS:
|
for func in HASH_FUNCTIONS:
|
||||||
b = func(value.encode()).digest()
|
b = func(value.encode()).digest()
|
||||||
|
@ -99,9 +99,9 @@ class Bloom:
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def format_hash(self, value: str) -> str:
|
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")
|
n_ones = bin(self.bitarray).count("1")
|
||||||
k = len(HASH_FUNCTIONS)
|
k = len(HASH_FUNCTIONS)
|
||||||
return (n_ones / self.size) ** k
|
return (n_ones / self.size) ** k
|
||||||
|
|
Loading…
Reference in New Issue
Block a user