-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloom.c
70 lines (61 loc) · 1.17 KB
/
bloom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include "hash.h"
#include <string.h>
#define MAX 21000000
int *bloom;
int init=0;
hash_function func[11]={RSHash,JSHash,PJWHash,ELFHash,BKDRHash,SDBMHash,DJBHash,DEKHash,BPHash,FNVHash,APHash};
int funclength=11;
unsigned int mod(unsigned int hash)
{
int modnum=MAX<<5;
while(hash>modnum)
{
hash-=modnum;
}
return hash;
}
void setbit(unsigned int hash)
{
bloom[hash>>5]=(1<<(hash&31))|bloom[hash>>5];
}
int getbit(unsigned int hash)
{
return (bloom[hash>>5]>>(hash&31))&1;
}
int urlBloom(char *url)
{
unsigned int hash = 0;
int i = 0,j=0;
int bit;
int flag=1;
if(init==0)
{
bloom=(int*)malloc(sizeof(int)*MAX);
memset(bloom,0,sizeof(int)*MAX);
init=1;
}
j=strlen(url);
while(url[j -1] == 10 || url[j -1] == 13)
{
url[j-1] = '\0';
j --;
}
for(i=0;i<funclength;i++){
hash = func[i](url,strlen(url));
hash = mod(hash);
bit = getbit(hash);
if(bit == 0) break;
}
if(bit==0)
{
flag=0;
for(i=0;i<funclength;i++){
hash = func[i](url,strlen(url));
hash = mod(hash);
setbit(hash);
}
}
return flag;
}