File tree Expand file tree Collapse file tree 1 file changed +10
-6
lines changed
lib/splitclient-rb/cache/filter Expand file tree Collapse file tree 1 file changed +10
-6
lines changed Original file line number Diff line number Diff line change @@ -8,31 +8,35 @@ module Filter
88 class BloomFilter
99 def initialize ( capacity , false_positive_probability = 0.001 )
1010 @capacity = capacity . round
11- m = best_m ( capacity , false_positive_probability )
12- @ba = BitArray . new ( m . round )
11+ @ m = best_m ( capacity , false_positive_probability )
12+ reset_filter
1313 @k = best_k ( capacity )
1414 end
1515
1616 def add ( string )
1717 return false if contains? ( string )
1818
1919 positions = hashes ( string )
20-
2120 positions . each { |position | @ba [ position ] = 1 }
2221
2322 true
2423 end
25-
24+
2625 def contains? ( string )
2726 !hashes ( string ) . any? { |ea | @ba [ ea ] == 0 }
2827 end
2928
3029 def clear
31- @ba . size . times { |i | @ba [ i ] = 0 }
30+ @ba = nil
31+ reset_filter
3232 end
33-
33+
3434 private
3535
36+ def reset_filter
37+ @ba = BitArray . new ( @m . round )
38+ end
39+
3640 # m is the required number of bits in the array
3741 def best_m ( capacity , false_positive_probability )
3842 -( capacity * Math . log ( false_positive_probability ) ) / ( Math . log ( 2 ) ** 2 )
You can’t perform that action at this time.
0 commit comments