Class: BloominSimple
- Inherits:
-
Object
- Object
- BloominSimple
- Defined in:
- lib/bloominsimple.rb
Instance Attribute Summary collapse
-
#bitfield ⇒ Object
Returns the value of attribute bitfield.
-
#hasher ⇒ Object
Returns the value of attribute hasher.
Class Method Summary collapse
-
.from_dump(data, &block) ⇒ Object
Creates a new bloom filter object from a stored dump (hasher has to be resent though for additions).
Instance Method Summary collapse
-
#&(other) ⇒ Object
Allows comparison between two filters.
-
#add(item) ⇒ Object
Add item to the filter.
-
#dump ⇒ Object
Dumps the bitfield for a bloom filter for storage.
-
#includes?(item) ⇒ Boolean
Find out if the filter possibly contains the supplied item.
-
#initialize(bitsize, &block) ⇒ BloominSimple
constructor
A new instance of BloominSimple.
Constructor Details
#initialize(bitsize, &block) ⇒ BloominSimple
Returns a new instance of BloominSimple.
11 12 13 14 15 16 17 18 |
# File 'lib/bloominsimple.rb', line 11 def initialize(bitsize, &block) @bitfield = BitField.new(bitsize) @size = bitsize @hasher = block || lambda do |word| word = word.downcase.strip [h1 = word.sum, h2 = word.hash, h2 + h1 ** 3] end end |
Instance Attribute Details
#bitfield ⇒ Object
Returns the value of attribute bitfield.
9 10 11 |
# File 'lib/bloominsimple.rb', line 9 def bitfield @bitfield end |
#hasher ⇒ Object
Returns the value of attribute hasher.
9 10 11 |
# File 'lib/bloominsimple.rb', line 9 def hasher @hasher end |
Class Method Details
.from_dump(data, &block) ⇒ Object
Creates a new bloom filter object from a stored dump (hasher has to be resent though for additions)
42 43 44 45 46 47 |
# File 'lib/bloominsimple.rb', line 42 def self.from_dump(data, &block) data = data.unpack("I*") temp = new(data[0], &block) temp.bitfield.field = data[1..-1] temp end |
Instance Method Details
#&(other) ⇒ Object
Allows comparison between two filters. Returns number of same bits.
31 32 33 34 |
# File 'lib/bloominsimple.rb', line 31 def &(other) raise "Wrong sizes" if self.bitfield.size != other.bitfield.size return (self.bitfield & other.bitfield).total_set end |
#add(item) ⇒ Object
Add item to the filter
21 22 23 |
# File 'lib/bloominsimple.rb', line 21 def add(item) @hasher[item].each { |hi| @bitfield[hi % @size] = 1 } end |
#dump ⇒ Object
Dumps the bitfield for a bloom filter for storage
37 38 39 |
# File 'lib/bloominsimple.rb', line 37 def dump [@size, *@bitfield.field].pack("I*") end |
#includes?(item) ⇒ Boolean
Find out if the filter possibly contains the supplied item
26 27 28 |
# File 'lib/bloominsimple.rb', line 26 def includes?(item) @hasher[item].each { |hi| return false unless @bitfield[hi % @size] == 1 } and true end |