Skip to content

Instantly share code, notes, and snippets.

@joeyrobert
Created October 13, 2010 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeyrobert/624925 to your computer and use it in GitHub Desktop.
Save joeyrobert/624925 to your computer and use it in GitHub Desktop.
require 'zlib'
class BloomFilter
def initialize(length = 100, hashes = 3, data = [])
@hashes = hashes
@length = length
@bits = Array.new(@length)
@inserted = 0
add(data)
end
def add(data)
case data
when Array then
data.each { |item| item_add(item) }
when String then
item_add(data)
end
end
def include?(item)
srand(Zlib.crc32(item))
@hashes.times { return false unless @bits[rand(@length)] }
true
end
def false_positive_prob
(1.0 - (1.0 - 1.0/@length)**(@hashes * @inserted))**@hashes
end
private
def item_add(item)
srand(Zlib.crc32(item))
@hashes.times { @bits[rand(@length)] = true }
@inserted += 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment