Update copyright year + FSF address
[idzebra-moved-to-github.git] / dict / lookgrep.c
1 /* $Id: lookgrep.c,v 1.32 2006-08-14 10:40:09 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20
21 */
22
23
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <assert.h>
29
30 #include <dfa.h>
31 #include "dict-p.h"
32
33 typedef unsigned MatchWord;
34 #define WORD_BITS 32
35 #define MAX_LENGTH 1024
36
37 /* This code is based 
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 ! 
42  *
43  * http://www.tgries.de/agrep/#AGREP1PS
44  * http://www.tgries.de/agrep/doc/agrep1ps.zip
45  */
46
47 typedef struct {
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 */
52 } MatchContext;
53
54 #define INLINE 
55
56 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
57 {
58     int off = state & (WORD_BITS-1);
59     int wno = state / WORD_BITS;
60   
61     m[mc->n * ch + wno] |= 1<<off;
62 }
63
64 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
65                                  int state)
66 {
67     int off = state & (WORD_BITS-1);
68     int wno = state / WORD_BITS;
69
70     return m[mc->n * ch + wno] & (1<<off);
71 }
72
73 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
74 {
75     MatchContext *mc = (MatchContext *) xmalloc (sizeof(*mc));
76     int s;
77
78     mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
79     mc->range = range;
80     mc->fact = (range+1)*mc->n;
81     mc->match_mask = (MatchWord *) xcalloc (mc->n, sizeof(*mc->match_mask));
82
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);
86     return mc;
87 }
88
89 static void rm_MatchContext (MatchContext **mc)
90 {
91     xfree ((*mc)->match_mask);
92     xfree (*mc);
93     *mc = NULL;
94 }
95
96 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
97                    struct DFA *dfa, int ch)
98 {
99     int j, s = 0;
100     MatchWord *Rsrc_p = Rsrc, mask;
101
102     for (j = 0; j<mc->n; j++)
103         Rdst[j] = 0;
104     while (1)
105     {
106         mask = *Rsrc_p++;
107         for (j = 0; j<WORD_BITS/4; j++)
108         {
109             if (mask & 15)
110             {
111                 if (mask & 1)
112                 {
113                     struct DFA_state *state = dfa->states[s];
114                     int i = state->tran_no;
115                     while (--i >= 0)
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);
119                 }
120                 if (mask & 2)
121                 {
122                     struct DFA_state *state = dfa->states[s+1];
123                     int i = state->tran_no;
124                     while (--i >= 0)
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);
128                 }
129                 if (mask & 4)
130                 {
131                     struct DFA_state *state = dfa->states[s+2];
132                     int i = state->tran_no;
133                     while (--i >= 0)
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);
137                 }
138                 if (mask & 8)
139                 {
140                     struct DFA_state *state = dfa->states[s+3];
141                     int i = state->tran_no;
142                     while (--i >= 0)
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);
146                 }
147             }
148             s += 4;
149             if (s >= dfa->no_states)
150                 return;
151             mask >>= 4;
152         }
153     }
154 }
155
156 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
157                    struct DFA *dfa)
158 {
159     int j, s = 0;
160     MatchWord *Rsrc_p = Rsrc, mask;
161     for (j = 0; j<mc->n; j++)
162         Rdst[j] = 0;
163     while (1)
164     {
165         mask = *Rsrc_p++;
166         for (j = 0; j<WORD_BITS/4; j++)
167         {
168             if (mask & 15)
169             {
170                 if (mask & 1)
171                 {
172                     struct DFA_state *state = dfa->states[s];
173                     int i = state->tran_no;
174                     while (--i >= 0)
175                         set_bit (mc, Rdst, 0, state->trans[i].to);
176                 }
177                 if (mask & 2)
178                 {
179                     struct DFA_state *state = dfa->states[s+1];
180                     int i = state->tran_no;
181                     while (--i >= 0)
182                         set_bit (mc, Rdst, 0, state->trans[i].to);
183                 }
184                 if (mask & 4)
185                 {
186                     struct DFA_state *state = dfa->states[s+2];
187                     int i = state->tran_no;
188                     while (--i >= 0)
189                         set_bit (mc, Rdst, 0, state->trans[i].to);
190                 }
191                 if (mask & 8)
192                 {
193                     struct DFA_state *state = dfa->states[s+3];
194                     int i = state->tran_no;
195                     while (--i >= 0)
196                         set_bit (mc, Rdst, 0, state->trans[i].to);
197                 }
198             }
199             s += 4;
200             if (s >= dfa->no_states)
201                 return;
202             mask >>= 4;
203         }
204     }
205 }
206
207 static void or (MatchContext *mc, MatchWord *Rdst,
208                 MatchWord *Rsrc1, MatchWord *Rsrc2)
209 {
210     int i;
211     for (i = 0; i<mc->n; i++)
212         Rdst[i] = Rsrc1[i] | Rsrc2[i];
213 }
214
215 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
216                         Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
217                         int range)
218 {
219     int d;
220     MatchWord *Rtmp_2 = Rtmp + mc->n;
221
222     mask_shift (mc, Rj1, Rj, dfa, ch);
223     for (d = 1; d <= mc->range; d++)
224     {
225         or (mc, Rtmp, Rj, Rj1);                         /* 2,3 */
226         
227         shift (mc, Rtmp_2, Rtmp, dfa);
228
229         mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch);      /* 1 */
230                 
231         or (mc, Rtmp, Rtmp_2, Rtmp);                    /* 1,2,3*/
232
233         Rj1 += mc->n;
234         
235         or (mc, Rj1, Rtmp, Rj);                         /* 1,2,3,4 */
236
237         Rj += mc->n;
238     }
239     return 1;
240
241 }
242
243
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)
249 {
250     int lo, hi, d;
251     void *p;
252     short *indxp;
253     char *info;
254
255     dict_bf_readp (dict->dbf, ptr, &p);
256     lo = 0;
257     hi = DICT_nodir(p)-1;
258     indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
259
260     while (lo <= hi)
261     {
262         if (indxp[-lo] > 0)
263         {
264             /* string (Dict_char *) DICT_EOS terminated */
265             /* unsigned char        length of information */
266             /* char *               information */
267             int j;
268             int was_match = 0;
269             info = (char*)p + indxp[-lo];
270             for (j=0; ; j++)
271             {
272                 Dict_char ch;
273                 MatchWord *Rj0 =    Rj + j    *mc->fact;
274                 MatchWord *Rj1 =    Rj + (j+1)*mc->fact;
275                 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
276                 int range;
277
278                 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
279                 prefix[pos+j] = ch;
280                 if (pos+j > *max_pos)
281                     *max_pos = pos+j;
282                 if (ch == DICT_EOS)
283                 {
284                     if (was_match)
285                         if ((*userfunc)((char*) prefix,
286                                         info+(j+1)*sizeof(Dict_char), client))
287                             return 1;
288                     break;
289                 }
290                 if (pos+j >= init_pos)
291                     range = mc->range;
292                 else
293                     range = 0;
294                 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
295                 for (d = mc->n; --d >= 0; )
296                     if (Rj1[range*mc->n + d])
297                         break;
298                 if (d < 0)
299                     break;
300                 was_match = 0;
301                 for (d = mc->n; --d >= 0; )
302                     if (Rj1[range*mc->n + d] & mc->match_mask[d])
303                     {
304                         was_match = 1;
305                         break;
306                     }
307             }
308         }
309         else
310         {
311             MatchWord *Rj1 =    Rj+  mc->fact;
312             MatchWord *Rj_tmp = Rj+2*mc->fact;
313             Dict_char ch;
314             int range;
315
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));
322             prefix[pos] = ch;
323             
324             if (pos > *max_pos)
325                 *max_pos = pos;
326             if (pos >= init_pos)
327                 range = mc->range;
328             else
329                 range = 0;
330             move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
331             for (d = mc->n; --d >= 0; )
332                 if (Rj1[range*mc->n + d])
333                     break;
334             if (d >= 0)
335             {
336                 Dict_ptr subptr;
337                 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
338                 {
339                     for (d = mc->n; --d >= 0; )
340                         if (Rj1[range*mc->n + d] & mc->match_mask[d])
341                         {
342                             prefix[pos+1] = DICT_EOS;
343                             if ((*userfunc)((char*) prefix,
344                                             info+sizeof(Dict_ptr)+
345                                             sizeof(Dict_char), client))
346                                 return 1;
347                             break;
348                         }
349                 }
350                 memcpy (&subptr, info, sizeof(Dict_ptr));
351                 if (subptr)
352                 {
353                     if (dict_grep (dict, subptr, mc, Rj1, pos+1,
354                                    client, userfunc, prefix, dfa, max_pos,
355                                    init_pos))
356                         return 1;
357                     dict_bf_readp (dict->dbf, ptr, &p);
358                     indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
359                 }
360             }
361         }
362         lo++;
363     }
364     return 0;
365 }
366
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,
370                                       void *client))
371 {
372     MatchWord *Rj;
373     Dict_char prefix[MAX_LENGTH+1];
374     const char *this_pattern = pattern;
375     MatchContext *mc;
376     struct DFA *dfa = dfa_init();
377     int i, d;
378
379 #if 0
380     debug_dfa_trav = 1;
381     debug_dfa_tran = 1;
382     debug_dfa_followpos = 1;
383     dfa_verbose = 1;
384 #endif
385
386     yaz_log (YLOG_DEBUG, "dict_lookup_grep range=%d", range);
387     for (i = 0; pattern[i]; i++)
388     {
389         yaz_log (YLOG_DEBUG, " %3d  %c", pattern[i],
390               (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
391     }
392    
393     dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
394
395     i = dfa_parse (dfa, &this_pattern);
396     if (i || *this_pattern)
397     {
398         yaz_log (YLOG_WARN, "dfa_parse fail=%d", i);
399         dfa_delete (&dfa);
400         return -1;
401     }
402     dfa_mkstate (dfa);
403
404     mc = mk_MatchContext (dfa, range);
405
406     Rj = (MatchWord *) xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
407
408     set_bit (mc, Rj, 0, 0);
409     for (d = 1; d<=mc->range; d++)
410     {
411         int s;
412         memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
413         for (s = 0; s<dfa->no_states; s++)
414         {
415             if (get_bit (mc, Rj, d-1, s))
416             {
417                 struct DFA_state *state = dfa->states[s];
418                 int i = state->tran_no;
419                 while (--i >= 0)
420                     set_bit (mc, Rj, d, state->trans[i].to);
421             }
422         }
423     }
424     *max_pos = 0;
425     if (dict->head.root)
426         i = dict_grep (dict, dict->head.root, mc, Rj, 0, client,
427                        userfunc, prefix,
428                        dfa, max_pos, init_pos);
429     else
430         i = 0;
431     yaz_log (YLOG_DEBUG, "max_pos = %d", *max_pos);
432     dfa_delete (&dfa);
433     xfree (Rj);
434     rm_MatchContext (&mc);
435     return i;
436 }
437
438 void dict_grep_cmap (Dict dict, void *vp,
439                      const char **(*cmap)(void *vp, const char **from, int len))
440 {
441     dict->grep_cmap = cmap;
442     dict->grep_cmap_data = vp;
443 }
444 /*
445  * Local variables:
446  * c-basic-offset: 4
447  * indent-tabs-mode: nil
448  * End:
449  * vim: shiftwidth=4 tabstop=8 expandtab
450  */
451