控制台字体颜色参考了https://blog.csdn.net/key_point/article/details/52667273这篇博文。代码使用哈希表实现了一个简易布隆过滤器,过滤器中存储了C++的所有关键字用于查询操作!详解待后续文章,先上代码!

头文件BitMap.h:

 1 /*BitMap.h*/
 2
 3 #ifndef BITMAP_H_INCLUDED
 4 #define BITMAP_H_INCLUDED
 5
 6 #include <stdio.h>
 7 #include <assert.h>
 8 #include <stdlib.h>
 9 #include <windows.h>
10
11 typedef struct BitMap
12 {
13     size_t* _bits;
14     size_t _range;
15 } BitMap;
16
17 #endif // BITMAP_H_INCLUDED

头文件BloomFilter.h:

 1 /*BloomFilter.h*/
 2
 3 #ifndef BLOOMFILTER_H_INCLUDED
 4 #define BLOOMFILTER_H_INCLUDED
 5
 6 #include "BitMap.h"
 7
 8 typedef size_t(*HASH_FUNC)(const char* str);
 9
10 typedef struct                     /*5个哈希函数*/
11 {
12     BitMap _bm;
13     HASH_FUNC hashfunc1;
14     HASH_FUNC hashfunc2;
15     HASH_FUNC hashfunc3;
16     HASH_FUNC hashfunc4;
17     HASH_FUNC hashfunc5;
18 } BloomFilter;
19
20 void BloomFilterInit(BloomFilter* bf, size_t range);
21 void BloomFilterSet(BloomFilter* bf, const char* x);
22 void BloomFilterReset(BloomFilter* bf, const char* x);
23 void BloomFilterTest();
24 void BloomFilterDestory(BloomFilter* bf);
25
26
27
28 #endif // BLOOMFILTER_H_INCLUDED

源文件BloomFilter.c:

  1 /*BloomFilter.c*/
  2
  3 #include "BloomFilter.h"
  4
  5 static size_t BKDRHash(const char* str)
  6 {
  7     size_t seed = 131;  // 31 131 1313 13131 131313
  8     size_t hash = 0;
  9     while (*str)
 10     {
 11         hash = hash * seed + (*str++);
 12     }
 13     return (hash & 0x7FFFFFFF);
 14 }
 15
 16 size_t SDBMHash(const char* str)
 17 {
 18     size_t ch;
 19     size_t hash = 0;
 20     while (ch = (size_t)*str++)
 21     {
 22         hash = 65599 * hash + ch;
 23         //hash = (size_t)ch + (hash << 6) + (hash << 16) - hash;
 24     }
 25     return hash;
 26 }
 27
 28 size_t RSHash(const char* str)
 29 {
 30     size_t ch;
 31     size_t hash = 0;
 32     size_t magic = 63689;
 33     while (ch = (size_t)*str++)
 34     {
 35         hash = hash * magic + ch;
 36         magic *= 378551;
 37     }
 38     return hash;
 39 }
 40
 41 size_t FHash(const char *str)
 42 {
 43     size_t ch;
 44     size_t hash = 0;
 45     size_t magic = 61456;
 46     while (ch = (size_t)*str++)
 47     {
 48         hash = hash * magic + ch;
 49         magic = magic * 45616;
 50     }
 51     return hash;
 52 }
 53
 54 size_t FiHash(const char *str)
 55 {
 56     size_t ch;
 57     size_t hash = 0;
 58     size_t magic = 60000;
 59     while (ch = (size_t)*str++)
 60     {
 61         hash = hash * magic + ch;
 62         magic = magic * 111;
 63     }
 64     return hash;
 65 }
 66
 67 void BloomFilterInit(BloomFilter* bf, size_t range)
 68 {
 69     assert(bf);
 70     BitMapInit(&bf->_bm, range);
 71     bf->hashfunc1 = BKDRHash;
 72     bf->hashfunc2 = SDBMHash;
 73     bf->hashfunc3 = RSHash;
 74     bf->hashfunc4 = FHash;
 75     bf->hashfunc5 = FiHash;
 76 }
 77
 78 void BloomFilterSet(BloomFilter* bf, const char* x)
 79 {
 80     size_t hash1, hash2, hash3, hash4, hash5;
 81     hash1 = bf->hashfunc1(x) % bf->_bm._range;
 82     hash2 = bf->hashfunc2(x) % bf->_bm._range;
 83     hash3 = bf->hashfunc3(x) % bf->_bm._range;
 84     hash4 = bf->hashfunc4(x) % bf->_bm._range;
 85     hash5 = bf->hashfunc5(x) % bf->_bm._range;
 86     BitMapSet(&bf->_bm, hash1);
 87     BitMapSet(&bf->_bm, hash2);
 88     BitMapSet(&bf->_bm, hash3);
 89     BitMapSet(&bf->_bm, hash4);
 90     BitMapSet(&bf->_bm, hash5);
 91 }
 92 int BloomFilterFind(BloomFilter* bf, const char* x)
 93 {
 94
 95     size_t hash1, hash2, hash3, hash4, hash5;
 96     hash1 = bf->hashfunc1(x) % bf->_bm._range;
 97     hash2 = bf->hashfunc2(x) % bf->_bm._range;
 98     hash3 = bf->hashfunc3(x) % bf->_bm._range;
 99     hash4 = bf->hashfunc4(x) % bf->_bm._range;
100     hash5 = bf->hashfunc5(x) % bf->_bm._range;
101     if (BitMapTest(&bf->_bm, hash1) == -1)
102         return -1;
103     if (BitMapTest(&bf->_bm, hash2) == -1)
104         return -1;
105     if (BitMapTest(&bf->_bm, hash3) == -1)
106         return -1;
107     if (BitMapTest(&bf->_bm, hash4) == -1)
108         return -1;
109     if (BitMapTest(&bf->_bm, hash5) == -1)
110         return -1;
111     return 0;
112 }
113
114 void BloomFilterDestory(BloomFilter* bf)
115 {
116     assert(bf);
117     BitMapDestroy(&bf->_bm);
118 }

