Skip to content

Instantly share code, notes, and snippets.

@cwvh
Created December 9, 2011 23:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cwvh/1453729 to your computer and use it in GitHub Desktop.
Save cwvh/1453729 to your computer and use it in GitHub Desktop.
Simple counting bloom filter in Python.
def hashfn(item):
h = hash(item)
return (1 << (h%64)) | (1 << (h/64%64))
def mask(val):
return bin(hashfn(val))[2:]
class CountingBloom(object):
def __init__(self):
self.items = [0] * 64
def add(self, item):
bits = mask(item)
for index, bit in enumerate(bits):
if bit == '1':
self.items[index] += 1
def query(self, item):
bits = mask(item)
for index, bit in enumerate(bits):
if bit == '1' and self.items[index] == 0:
return False
return True
def remove(self, item):
bits = mask(item)
for index, bit in enumerate(bits):
if bit == '1' and self.items[index]:
self.items[index] -= 1
bloom = CountingBloom()
args = ('foo', 'bar', 'baz')
for arg in args:
bloom.add(arg)
print ', '.join(str(bloom.query(arg)) for arg in args)
for arg in args:
bloom.remove(arg)
print ', '.join(str(bloom.query(arg)) for arg in args)
# $ python bloom.py
# True, False, False
# True, True, False
# True, True, True
# False, True, True
# False, False, True
# False, False, False
# $
@DarthJahus
Copy link

Can you please explain why you used two shifts at line 3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment