-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw4_submitversion_2.c
331 lines (283 loc) · 8.35 KB
/
hw4_submitversion_2.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h>
#include <stdlib.h>
#define BLOCKSIZE 256
typedef char object_t;
//#define MAXP 46337 /* prime, and 46337*46337 < 2147483647 */
#define MAXP 15485863
typedef struct l_node { char *key;
object_t *obj;
struct l_node *next; } list_node_t;
typedef struct htp_l_node { int a;
struct htp_l_node *next; } htp_l_node_t;
typedef struct { int b; int size;
struct htp_l_node *a_list;} hf_param_t;
char b_array_0[250000];
char b_array_1[250000];
char b_array_2[250000];
char b_array_3[250000];
char b_array_4[250000];
char b_array_5[250000];
char b_array_6[250000];
char b_array_7[250000];
typedef struct bloom {
hf_param_t param_0;
hf_param_t param_1;
hf_param_t param_2;
hf_param_t param_3;
hf_param_t param_4;
hf_param_t param_5;
hf_param_t param_6;
hf_param_t param_7;
} bf_t;
bf_t new_bloom;
list_node_t *currentblock = NULL;
int size_left;
list_node_t *free_list = NULL;
list_node_t *get_node()
{ list_node_t *tmp;
if( free_list != NULL )
{ tmp = free_list;
free_list = free_list -> next;
}
else
{ if( currentblock == NULL || size_left == 0)
{ currentblock =
(list_node_t *) malloc( BLOCKSIZE * sizeof(list_node_t) );
size_left = BLOCKSIZE;
}
tmp = currentblock++;
size_left -= 1;
}
return( tmp );
}
void return_node(list_node_t *node)
{ node->next = free_list;
free_list = node;
}
int universalhashfunction(char *key, hf_param_t hfp)
{ int sum;
htp_l_node_t *al;
sum = hfp.b;
al = hfp.a_list;
while( *key != '\0' )
{ if( al->next == NULL )
{ al->next = (htp_l_node_t *) get_node();
al->next->next = NULL;
al->a = rand()%MAXP;
}
sum += ( (al->a)*((int) *key))%MAXP;
key += 1;
al = al->next;
}
return ( abs(sum)%hfp.size );
}
bf_t * create_bf() {
// create 8 bit arrays, each size is 250,000 char
//bf_t *new_bloom;
int size = 8;
int i,j;
for (j=0;j<250000;j++){
b_array_0[j] = 0;
b_array_1[j] = 0;
b_array_2[j] = 0;
b_array_3[j] = 0;
b_array_4[j] = 0;
b_array_5[j] = 0;
b_array_6[j] = 0;
b_array_7[j] = 0;
}
// for(i=0; i < size; i++) {
// new_bloom->param[i].b = rand()%MAXP;
// new_bloom->param[i].size = 2000000;
// new_bloom->param[i].a_list = (htp_l_node_t *) get_node();
// new_bloom->param[i].a_list->next = NULL;
// }
new_bloom.param_0.b = rand()%MAXP;
new_bloom.param_0.size = 2000000;
new_bloom.param_0.a_list = (htp_l_node_t *) get_node();
new_bloom.param_0.a_list->next = NULL;
new_bloom.param_1.b = rand()%MAXP;
new_bloom.param_1.size = 2000000;
new_bloom.param_1.a_list = (htp_l_node_t *) get_node();
new_bloom.param_1.a_list->next = NULL;
new_bloom.param_2.b = rand()%MAXP;
new_bloom.param_2.size = 2000000;
new_bloom.param_2.a_list = (htp_l_node_t *) get_node();
new_bloom.param_2.a_list->next = NULL;
new_bloom.param_3.b = rand()%MAXP;
new_bloom.param_3.size = 2000000;
new_bloom.param_3.a_list = (htp_l_node_t *) get_node();
new_bloom.param_3.a_list->next = NULL;
new_bloom.param_4.b = rand()%MAXP;
new_bloom.param_4.size = 2000000;
new_bloom.param_4.a_list = (htp_l_node_t *) get_node();
new_bloom.param_4.a_list->next = NULL;
new_bloom.param_5.b = rand()%MAXP;
new_bloom.param_5.size = 2000000;
new_bloom.param_5.a_list = (htp_l_node_t *) get_node();
new_bloom.param_5.a_list->next = NULL;
new_bloom.param_6.b = rand()%MAXP;
new_bloom.param_6.size = 2000000;
new_bloom.param_6.a_list = (htp_l_node_t *) get_node();
new_bloom.param_6.a_list->next = NULL;
new_bloom.param_7.b = rand()%MAXP;
new_bloom.param_7.size = 2000000;
new_bloom.param_7.a_list = (htp_l_node_t *) get_node();
new_bloom.param_7.a_list->next = NULL;
return &new_bloom;
}
void insert_bf(bf_t *b, char *s) {
int i=0;
int h_result;
h_result = universalhashfunction(s,b->param_0);
b_array_0[h_result/8] |= 1 <<(h_result%8);
h_result = universalhashfunction(s,b->param_1);
b_array_1[h_result/8] |= 1 <<(h_result%8);
h_result = universalhashfunction(s,b->param_2);
b_array_2[h_result/8] |= 1 <<(h_result%8);
h_result = universalhashfunction(s,b->param_3);
b_array_3[h_result/8] |= 1 <<(h_result%8);
i++;
h_result = universalhashfunction(s,b->param_4);
b_array_4[h_result/8] |= 1 <<(h_result%8);
i++;
h_result = universalhashfunction(s,b->param_5);
b_array_5[h_result/8] |= 1 <<(h_result%8);
i++;
h_result = universalhashfunction(s,b->param_6);
b_array_6[h_result/8] |= 1 <<(h_result%8);
i++;
h_result = universalhashfunction(s,b->param_7);
b_array_7[h_result/8] |= 1 <<(h_result%8);
return;
}
int is_element(bf_t *b, char *q) {
int i=0;
int k;
k = universalhashfunction(q,b->param_0);
if ((b_array_0[k/8] & (1 << (k%8) )) == 0)
return 0;
k = universalhashfunction(q,b->param_1);
if ((b_array_1[k/8] & (1 << (k%8) )) == 0)
return 0;
k = universalhashfunction(q,b->param_2);
if ((b_array_2[k/8] & (1 << (k%8) )) == 0)
return 0;
k = universalhashfunction(q,b->param_3);
if ((b_array_3[k/8] & (1 << (k%8) )) == 0)
return 0;
k = universalhashfunction(q,b->param_4);
if ((b_array_4[k/8] & (1 << (k%8) )) == 0)
return 0;
k = universalhashfunction(q,b->param_5);
if ((b_array_5[k/8] & (1 << (k%8) )) == 0)
return 0;
k = universalhashfunction(q,b->param_6);
if ((b_array_6[k/8] & (1 << (k%8) )) == 0)
return 0;
k = universalhashfunction(q,b->param_7);
if ((b_array_7[k/8] & (1 << (k%8) )) == 0)
return 0;
return 1;
}
void sample_string_A(char *s, long i)
{ s[0] = (char)(1 + (i%254));
s[1] = (char)(1 + ((i/254)%254));
s[2] = (char)(1 + (((i/254)/254)%254));
s[3] = 'a';
s[4] = 'b';
s[5] = (char)(1 + ((i*(i-3)) %217));
s[6] = (char)(1 + ((17*i+129)%233 ));
s[7] = '\0';
}
void sample_string_B(char *s, long i)
{ s[0] = (char)(1 + (i%254));
s[1] = (char)(1 + ((i/254)%254));
s[2] = (char)(1 + (((i/254)/254)%254));
s[3] = 'a';
s[4] = (char)(1 + ((i*(i-3)) %217));
s[5] = (char)(1 + ((17*i+129)%233 ));
s[6] = '\0';
}
void sample_string_C(char *s, long i)
{ s[0] = (char)(1 + (i%254));
s[1] = (char)(1 + ((i/254)%254));
s[2] = 'a';
s[3] = (char)(1 + ((i*(i-3)) %217));
s[4] = (char)(1 + ((17*i+129)%233 ));
s[5] = '\0';
}
void sample_string_D(char *s, long i)
{ s[0] = (char)(1 + (i%254));
s[1] = (char)(1 + ((i/254)%254));
s[2] = (char)(1 + (((i/254)/254)%254));
s[3] = 'b';
s[4] = 'b';
s[5] = (char)(1 + ((i*(i-3)) %217));
s[6] = (char)(1 + ((17*i+129)%233 ));
s[7] = '\0';
}
void sample_string_E(char *s, long i)
{ s[0] = (char)(1 + (i%254));
s[1] = (char)(1 + ((i/254)%254));
s[2] = (char)(1 + (((i/254)/254)%254));
s[3] = 'a';
s[4] = (char)(2 + ((i*(i-3)) %217));
s[5] = (char)(1 + ((17*i+129)%233 ));
s[6] = '\0';
}
int main()
{ long i,j;
bf_t * bloom;
bloom = create_bf();
printf("Created Filter\n");
for( i= 0; i< 1450000; i++ )
{ char s[8];
sample_string_A(s,i);
insert_bf( bloom, s );
}
for( i= 0; i< 500000; i++ )
{ char s[7];
sample_string_B(s,i);
insert_bf( bloom, s );
}
for( i= 0; i< 50000; i++ )
{ char s[6];
sample_string_C(s,i);
insert_bf( bloom, s );
}
printf("inserted 2,000,000 strings of length 8,7,6.\n");
for( i= 0; i< 1450000; i++ )
{ char s[8];
sample_string_A(s,i);
if( is_element( bloom, s ) != 1 )
{ printf("found negative error (1)\n"); exit(0); }
}
for( i= 0; i< 500000; i++ )
{ char s[7];
sample_string_B(s,i);
if( is_element( bloom, s ) != 1 )
{ printf("found negative error (2)\n"); exit(0); }
}
for( i= 0; i< 50000; i++ )
{ char s[6];
sample_string_C(s,i);
if( is_element( bloom, s ) != 1 )
{ printf("found negative error (3)\n"); exit(0); }
}
j = 0;
for( i= 0; i< 500000; i++ )
{ char s[8];
sample_string_D(s,i);
if( is_element( bloom, s ) != 0 )
j+=1;
}
for( i= 0; i< 500000; i++ )
{ char s[7];
sample_string_E(s,i);
if( is_element( bloom, s ) != 0 )
j+=1;
}
printf("Found %d positive errors out of 1,000,000 tests.\n",j);
printf("Positive error rate %f\%.\n", (float)j/10000.0);
}