-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.c
316 lines (270 loc) · 8.52 KB
/
index.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
/* ################################################################### */
/* Copyright 2015, Pierre Gentile (p.gen.progs@gmail.com) */
/* */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* ################################################################### */
/* ************************************************************************ */
/* Ternary Search Tree and sorted array creation functions. */
/* Inspired by a code described in "Ternary Search Trees" by Jon */
/* Bentley and Robert Sedgewick in the April, 1998, Dr. Dobb's Journal. */
/* Links: */
/* https://www.drdobbs.com/database/ternary-search-trees/184410528?pgno=1 */
/* https://www.cs.princeton.edu/~rs/strings/tstdemo.c. */
/* ************************************************************************ */
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <wchar.h>
#include <string.h>
#include "xmalloc.h"
#include "list.h"
#include "utils.h"
#include "index.h"
/* List of words matching the current search. */
/* """""""""""""""""""""""""""""""""""""""""" */
ll_t *tst_search_list; /* Must be initialized by ll_new() before use. */
/* ======================================= */
/* Ternary search tree insertion function. */
/* ======================================= */
tst_node_t *
tst_insert(tst_node_t *p, wchar_t *w, void *data)
{
if (p == NULL)
{
p = (tst_node_t *)xmalloc(sizeof(tst_node_t));
p->splitchar = *w;
p->lokid = p->eqkid = p->hikid = NULL;
p->data = NULL;
}
if (*w < p->splitchar)
p->lokid = tst_insert(p->lokid, w, data);
else if (*w == p->splitchar)
{
if (*w == L'\0')
{
p->data = data;
p->eqkid = NULL;
}
else
p->eqkid = tst_insert(p->eqkid, w + 1, data);
}
else
p->hikid = tst_insert(p->hikid, w, data);
return (p);
}
/* ====================================== */
/* Ternary search tree deletion function. */
/* User data area not cleaned. */
/* ====================================== */
void
tst_cleanup(tst_node_t *p)
{
if (p != NULL)
{
tst_cleanup(p->lokid);
if (p->splitchar != L'\0')
tst_cleanup(p->eqkid);
tst_cleanup(p->hikid);
free(p);
}
}
/* ========================================================== */
/* Recursive traversal of a ternary tree. A callback function */
/* is also called when a complete string is found. */
/* Returns 1 if the callback function succeed (returned 1) at */
/* least once. */
/* The first_call argument is for initializing the static */
/* variable. */
/* ========================================================== */
int
tst_traverse(tst_node_t *p, int (*callback)(void *), int first_call)
{
static int rc;
if (first_call)
rc = 0;
if (p == NULL)
return 0;
tst_traverse(p->lokid, callback, 0);
if (p->splitchar != L'\0')
tst_traverse(p->eqkid, callback, 0);
else
rc += (*callback)(p->data);
tst_traverse(p->hikid, callback, 0);
return !!rc;
}
/* ======================================================================= */
/* Traverses the word tst looking for a wchar and build a list of pointers */
/* containing all the sub-tst potentially leading to words containing the */
/* next wchar of the search string. */
/* ======================================================================= */
int
tst_substring_traverse(tst_node_t *p,
int (*callback)(void *),
int first_call,
wchar_t w)
{
static int rc;
if (first_call)
rc = 0;
if (p == NULL)
return 0;
if (p->splitchar == w)
{
ll_node_t *node;
sub_tst_t *sub_tst_data;
node = tst_search_list->tail;
sub_tst_data = (sub_tst_t *)(node->data);
if (p->eqkid != NULL)
insert_sorted_ptr(&(sub_tst_data->array),
&(sub_tst_data->size),
&(sub_tst_data->count),
p->eqkid);
rc = 1;
}
tst_substring_traverse(p->lokid, callback, 0, w);
if (p->splitchar != L'\0')
tst_substring_traverse(p->eqkid, callback, 0, w);
else if (callback != NULL)
rc += (*callback)(p->data);
tst_substring_traverse(p->hikid, callback, 0, w);
return !!rc;
}
/* ======================================================================== */
/* Traverses the word tst looking for a wchar and build a list of pointers */
/* containing all the sub-tst nodes potentially leading to words containing */
/* the next wchar os the search string. */
/* ======================================================================== */
int
tst_fuzzy_traverse(tst_node_t *p,
int (*callback)(void *),
int first_call,
wchar_t w)
{
static int rc;
wchar_t w1s[2];
wchar_t w2s[2];
w1s[1] = w2s[1] = L'\0';
if (first_call)
rc = 0;
if (p == NULL)
return 0;
w1s[0] = p->splitchar;
w2s[0] = w;
if (my_wcscasecmp(w1s, w2s) == 0)
{
ll_node_t *node;
sub_tst_t *sub_tst_data;
node = tst_search_list->tail;
sub_tst_data = (sub_tst_t *)(node->data);
if (p->eqkid != NULL)
insert_sorted_ptr(&(sub_tst_data->array),
&(sub_tst_data->size),
&(sub_tst_data->count),
p->eqkid);
rc += 1;
}
tst_fuzzy_traverse(p->lokid, callback, 0, w);
if (p->splitchar != L'\0')
tst_fuzzy_traverse(p->eqkid, callback, 0, w);
else if (callback != NULL)
rc += (*callback)(p->data);
tst_fuzzy_traverse(p->hikid, callback, 0, w);
return !!rc;
}
/* ======================================================================= */
/* Searches a complete string in a ternary tree starting from a root node. */
/* ======================================================================= */
void *
tst_search(tst_node_t *root, wchar_t *w)
{
tst_node_t *p;
p = root;
while (p)
{
if (*w < p->splitchar)
p = p->lokid;
else if (*w == p->splitchar)
{
if (*w++ == L'\0')
return p->data;
p = p->eqkid;
}
else
p = p->hikid;
}
return NULL;
}
/* ============================================================== */
/* Searches all strings beginning with the same prefix. */
/* the callback function will be applied to each of these strings */
/* returns NULL if no string matched the prefix. */
/* ============================================================== */
void *
tst_prefix_search(tst_node_t *root, wchar_t *w, int (*callback)(void *))
{
tst_node_t *p = root;
size_t len = wcslen(w);
size_t rc;
while (p)
{
if (*w < p->splitchar)
p = p->lokid;
else if (*w == p->splitchar)
{
len--;
if (*w++ == L'\0')
return p->data;
if (len == 0)
{
rc = tst_traverse(p->eqkid, callback, 1);
return (void *)(long)rc;
}
p = p->eqkid;
}
else
p = p->hikid;
}
return NULL;
}
/* ========================================================= */
/* Insertion of an pointer in a already sorted pointer array */
/* without duplications. */
/* ========================================================= */
void
insert_sorted_ptr(tst_node_t ***array_ptr,
long *size,
long *nb,
tst_node_t *ptr)
{
long pos = *nb;
long left = 0, right = *nb, middle;
if (*nb > 0)
{
/* Bisection search. */
/* """"""""""""""""" */
while (left < right)
{
middle = (left + right) / 2;
if ((intptr_t)((*array_ptr)[middle]) == (intptr_t)ptr)
return; /* Value already in array. */
if ((intptr_t)ptr < (intptr_t)((*array_ptr)[middle]))
right = middle;
else
left = middle + 1;
}
pos = left;
}
if (*nb == *size)
{
*size += 64;
*array_ptr = xrealloc(*array_ptr, *size * sizeof(long));
}
if (*nb > pos)
memmove((*array_ptr) + pos + 1,
(*array_ptr) + pos,
sizeof(ptr) * (*nb - pos));
(*nb)++;
(*array_ptr)[pos] = ptr;
}