当前位置:   article > 正文

C++实现布隆过滤器_布隆滤波器c++实现

布隆滤波器c++实现

目录

一、什么是布隆过滤器

二、布隆过滤器的映射

三、布隆过滤器的作用

四、布隆过滤器的实现

五、总结+测试


一、什么是布隆过滤器

之前我们学习了位图,我们知道位图主要是实现了整形的映射bit位,这样可以大幅度的节省空间,那么针对于我们也经常用的string类型,或者其他类型,该如何去映射呢?

可以说string类的个数是无限的,无线个数去映射到有限的位置,那么必定会发生哈希冲突,这是无法避免的,我们有没有办法去减少这种哈希冲突呢?

大佬布隆就提出了布隆过滤器。(不是弗雷尔卓德之心)

布隆过滤器是由布隆(Burton Howard Bloom)在1970年提出的 一种紧凑型的、比较巧妙的概率型数据结构,特点是高效地插入和查询,可以用来告诉你 “某样东西一定不存在或者可能存在”,它是用多个哈希函数,将一个数据映射到位图结构中。此种方式不仅可以提升查询效率,也可以节省大量的内存空间

二、布隆过滤器的映射

比如说,如下有三个string字符串,分别为sort,left,right,利用哈希的思想,我们可以将他们映射到相应的位置,这里跟位图一样,映射到了bit位,就算我们哈希函数设置得再好,也无法避免哈希冲突。

比如下面sort和left发生了哈希冲突,如果我现在先插入了sort,将该bit位置为了1,当我去查询left的时候,会发现left映射的bit位为1,我们就会以为left存在,实际上left并没有存在,这样就会导致误判的发生。

大佬布隆提出了一个思想,我可以给字符串使用更多的哈希函数,同时映射更多的位置, 这些位置相互验证,几个映射位置一同存在,这样会更具有可靠性(虽然他也还没完全解决可靠问题,也无法解决)

具体情况如下,sort映射到了5,16,18 ,left映射到5,11,13, right映射到16,20,26。

这样映射后,虽然浪费了一些空间,但就算某个映射发生重复了,也没关系,只要不是三个位置都冲突就没事,大大的降低了冲突概率。(也可以选择更多的映射,但同时也会浪费更多的空间)

三、布隆过滤器的作用

前面我们分析了那么多,感觉这个布隆过滤器没啥用啊,始终有概率发生冲突,是不是在实际中用不上啊,大家别急,我们先来分析一下布隆过滤器的可靠性问题。

  • 如果该string不存在,那么该bit位还没被映射,证明确实不存在,这肯定是可靠的。
  • 如果该string存在,他映射的位置都存在,这有可能是其他string映射到这里的,这会导致告诉我们的是虚假消息(本来不存在,你说存在),这是不可靠的。

不存在一定可靠,存在可能是虚假消息。根据这点,我们来看下面的用例 

        我爱玩英雄联盟,在我们玩LOL之前,都需要取名,并且每一个区名字不可以重复,用户名这个数据一般都存放在服务器,如果每一个输入的名字,我们都要去服务器里面判断是否存在,再返回给用户,这样就会牵扯到网络相关知识,同时也会让服务器访问变多。

        如果在客户端里面塞一个布隆过滤器,在服务器启动时,往布隆过滤器里添加用户名的映射关系,如果用户名不存在,是可靠的,那么我就告诉你,这个名字可以取。如果用户名映射关系告诉我们存在,这是不可靠的,他可能不存在,因此只要映射到存在,我们就去服务器再寻找一下,看能不看找到,结果再返回给用户。

        这样既不会让客户端大很多,也不会让服务器承担很多,是不是一举两得。这也是为什么我们叫他过滤器的原因,他的作用是过滤。

四、布隆过滤器的实现

 这里我们BloomFilter类模板参数N,和类型K,还有三个哈希函数类,变量_bs使用了库里面的bitset来构建。