源文件test.c:

 1 /*test.c*/
 2
 3 #include "BloomFilter.h"
 4
 5 int main()
 6 {
 7     int n;
 8     BloomFilter bf;
 9     char str[50000];
10     FILE *out, *in;
11     out = fopen("关键字.txt", "r");
12     BloomFilterInit(&bf, 10000);
13     while (!feof(out))
14     {
15
16         fscanf(out, "%s", str);
17         /*  printf("%s\n",str);*/
18         BloomFilterSet(&bf, str);
19     }
20     printf("                      ****************请输入你要执行的操作************************\n");
21     printf("                      ****************1.查询是否存在该关键字***********************\n");
22     printf("                      ****************2.贮存关键字*********************************\n");
23     printf("                      ****************3.结束此次操作*******************************\n");
24     while (scanf("%d", &n) != EOF)
25     {
26         if (n == 3)
27             break;
28         if (n == 1)
29         {
30             printf("请输入你要查询的代码,按#回到主界面\n");
31             while (1)
32             {
33
34                 scanf("%s", str);
35                 if (str[0] == '#')
36                     break;
37
38                 if (BloomFilterFind(&bf, str) == 0)
39                 {
40                     printf("该关键字(");
41                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
42                              FOREGROUND_BLUE |FOREGROUND_INTENSITY );
43                     printf("%s", str);
44                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
45                              FOREGROUND_RED |FOREGROUND_GREEN | FOREGROUND_BLUE);
46                     printf(")在表中!\n");
47                 }
48                 else
49                 {
50                     printf("该关键字(");
51                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
52                              FOREGROUND_RED |FOREGROUND_INTENSITY );
53                     printf("%s",str);
54                     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
55                              FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
56                     printf(")不在表中,请检查是否拼写错误!或者将其加入表中!\n");
57                 }
58             }
59         }
60         if (n == 2)
61         {
62             while (1)
63             {
64                 printf("请输入你要保存的关键字\n按0退出\n");
65                 scanf("%s", str);
66                 if (str[0] == '0')
67                     break;
68                 in = fopen("关键字.txt", "a");
69                 fprintf(in, "\n%s", str);
70             }
71         }
72     }
73     return 0;
74 }

程序运行截图如下:

