简单布隆过滤器实现、布隆过滤器扩展

来源:互联网 发布:sql分页查询语句join 编辑:程序博客网 时间:2024/05/16 05:06

布隆过滤器

1.实现一个简单的布隆过滤器。

如果想要判断一个元素是不是在一个集合里,一般想到的是将所有元素保存起来,然后通过比较确定。链表,树等等数据结构都是这种思路.
但是随着集合中元素的增加,我们需要的存储空间越来越大,检索速度也越来越慢(O(n),O(logn))。不过世界上还有一种叫作散列表(又叫哈希表,Hash
table)的数据结构。它可以通过一个Hash函数将一个元素映射成一个位阵列(Bit
array)中的一个点。这样一来,我们只要看看这个点是不是1就可以知道集合中有没有它了。这就是布隆过滤器的基本思想。

优点

相比于其它的数据结构,布隆过滤器在空间和时间方面都有巨大的优势。布隆过滤器存储空间和插入/查询时间都是常数。另外,
Hash函数相互之间没有关系,方便由硬件并行实现。布隆过滤器不需要存储元素本身,在某些对保密要求非常严格的场合有优势。
布隆过滤器可以表示全集,其它任何数据结构都不能; k和m相同,使用同一组Hash函数的两个布隆过滤器的交并差运算可以使用位操作进行。
布隆过滤器

缺点

但是布隆过滤器的缺点和优点一样明显。误算率是其中之一。随着存入的元素数量增加,误算率随之增加。但是如果元素数量太少,则使用散列表足矣。
另外,一般情况下不能从布隆过滤器中删除元素。我们很容易想到把位列阵变成整数数组,每插入一个元素相应的计数器加1,
这样删除元素时将计数器减掉就可以了。然而要保证安全的删除元素并非如此简单。首先我们必须保证删除的元素的确在布隆过滤器里面.
这一点单凭这个过滤器是无法保证的。另外计数器回绕也会造成问题。 在降低误算率方面,有不少工作,使得出现了很多布隆过滤器的变种。

代码实现:

#include<iostream>#include"BitSet.h"using namespace std;//布隆过滤器//实现思路: 位图+hash//hash 确定位置进行定位template<class K>struct _HashFunc1{    size_t BKDRHash(const char *str)    {        register size_t hash = 0;        while (size_t ch = (size_t)*str++)        {            hash = hash * 131 + ch;   // 也可以乘以31、131、1313、13131、131313..         }        return hash;    }    size_t operator()(const string &s)    {        return BKDRHash(s.c_str());    }};template<class K>struct _HashFunc2{    size_t SDBMHash(const char *str)    {        register size_t hash = 0;        while (size_t ch = (size_t)*str++)        {            hash = 65599 * hash + ch;            //hash = (size_t)ch + (hash << 6) + (hash << 16) - hash;         }        return hash;    }    size_t operator()(const string &s)    {        return SDBMHash(s.c_str());    }};template<class K>struct _HashFunc3{    size_t RSHash(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;    }    size_t operator()(const string &s)    {        return RSHash(s.c_str());    }};template<class K>struct _HashFunc4{    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 operator()(const string&s)    {        return RSHash(s.c_str());    }};template<class K>struct _HashFunc5{    size_t RSHash(const char *str)    {        register size_t hash = 0;        size_t ch;        for (long i = 0; ch = (size_t)*str++; i++)        {            if ((i & 1) == 0)            {                hash ^= ((hash << 7) ^ ch ^ (hash >> 3));            }            else            {                hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));            }        }        return hash;    }    size_t operator()(const string &s)    {        return RSHash(s.c_str());    }};template<class K=string,class HashFunc1=_HashFunc1<K>,class HashFunc2=_HashFunc2<K>,class HashFunc3=_HashFunc3<K>,class HashFunc4=_HashFunc4<K>,class HashFunc5 =_HashFunc5<K >>class BloomFilter{public:    BloomFilter(size_t size)    {        _capacity = _GetNextPrime(size);        _bitmap.Resize(_capacity);    }    void Set(const K &key)    {        size_t index1 = HashFunc1()(key);        size_t index2 = HashFunc2()(key);        size_t index3 = HashFunc3()(key);        size_t index4 = HashFunc4()(key);        size_t index5 = HashFunc5()(key);        _bitmap.Set(index1%_capacity);        _bitmap.Set(index2%_capacity);        _bitmap.Set(index3%_capacity);        _bitmap.Set(index4%_capacity);        _bitmap.Set(index5%_capacity);    }    bool IsIn(const K&key)    {        size_t index1 = HashFunc1()(key);        if (!_bitmap.Test(index1%_capacity))        {            return false;        }        size_t index2 = HashFunc2()(key);        if (!_bitmap.Test(index2%_capacity))        {            return false;        }        size_t index3 = HashFunc3()(key);        if (!_bitmap.Test(index3%_capacity))        {            return false;        }        size_t index4 = HashFunc4()(key);        if (!_bitmap.Test(index4%_capacity))        {            return false;        }        size_t index5 = HashFunc5()(key);        if (!_bitmap.Test(index5%_capacity))        {            return false;        }        return true;    }protected:    unsigned long _GetNextPrime(unsigned long num)    {        const int _PrimeSize = 28;        static const unsigned long _PrimeList[_PrimeSize] =        {            53ul, 97ul, 193ul, 389ul, 769ul,            1543ul, 3079ul, 6151ul, 12289ul, 24593ul,            49157ul, 98317ul, 196613ul, 393241ul, 786433ul,            1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,            50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,            1610612741ul, 3221225473ul, 4294967291ul        };        size_t pos = 0;        while (pos < _PrimeSize)        {            if (_PrimeList[pos] > num)            {                break;            }            ++pos;        }        return _PrimeList[pos];    }private:    BitSet _bitmap;    size_t _capacity;};void Test2(){    BitSet bitmap(60);    for (int i = 4; i < 10; i++)    {        bitmap.Set(i);    }    for (int i = 0; i < 20; i++)    {        cout << i << ":" << bitmap.Test(i) << endl;    }}void Test1(){     BloomFilter<>bf(30);    string s11("www");    bf.Set(s11);    bf.Set("张三");    bf.Set("王五");    bf.Set("李四");    cout << bf.IsIn("布隆过滤器") << endl;    cout << bf.IsIn("ww") << endl;    cout << bf.IsIn("www") << endl;    cout << bf.IsIn("王五") << endl;}int main(){    Test1();    return 0;}

