Permalink
Browse files

Bloom filters (one hash function only, at present).

  • Loading branch information...
1 parent d8df98d commit 56cb274055c93ddca9f3c235a08c814a734bae3b @seanjtaylor committed Feb 12, 2012
Showing with 25 additions and 0 deletions.
  1. +25 −0 bloomfilter.py
View
@@ -0,0 +1,25 @@
+
+import math
+from bitarray import bitarray
+
+class BF(object):
+ def __init__(self, nbits=2**8):
+ self.nbits = nbits
+ self.ba = bitarray(nbits)
+ self.ba[:] = False
+ self.items = 0
+ self.n_hashes = 1
+
+ def add(self, obj):
+ self.ba[hash(obj) % self.nbits] = True
+ self.items += 1
+
+ def __contains__(self, obj):
+ return self.ba[hash(obj) % self.nbits]
+
+ def false_positive_rate(self):
+ p = (1 - math.exp(-(self.n_hashes*self.items) /
+ float(self.nbits)))**self.n_hashes
+ return p
+
+

0 comments on commit 56cb274

Please sign in to comment.