set函数就是分别计算三个哈希函数的映射值,再用_bs.set()置为1,Test函数就是分别判断_bs.test()是否为true,有一个是false就会返回false(这是准确的),三个都为true才会返回true(这是不准的)。

  1. #pragma once
  2. #include<bitset>
  3. #include<string>
  4. template<size_t N,
  5. class K,
  6. class HashFunc1
  7. class HashFunc2
  8. class HashFunc3>
  9. class BloomFilter
  10. {
  11. public:
  12. void Set(const K& key)
  13. {
  14. HashFunc1 kf1;
  15. size_t hash1 = kf1(key) % N;
  16. size_t hash2 = HashFunc2()(key) % N;
  17. size_t hash3 = HashFunc3()(key) % N;
  18. _bs.set(hash1);
  19. _bs.set(hash2);
  20. _bs.set(hash3);
  21. }
  22. bool Test(const K& key)
  23. {
  24. //false是准确的
  25. size_t hash1 = HashFunc1()(key) % N;
  26. if (_bs.test(hash1) == false)
  27. return false;
  28. size_t hash2 = HashFunc2()(key) % N;
  29. if (_bs.test(hash2) == false)
  30. return false;
  31. size_t hash3 = HashFunc3()(key) % N;
  32. if (_bs.test(hash3) == false)
  33. return false;
  34. //可能误判
  35. return true;
  36. }
  37. private:
  38. bitset<N> _bs;
  39. };

一般K为string,我们就可以给他一个string的缺省值,至于三个哈希函数我们去大佬博客字符串哈希函数拷贝三个过来使用即可。

哈希函数如下

  1. struct BKDRHash
  2. {
  3. size_t operator()(const string& key)
  4. {
  5. // BKDR
  6. size_t hash = 0;
  7. for (auto e : key)
  8. {
  9. hash *= 31;
  10. hash += e;
  11. }
  12. return hash;
  13. }
  14. };
  15. struct APHash
  16. {
  17. size_t operator()(const string& key)
  18. {
  19. size_t hash = 0;
  20. for (size_t i = 0; i < key.size(); i++)
  21. {
  22. char ch = key[i];
  23. if ((i & 1) == 0)
  24. {
  25. hash ^= ((hash << 7) ^ ch ^ (hash >> 3));
  26. }
  27. else
  28. {
  29. hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));
  30. }
  31. }
  32. return hash;
  33. }
  34. };
  35. struct DJBHash
  36. {
  37. size_t operator()(const string& key)
  38. {
  39. size_t hash = 5381;
  40. for (auto ch : key)
  41. {
  42. hash += (hash << 5) + ch;
  43. }
  44. return hash;
  45. }
  46. };

修改一下缺省值 

测试一下啊,运气还算不错,没有冲突 

五、总结+测试

我们将将数据放大一下,看看冲突情况,这里我们开辟了十倍的空间,发现冲突还算比较小。

现在开辟了5倍的空间,发现冲突就变大了一些。

