• 【C++ 学习 ㉖】- 布隆过滤器详解(哈希扩展)


    目录

    一、布隆过滤器的简介

    二、布隆过滤器的实现

    2.1 - BloomFilter.h

    2.2 - test.cpp



    一、布隆过滤器的简介

    布隆过滤器(Bloom Filter)是由 Burton Howard Bloom 在 1970 年提出的一种紧凑型的、比较巧妙的概率型数据结构(probabilistic data structure),特点是高效地插入和查询,可以用来告诉你 "某样东西一定不存在或者可能存在"

    布隆过滤器的原理:当插入一个数据时,通过 K 个哈希函数将这个数据映射到位图结构中的 K 个比特位,并把它们都置为 1。检索时,如果 K 个比特位中的任何一个为 0,则被检索的数据一定不存在;如果都为 1,则被检索的数据可能存在(之所以说 "可能" 是误差的存在)


    二、布隆过滤器的实现

    2.1 - BloomFilter.h

    1. #pragma once
    2. #include "bitset.h"
    3. #include
    4. namespace yzz
    5. {
    6. struct BKDRHash
    7. {
    8. size_t operator()(const std::string& str)
    9. {
    10. size_t hash = 0;
    11. for (const char& ch : str)
    12. {
    13. hash = hash * 131 + ch;
    14. }
    15. return hash;
    16. }
    17. };
    18. struct APHash
    19. {
    20. size_t operator()(const std::string& str)
    21. {
    22. size_t hash = 0;
    23. for (size_t i = 0; i < str.size(); ++i)
    24. {
    25. size_t ch = str[i];
    26. if ((i & 1) == 0)
    27. hash ^= (hash << 7) ^ ch ^ (hash >> 3);
    28. else
    29. hash ^= (~(hash << 11)) ^ ch ^ (hash >> 5);
    30. }
    31. return hash;
    32. }
    33. };
    34. struct DJBHash
    35. {
    36. size_t operator()(const std::string& str)
    37. {
    38. size_t hash = 5381;
    39. for (const char& ch : str)
    40. {
    41. hash = hash * 33 ^ ch;
    42. }
    43. return hash;
    44. }
    45. };
    46. template<size_t N, class K = std::string,
    47. class Hash1 = BKDRHash, class Hash2 = APHash, class Hash3 = DJBHash>
    48. class BloomFilter
    49. {
    50. public:
    51. void set(const K& key)
    52. {
    53. _bs.set(Hash1()(key) % N);
    54. _bs.set(Hash2()(key) % N);
    55. _bs.set(Hash3()(key) % N);
    56. }
    57. bool test(const K& key) const
    58. {
    59. if (_bs.test(Hash1()(key) % N) == 0)
    60. return false;
    61. if (_bs.test(Hash2()(key) % N) == 0)
    62. return false;
    63. if (_bs.test(Hash3()(key) % N) == 0)
    64. return false;
    65. return true;
    66. }
    67. private:
    68. bitset _bs;
    69. };
    70. }

    各种字符串Hash函数 - clq - 博客园 (cnblogs.com)

    注意:传统的布隆过滤器不支持删除操作,因为删除一个数据,可能同时删除了其它数据,例如

    删除 "dantezhao" 的同时,也删除了 "yyj"

    Counting Bloom Filter 的出现,解决了上述问题,它将 Bloom Filter 的位数组的每一位扩展为一个小的计数器(Counter)。插入数据时,给对应的 K(K 为哈希函数的个数)个 Counter 的值分别加 1;删除数据时,给对应的 K 个 Counter 的值分别减 1。Counting Bloom Filter 通过多占用几倍的存储空间的代价,给 Bloom Filter 增加了删除操作

    2.2 - test.cpp

    1. #include "BloomFilter.h"
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. yzz::BloomFilter<100> bf;
    7. bf.set("唐三藏");
    8. bf.set("白龙马");
    9. bf.set("孙悟空");
    10. bf.set("猪八戒");
    11. cout << bf.test("唐三藏") << endl;  // 1
    12. cout << bf.test("白龙马") << endl;  // 1
    13. cout << bf.test("孙悟空") << endl;  // 1
    14. cout << bf.test("猪八戒") << endl;  // 1
    15. cout << bf.test("沙悟净") << endl;  // 0
    16. return 0;
    17. }
  • 相关阅读:
    文件夹名称提取到excel,批量提取
    Mybatis中@MapKey注解简介说明
    Vue(3)
    DBA笔记(1)
    Flink CEP(复杂事件处理)高级进阶
    抖音短视频seo矩阵系统源代码开发系统架构及功能解析
    [Git] 系列四Push & Pull —— Git 远程仓库和高级操作
    【社媒营销】Facebook速推帖子如何运作?值得吗?
    你和我的匆匆四年
    PHP数组处理$arr1转换为$arr2
  • 原文地址:https://blog.csdn.net/melonyzzZ/article/details/133710945