A Brief Bloom Filter(英文标题唬人罢了)相关推荐

  1. Bloom Filter算法

    一.概念 Bloom Filter的中文翻译叫做布隆过滤器,是1970年由布隆提出的.它实际上是一个很长的二进制向量和一系列随机映射函数.布隆过滤器可以用于检索一个元素是否在一个集合中.它的优点是空间 ...

  2. Redis缓存击穿解决办法之bloom filter布隆过滤器

    转载地址:http://blog.jobbole.com/113396/ 布隆过滤器:Google Guava类库源码分析及基于Redis Bitmaps的重构 2017/12/30 · 开发 · B ...

  3. C++拾取——Linux下实测布隆过滤器(Bloom filter)和unordered_multiset查询效率

    布隆过滤器是一种判定元素是否存在于集合中的方法.其基本原理是使用哈希方法将数据映射到一个很长的向量上.在维基百科上,它被称为"空间效率和查询时间都远远超过一般的算法"的方法.由于它 ...

  4. Bloom Filter:海量数据的HashSet

    Bloom Filter一般用于数据的去重计算,近似于HashSet的功能:但是不同于Bitmap(用于精确计算),其为一种估算的数据结构,存在误判(false positive)的情况. 1. 基本 ...

  5. mysql布隆过滤器源码_布隆过滤器(Bloom Filter)的原理和实现

    什么情况下需要布隆过滤器? 先来看几个比较常见的例子 字处理软件中,需要检查一个英语单词是否拼写正确 在 FBI,一个嫌疑人的名字是否已经在嫌疑名单上 在网络爬虫里,一个网址是否被访问过 yahoo, ...

  6. 布隆过滤器+布隆过滤器(Bloom Filter)详解

    布隆过滤器+布隆过滤器(Bloom Filter)详解 程序 = 数据结构 + 算法 -- 图灵奖得主,计算机科学家N.Wirth(沃斯) A Bloom filter is a space effi ...

  7. 探索C#之布隆过滤器(Bloom filter)

    阅读目录: 背景介绍 算法原理 误判率 BF改进 总结 背景介绍 Bloom filter(后面简称BF)是Bloom在1970年提出的二进制向量数据结构.通俗来说就是在大数据集合下高效判断某个成员是 ...

  8. 大数据量下的集合过滤—Bloom Filter

    算法背景 如果想判断一个元素是不是在一个集合里,一般想到的是将集合中所有元素保存起来,然后通过比较确定.链表.树.散列表(又叫哈希表,Hash table)等等数据结构都是这种思路,存储位置要么是磁盘 ...

  9. Bloom Filter 大规模数据处理利器

    2019独角兽企业重金招聘Python工程师标准>>> 最近工作中涉及到bloom Filter,真是一把科研利器呀,大数据.网络.云等等都可以用到! Bloom Filter是由B ...

最新文章

  1. CALMS:多语言摘要中的信息抽取与共享 | ​ACL 2021 Findings
  2. 常见Web安全漏洞--------sql注入
  3. Linux数据报文接收发送总结2
  4. Hyperic HQ 应用性能管理解决方案
  5. power bi_如何将Power BI模型的尺寸减少90%!
  6. [react] React的事件和普通的HTML事件有什么不同
  7. 【Shell 编程基础第一部分】Shell脚本HelloShell及简单的Shell基础
  8. 虚拟服务器怎么搭建php,怎样搭建Apache+MySQL+PHP服务器
  9. 深入学习之mysql(二)表的操作
  10. PHP如果某商品下的所有货品库存都为0,则下架该商品
  11. 计算机毕业论文指导过程记录表6,毕业论文指导记录表范文
  12. pc彩色版伏魔记开放Java源码
  13. 系统版本aplpha,Beta等表示的意思
  14. 教教大家Win10怎么看处理器是几核
  15. python自动玩2048
  16. matlab中geoshow函数的使用/属性设置
  17. UE4-植被系统使用
  18. MCMC和贝叶斯统计在宇宙微波背景辐射(CMB)中应用
  19. 海康威视(hikvision) demo部署
  20. 爱普生打印机怎么安装使用

热门文章

  1. [书目20160623]编程匠艺——编写卓越的代码
  2. 使用wps2019去除pdf的水印
  3. 华为麒麟990国产(笔记本/UOS操作系统)搭载h5stream视频流媒体软件
  4. Vue组件通信以及.sync修饰符的使用
  5. 全球首个AI框架CC EAL2+证书 ,昇思MindSpore推开可信AI大门
  6. docker 集群的优点
  7. C++笔记(16)函数默认参数和函数重载
  8. 7-28 猴子选大王 (20 分)
  9. 解决docker拉取镜像慢的方法(亲测有效)
  10. 如何使用xcode将应用安装到IPhone上