1 /* $Id: lookgrep.c,v 1.33 2006-08-29 13:36:26 adam Exp $
2 Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 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)
286 int ret = userfunc((char*) prefix,
287 info+(j+1)*sizeof(Dict_char), client);
293 if (pos+j >= init_pos)
297 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
298 for (d = mc->n; --d >= 0; )
299 if (Rj1[range*mc->n + d])
304 for (d = mc->n; --d >= 0; )
305 if (Rj1[range*mc->n + d] & mc->match_mask[d])
314 MatchWord *Rj1 = Rj+ mc->fact;
315 MatchWord *Rj_tmp = Rj+2*mc->fact;
319 /* Dict_ptr subptr */
320 /* Dict_char sub char */
321 /* unsigned char length of information */
322 /* char * information */
323 info = (char*)p - indxp[-lo];
324 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
333 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
334 for (d = mc->n; --d >= 0; )
335 if (Rj1[range*mc->n + d])
340 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
342 for (d = mc->n; --d >= 0; )
343 if (Rj1[range*mc->n + d] & mc->match_mask[d])
346 prefix[pos+1] = DICT_EOS;
347 ret = userfunc((char*) prefix,
348 info+sizeof(Dict_ptr)+
349 sizeof(Dict_char), client);
355 memcpy (&subptr, info, sizeof(Dict_ptr));
358 int ret = grep(dict, subptr, mc, Rj1, pos+1,
359 client, userfunc, prefix, dfa, max_pos,
364 dict_bf_readp (dict->dbf, ptr, &p);
365 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
374 int dict_lookup_grep(Dict dict, const char *pattern, int range, void *client,
375 int *max_pos, int init_pos,
376 int (*userfunc)(char *name, const char *info,
380 Dict_char prefix[MAX_LENGTH+1];
381 const char *this_pattern = pattern;
383 struct DFA *dfa = dfa_init();
389 debug_dfa_followpos = 1;
393 yaz_log(YLOG_DEBUG, "dict_lookup_grep range=%d", range);
394 for (i = 0; pattern[i]; i++)
396 yaz_log(YLOG_DEBUG, " %2d %3d %c", i, pattern[i],
397 (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
400 dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
402 i = dfa_parse (dfa, &this_pattern);
403 if (i || *this_pattern)
405 yaz_log(YLOG_WARN, "dfa_parse fail=%d", i);
411 mc = mk_MatchContext (dfa, range);
413 Rj = (MatchWord *) xcalloc((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
415 set_bit (mc, Rj, 0, 0);
416 for (d = 1; d<=mc->range; d++)
419 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
420 for (s = 0; s < dfa->no_states; s++)
422 if (get_bit (mc, Rj, d-1, s))
424 struct DFA_state *state = dfa->states[s];
425 int i = state->tran_no;
427 set_bit (mc, Rj, d, state->trans[i].to);
433 ret = grep(dict, dict->head.root, mc, Rj, 0, client,
435 dfa, max_pos, init_pos);
436 yaz_log(YLOG_DEBUG, "max_pos = %d", *max_pos);
439 rm_MatchContext (&mc);
443 void dict_grep_cmap (Dict dict, void *vp,
444 const char **(*cmap)(void *vp, const char **from, int len))
446 dict->grep_cmap = cmap;
447 dict->grep_cmap_data = vp;
452 * indent-tabs-mode: nil
454 * vim: shiftwidth=4 tabstop=8 expandtab