frombitarrayimportbitarrayimportmmh3classBloomFilter:def__init__(self,size:int=100_000_000,hash_count:int=7):self.size=sizeself.hash_count=hash_countself.bit_array=bitarray(size)self.bit_array.setall(0)def_get_hash_positions(self,url:str)->list[int]:"""
ge
Args:
url: The URL to get the hash positions for.
Returns:
A list of hash positions.
"""positions=[]forseedinrange(self.hash_count):index=mmh3.hash(url,seed)%self.sizepositions.append(index)returnpositionsdefadd(self,url:str)->bool:forposinself._get_hash_positions(url):ifnotself.bit_array[pos]:self.bit_array[pos]=1else:returnFalsereturnTruedefcontains(self,url:str)->bool:returnall(self.bit_array[pos]forposinself._get_hash_positions(url))bloom_filter=BloomFilter(size=100_000_000,hash_count=7)urls=["https://example.com/page/1","https://example.com/page/2","https://example.com/page/1",]forurlinurls:exists=bloom_filter.contains(url)ifexists:print(f"URL {url} already exists in bloom filter")continueadd_success=bloom_filter.add(url)ifnotadd_success:print(f"URL {url} already exists in bloom filter")
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)