Class: BloominSimple

Inherits:
Object
  • Object
show all
Defined in:
lib/bloominsimple.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (BloominSimple) initialize(bitsize, &block)

Returns a new instance of BloominSimple



44
45
46
47
48
49
50
51
# File 'lib/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

- (Object) bitfield

Returns the value of attribute bitfield



42
43
44
# File 'lib/bloominsimple.rb', line 42

def bitfield
  @bitfield
end

- (Object) hasher

Returns the value of attribute hasher



42
43
44
# File 'lib/bloominsimple.rb', line 42

def hasher
  @hasher
end

Class Method Details

+ (Object) from_dump(data, &block)

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/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

- (Object) &(other)

Allows comparison between two filters. Returns number of same bits.



64
65
66
67
68
69
70
71
# File 'lib/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

- (Object) add(item)

Add item to the filter



54
55
56
# File 'lib/bloominsimple.rb', line 54

def add(item)
  @hasher[item].each { |hi| @bitfield[hi % @size] = 1 }
end

- (Object) dump

Dumps the bitfield for a bloom filter for storage



74
75
76
77
# File 'lib/bloominsimple.rb', line 74

def dump
  [@size, *@bitfield.field].pack("I*")
  #Marshal.dump([@size, @bitfield])
end

- (Boolean) includes?(item)

Find out if the filter possibly contains the supplied item

Returns:

  • (Boolean)


59
60
61
# File 'lib/bloominsimple.rb', line 59

def includes?(item)
  @hasher[item].each { |hi| return false unless @bitfield[hi % @size] == 1 } and true
end