Download code

Jump to: navigation, search

Back to Bloom_filter_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

bloom.h

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Bloom_filter_(C)?oldid=19641
14 */
15 
16 #ifndef __BLOOM_H__
17 #define __BLOOM_H__
18 
19 #include<stdlib.h>
20 
21 typedef unsigned int (*hashfunc_t)(const char *);
22 typedef struct {
23 	size_t asize;
24 	unsigned char *a;
25 	size_t nfuncs;
26 	hashfunc_t *funcs;
27 } BLOOM;
28 
29 BLOOM *bloom_create(size_t size, size_t nfuncs, ...);
30 int bloom_destroy(BLOOM *bloom);
31 int bloom_add(BLOOM *bloom, const char *s);
32 int bloom_check(BLOOM *bloom, const char *s);
33 
34 #endif
35 


hijacker
hijacker
hijacker
hijacker

Makefile

# The authors of this work have released all rights to it and placed it
# in the public domain under the Creative Commons CC0 1.0 waiver
# (http://creativecommons.org/publicdomain/zero/1.0/).
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 
# Retrieved from: http://en.literateprograms.org/Bloom_filter_(C)?oldid=19641

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


hijacker
hijacker
hijacker
hijacker

test.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Bloom_filter_(C)?oldid=19641
14 */
15 
16 
17 #include<stdio.h>
18 #include<string.h>
19 
20 #include"bloom.h"
21 
22 unsigned int sax_hash(const char *key)
23 {
24 	unsigned int h=0;
25 
26 	while(*key) h^=(h<<5)+(h>>2)+(unsigned char)*key++;
27 
28 	return h;
29 }
30 
31 unsigned int sdbm_hash(const char *key)
32 {
33 	unsigned int h=0;
34 	while(*key) h=(unsigned char)*key++ + (h<<6) + (h<<16) - h;
35 	return h;
36 }
37 
38 int main(int argc, char *argv[])
39 {
40 	FILE *fp;
41 	char line[1024];
42 	char *p;
43 	BLOOM *bloom;
44 	
45 	if(argc<2) {
46 		fprintf(stderr, "ERROR: No word file specified\n");
47 		return EXIT_FAILURE;
48 	}
49 
50 	if(!(bloom=bloom_create(2500000, 2, sax_hash, sdbm_hash))) {
51 		fprintf(stderr, "ERROR: Could not create bloom filter\n");
52 		return EXIT_FAILURE;
53 	}
54 
55 	if(!(fp=fopen(argv[1], "r"))) {
56 		fprintf(stderr, "ERROR: Could not open file %s\n", argv[1]);
57 		return EXIT_FAILURE;
58 	}
59 
60 	while(fgets(line, 1024, fp)) {
61 		if((p=strchr(line, '\r'))) *p='\0';
62 		if((p=strchr(line, '\n'))) *p='\0';
63 
64 		bloom_add(bloom, line);
65 	}
66 
67 	fclose(fp);
68 
69 	while(fgets(line, 1024, stdin)) {
70 		if((p=strchr(line, '\r'))) *p='\0';
71 		if((p=strchr(line, '\n'))) *p='\0';
72 
73 		p=strtok(line, " \t,.;:\r\n?!-/()");
74 		while(p) {
75 			if(!bloom_check(bloom, p)) {
76 				printf("No match for word \"%s\"\n", p);
77 			}
78 			p=strtok(NULL, " \t,.;:\r\n?!-/()");
79 		}
80 	}
81 
82 	bloom_destroy(bloom);
83 
84 	return EXIT_SUCCESS;
85 }


hijacker
hijacker
hijacker
hijacker

bloom.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Bloom_filter_(C)?oldid=19641
14 */
15 
16 #include<limits.h>
17 #include<stdarg.h>
18 
19 #include"bloom.h"
20 
21 #define SETBIT(a, n) (a[n/CHAR_BIT] |= (1@<<(n%CHAR_BIT)))
22 #define GETBIT(a, n) (a[n/CHAR_BIT] & (1@<<(n%CHAR_BIT)))
23 
24 BLOOM *bloom_create(size_t size, size_t nfuncs, ...)
25 {
26 	BLOOM *bloom;
27 	va_list l;
28 	int n;
29 	
30 	if(!(bloom=malloc(sizeof(BLOOM)))) return NULL;
31 	if(!(bloom->a=calloc((size+CHAR_BIT-1)/CHAR_BIT, sizeof(char)))) {
32 		free(bloom);
33 		return NULL;
34 	}
35 	if(!(bloom->funcs=(hashfunc_t*)malloc(nfuncs*sizeof(hashfunc_t)))) {
36 		free(bloom->a);
37 		free(bloom);
38 		return NULL;
39 	}
40 
41 	va_start(l, nfuncs);
42 	for(n=0; n<nfuncs; ++n) {
43 		bloom->funcs[n]=va_arg(l, hashfunc_t);
44 	}
45 	va_end(l);
46 
47 	bloom->nfuncs=nfuncs;
48 	bloom->asize=size;
49 
50 	return bloom;
51 }
52 
53 int bloom_destroy(BLOOM *bloom)
54 {
55 	free(bloom->a);
56 	free(bloom->funcs);
57 	free(bloom);
58 
59 	return 0;
60 }
61 
62 int bloom_add(BLOOM *bloom, const char *s)
63 {
64 	size_t n;
65 
66 	for(n=0; n<bloom->nfuncs; ++n) {
67 		SETBIT(bloom->a, bloom->funcs[n](s)%bloom->asize);
68 	}
69 
70 	return 0;
71 }
72 
73 int bloom_check(BLOOM *bloom, const char *s)
74 {
75 	size_t n;
76 
77 	for(n=0; n<bloom->nfuncs; ++n) {
78 		if(!(GETBIT(bloom->a, bloom->funcs[n](s)%bloom->asize))) return 0;
79 	}
80 
81 	return 1;
82 }


hijacker
hijacker
hijacker
hijacker