Class: BloominSimple
- Inherits:
-
Object
- Object
- BloominSimple
- Defined in:
- lib/whatlanguage/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.
44 45 46 47 48 49 50 51 |
# File 'lib/whatlanguage/bloominsimple.rb', line 44 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.
42 43 44 |
# File 'lib/whatlanguage/bloominsimple.rb', line 42 def bitfield @bitfield end |
#hasher ⇒ Object
Returns the value of attribute hasher.
42 43 44 |
# File 'lib/whatlanguage/bloominsimple.rb', line 42 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)
80 81 82 83 84 85 86 |
# File 'lib/whatlanguage/bloominsimple.rb', line 80 def self.from_dump(data, &block) data = data.unpack("I*") #data = Marshal.load(data) 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.
64 65 66 67 68 69 70 71 |
# File 'lib/whatlanguage/bloominsimple.rb', line 64 def &(other) raise "Wrong sizes" if self.bitfield.size != other.bitfield.size same = 0 #self.bitfield.size.times do |pos| # same += 1 if self.bitfield[pos] & other.bitfield[pos] == 1 #end self.bitfield.total_set.to_s + "--" + other.bitfield.total_set.to_s end |
#add(item) ⇒ Object
Add item to the filter
54 55 56 |
# File 'lib/whatlanguage/bloominsimple.rb', line 54 def add(item) @hasher[item].each { |hi| @bitfield[hi % @size] = 1 } end |
#dump ⇒ Object
Dumps the bitfield for a bloom filter for storage
74 75 76 77 |
# File 'lib/whatlanguage/bloominsimple.rb', line 74 def dump [@size, *@bitfield.field].pack("I*") #Marshal.dump([@size, @bitfield]) end |
#includes?(item) ⇒ Boolean
Find out if the filter possibly contains the supplied item
59 60 61 |
# File 'lib/whatlanguage/bloominsimple.rb', line 59 def includes?(item) @hasher[item].each { |hi| return false unless @bitfield[hi % @size] == 1 } and true end |