2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-01-24 16:01:02 adam
8 * Added -ansi to CFLAGS.
9 * Use new API of dfa module.
11 * Revision 1.3 1994/10/05 12:16:50 adam
12 * Pagesize is a resource now.
14 * Revision 1.2 1994/10/04 12:08:07 adam
15 * Some bug fixes and some optimizations.
17 * Revision 1.1 1994/10/03 17:23:04 adam
18 * First version of dictionary lookup with regular expressions and errors.
30 typedef unsigned MatchWord;
32 #define MAX_LENGTH 1024
35 int n; /* no of MatchWord needed */
36 int range; /* max no. of errors */
37 int fact; /* (range+1)*n */
38 MatchWord *match_mask; /* match_mask */
43 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
45 int off = state & (WORD_BITS-1);
46 int wno = state / WORD_BITS;
48 m[mc->n * ch + wno] |= 1<<off;
51 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
54 int off = state & (WORD_BITS-1);
55 int wno = state / WORD_BITS;
57 return m[mc->n * ch + wno] & (1<<off);
60 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
62 MatchContext *mc = xmalloc (sizeof(*mc));
65 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
67 mc->fact = (range+1)*mc->n;
68 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
70 for (s = 0; s<dfa->no_states; s++)
71 if (dfa->states[s]->rule_no)
72 set_bit (mc, mc->match_mask, 0, s);
76 static void rm_MatchContext (MatchContext **mc)
78 xfree ((*mc)->match_mask);
83 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
84 struct DFA *dfa, int ch)
87 MatchWord *Rsrc_p = Rsrc, mask;
89 for (j = 0; j<mc->n; j++)
94 for (j = 0; j<WORD_BITS/4; j++)
100 struct DFA_state *state = dfa->states[s];
101 int i = state->tran_no;
103 if (ch >= state->trans[i].ch[0] &&
104 ch <= state->trans[i].ch[1])
105 set_bit (mc, Rdst, 0, state->trans[i].to);
109 struct DFA_state *state = dfa->states[s+1];
110 int i = state->tran_no;
112 if (ch >= state->trans[i].ch[0] &&
113 ch <= state->trans[i].ch[1])
114 set_bit (mc, Rdst, 0, state->trans[i].to);
118 struct DFA_state *state = dfa->states[s+2];
119 int i = state->tran_no;
121 if (ch >= state->trans[i].ch[0] &&
122 ch <= state->trans[i].ch[1])
123 set_bit (mc, Rdst, 0, state->trans[i].to);
127 struct DFA_state *state = dfa->states[s+3];
128 int i = state->tran_no;
130 if (ch >= state->trans[i].ch[0] &&
131 ch <= state->trans[i].ch[1])
132 set_bit (mc, Rdst, 0, state->trans[i].to);
136 if (s >= dfa->no_states)
143 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
147 MatchWord *Rsrc_p = Rsrc, mask;
148 for (j = 0; j<mc->n; j++)
153 for (j = 0; j<WORD_BITS/4; j++)
159 struct DFA_state *state = dfa->states[s];
160 int i = state->tran_no;
162 set_bit (mc, Rdst, 0, state->trans[i].to);
166 struct DFA_state *state = dfa->states[s+1];
167 int i = state->tran_no;
169 set_bit (mc, Rdst, 0, state->trans[i].to);
173 struct DFA_state *state = dfa->states[s+2];
174 int i = state->tran_no;
176 set_bit (mc, Rdst, 0, state->trans[i].to);
180 struct DFA_state *state = dfa->states[s+3];
181 int i = state->tran_no;
183 set_bit (mc, Rdst, 0, state->trans[i].to);
187 if (s >= dfa->no_states)
194 static void or (MatchContext *mc, MatchWord *Rdst,
195 MatchWord *Rsrc1, MatchWord *Rsrc2)
198 for (i = 0; i<mc->n; i++)
199 Rdst[i] = Rsrc1[i] | Rsrc2[i];
202 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
203 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
206 MatchWord *Rtmp_2 = Rtmp + mc->n;
208 mask_shift (mc, Rj1, Rj, dfa, ch);
209 for (d = 1; d <= mc->range; d++)
211 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
213 shift (mc, Rtmp_2, Rtmp, dfa);
215 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
217 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
221 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
230 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
231 MatchWord *Rj, int pos,
232 int (*userfunc)(Dict_char *name, char *info),
233 Dict_char *prefix, struct DFA *dfa)
240 dict_bf_readp (dict->dbf, ptr, &p);
242 hi = DICT_nodir(p)-1;
243 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
249 /* string (Dict_char *) DICT_EOS terminated */
250 /* unsigned char length of information */
251 /* char * information */
254 info = (char*)p + indxp[-lo];
258 MatchWord *Rj0 = Rj + j *mc->fact;
259 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
260 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
262 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
267 (*userfunc)(prefix, info+(j+1)*sizeof(Dict_char));
270 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
271 for (d = mc->n; --d >= 0; )
272 if (Rj1[mc->range*mc->n + d])
277 for (d = mc->n; --d >= 0; )
278 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
287 MatchWord *Rj1 = Rj+ mc->fact;
288 MatchWord *Rj_tmp = Rj+2*mc->fact;
291 /* Dict_ptr subptr */
292 /* Dict_char sub char */
293 /* unsigned char length of information */
294 /* char * information */
295 info = (char*)p - indxp[-lo];
296 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
299 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
300 for (d = mc->n; --d >= 0; )
301 if (Rj1[mc->range*mc->n + d])
306 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
308 for (d = mc->n; --d >= 0; )
309 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
311 prefix[pos+1] = DICT_EOS;
312 (*userfunc)(prefix, info+sizeof(Dict_ptr)+
317 memcpy (&subptr, info, sizeof(Dict_ptr));
320 dict_grep (dict, subptr, mc, Rj1, pos+1,
321 userfunc, prefix, dfa);
322 dict_bf_readp (dict->dbf, ptr, &p);
323 indxp = (short*) ((char*) p+DICT_pagesize(dict)
333 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range,
334 int (*userfunc)(Dict_char *name, char *info))
337 Dict_char prefix[MAX_LENGTH+1];
338 char *this_pattern = pattern;
340 struct DFA *dfa = dfa_init();
343 i = dfa_parse (dfa, &this_pattern);
344 if (i || *this_pattern)
351 mc = mk_MatchContext (dfa, range);
353 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
355 set_bit (mc, Rj, 0, 0);
356 for (d = 1; d<=mc->range; d++)
359 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
360 for (s = 0; s<dfa->no_states; s++)
362 if (get_bit (mc, Rj, d-1, s))
364 struct DFA_state *state = dfa->states[s];
365 int i = state->tran_no;
367 set_bit (mc, Rj, d, state->trans[i].to);
371 i = dict_grep (dict, 1, mc, Rj, 0, userfunc, prefix, dfa);
375 rm_MatchContext (&mc);