总结:适当的多开辟一些空间,会让误判率变得较小,足够多甚至可能会没有冲突,但同样也会浪费很多空间。但是这毕竟不算可靠,始终需要去数据库里再判断,因此适当开辟就好。

 最后附上总代码BloomFilter.h

  1. #pragma once
  2. #include<bitset>
  3. #include<string>
  4. struct BKDRHash
  5. {
  6. size_t operator()(const string& key)
  7. {
  8. // BKDR
  9. size_t hash = 0;
  10. for (auto e : key)
  11. {
  12. hash *= 31;
  13. hash += e;
  14. }
  15. return hash;
  16. }
  17. };
  18. struct APHash
  19. {
  20. size_t operator()(const string& key)
  21. {
  22. size_t hash = 0;
  23. for (size_t i = 0; i < key.size(); i++)
  24. {
  25. char ch = key[i];
  26. if ((i & 1) == 0)
  27. {
  28. hash ^= ((hash << 7) ^ ch ^ (hash >> 3));
  29. }
  30. else
  31. {
  32. hash ^= (~((hash << 11) ^ ch ^ (hash >> 5)));
  33. }
  34. }
  35. return hash;
  36. }
  37. };
  38. struct DJBHash
  39. {
  40. size_t operator()(const string& key)
  41. {
  42. size_t hash = 5381;
  43. for (auto ch : key)
  44. {
  45. hash += (hash << 5) + ch;
  46. }
  47. return hash;
  48. }
  49. };
  50. template<size_t N,
  51. class K = string,
  52. class HashFunc1 = BKDRHash,
  53. class HashFunc2 = APHash,
  54. class HashFunc3 = DJBHash>
  55. class BloomFilter
  56. {
  57. public:
  58. void Set(const K& key)
  59. {
  60. HashFunc1 kf1;
  61. size_t hash1 = kf1(key) % N;
  62. size_t hash2 = HashFunc2()(key) % N;
  63. size_t hash3 = HashFunc3()(key) % N;
  64. _bs.set(hash1);
  65. _bs.set(hash2);
  66. _bs.set(hash3);
  67. }
  68. bool Test(const K& key)
  69. {
  70. //false是准确的
  71. size_t hash1 = HashFunc1()(key) % N;
  72. if (_bs.test(hash1) == false)
  73. return false;
  74. size_t hash2 = HashFunc2()(key) % N;
  75. if (_bs.test(hash2) == false)
  76. return false;
  77. size_t hash3 = HashFunc3()(key) % N;
  78. if (_bs.test(hash3) == false)
  79. return false;
  80. //可能误判
  81. return true;
  82. }
  83. private:
  84. bitset<N> _bs;
  85. };

test.cpp 

  1. #include<iostream>
  2. using namespace std;
  3. #include"BloomFilter.h"
  4. void test01()
  5. {
  6. BloomFilter<100> bf;
  7. bf.Set("猪八戒");
  8. bf.Set("沙悟净");
  9. bf.Set("孙悟空");
  10. bf.Set("二郎神");
  11. cout << bf.Test("猪八戒") << endl;
  12. cout << bf.Test("沙悟净") << endl;
  13. cout << bf.Test("孙悟空") << endl;
  14. cout << bf.Test("二郎神") << endl;
  15. cout << bf.Test("二郎神1") << endl;
  16. cout << bf.Test("二郎神2") << endl;
  17. cout << bf.Test("二郎神 ") << endl;
  18. cout << bf.Test("太白晶星") << endl;
  19. }
  20. void test02()
  21. {
  22. srand(time(0));
  23. const size_t N = 100000;
  24. BloomFilter<N * 5> bf;
  25. std::vector<std::string> v1;
  26. //std::string url = "https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html";
  27. std::string url = "猪八戒";
  28. for (size_t i = 0; i < N; ++i)
  29. {
  30. v1.push_back(url + std::to_string(i));
  31. }
  32. for (auto& str : v1)
  33. {
  34. bf.Set(str);
  35. }
  36. // v2跟v1是相似字符串集(前缀一样),但是不一样
  37. std::vector<std::string> v2;
  38. for (size_t i = 0; i < N; ++i)
  39. {
  40. std::string urlstr = url;
  41. urlstr += std::to_string(9999999 + i);
  42. v2.push_back(urlstr);
  43. }
  44. size_t n2 = 0;
  45. for (auto& str : v2)
  46. {
  47. if (bf.Test(str)) // 误判
  48. {
  49. ++n2;
  50. }
  51. }
  52. cout << "相似字符串误判率:" << (double)n2 / (double)N << endl;
  53. // 不相似字符串集
  54. std::vector<std::string> v3;
  55. for (size_t i = 0; i < N; ++i)
  56. {
  57. //string url = "zhihu.com";
  58. string url = "孙悟空";
  59. url += std::to_string(i + rand());
  60. v3.push_back(url);
  61. }
  62. size_t n3 = 0;
  63. for (auto& str : v3)
  64. {
  65. if (bf.Test(str))
  66. {
  67. ++n3;
  68. }
  69. }
  70. cout << "不相似字符串误判率:" << (double)n3 / (double)N << endl;
  71. }
  72. int main()
  73. {
  74. //test01();
  75. test02();
  76. }

 谢谢大家观看!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/621659
推荐阅读
相关标签
  

闽ICP备14008679号