forked from sysprog21/dict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ref.c
218 lines (194 loc) · 6.67 KB
/
test_ref.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
#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 };
#define REF INS
#define CPY DEL
#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 rtn = 0, idx = 0, sidx = 0;
FILE *fp = fopen(IN_FILE, "r");
double t1, t2;
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);
/* memory pool */
char *pool = (char *) malloc(poolsize * sizeof(char));
char *Top = pool;
while ((rtn = fscanf(fp, "%s", Top)) != EOF) {
char *p = Top;
/* insert reference to each string */
if (!tst_ins_del(&root, &p, INS, REF)) { /* fail to insert */
fprintf(stderr, "error: memory exhausted, tst_insert.\n");
fclose(fp);
return 1;
} else { /* update bloom filter */
bloom_add(bloom, Top);
}
idx++;
Top += (strlen(Top) + 1);
}
t2 = tvgetf();
fclose(fp);
printf("ternary_tree, loaded %d words in %.6f sec\n", idx, t2 - t1);
if (argc == 2 && strcmp(argv[1], "--bench1") == 0) {
int stat = bench_test(root, BENCH_TEST_FILE, LMAX);
tst_free_all(root);
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 (;;) {
char *p;
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 > 1 && strcmp(argv[1], "--bench") == 0) // a for auto
strcpy(word, argv[2]);
else
fgets(word, sizeof word, stdin);
p = NULL;
switch (*word) {
case 'a':
printf("enter word to add: ");
if (argc > 1 && strcmp(argv[1], "--bench") == 0)
strcpy(Top, argv[3]);
else if (!fgets(Top, sizeof word, stdin)) {
fprintf(stderr, "error: insufficient input.\n");
break;
}
rmcrlf(Top);
p = Top;
t1 = tvgetf();
if (bloom_test(bloom, Top) == 1) /* if detected by filter, skip */
res = NULL;
else { /* update via tree traversal and bloom filter */
bloom_add(bloom, Top);
res = tst_ins_del(&root, &p, INS, REF);
}
t2 = tvgetf();
if (res) {
idx++;
Top += (strlen(Top) + 1);
printf(" %s - inserted in %.10f sec. (%d words in tree)\n",
(char *) res, t2 - t1, idx);
} else
printf(" %s - already exists in list.\n", (char *) res);
if (argc > 1 && 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) == 1) {
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 > 1 && strcmp(argv[1], "--bench") == 0)
strcpy(word, argv[3]);
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 > 1 && 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);
p = word;
printf(" deleting %s\n", word);
t1 = tvgetf();
/* FIXME: remove reference to each string */
res = tst_ins_del(&root, &p, DEL, REF);
t2 = tvgetf();
if (res)
printf(" delete failed.\n");
else {
printf(" deleted %s in %.6f sec\n", word, t2 - t1);
idx--;
}
break;
quit:
case 'q':
tst_free(root);
return 0;
break;
default:
fprintf(stderr, "error: invalid selection.\n");
break;
}
}
bloom_free(bloom);
return 0;
}