布隆过滤器
Posted SimonKly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了布隆过滤器相关的知识,希望对你有一定的参考价值。
1 /***************************************************** 2 copyright (C), 2014-2015, Lighting Studio. Co., Ltd. 3 File name: 4 Author:Jerey_Jobs Version:0.1 Date: 5 Description: 6 Funcion List: 7 *****************************************************/ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <math.h> 12 #include <time.h> 13 14 #define S_size 10000 15 #define barrel_size 5000 16 #define Hash_count 4 17 18 int hashvalue(int data,double rand_num) 19 { 20 // double rand_num; 21 int hash; 22 // rand_num=(rand()%barrel_size)/(double)barrel_size; 23 hash=(int)(barrel_size*(fmod(rand_num*data,1.0))); 24 25 return hash; 26 } 27 28 void creat_randnum(double *hash) 29 { 30 int i; 31 //srand(time(0)); 32 for(i=0;i<Hash_count;i++) 33 { 34 *(hash+i)=(rand()%(barrel_size/10))/(double)barrel_size; 35 } 36 } 37 38 int main() 39 { 40 int S[S_size];//S集合 41 int barrel[barrel_size]={0};//桶的大小 42 int data; 43 int i=0; 44 int count_estimate=0;//估计值 45 int count_only=0;//query中在S集合中的个数 46 double hash[Hash_count];//hash的值 47 srand((int)time(NULL)); 48 FILE *fp=fopen("./stream_for_bm.txt","r");// 49 creat_randnum(hash); 50 if(fp == NULL)//stream_for_bm.txt打开失败 51 { 52 printf("stream_for_bm can‘t open\n‘"); 53 return 0; 54 } 55 56 while(!feof(fp)) 57 { 58 fscanf(fp,"%d",&data);//存入S集合 59 S[i++]=data; 60 61 for(int j=0;j<Hash_count;j++)//Hash_count数目个hash函数 62 { 63 barrel[hashvalue(data,hash[j])]=1;//存入桶中 64 } 65 } 66 67 FILE *fp_query=fopen("./stream_for_query.txt","r"); 68 if(fp_query == NULL) 69 { 70 printf("stream_for_query.txt can‘t open\n"); 71 return 0; 72 } 73 while(!feof(fp_query)) 74 { 75 fscanf(fp_query,"%d",&data); 76 for(int j=0;j<S_size;j++) 77 { 78 if(data == S[j]) 79 count_only++;//精确值 80 } 81 int tmp_count=0; 82 for(int j=0;j<Hash_count;j++)//Hash_count数目个hash函数 83 { 84 if(barrel[hashvalue(data,hash[j])] == 0) 85 break; 86 else 87 tmp_count++; 88 } 89 if(tmp_count == Hash_count) 90 count_estimate++; 91 } 92 printf("count_only=%d\ncount_estimate=%d\n",count_only,count_estimate); 93 printf("error rate:%.2lf\n",(count_estimate-count_only)/(double)20000); 94 95 fclose(fp); 96 fclose(fp_query); 97 system("pause"); 98 return 0; 99 }
以上是关于布隆过滤器的主要内容,如果未能解决你的问题,请参考以下文章