-
Notifications
You must be signed in to change notification settings - Fork 106
/
test_common.c
250 lines (221 loc) · 7.32 KB
/
test_common.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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "bench.c"
#include "bloom.h"
#include "tst.h"
#define TableSize 5000000 /* size of bloom filter */
#define HashNumber 2 /* number of hash functions */
/** constants insert, delete, max word(s) & stack nodes */
enum { INS, DEL, WRDMAX = 256, STKMAX = 512, LMAX = 1024 };
int REF = INS;
#define BENCH_TEST_FILE "bench_ref.txt"
long poolsize = 2000000 * WRDMAX;
/* simple trim '\n' from end of buffer filled by fgets */
static void rmcrlf(char *s)
{
size_t len = strlen(s);
if (len && s[len - 1] == '\n')
s[--len] = 0;
}
#define IN_FILE "cities.txt"
int main(int argc, char **argv)
{
char word[WRDMAX] = "";
char *sgl[LMAX] = {NULL};
tst_node *root = NULL, *res = NULL;
int idx = 0, sidx = 0;
double t1, t2;
int CPYmask = -1;
if (argc < 2) {
printf("too less argument\n");
return 1;
}
if (!strcmp(argv[1], "CPY") || (argc > 2 && !strcmp(argv[2], "CPY"))) {
CPYmask = 0;
REF = DEL;
printf("CPY mechanism\n");
} else
printf("REF mechanism\n");
char *Top = word;
char *pool = NULL;
if (CPYmask) { // Only allacte pool in REF mechanism
pool = malloc(poolsize);
if (!pool) {
fprintf(stderr, "Failed to allocate memory pool.\n");
return 1;
}
Top = pool;
}
FILE *fp = fopen(IN_FILE, "r");
if (!fp) { /* prompt, open, validate file for reading */
fprintf(stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
t1 = tvgetf();
bloom_t bloom = bloom_create(TableSize);
char buf[WORDMAX];
while (fgets(buf, WORDMAX, fp)) {
int offset = 0;
for (int i = 0, j = 0; buf[i + offset]; i++) {
Top[i] =
(buf[i + j] == ',' || buf[i + j] == '\n') ? '\0' : buf[i + j];
j += (buf[i + j] == ',');
}
while (*Top) {
if (!tst_ins(&root, Top, REF)) { /* fail to insert */
fprintf(stderr, "error: memory exhausted, tst_insert.\n");
fclose(fp);
return 1;
}
bloom_add(bloom, Top);
idx++;
int len = strlen(Top);
offset += len + 1;
Top += len + 1;
}
Top -= offset & ~CPYmask;
memset(Top, '\0', WORDMAX);
}
t2 = tvgetf();
fclose(fp);
printf("ternary_tree, loaded %d words in %.6f sec\n", idx, t2 - t1);
if (argc == 3 && strcmp(argv[1], "--bench") == 0) {
int stat = bench_test(root, BENCH_TEST_FILE, LMAX);
tst_free(root);
free(pool);
return stat;
}
FILE *output;
output = fopen("ref.txt", "a");
if (output != NULL) {
fprintf(output, "%.6f\n", t2 - t1);
fclose(output);
} else
printf("open file error\n");
for (;;) {
printf(
"\nCommands:\n"
" a add word to the tree\n"
" f find word in tree\n"
" s search words matching prefix\n"
" d delete word from the tree\n"
" q quit, freeing all data\n\n"
"choice: ");
if (argc > 2 && strcmp(argv[1], "--bench") == 0) // a for auto
strcpy(word, argv[3]);
else
fgets(word, sizeof word, stdin);
switch (*word) {
case 'a':
printf("enter word to add: ");
if (argc > 2 && strcmp(argv[1], "--bench") == 0)
strcpy(Top, argv[4]);
else if (!fgets(Top, sizeof word, stdin)) {
fprintf(stderr, "error: insufficient input.\n");
break;
}
rmcrlf(Top);
t1 = tvgetf();
if (bloom_test(bloom, Top)) /* if detected by filter, skip */
res = NULL;
else { /* update via tree traversal and bloom filter */
bloom_add(bloom, Top);
res = tst_ins(&root, Top, REF);
}
t2 = tvgetf();
if (res) {
idx++;
Top += (strlen(Top) + 1) & CPYmask;
printf(" %s - inserted in %.10f sec. (%d words in tree)\n",
(char *) res, t2 - t1, idx);
}
if (argc > 2 && strcmp(argv[1], "--bench") == 0) // a for auto
goto quit;
break;
case 'f':
printf("find word in tree: ");
if (!fgets(word, sizeof word, stdin)) {
fprintf(stderr, "error: insufficient input.\n");
break;
}
rmcrlf(word);
t1 = tvgetf();
if (bloom_test(bloom, word)) {
t2 = tvgetf();
printf(" Bloomfilter found %s in %.6f sec.\n", word, t2 - t1);
printf(
" Probability of false positives:%lf\n",
pow(1 - exp(-(double) HashNumber /
(double) ((double) TableSize / (double) idx)),
HashNumber));
t1 = tvgetf();
res = tst_search(root, word);
t2 = tvgetf();
if (res)
printf(" ----------\n Tree found %s in %.6f sec.\n",
(char *) res, t2 - t1);
else
printf(" ----------\n %s not found by tree.\n", word);
} else
printf(" %s not found by bloom filter.\n", word);
break;
case 's':
printf("find words matching prefix (at least 1 char): ");
if (argc > 2 && strcmp(argv[1], "--bench") == 0)
strcpy(word, argv[4]);
else if (!fgets(word, sizeof word, stdin)) {
fprintf(stderr, "error: insufficient input.\n");
break;
}
rmcrlf(word);
t1 = tvgetf();
res = tst_search_prefix(root, word, sgl, &sidx, LMAX);
t2 = tvgetf();
if (res) {
printf(" %s - searched prefix in %.6f sec\n\n", word, t2 - t1);
for (int i = 0; i < sidx; i++)
printf("suggest[%d] : %s\n", i, sgl[i]);
} else
printf(" %s - not found\n", word);
if (argc > 2 && strcmp(argv[1], "--bench") == 0) // a for auto
goto quit;
break;
case 'd':
printf("enter word to del: ");
if (!fgets(word, sizeof word, stdin)) {
fprintf(stderr, "error: insufficient input.\n");
break;
}
rmcrlf(word);
printf(" deleting %s\n", word);
t1 = tvgetf();
/* FIXME: remove reference to each string */
res = tst_del(&root, word, REF);
t2 = tvgetf();
if (res)
printf(" delete failed.\n");
else {
printf(" deleted %s in %.6f sec\n", word, t2 - t1);
idx--;
}
break;
case 'q':
goto quit;
default:
fprintf(stderr, "error: invalid selection.\n");
break;
}
}
quit:
free(pool);
/* for REF mechanism */
if (CPYmask)
tst_free(root);
else
tst_free_all(root);
bloom_free(bloom);
return 0;
}