2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-09-14 11:52:59 adam
8 * Grep handle function parameter info is const now.
10 * Revision 1.4 1995/01/24 16:01:02 adam
11 * Added -ansi to CFLAGS.
12 * Use new API of dfa module.
14 * Revision 1.3 1994/10/05 12:16:50 adam
15 * Pagesize is a resource now.
17 * Revision 1.2 1994/10/04 12:08:07 adam
18 * Some bug fixes and some optimizations.
20 * Revision 1.1 1994/10/03 17:23:04 adam
21 * First version of dictionary lookup with regular expressions and errors.
33 typedef unsigned MatchWord;
35 #define MAX_LENGTH 1024
38 int n; /* no of MatchWord needed */
39 int range; /* max no. of errors */
40 int fact; /* (range+1)*n */
41 MatchWord *match_mask; /* match_mask */
46 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
48 int off = state & (WORD_BITS-1);
49 int wno = state / WORD_BITS;
51 m[mc->n * ch + wno] |= 1<<off;
54 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
57 int off = state & (WORD_BITS-1);
58 int wno = state / WORD_BITS;
60 return m[mc->n * ch + wno] & (1<<off);
63 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
65 MatchContext *mc = xmalloc (sizeof(*mc));
68 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
70 mc->fact = (range+1)*mc->n;
71 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
73 for (s = 0; s<dfa->no_states; s++)
74 if (dfa->states[s]->rule_no)
75 set_bit (mc, mc->match_mask, 0, s);
79 static void rm_MatchContext (MatchContext **mc)
81 xfree ((*mc)->match_mask);
86 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
87 struct DFA *dfa, int ch)
90 MatchWord *Rsrc_p = Rsrc, mask;
92 for (j = 0; j<mc->n; j++)
97 for (j = 0; j<WORD_BITS/4; j++)
103 struct DFA_state *state = dfa->states[s];
104 int i = state->tran_no;
106 if (ch >= state->trans[i].ch[0] &&
107 ch <= state->trans[i].ch[1])
108 set_bit (mc, Rdst, 0, state->trans[i].to);
112 struct DFA_state *state = dfa->states[s+1];
113 int i = state->tran_no;
115 if (ch >= state->trans[i].ch[0] &&
116 ch <= state->trans[i].ch[1])
117 set_bit (mc, Rdst, 0, state->trans[i].to);
121 struct DFA_state *state = dfa->states[s+2];
122 int i = state->tran_no;
124 if (ch >= state->trans[i].ch[0] &&
125 ch <= state->trans[i].ch[1])
126 set_bit (mc, Rdst, 0, state->trans[i].to);
130 struct DFA_state *state = dfa->states[s+3];
131 int i = state->tran_no;
133 if (ch >= state->trans[i].ch[0] &&
134 ch <= state->trans[i].ch[1])
135 set_bit (mc, Rdst, 0, state->trans[i].to);
139 if (s >= dfa->no_states)
146 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
150 MatchWord *Rsrc_p = Rsrc, mask;
151 for (j = 0; j<mc->n; j++)
156 for (j = 0; j<WORD_BITS/4; j++)
162 struct DFA_state *state = dfa->states[s];
163 int i = state->tran_no;
165 set_bit (mc, Rdst, 0, state->trans[i].to);
169 struct DFA_state *state = dfa->states[s+1];
170 int i = state->tran_no;
172 set_bit (mc, Rdst, 0, state->trans[i].to);
176 struct DFA_state *state = dfa->states[s+2];
177 int i = state->tran_no;
179 set_bit (mc, Rdst, 0, state->trans[i].to);
183 struct DFA_state *state = dfa->states[s+3];
184 int i = state->tran_no;
186 set_bit (mc, Rdst, 0, state->trans[i].to);
190 if (s >= dfa->no_states)
197 static void or (MatchContext *mc, MatchWord *Rdst,
198 MatchWord *Rsrc1, MatchWord *Rsrc2)
201 for (i = 0; i<mc->n; i++)
202 Rdst[i] = Rsrc1[i] | Rsrc2[i];
205 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
206 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
209 MatchWord *Rtmp_2 = Rtmp + mc->n;
211 mask_shift (mc, Rj1, Rj, dfa, ch);
212 for (d = 1; d <= mc->range; d++)
214 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
216 shift (mc, Rtmp_2, Rtmp, dfa);
218 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
220 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
224 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
233 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
234 MatchWord *Rj, int pos,
235 int (*userfunc)(Dict_char *name, const char *info),
236 Dict_char *prefix, struct DFA *dfa)
243 dict_bf_readp (dict->dbf, ptr, &p);
245 hi = DICT_nodir(p)-1;
246 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
252 /* string (Dict_char *) DICT_EOS terminated */
253 /* unsigned char length of information */
254 /* char * information */
257 info = (char*)p + indxp[-lo];
261 MatchWord *Rj0 = Rj + j *mc->fact;
262 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
263 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
265 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
270 (*userfunc)(prefix, info+(j+1)*sizeof(Dict_char));
273 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
274 for (d = mc->n; --d >= 0; )
275 if (Rj1[mc->range*mc->n + d])
280 for (d = mc->n; --d >= 0; )
281 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
290 MatchWord *Rj1 = Rj+ mc->fact;
291 MatchWord *Rj_tmp = Rj+2*mc->fact;
294 /* Dict_ptr subptr */
295 /* Dict_char sub char */
296 /* unsigned char length of information */
297 /* char * information */
298 info = (char*)p - indxp[-lo];
299 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
302 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
303 for (d = mc->n; --d >= 0; )
304 if (Rj1[mc->range*mc->n + d])
309 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
311 for (d = mc->n; --d >= 0; )
312 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
314 prefix[pos+1] = DICT_EOS;
315 (*userfunc)(prefix, info+sizeof(Dict_ptr)+
320 memcpy (&subptr, info, sizeof(Dict_ptr));
323 dict_grep (dict, subptr, mc, Rj1, pos+1,
324 userfunc, prefix, dfa);
325 dict_bf_readp (dict->dbf, ptr, &p);
326 indxp = (short*) ((char*) p+DICT_pagesize(dict)
336 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range,
337 int (*userfunc)(Dict_char *name, const char *info))
340 Dict_char prefix[MAX_LENGTH+1];
341 char *this_pattern = pattern;
343 struct DFA *dfa = dfa_init();
346 i = dfa_parse (dfa, &this_pattern);
347 if (i || *this_pattern)
354 mc = mk_MatchContext (dfa, range);
356 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
358 set_bit (mc, Rj, 0, 0);
359 for (d = 1; d<=mc->range; d++)
362 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
363 for (s = 0; s<dfa->no_states; s++)
365 if (get_bit (mc, Rj, d-1, s))
367 struct DFA_state *state = dfa->states[s];
368 int i = state->tran_no;
370 set_bit (mc, Rj, d, state->trans[i].to);
374 i = dict_grep (dict, 1, mc, Rj, 0, userfunc, prefix, dfa);
378 rm_MatchContext (&mc);