- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章bloom filter概念讲解以及代码分析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
一. 简介 1.什么是bloom filter? Bloom filter 是由 Howard Bloom 在 1970 年提出的二进制向量数据结构,它具有很好的空间和时间效率,被用来检测一个元素是不是集合中的一个成员,这种检测只会对在集合内的数据错判,而不会对不是集合内的数据进行错判,这样每个检测请求返回有“在集合内(可能错误)”和“不在集合内(绝对不在集合内)”两种情况,可见 Bloom filter 是牺牲了正确率换取时间和空间。 2.bloom filter的计算方法? 如需要判断一个元素是不是在一个集合中,我们通常做法是把所有元素保存下来,然后通过比较知道它是不是在集合内,链表、树都是基于这种思路,当集合内元素个数的变大,我们需要的空间和时间都线性变大,检索速度也越来越慢。 Bloom filter 采用的是哈希函数的方法,将一个元素映射到一个 m 长度的阵列上的一个点,当这个点是 1 时,那么这个元素在集合内,反之则不在集合内。这个方法的缺点就是当检测的元素很多的时候可能有冲突,解决方法就是使用 k 个哈希 函数对应 k 个点,如果所有点都是 1 的话,那么元素在集合内,如果有 0 的话,元素则不在集合内.
3.bloom filter的特点? Bloom filter 优点就是它的插入和查询时间都是常数,另外它查询元素却不保存元素本身,具有良好的安全性。它的缺点也是显而易见的,当插入的元素越多,错判“在集合内”的概率就越大了,另外 Bloom filter 也不能删除一个元素,因为多个元素哈希的结果可能在 Bloom filter 结构中占用的是同一个位,如果删除了一个比特位,可能会影响多个元素的检测.
二. 代码实现 现下面在linux下实现了bloom filter的功能代码:
// bloom.h: #ifndef __BLOOM_H__ #define __BLOOM_H__ 。
。
#include<stdlib.h> 。
typedef unsigned int (*hashfunc_t)(const char *); typedef struct { size_t asize; unsigned char *a; size_t nfuncs; hashfunc_t *funcs; } BLOOM,
BLOOM *bloom_create(size_t size, size_t nfuncs, ...); int bloom_destroy(BLOOM *bloom); int bloom_add(BLOOM *bloom, const char *s); int bloom_check(BLOOM *bloom, const char *s),
#endif 。
// bloom.c: #include<limits.h> #include<stdarg.h> 。
#include"bloom.h" 。
#define SETBIT(a, n) (a[n/CHAR_BIT] |= (1<<(n%CHAR_BIT))) #define GETBIT(a, n) (a[n/CHAR_BIT] & (1<<(n%CHAR_BIT))) 。
BLOOM *bloom_create(size_t size, size_t nfuncs, ...) { BLOOM *bloom; va_list l; int n,
if(!(bloom=malloc(sizeof(BLOOM)))) return NULL; if(!(bloom->a=calloc((size+CHAR_BIT-1)/CHAR_BIT, sizeof(char)))) { free(bloom); return NULL; } if(!(bloom->funcs=(hashfunc_t*)malloc(nfuncs*sizeof(hashfunc_t)))) { free(bloom->a); free(bloom); return NULL; } 。
va_start(l, nfuncs); for(n=0; n<nfuncs; ++n) { bloom->funcs[n]=va_arg(l, hashfunc_t); } va_end(l),
bloom->nfuncs=nfuncs; bloom->asize=size,
return bloom; } 。
int bloom_destroy(BLOOM *bloom) { free(bloom->a); free(bloom->funcs); free(bloom),
return 0; } 。
int bloom_add(BLOOM *bloom, const char *s) { size_t n,
for(n=0; n<bloom->nfuncs; ++n) { SETBIT(bloom->a, bloom->funcs[n](s)%bloom->asize); } 。
return 0; } 。
int bloom_check(BLOOM *bloom, const char *s) { size_t n,
for(n=0; n<bloom->nfuncs; ++n) { if(!(GETBIT(bloom->a, bloom->funcs[n](s)%bloom->asize))) return 0; } 。
return 1; } // test.c: #include<stdio.h> #include<string.h> 。
#include"bloom.h" //下面为两种哈希算法函数 unsigned int sax_hash(const char *key) { unsigned int h=0,
while(*key) h^=(h<<5)+(h>>2)+(unsigned char)*key++,
return h; } 。
unsigned int sdbm_hash(const char *key) { unsigned int h=0; while(*key) h=(unsigned char)*key++ + (h<<6) + (h<<16) - h; return h; } 。
int main(int argc, char *argv[]) { FILE *fp; char line[1024]; char *p; BLOOM *bloom,
if(argc<2) { fprintf(stderr, "ERROR: No word file specified\n"); return EXIT_FAILURE; } 。
if(!(bloom=bloom_create(2500000, 2, sax_hash, sdbm_hash))) { fprintf(stderr, "ERROR: Could not create bloom filter\n"); return EXIT_FAILURE; } 。
if(!(fp=fopen(argv[1], "r"))) { fprintf(stderr, "ERROR: Could not open file %s\n", argv[1]); return EXIT_FAILURE; } 。
while(fgets(line, 1024, fp)) { if((p=strchr(line, '\r'))) *p='\0';//回车 if((p=strchr(line, '\n'))) *p='\0';//换行 。
bloom_add(bloom, line); } 。
fclose(fp),
while(fgets(line, 1024, stdin)) { if((p=strchr(line, '\r'))) *p='\0'; if((p=strchr(line, '\n'))) *p='\0',
p=strtok(line, " \t,.;:\r\n?!-/()"); while(p) { if(!bloom_check(bloom, p)) { printf("No match for ford \"%s\"\n", p); } else printf("match for ford \"%s\"\n",p); p=strtok(NULL, " \t,.;:\r\n?!-/()"); } } 。
bloom_destroy(bloom),
return EXIT_SUCCESS; } 。
// Makefile: all: bloom 。
bloom: bloom.o test.o cc -o bloom -Wall -pedantic bloom.o test.o 。
bloom.o: bloom.c bloom.h cc -o bloom.o -Wall -pedantic -ansi -c bloom.c 。
test.o: test.c bloom.h cc -o test.o -Wall -pedantic -ansi -c test.c 。
。
最后此篇关于bloom filter概念讲解以及代码分析的文章就讲到这里了,如果你想了解更多关于bloom filter概念讲解以及代码分析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
一. 简介 1.什么是bloom filter? Bloom filter 是由 Howard Bloom 在 1970 年提出的二进制向量数据结构,它具有很好的空间和时间效率,被用来检测一个元素是
如果我实现了只使用一种散列算法的布隆过滤器(例如 murmur),这是否仍被视为布隆过滤器? 例如,如果 a散列到 5 ,然后将设置过滤器的第 5 位。如 b散列到 1 ,然后将设置过滤器的第 1 位
上下文:我正在阅读有关 RocksDB 和 LSM 树的信息,根据我的理解,Bloom 过滤器用于避免所有存储级别中的项目检索的多个 I/O。我对此没有意见。 显然,挑战之一是布隆过滤器不能用于范围查
您好,在我的系统中将有一个主节点和 n 个从节点,其中主节点会将传入请求分发到其从节点之一。为了利用缓存内存内容,我想跟踪从属节点已经服务的最后 50 个请求(传入请求的哈希值)(假设最后 50 个请
我在 Guava v.11.0.1 中使用了 BloomFilter,当我的插入很大时,我似乎遇到了异常。我用 0.001 fpp 尝试了 1000 万,但失败了。 java.lang.Illegal
我一直在尝试实现我自己的(简单的)Bloom Filter,但一直坚持哈希,我理解多次对项目进行哈希并用索引填充位数组的概念。 但是,我在我的散列中看到了大量的冲突,我正在使用一种散列算法(我已经尝试
引言 在介绍布隆过滤器之前我们首先引入几个场景。 场景一 在一个高并发的计数系统中,如果一个key没有计数,此时我们应该返回0,但是访问的key不存在,相当于每次访问缓存都不起作用了。那么如何
布隆过滤器原理很简单:就是把一个字符串哈希成一个整数key,然后选取一个很长的比特序列,开始都是0,在key把此位置的0变为1;下次进来一个字符串,哈希之后的值key,如果在此比特位上的值也是1,那
使用 Guava 库创建布隆过滤器时,您需要提供一个漏斗和预期的插入次数(以及可选的所需误报率)。有没有办法设置布隆过滤器应该使用哪些哈希函数?如果无法设置哈希函数,默认使用什么? 布隆过滤器是 co
18.0 中的 Guava 布隆过滤器线程安全吗?换句话说,我可以让多个线程共享一个 Bloom Filter 实例并同时使用 myBloomInstance.mightContain 和 myBlo
我有一个使用 Reach 图形设置在 C# XNA 4.0 中编码的 Windows 平台游戏。我的项目基于 GameStateManagement 示例,但后来我向其中添加了 Bloom 和 spr
如何在iOS平台的应用中安装其他应用? 例如,在“Bloom”应用中,拉至底部,点击“PEAK”,会出现“Peek”店铺详情 View ,您可以在该 View 中下载“PEAK”应用。无需打开应用商店
我需要在 C# 中存储 4000 个固定大小(8 个字符)的字符串,但我不知道关于添加和检索项目的空间和时间最好使用什么:布隆过滤器、哈希表或字典?如果有人可以帮助我,请帮助我 最佳答案 在这个问题中
这个问题以前有人问过,但当时没有答案,所以我决定再问一次。 我需要用 C(不是 C++)高效地实现布隆过滤器。如果没有这样的东西可用,我不介意实现一个,如果提供一些很好的引用,这样它就不会占用我太多时
最近几天我阅读了很多关于使用 bloom 等进行后处理的文章,并且我能够通过一个单独的着色器运行这个纹理来实现渲染到纹理的功能。现在我对整个事情有一些疑问。 我必须同时渲染两者吗?场景和纹理放在全屏四
我又遇到了一个我自己似乎无法解决的僵局。我真的希望有人能帮助我。 我一直在尝试使用 GLSL 创建一个漂亮的小光晕效果,效果非常好。当我尝试在我的场景中加入一些移动的东西时,我注意到我忘记在渲染到它们
我有一个 Spark 作业,其最终输出是一个 Algebird 布隆过滤器,我需要在另一个 Spark 作业中重用这个布隆过滤器。有没有办法使用 Twitter Storehaus 将此布隆过滤器存储
我已经使用 OpenGL 和 GLSL 集成了bloom HDR 渲染......至少我认为!我不太确定结果。 我遵循了英特尔网站上的教程: https://software.intel.com/en
我正在关注这个 tutorial用于绽放效果。 我正在使用 Qt,我尝试渲染的场景由五个立方体和五个随机放置的灯组成,类似于这样。 不幸的是,当我尝试使用帧缓冲区时,渲染的场景完全是空的(只有背景颜色
Hadoop 新手...我有一系列 HDFS 目录,命名约定为 filename.seq。每个目录包含一个索引、数据和 bloom 文件。这些具有二进制内容并且似乎是 SequenceFiles(SE
我是一名优秀的程序员,十分优秀!