bloom_filter

  • 2022-12-14
  • 浏览 (515)

bloom_filter.py 源码

import mmh3
from bitarray import bitarray


# Bloom-Filter 实现


class BloomFilter:

    def __init__(self, size, hash_num):
        self.size = size
        self.hash_num = hash_num
        self.bit_array = bitarray(size)
        self.bit_array.setall(0)

    def add(self, val):
        for seed in range(self.hash_num):
            res = mmh3.hash(val, seed) % self.size
            self.bit_array[res] = 1

    def lookup(self, val):
        for seed in range(self.hash_num):
            res = mmh3.hash(val, seed) % self.size
            if self.bit_array[res] == 0:
                return "None"
        return 'Probably'

你可能感兴趣的文章

algo-learn

contains_duplicate

container_with_most_water

0  赞