2.–布隆过滤器的扩展:如何扩展BloomFilter使得它支持删除元素的操作?请设计实现一个支持删除的布隆过滤器.

//支持Reset的bloomfliter  template<typename K=string      ,class HashFunc1=_HashFunc1      , class HashFunc2 = _HashFunc2      , class HashFunc3= _HashFunc3      , class HashFunc4= _HashFunc4      , class HashFunc5 = _HashFunc5>  class BloomFilter  {  public:      BloomFilter(size_t range)      {          _bitmap.resize(range*5);           //为了减少误判,提高精度,用5个位置来表示一个数      }      void Set(const K& key)          //要设置为1,必须将5个位置都设置      {          size_t index1 = HashFunc1()(key) % _bitmap.size();          size_t index2 = HashFunc2()(key) % _bitmap.size();          size_t index3 = HashFunc3()(key) % _bitmap.size();          size_t index4 = HashFunc4()(key) % _bitmap.size();          size_t index5 = HashFunc5()(key) % _bitmap.size();          _bitmap[index1]++;          _bitmap[index2]++;          _bitmap[index3]++;          _bitmap[index4]++;          _bitmap[index5]++;      }      bool ReSet(const K& key)          //采用引用计数的方式复位      {          size_t index1 = HashFunc1()(key) % _bitmap.size();          size_t index2 = HashFunc2()(key) % _bitmap.size();          size_t index3 = HashFunc3()(key) % _bitmap.size();          size_t index4 = HashFunc4()(key) % _bitmap.size();          size_t index5 = HashFunc5()(key) % _bitmap.size();          if (_bitmap[index1] == 0 ||              _bitmap[index2] == 0 ||              _bitmap[index3] == 0 ||              _bitmap[index4] == 0 ||              _bitmap[index5] == 0)                   //只要有一个为0,说明这个key不存在              return false;          //要是都不为0,才减一          _bitmap[index1]--;          _bitmap[index2]--;          _bitmap[index3]--;          _bitmap[index4]--;          _bitmap[index5]--;          return true;      }      bool Test(const K& key)      {          size_t index1 = HashFunc1()(key) % _bitmap.size();          size_t index2 = HashFunc2()(key) % _bitmap.size();          size_t index3 = HashFunc3()(key) % _bitmap.size();          size_t index4 = HashFunc4()(key) % _bitmap.size();          size_t index5 = HashFunc5()(key) % _bitmap.size();          //只有五个位置都为1,才存在          if (_bitmap[index1] != 0 &&              _bitmap[index2] != 0 &&              _bitmap[index3] != 0 &&              _bitmap[index4] != 0 &&              _bitmap[index5] != 0)              return true;          return false;      }  private:      vector<size_t> _bitmap;  };  
原创粉丝点击