forked from sysprog21/prefix-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ref.c
197 lines (176 loc) · 5.44 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#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
/* timing helper function */
static double tvgetf(void)
{
struct timespec ts;
double sec;
clock_gettime(CLOCK_REALTIME, &ts);
sec = ts.tv_nsec;
sec /= 1e9;
sec += ts.tv_sec;
return sec;
}
/* 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"
#define TEST_INPUT "tinput.txt"
#define TEST_OUTPUT "ref_bench.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();
while ((rtn = fscanf(fp, "%s", word)) != EOF) {
char *p = strdup(word);
if (word[strlen(word)-1] == ',') word[strlen(word)-1] = 0;
if (!tst_ins_del(&root, &p, INS, REF)) {
fprintf(stderr, "error: memory exhausted, tst_insert.\n");
fclose(fp);
return 1;
}
idx++;
}
t2 = tvgetf();
fclose(fp);
/* test bench */
if (argc >= 2 && !strncmp(argv[1], "--bench", 7)) {
fp = fopen(TEST_INPUT, "r");
int cnt = 0;
if (!fp) {
fprintf(stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
FILE *fp2 = fopen(TEST_OUTPUT, "w");
if (!fp2) {
fclose(fp);
fprintf(stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
while (fscanf(fp, "%s", word) != EOF) {
t1 = tvgetf();
tst_search_prefix(root, word, sgl, &sidx, LMAX);
t2 = tvgetf();
/* save for gnuplot */
fprintf(fp2, "%d %.6f\n", ++cnt, t2 - t1);
printf("%d %.6f\n", cnt, t2 - t1);
}
fclose(fp);
fclose(fp2);
return 0;
}
printf("ternary_tree, loaded %d words in %.6f sec\n", idx, t2 - t1);
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: ");
fgets(word, sizeof word, stdin);
p = NULL;
switch (*word) {
case 'a':
printf("enter word to add: ");
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, REF);
t2 = tvgetf();
if (res) {
idx++;
printf(" %s - inserted in %.6f sec. (%d words in tree)\n",
(char *) res, t2 - t1, idx);
} else
printf(" %s - already exists in list.\n", (char *) res);
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 (!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);
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, REF);
t2 = tvgetf();
if (res)
printf(" delete failed.\n");
else {
printf(" deleted %s in %.6f sec\n", word, t2 - t1);
idx--;
}
break;
case 'q':
tst_free_all(root);
return 0;
break;
default:
fprintf(stderr, "error: invalid selection.\n");
break;
}
}
return 0;
}