File tree Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Original file line number Diff line number Diff line change 7
7
8
8
9
9
class Bloom :
10
+ # number of hash functions is fixed
11
+ HASH_FUNCTIONS = (sha256 , md5 )
12
+
10
13
def __init__ (self , size = 8 ):
11
14
self .bitstring = 0b0
12
15
self .size = size
@@ -42,7 +45,7 @@ def format_bin(self, value):
42
45
43
46
def hash (self , value ):
44
47
res = 0b0
45
- for func in ( sha256 , md5 ) :
48
+ for func in HASH_FUNCTIONS :
46
49
b = func (value .encode ()).digest ()
47
50
position = int .from_bytes (b , "little" ) % self .size
48
51
res |= 2 ** position
@@ -74,8 +77,7 @@ def test_probability(m=64, n=20):
74
77
for a in added :
75
78
b .add (a )
76
79
77
- # number of hash functions is fixed
78
- k = 2
80
+ k = len (b .HASH_FUNCIONS )
79
81
80
82
n_ones = bin (b .bitstring ).count ("1" )
81
83
expected_probability = (n_ones / m ) ** k
@@ -95,7 +97,7 @@ def test_probability(m=64, n=20):
95
97
96
98
assert (
97
99
abs (expected_probability - fail_rate ) <= 0.05
98
- ) # 5% margin calculated experiementally
100
+ ) # 5% absolute margin calculated experiementally
99
101
100
102
101
103
if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments