Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 08bc970

Browse files
committed
has functions constant
1 parent 173ab0e commit 08bc970

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

data_structures/hashing/bloom_filter.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88

99
class Bloom:
10+
# number of hash functions is fixed
11+
HASH_FUNCTIONS = (sha256, md5)
12+
1013
def __init__(self, size=8):
1114
self.bitstring = 0b0
1215
self.size = size
@@ -42,7 +45,7 @@ def format_bin(self, value):
4245

4346
def hash(self, value):
4447
res = 0b0
45-
for func in (sha256, md5):
48+
for func in HASH_FUNCTIONS:
4649
b = func(value.encode()).digest()
4750
position = int.from_bytes(b, "little") % self.size
4851
res |= 2**position
@@ -74,8 +77,7 @@ def test_probability(m=64, n=20):
7477
for a in added:
7578
b.add(a)
7679

77-
# number of hash functions is fixed
78-
k = 2
80+
k = len(b.HASH_FUNCIONS)
7981

8082
n_ones = bin(b.bitstring).count("1")
8183
expected_probability = (n_ones / m) ** k
@@ -95,7 +97,7 @@ def test_probability(m=64, n=20):
9597

9698
assert (
9799
abs(expected_probability - fail_rate) <= 0.05
98-
) # 5% margin calculated experiementally
100+
) # 5% absolute margin calculated experiementally
99101

100102

101103
if __name__ == "__main__":

0 commit comments

Comments
 (0)