Created
December 9, 2011 23:04
-
-
Save cwvh/1453729 to your computer and use it in GitHub Desktop.
Simple counting bloom filter in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# $ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you please explain why you used two shifts at line 3?