Class: Bloomer
- Inherits:
-
Object
- Object
- Bloomer
- Defined in:
- lib/bloomer.rb,
lib/bloomer/version.rb
Defined Under Namespace
Classes: Scalable
Constant Summary collapse
- VERSION =
"0.0.5"
Instance Method Summary collapse
-
#add(string) ⇒ Object
returns true if item did had not already been added.
-
#capacity ⇒ Object
If count exceeds capacity, the provided #false_positive_probability will probably be exceeded.
-
#count ⇒ Object
The number of unique strings given to #add (including false positives, which can mean this number under-counts).
-
#include?(string) ⇒ Boolean
returns false if the item hadn’t already been added returns true if it is likely that string had been added.
-
#initialize(capacity, false_positive_probability = 0.001) ⇒ Bloomer
constructor
A new instance of Bloomer.
Constructor Details
#initialize(capacity, false_positive_probability = 0.001) ⇒ Bloomer
Returns a new instance of Bloomer.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/bloomer.rb', line 5 def initialize(capacity, false_positive_probability = 0.001) @capacity = capacity.round # m is the required number of bits in the array m = -(capacity * Math.log(false_positive_probability)) / (Math.log(2) ** 2) @ba = BitArray.new(m.round) # count is the number of unique additions to this filter. @count = 0 # k is the number of hash functions that minimizes the probability of false positives @k = (Math.log(2) * (@ba.size / capacity)).round end |
Instance Method Details
#add(string) ⇒ Object
returns true if item did had not already been added
17 18 19 20 21 22 23 |
# File 'lib/bloomer.rb', line 17 def add string count = 0 hashes(string).each { |ea| count += @ba[ea]; @ba[ea] = 1 } previously_included = (count == @k) @count += 1 unless previously_included !previously_included end |
#capacity ⇒ Object
If count exceeds capacity, the provided #false_positive_probability will probably be exceeded.
38 39 40 |
# File 'lib/bloomer.rb', line 38 def capacity @capacity end |
#count ⇒ Object
The number of unique strings given to #add (including false positives, which can mean this number under-counts)
33 34 35 |
# File 'lib/bloomer.rb', line 33 def count @count end |
#include?(string) ⇒ Boolean
returns false if the item hadn’t already been added returns true if it is likely that string had been added. See #false_positive_probability
27 28 29 |
# File 'lib/bloomer.rb', line 27 def include? string !hashes(string).any? { |ea| @ba[ea] == 0 } end |