-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloom_filter.rb
63 lines (52 loc) · 1.66 KB
/
bloom_filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'zlib' # stdlib
require 'compsci/bit_set'
module CompSci
class BloomFilter
MAX_BITS = Integer(2**32) # CRC32 yields 32-bit values
attr_reader :bits, :aspects, :bitmap
# The default values require 8 kilobytes of storage and recognize:
# < 7000 strings at 1% False Positive Rate (4k @ 0.1%) (10k @ 5%)
# FPR goes up as more strings are added
def initialize(bits: Integer(2**16), aspects: 5)
@bits = bits
raise("bits: #{@bits}") if @bits > MAX_BITS
@aspects = aspects
@bitmap = BitSet.new(@bits)
end
# Return an array of bit indices ("on bits") corresponding to
# multiple rounds of string hashing (CRC32 is fast and ~fine~)
def index(str)
val = 0
Array.new(@aspects) { (val = Zlib.crc32(str, val)) % @bits }
end
def add(str)
self.index(str).each { |i| @bitmap.add(i) }
end
alias_method(:<<, :add)
# true or false; a `true` result mayb be a "false positive"
def include?(str)
self.index(str).all? { |i| @bitmap.include?(i) }
end
# returns either 0 or a number like 0.95036573
def likelihood(str)
self.include?(str) ? 1.0 - self.fpr : 0.0
end
alias_method(:[], :likelihood)
# relatively expensive; don't test against this in a loop
def percent_full
@bitmap.on_bits.count.to_f / @bits
end
def fpr
self.percent_full**@aspects
end
def algo
'CRC32'
end
def to_s
format("%i bits (%.1f kB, %i aspects %s) %i%% full; FPR: %.3f%%",
@bits, @bits.to_f / 2**13, @aspects, self.algo,
self.percent_full * 100, self.fpr * 100)
end
alias_method(:inspect, :to_s)
end
end