1 /* $Id: lookgrep.c,v 1.31 2006-05-10 08:13:18 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
33 typedef unsigned MatchWord;
35 #define MAX_LENGTH 1024
38 * Sun Wu and Udi Manber: Fast Text Searching Allowing Errors.
39 * Communications of the ACM, pp. 83-91, Vol. 35, No. 10, Oct. 1992, USA.
40 * PostScript version of the paper in its submitted form: agrep1.ps)
41 * recommended reading to understand AGREP !
43 * http://www.tgries.de/agrep/#AGREP1PS
44 * http://www.tgries.de/agrep/doc/agrep1ps.zip
48 int n; /* no of MatchWord needed */
49 int range; /* max no. of errors */
50 int fact; /* (range+1)*n */
51 MatchWord *match_mask; /* match_mask */
56 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
58 int off = state & (WORD_BITS-1);
59 int wno = state / WORD_BITS;
61 m[mc->n * ch + wno] |= 1<<off;
64 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
67 int off = state & (WORD_BITS-1);
68 int wno = state / WORD_BITS;
70 return m[mc->n * ch + wno] & (1<<off);
73 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
75 MatchContext *mc = (MatchContext *) xmalloc (sizeof(*mc));
78 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
80 mc->fact = (range+1)*mc->n;
81 mc->match_mask = (MatchWord *) xcalloc (mc->n, sizeof(*mc->match_mask));
83 for (s = 0; s<dfa->no_states; s++)
84 if (dfa->states[s]->rule_no)
85 set_bit (mc, mc->match_mask, 0, s);
89 static void rm_MatchContext (MatchContext **mc)
91 xfree ((*mc)->match_mask);
96 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
97 struct DFA *dfa, int ch)
100 MatchWord *Rsrc_p = Rsrc, mask;
102 for (j = 0; j<mc->n; j++)
107 for (j = 0; j<WORD_BITS/4; j++)
113 struct DFA_state *state = dfa->states[s];
114 int i = state->tran_no;
116 if (ch >= state->trans[i].ch[0] &&
117 ch <= state->trans[i].ch[1])
118 set_bit (mc, Rdst, 0, state->trans[i].to);
122 struct DFA_state *state = dfa->states[s+1];
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+2];
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+3];
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 if (s >= dfa->no_states)
156 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
160 MatchWord *Rsrc_p = Rsrc, mask;
161 for (j = 0; j<mc->n; j++)
166 for (j = 0; j<WORD_BITS/4; j++)
172 struct DFA_state *state = dfa->states[s];
173 int i = state->tran_no;
175 set_bit (mc, Rdst, 0, state->trans[i].to);
179 struct DFA_state *state = dfa->states[s+1];
180 int i = state->tran_no;
182 set_bit (mc, Rdst, 0, state->trans[i].to);
186 struct DFA_state *state = dfa->states[s+2];
187 int i = state->tran_no;
189 set_bit (mc, Rdst, 0, state->trans[i].to);
193 struct DFA_state *state = dfa->states[s+3];
194 int i = state->tran_no;
196 set_bit (mc, Rdst, 0, state->trans[i].to);
200 if (s >= dfa->no_states)
207 static void or (MatchContext *mc, MatchWord *Rdst,
208 MatchWord *Rsrc1, MatchWord *Rsrc2)
211 for (i = 0; i<mc->n; i++)
212 Rdst[i] = Rsrc1[i] | Rsrc2[i];
215 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
216 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
220 MatchWord *Rtmp_2 = Rtmp + mc->n;
222 mask_shift (mc, Rj1, Rj, dfa, ch);
223 for (d = 1; d <= mc->range; d++)
225 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
227 shift (mc, Rtmp_2, Rtmp, dfa);
229 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
231 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
235 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
244 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
245 MatchWord *Rj, int pos, void *client,
246 int (*userfunc)(char *, const char *, void *),
247 Dict_char *prefix, struct DFA *dfa,
248 int *max_pos, int init_pos)
255 dict_bf_readp (dict->dbf, ptr, &p);
257 hi = DICT_nodir(p)-1;
258 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
264 /* string (Dict_char *) DICT_EOS terminated */
265 /* unsigned char length of information */
266 /* char * information */
269 info = (char*)p + indxp[-lo];
273 MatchWord *Rj0 = Rj + j *mc->fact;
274 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
275 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
278 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
280 if (pos+j > *max_pos)
285 if ((*userfunc)((char*) prefix,
286 info+(j+1)*sizeof(Dict_char), client))
290 if (pos+j >= init_pos)
294 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
295 for (d = mc->n; --d >= 0; )
296 if (Rj1[range*mc->n + d])
301 for (d = mc->n; --d >= 0; )
302 if (Rj1[range*mc->n + d] & mc->match_mask[d])
311 MatchWord *Rj1 = Rj+ mc->fact;
312 MatchWord *Rj_tmp = Rj+2*mc->fact;
316 /* Dict_ptr subptr */
317 /* Dict_char sub char */
318 /* unsigned char length of information */
319 /* char * information */
320 info = (char*)p - indxp[-lo];
321 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
330 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
331 for (d = mc->n; --d >= 0; )
332 if (Rj1[range*mc->n + d])
337 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
339 for (d = mc->n; --d >= 0; )
340 if (Rj1[range*mc->n + d] & mc->match_mask[d])
342 prefix[pos+1] = DICT_EOS;
343 if ((*userfunc)((char*) prefix,
344 info+sizeof(Dict_ptr)+
345 sizeof(Dict_char), client))
350 memcpy (&subptr, info, sizeof(Dict_ptr));
353 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
354 client, userfunc, prefix, dfa, max_pos,
357 dict_bf_readp (dict->dbf, ptr, &p);
358 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
367 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
368 int *max_pos, int init_pos,
369 int (*userfunc)(char *name, const char *info,
373 Dict_char prefix[MAX_LENGTH+1];
374 const char *this_pattern = pattern;
376 struct DFA *dfa = dfa_init();
382 debug_dfa_followpos = 1;
386 yaz_log (YLOG_DEBUG, "dict_lookup_grep range=%d", range);
387 for (i = 0; pattern[i]; i++)
389 yaz_log (YLOG_DEBUG, " %3d %c", pattern[i],
390 (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
393 dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
395 i = dfa_parse (dfa, &this_pattern);
396 if (i || *this_pattern)
398 yaz_log (YLOG_WARN, "dfa_parse fail=%d", i);
404 mc = mk_MatchContext (dfa, range);
406 Rj = (MatchWord *) xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
408 set_bit (mc, Rj, 0, 0);
409 for (d = 1; d<=mc->range; d++)
412 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
413 for (s = 0; s<dfa->no_states; s++)
415 if (get_bit (mc, Rj, d-1, s))
417 struct DFA_state *state = dfa->states[s];
418 int i = state->tran_no;
420 set_bit (mc, Rj, d, state->trans[i].to);
426 i = dict_grep (dict, dict->head.root, mc, Rj, 0, client,
428 dfa, max_pos, init_pos);
431 yaz_log (YLOG_DEBUG, "max_pos = %d", *max_pos);
434 rm_MatchContext (&mc);
438 void dict_grep_cmap (Dict dict, void *vp,
439 const char **(*cmap)(void *vp, const char **from, int len))
441 dict->grep_cmap = cmap;
442 dict->grep_cmap_data = vp;
447 * indent-tabs-mode: nil
449 * vim: shiftwidth=4 tabstop=8 expandtab