简单的布隆过滤器

来源:互联网 发布:什么情况下多进程编程 编辑:程序博客网 时间:2024/05/01 23:29

原理

当一个元素被加入集合时,通过 K 个 Hash 函数将这个元素映射成一个位阵列(Bit array)中的 K 个点,把它们置为 1。检索时,我们只要看看这些点是不是都是 1 就(大约)知道集合中有没有它了:如果这些点有任何一个 0,则被检索元素一定不在;如果都是 1,则被检索元素很可能在。

优点

它的优点是空间效率和查询时间都远远超过一般的算法,布隆过滤器存储空间和插入 / 查询时间都是常数O(k)。另外, 散列函数相互之间没有关系,方便由硬件并行实现。布隆过滤器不需要存储元素本身,在某些对保密要求非常严格的场合有优势。

缺点

但是布隆过滤器的缺点和优点一样明显。误算率是其中之一。随着存入的元素数量增加,误算率随之增加。但是如果元素数量太少,则使用散列表足矣。(误判补救方法是:再建立一个小的白名单,存储那些可能被误判的信息。)另外,一般情况下不能从布隆过滤器中删除元素. 我们很容易想到把位数组变成整数数组,每插入一个元素相应的计数器加 1, 这样删除元素时将计数器减掉就可以了。然而要保证安全地删除元素并非如此简单。首先我们必须保证删除的元素的确在布隆过滤器里面. 这一点单凭这个过滤器是无法保证的。另外计数器回绕也会造成问题。

布隆过滤器的实现

//实现一个布隆过滤器 需要哈希函数和位图//string 转换成为数字static size_t BKDRHash(const char* str){    unsigned int seed = 131;    unsigned int hash = 0;    while (*str)    {        hash = hash*seed + (*str++);    }    return (hash & 0x7FFFFFFF);}//哈希函数template<typename T>class HashFunDef{public:    size_t operator()(const T& key)    {        return key;    }};template<>class HashFunDef<string>{public:    size_t operator()(const string& key)    {        return BKDRHash(key.c_str());    }};size_t SDBMHash(const char* str){    register size_t hash = 0;    while (size_t ch=(size_t)*str++)    {        hash = 65599 * hash + ch;    }    return hash;}size_t RSHash(const char* str){    register size_t hash = 0;    size_t magic = 63689;    while (size_t ch=(size_t)*str++)    {        hash = hash*magic + ch;        magic *= 378551;    }    return hash;}size_t APHash(const char* str){    register size_t hash = 0;    size_t ch;    for (long i = 0; ch=(size_t)*str++; i++)    {        if (0 == (i & 1))        {            hash ^= (hash << 7) ^ (hash >> 3);        }        else        {            hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));        }    }    return hash;}size_t JSHash(const char* str){    if (!*str)        return 0;    register size_t hash = 1315423911;    while (size_t ch=(size_t)*str++)    {        hash ^= ((hash << 5) + ch + (hash >> 2));    }    return hash;}template<class T>struct _HashFunc1{    size_t operator()(const T& key)    {        return SDBMHash(key.c_str());    }};template<class T>struct _HashFunc2{    size_t operator()(const T& key)    {        return SDBMHash(key.c_str());    }};template<class T>struct _HashFunc3{    size_t operator()(const T& key)    {        return RSHash(key.c_str());    }};template<class T>struct _HashFunc4{    size_t operator()(const T&key)    {        return APHash(key.c_str());    }};template<class T>struct _HashFunc5{    size_t operator()(const T& key)    {        return JSHash(key.c_str());    }};template<class K,class HashFunc=_HashFunc1<string> >class BloomFile{public:    BloomFile(size_t size)        :_map(size)    {}    bool Insert(string str)    {        size_t idx1 = _HashFunc1<string>()(str);        size_t idx2 = _HashFunc2<string>()(str);        size_t idx3 = _HashFunc3<string>()(str);        size_t idx4 = _HashFunc4<string>()(str);        size_t idx5 = _HashFunc5<string>()(str);        _map.Set(idx1);        _map.Set(idx2);        _map.Set(idx3);        _map.Set(idx4);        _map.Set(idx5);        return true;    }    bool Find(string str)    {        size_t idx1 = _HashFunc1<string>()(str);        size_t idx2 = _HashFunc2<string>()(str);        size_t idx3 = _HashFunc3<string>()(str);        size_t idx4 = _HashFunc4<string>()(str);        size_t idx5 = _HashFunc5<string>()(str);        if (!_map.Exit(idx1))            return false;        if (!_map.Exit(idx2))            return false;        if (!_map.Exit(idx3))            return false;        if (!_map.Exit(idx4))            return false;        if (!_map.Exit(idx5))            return false;        return true;    }private:    BitMap _map;};

位图

//位图class BitMap{public:    BitMap()    {};    BitMap(size_t size)    {        _table.resize((size >> 5) + 1);    }    //置1    void Set(int data)    {        size_t byteNu = data >> 5;        size_t bitNu = data % 32;        _table[byteNu] |= (1 << (bitNu));    }    void ReSet(int data)    {        _table[data >> 5] &= ~(1 << (data % 32));    }    //是否存在    bool Exit(int data)    {        if ((_table[data >> 5]) & (1<<(data % 32))  )            return true;        return false;    }private:    vector<int> _table;};
原创粉丝点击