2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1995-12-06 14:43:02 adam
8 * New function: dict_delete.
10 * Revision 1.10 1995/11/16 17:00:44 adam
13 * Revision 1.9 1995/10/27 13:58:09 adam
14 * Makes 'Database unavailable' diagnostic.
16 * Revision 1.8 1995/10/19 14:57:21 adam
17 * New feature: grep lookup saves length of longest prefix match.
19 * Revision 1.7 1995/10/17 18:01:22 adam
20 * Userfunc may return non-zero in which case the the grepping stops
23 * Revision 1.6 1995/10/09 16:18:32 adam
24 * Function dict_lookup_grep got extra client data parameter.
26 * Revision 1.5 1995/09/14 11:52:59 adam
27 * Grep handle function parameter info is const now.
29 * Revision 1.4 1995/01/24 16:01:02 adam
30 * Added -ansi to CFLAGS.
31 * Use new API of dfa module.
33 * Revision 1.3 1994/10/05 12:16:50 adam
34 * Pagesize is a resource now.
36 * Revision 1.2 1994/10/04 12:08:07 adam
37 * Some bug fixes and some optimizations.
39 * Revision 1.1 1994/10/03 17:23:04 adam
40 * First version of dictionary lookup with regular expressions and errors.
52 typedef unsigned MatchWord;
54 #define MAX_LENGTH 1024
57 int n; /* no of MatchWord needed */
58 int range; /* max no. of errors */
59 int fact; /* (range+1)*n */
60 MatchWord *match_mask; /* match_mask */
65 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
67 int off = state & (WORD_BITS-1);
68 int wno = state / WORD_BITS;
70 m[mc->n * ch + wno] |= 1<<off;
73 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
76 int off = state & (WORD_BITS-1);
77 int wno = state / WORD_BITS;
79 return m[mc->n * ch + wno] & (1<<off);
82 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
84 MatchContext *mc = xmalloc (sizeof(*mc));
87 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
89 mc->fact = (range+1)*mc->n;
90 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
92 for (s = 0; s<dfa->no_states; s++)
93 if (dfa->states[s]->rule_no)
94 set_bit (mc, mc->match_mask, 0, s);
98 static void rm_MatchContext (MatchContext **mc)
100 xfree ((*mc)->match_mask);
105 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
106 struct DFA *dfa, int ch)
109 MatchWord *Rsrc_p = Rsrc, mask;
111 for (j = 0; j<mc->n; j++)
116 for (j = 0; j<WORD_BITS/4; j++)
122 struct DFA_state *state = dfa->states[s];
123 int i = state->tran_no;
125 if (ch >= state->trans[i].ch[0] &&
126 ch <= state->trans[i].ch[1])
127 set_bit (mc, Rdst, 0, state->trans[i].to);
131 struct DFA_state *state = dfa->states[s+1];
132 int i = state->tran_no;
134 if (ch >= state->trans[i].ch[0] &&
135 ch <= state->trans[i].ch[1])
136 set_bit (mc, Rdst, 0, state->trans[i].to);
140 struct DFA_state *state = dfa->states[s+2];
141 int i = state->tran_no;
143 if (ch >= state->trans[i].ch[0] &&
144 ch <= state->trans[i].ch[1])
145 set_bit (mc, Rdst, 0, state->trans[i].to);
149 struct DFA_state *state = dfa->states[s+3];
150 int i = state->tran_no;
152 if (ch >= state->trans[i].ch[0] &&
153 ch <= state->trans[i].ch[1])
154 set_bit (mc, Rdst, 0, state->trans[i].to);
158 if (s >= dfa->no_states)
165 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
169 MatchWord *Rsrc_p = Rsrc, mask;
170 for (j = 0; j<mc->n; j++)
175 for (j = 0; j<WORD_BITS/4; j++)
181 struct DFA_state *state = dfa->states[s];
182 int i = state->tran_no;
184 set_bit (mc, Rdst, 0, state->trans[i].to);
188 struct DFA_state *state = dfa->states[s+1];
189 int i = state->tran_no;
191 set_bit (mc, Rdst, 0, state->trans[i].to);
195 struct DFA_state *state = dfa->states[s+2];
196 int i = state->tran_no;
198 set_bit (mc, Rdst, 0, state->trans[i].to);
202 struct DFA_state *state = dfa->states[s+3];
203 int i = state->tran_no;
205 set_bit (mc, Rdst, 0, state->trans[i].to);
209 if (s >= dfa->no_states)
216 static void or (MatchContext *mc, MatchWord *Rdst,
217 MatchWord *Rsrc1, MatchWord *Rsrc2)
220 for (i = 0; i<mc->n; i++)
221 Rdst[i] = Rsrc1[i] | Rsrc2[i];
224 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
225 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
228 MatchWord *Rtmp_2 = Rtmp + mc->n;
230 mask_shift (mc, Rj1, Rj, dfa, ch);
231 for (d = 1; d <= mc->range; d++)
233 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
235 shift (mc, Rtmp_2, Rtmp, dfa);
237 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
239 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
243 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
252 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
253 MatchWord *Rj, int pos, void *client,
254 int (*userfunc)(Dict_char *, const char *, void *),
255 Dict_char *prefix, struct DFA *dfa,
263 dict_bf_readp (dict->dbf, ptr, &p);
265 hi = DICT_nodir(p)-1;
266 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
272 /* string (Dict_char *) DICT_EOS terminated */
273 /* unsigned char length of information */
274 /* char * information */
277 info = (char*)p + indxp[-lo];
281 MatchWord *Rj0 = Rj + j *mc->fact;
282 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
283 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
285 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
287 if (pos+j > *max_pos)
292 if ((*userfunc)(prefix, info+(j+1)*sizeof(Dict_char),
297 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
298 for (d = mc->n; --d >= 0; )
299 if (Rj1[mc->range*mc->n + d])
304 for (d = mc->n; --d >= 0; )
305 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
314 MatchWord *Rj1 = Rj+ mc->fact;
315 MatchWord *Rj_tmp = Rj+2*mc->fact;
318 /* Dict_ptr subptr */
319 /* Dict_char sub char */
320 /* unsigned char length of information */
321 /* char * information */
322 info = (char*)p - indxp[-lo];
323 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
328 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
329 for (d = mc->n; --d >= 0; )
330 if (Rj1[mc->range*mc->n + d])
335 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
337 for (d = mc->n; --d >= 0; )
338 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
340 prefix[pos+1] = DICT_EOS;
341 if ((*userfunc)(prefix, info+sizeof(Dict_ptr)+
342 sizeof(Dict_char), client))
347 memcpy (&subptr, info, sizeof(Dict_ptr));
350 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
351 client, userfunc, prefix, dfa, max_pos))
353 dict_bf_readp (dict->dbf, ptr, &p);
354 indxp = (short*) ((char*) p+DICT_pagesize(dict)
364 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range, void *client,
366 int (*userfunc)(Dict_char *name, const char *info,
370 Dict_char prefix[MAX_LENGTH+1];
371 char *this_pattern = pattern;
373 struct DFA *dfa = dfa_init();
376 logf (LOG_DEBUG, "dict_lookup_grep '%s' range=%d", pattern, range);
377 i = dfa_parse (dfa, &this_pattern);
378 if (i || *this_pattern)
385 mc = mk_MatchContext (dfa, range);
387 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
389 set_bit (mc, Rj, 0, 0);
390 for (d = 1; d<=mc->range; d++)
393 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
394 for (s = 0; s<dfa->no_states; s++)
396 if (get_bit (mc, Rj, d-1, s))
398 struct DFA_state *state = dfa->states[s];
399 int i = state->tran_no;
401 set_bit (mc, Rj, d, state->trans[i].to);
406 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix, dfa,
408 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
411 rm_MatchContext (&mc);