forked from sysprog21/dict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cpy.c
188 lines (173 loc) · 5.62 KB
/
test_cpy.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "bloom.h"
#include "bench.c"
#include "tst.h"
/** constants insert, delete, max word(s) & stack nodes */
enum { INS, DEL, WRDMAX = 256, STKMAX = 512, LMAX = 1024 };
#define REF INS
#define CPY DEL
long poolsize = 2000000 * WRDMAX;
#define BENCH_TEST_FILE "bench_cpy.txt"
#define TableSize 5000000
/* 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);
// break;
}
t2 = tvgetf();
fclose(fp);
printf("ternary_tree, loaded %d words in %.6f sec\n", idx, t2 - t1);
if (argc == 2 && strcmp(argv[1], "--bench") == 0) {
int stat = bench_test(root, BENCH_TEST_FILE, LMAX);
tst_free_all(root);
return stat;
}
FILE *output;
output = fopen("cpy.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(word, argv[3]);
else if (!fgets(word, sizeof word, stdin)) {
fprintf(stderr, "error: insufficient input.\n");
break;
}
rmcrlf(word);
p = word;
t1 = tvgetf();
res = tst_ins_del(&root, &p, INS, CPY);
t2 = tvgetf();
if (res) {
idx++;
printf(" %s - inserted in %.12f 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();
res = tst_search(root, word);
t2 = tvgetf();
if (res)
printf(" found %s in %.6f sec.\n", (char *) res, t2 - t1);
else
printf(" %s not found.\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();
res = tst_ins_del(&root, &p, DEL, CPY);
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;
}
}
return 0;
}