1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 typedef unsigned MatchWord;
32 #define MAX_LENGTH 1024
35 * Sun Wu and Udi Manber: Fast Text Searching Allowing Errors.
36 * Communications of the ACM, pp. 83-91, Vol. 35, No. 10, Oct. 1992, USA.
37 * PostScript version of the paper in its submitted form: agrep1.ps)
38 * recommended reading to understand AGREP !
40 * http://www.tgries.de/agrep/#AGREP1PS
41 * http://www.tgries.de/agrep/doc/agrep1ps.zip
45 int n; /* no of MatchWord needed */
46 int range; /* max no. of errors */
47 int fact; /* (range+1)*n */
48 MatchWord *match_mask; /* match_mask */
53 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
55 int off = state & (WORD_BITS-1);
56 int wno = state / WORD_BITS;
58 m[mc->n * ch + wno] |= 1<<off;
61 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
64 int off = state & (WORD_BITS-1);
65 int wno = state / WORD_BITS;
67 return m[mc->n * ch + wno] & (1<<off);
70 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
72 MatchContext *mc = (MatchContext *) xmalloc (sizeof(*mc));
75 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
77 mc->fact = (range+1)*mc->n;
78 mc->match_mask = (MatchWord *) xcalloc (mc->n, sizeof(*mc->match_mask));
80 for (s = 0; s<dfa->no_states; s++)
81 if (dfa->states[s]->rule_no)
82 set_bit (mc, mc->match_mask, 0, s);
86 static void rm_MatchContext (MatchContext **mc)
88 xfree ((*mc)->match_mask);
93 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
94 struct DFA *dfa, int ch)
97 MatchWord *Rsrc_p = Rsrc, mask;
99 for (j = 0; j<mc->n; j++)
104 for (j = 0; j<WORD_BITS/4; j++)
110 struct DFA_state *state = dfa->states[s];
111 int i = state->tran_no;
113 if (ch >= state->trans[i].ch[0] &&
114 ch <= state->trans[i].ch[1])
115 set_bit (mc, Rdst, 0, state->trans[i].to);
119 struct DFA_state *state = dfa->states[s+1];
120 int i = state->tran_no;
122 if (ch >= state->trans[i].ch[0] &&
123 ch <= state->trans[i].ch[1])
124 set_bit (mc, Rdst, 0, state->trans[i].to);
128 struct DFA_state *state = dfa->states[s+2];
129 int i = state->tran_no;
131 if (ch >= state->trans[i].ch[0] &&
132 ch <= state->trans[i].ch[1])
133 set_bit (mc, Rdst, 0, state->trans[i].to);
137 struct DFA_state *state = dfa->states[s+3];
138 int i = state->tran_no;
140 if (ch >= state->trans[i].ch[0] &&
141 ch <= state->trans[i].ch[1])
142 set_bit (mc, Rdst, 0, state->trans[i].to);
146 if (s >= dfa->no_states)
153 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
157 MatchWord *Rsrc_p = Rsrc, mask;
158 for (j = 0; j<mc->n; j++)
163 for (j = 0; j<WORD_BITS/4; j++)
169 struct DFA_state *state = dfa->states[s];
170 int i = state->tran_no;
172 set_bit (mc, Rdst, 0, state->trans[i].to);
176 struct DFA_state *state = dfa->states[s+1];
177 int i = state->tran_no;
179 set_bit (mc, Rdst, 0, state->trans[i].to);
183 struct DFA_state *state = dfa->states[s+2];
184 int i = state->tran_no;
186 set_bit (mc, Rdst, 0, state->trans[i].to);
190 struct DFA_state *state = dfa->states[s+3];
191 int i = state->tran_no;
193 set_bit (mc, Rdst, 0, state->trans[i].to);
197 if (s >= dfa->no_states)
204 static void or (MatchContext *mc, MatchWord *Rdst,
205 MatchWord *Rsrc1, MatchWord *Rsrc2)
208 for (i = 0; i<mc->n; i++)
209 Rdst[i] = Rsrc1[i] | Rsrc2[i];
212 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
213 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
217 MatchWord *Rtmp_2 = Rtmp + mc->n;
219 mask_shift (mc, Rj1, Rj, dfa, ch);
220 for (d = 1; d <= mc->range; d++)
222 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
224 shift (mc, Rtmp_2, Rtmp, dfa);
226 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
228 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
232 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
241 static int grep(Dict dict, Dict_ptr ptr, MatchContext *mc,
242 MatchWord *Rj, int pos, void *client,
243 int (*userfunc)(char *, const char *, void *),
244 Dict_char *prefix, struct DFA *dfa,
245 int *max_pos, int init_pos)
252 dict_bf_readp (dict->dbf, ptr, &p);
254 hi = DICT_nodir(p)-1;
255 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
261 /* string (Dict_char *) DICT_EOS terminated */
262 /* unsigned char length of information */
263 /* char * information */
266 info = (char*)p + indxp[-lo];
270 MatchWord *Rj0 = Rj + j *mc->fact;
271 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
272 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
275 memcpy(&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
277 if (pos+j > *max_pos)
283 int ret = userfunc((char*) prefix,
284 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])
343 prefix[pos+1] = DICT_EOS;
344 ret = userfunc((char*) prefix,
345 info+sizeof(Dict_ptr)+
346 sizeof(Dict_char), client);
352 memcpy (&subptr, info, sizeof(Dict_ptr));
355 int ret = grep(dict, subptr, mc, Rj1, pos+1,
356 client, userfunc, prefix, dfa, max_pos,
361 dict_bf_readp (dict->dbf, ptr, &p);
362 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
371 int dict_lookup_grep(Dict dict, const char *pattern, int range, void *client,
372 int *max_pos, int init_pos,
373 int (*userfunc)(char *name, const char *info,
377 Dict_char prefix[MAX_LENGTH+1];
378 const char *this_pattern = pattern;
380 struct DFA *dfa = dfa_init();
386 debug_dfa_followpos = 1;
390 dfa_anyset_includes_nl(dfa);
392 yaz_log(YLOG_DEBUG, "dict_lookup_grep range=%d", range);
393 for (i = 0; pattern[i]; i++)
395 yaz_log(YLOG_DEBUG, " %2d %3d %c", i, pattern[i],
396 (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
399 dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
401 i = dfa_parse (dfa, &this_pattern);
402 if (i || *this_pattern)
404 yaz_log(YLOG_WARN, "dfa_parse fail=%d", i);
410 mc = mk_MatchContext (dfa, range);
412 Rj = (MatchWord *) xcalloc((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
414 set_bit (mc, Rj, 0, 0);
415 for (d = 1; d<=mc->range; d++)
418 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
419 for (s = 0; s < dfa->no_states; s++)
421 if (get_bit (mc, Rj, d-1, s))
423 struct DFA_state *state = dfa->states[s];
424 int i = state->tran_no;
426 set_bit (mc, Rj, d, state->trans[i].to);
432 ret = grep(dict, dict->head.root, mc, Rj, 0, client,
434 dfa, max_pos, init_pos);
435 yaz_log(YLOG_DEBUG, "max_pos = %d", *max_pos);
438 rm_MatchContext (&mc);
442 void dict_grep_cmap (Dict dict, void *vp,
443 const char **(*cmap)(void *vp, const char **from, int len))
445 dict->grep_cmap = cmap;
446 dict->grep_cmap_data = vp;
451 * indent-tabs-mode: nil
453 * vim: shiftwidth=4 tabstop=8 expandtab