Added support for character mapping.
[idzebra-moved-to-github.git] / dict / lookgrep.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lookgrep.c,v $
7  * Revision 1.17  1996-06-04 10:20:06  adam
8  * Added support for character mapping.
9  *
10  * Revision 1.16  1996/05/24  14:46:04  adam
11  * Added dict_grep_cmap function to define user-mapping in grep lookups.
12  *
13  * Revision 1.15  1996/03/20  09:35:18  adam
14  * Function dict_lookup_grep got extra parameter, init_pos, which marks
15  * from which position in pattern approximate pattern matching should occur.
16  *
17  * Revision 1.14  1996/02/02  13:43:51  adam
18  * The public functions simply use char instead of Dict_char to represent
19  * search strings. Dict_char is used internally only.
20  *
21  * Revision 1.13  1996/01/08  09:09:30  adam
22  * Function dfa_parse got 'const' string argument.
23  *
24  * Revision 1.12  1995/12/11  09:04:48  adam
25  * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
26  *
27  * Revision 1.11  1995/12/06  14:43:02  adam
28  * New function: dict_delete.
29  *
30  * Revision 1.10  1995/11/16  17:00:44  adam
31  * Changed stupid log.
32  *
33  * Revision 1.9  1995/10/27  13:58:09  adam
34  * Makes 'Database unavailable' diagnostic.
35  *
36  * Revision 1.8  1995/10/19  14:57:21  adam
37  * New feature: grep lookup saves length of longest prefix match.
38  *
39  * Revision 1.7  1995/10/17  18:01:22  adam
40  * Userfunc may return non-zero in which case the the grepping stops
41  * immediately.
42  *
43  * Revision 1.6  1995/10/09  16:18:32  adam
44  * Function dict_lookup_grep got extra client data parameter.
45  *
46  * Revision 1.5  1995/09/14  11:52:59  adam
47  * Grep handle function parameter info is const now.
48  *
49  * Revision 1.4  1995/01/24  16:01:02  adam
50  * Added -ansi to CFLAGS.
51  * Use new API of dfa module.
52  *
53  * Revision 1.3  1994/10/05  12:16:50  adam
54  * Pagesize is a resource now.
55  *
56  * Revision 1.2  1994/10/04  12:08:07  adam
57  * Some bug fixes and some optimizations.
58  *
59  * Revision 1.1  1994/10/03  17:23:04  adam
60  * First version of dictionary lookup with regular expressions and errors.
61  *
62  */
63
64 #include <stdlib.h>
65 #include <string.h>
66 #include <stdio.h>
67 #include <assert.h>
68
69 #include <dfa.h>
70 #include <dict.h>
71
72 typedef unsigned MatchWord;
73 #define WORD_BITS 32
74 #define MAX_LENGTH 1024
75
76 typedef struct {
77   int n;                 /* no of MatchWord needed */
78   int range;             /* max no. of errors */
79   int fact;              /* (range+1)*n */
80   MatchWord *match_mask; /* match_mask */
81 } MatchContext;
82
83 #define INLINE 
84
85 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
86 {
87     int off = state & (WORD_BITS-1);
88     int wno = state / WORD_BITS;
89   
90     m[mc->n * ch + wno] |= 1<<off;
91 }
92
93 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
94                                  int state)
95 {
96     int off = state & (WORD_BITS-1);
97     int wno = state / WORD_BITS;
98
99     return m[mc->n * ch + wno] & (1<<off);
100 }
101
102 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
103 {
104     MatchContext *mc = xmalloc (sizeof(*mc));
105     int s;
106
107     mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
108     mc->range = range;
109     mc->fact = (range+1)*mc->n;
110     mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
111
112     for (s = 0; s<dfa->no_states; s++)
113         if (dfa->states[s]->rule_no)
114             set_bit (mc, mc->match_mask, 0, s);
115     return mc;
116 }
117
118 static void rm_MatchContext (MatchContext **mc)
119 {
120     xfree ((*mc)->match_mask);
121     xfree (*mc);
122     *mc = NULL;
123 }
124
125 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
126                    struct DFA *dfa, int ch)
127 {
128     int j, s = 0;
129     MatchWord *Rsrc_p = Rsrc, mask;
130
131     for (j = 0; j<mc->n; j++)
132         Rdst[j] = 0;
133     while (1)
134     {
135         mask = *Rsrc_p++;
136         for (j = 0; j<WORD_BITS/4; j++)
137         {
138             if (mask & 15)
139             {
140                 if (mask & 1)
141                 {
142                     struct DFA_state *state = dfa->states[s];
143                     int i = state->tran_no;
144                     while (--i >= 0)
145                         if (ch >= state->trans[i].ch[0] &&
146                             ch <= state->trans[i].ch[1])
147                             set_bit (mc, Rdst, 0, state->trans[i].to);
148                 }
149                 if (mask & 2)
150                 {
151                     struct DFA_state *state = dfa->states[s+1];
152                     int i = state->tran_no;
153                     while (--i >= 0)
154                         if (ch >= state->trans[i].ch[0] &&
155                             ch <= state->trans[i].ch[1])
156                             set_bit (mc, Rdst, 0, state->trans[i].to);
157                 }
158                 if (mask & 4)
159                 {
160                     struct DFA_state *state = dfa->states[s+2];
161                     int i = state->tran_no;
162                     while (--i >= 0)
163                         if (ch >= state->trans[i].ch[0] &&
164                             ch <= state->trans[i].ch[1])
165                             set_bit (mc, Rdst, 0, state->trans[i].to);
166                 }
167                 if (mask & 8)
168                 {
169                     struct DFA_state *state = dfa->states[s+3];
170                     int i = state->tran_no;
171                     while (--i >= 0)
172                         if (ch >= state->trans[i].ch[0] &&
173                             ch <= state->trans[i].ch[1])
174                             set_bit (mc, Rdst, 0, state->trans[i].to);
175                 }
176             }
177             s += 4;
178             if (s >= dfa->no_states)
179                 return;
180             mask >>= 4;
181         }
182     }
183 }
184
185 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
186                    struct DFA *dfa)
187 {
188     int j, s = 0;
189     MatchWord *Rsrc_p = Rsrc, mask;
190     for (j = 0; j<mc->n; j++)
191         Rdst[j] = 0;
192     while (1)
193     {
194         mask = *Rsrc_p++;
195         for (j = 0; j<WORD_BITS/4; j++)
196         {
197             if (mask & 15)
198             {
199                 if (mask & 1)
200                 {
201                     struct DFA_state *state = dfa->states[s];
202                     int i = state->tran_no;
203                     while (--i >= 0)
204                         set_bit (mc, Rdst, 0, state->trans[i].to);
205                 }
206                 if (mask & 2)
207                 {
208                     struct DFA_state *state = dfa->states[s+1];
209                     int i = state->tran_no;
210                     while (--i >= 0)
211                         set_bit (mc, Rdst, 0, state->trans[i].to);
212                 }
213                 if (mask & 4)
214                 {
215                     struct DFA_state *state = dfa->states[s+2];
216                     int i = state->tran_no;
217                     while (--i >= 0)
218                         set_bit (mc, Rdst, 0, state->trans[i].to);
219                 }
220                 if (mask & 8)
221                 {
222                     struct DFA_state *state = dfa->states[s+3];
223                     int i = state->tran_no;
224                     while (--i >= 0)
225                         set_bit (mc, Rdst, 0, state->trans[i].to);
226                 }
227             }
228             s += 4;
229             if (s >= dfa->no_states)
230                 return;
231             mask >>= 4;
232         }
233     }
234 }
235
236 static void or (MatchContext *mc, MatchWord *Rdst,
237                 MatchWord *Rsrc1, MatchWord *Rsrc2)
238 {
239     int i;
240     for (i = 0; i<mc->n; i++)
241         Rdst[i] = Rsrc1[i] | Rsrc2[i];
242 }
243
244 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
245                         Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
246                         int range)
247 {
248     int d;
249     MatchWord *Rtmp_2 = Rtmp + mc->n;
250
251     mask_shift (mc, Rj1, Rj, dfa, ch);
252     for (d = 1; d <= mc->range; d++)
253     {
254         or (mc, Rtmp, Rj, Rj1);                         /* 2,3 */
255         
256         shift (mc, Rtmp_2, Rtmp, dfa);
257
258         mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch);      /* 1 */
259                 
260         or (mc, Rtmp, Rtmp_2, Rtmp);                    /* 1,2,3*/
261
262         Rj1 += mc->n;
263         
264         or (mc, Rj1, Rtmp, Rj);                         /* 1,2,3,4 */
265
266         Rj += mc->n;
267     }
268     return 1;
269
270 }
271
272
273 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
274                       MatchWord *Rj, int pos, void *client,
275                       int (*userfunc)(char *, const char *, void *),
276                       Dict_char *prefix, struct DFA *dfa,
277                       int *max_pos, int init_pos)
278 {
279     int lo, hi, d;
280     void *p;
281     short *indxp;
282     char *info;
283
284     dict_bf_readp (dict->dbf, ptr, &p);
285     lo = 0;
286     hi = DICT_nodir(p)-1;
287     indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));    
288
289     while (lo <= hi)
290     {
291         if (indxp[-lo] > 0)
292         {
293             /* string (Dict_char *) DICT_EOS terminated */
294             /* unsigned char        length of information */
295             /* char *               information */
296             int j;
297             int was_match = 0;
298             info = (char*)p + indxp[-lo];
299             for (j=0; ; j++)
300             {
301                 Dict_char ch;
302                 MatchWord *Rj0 =    Rj + j    *mc->fact;
303                 MatchWord *Rj1 =    Rj + (j+1)*mc->fact;
304                 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
305                 int range;
306
307                 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
308                 prefix[pos+j] = ch;
309                 if (pos+j > *max_pos)
310                     *max_pos = pos+j;
311                 if (ch == DICT_EOS)
312                 {
313                     if (was_match)
314                         if ((*userfunc)((char*) prefix,
315                                         info+(j+1)*sizeof(Dict_char), client))
316                             return 1;
317                     break;
318                 }
319                 if (pos+j >= init_pos)
320                     range = mc->range;
321                 else
322                     range = 0;
323                 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
324                 for (d = mc->n; --d >= 0; )
325                     if (Rj1[range*mc->n + d])
326                         break;
327                 if (d < 0)
328                     break;
329                 was_match = 0;
330                 for (d = mc->n; --d >= 0; )
331                     if (Rj1[range*mc->n + d] & mc->match_mask[d])
332                     {
333                         was_match = 1;
334                         break;
335                     }
336             }
337         }
338         else
339         {
340             MatchWord *Rj1 =    Rj+  mc->fact;
341             MatchWord *Rj_tmp = Rj+2*mc->fact;
342             Dict_char ch;
343             int range;
344
345             /* Dict_ptr             subptr */
346             /* Dict_char            sub char */
347             /* unsigned char        length of information */
348             /* char *               information */
349             info = (char*)p - indxp[-lo];
350             memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
351             prefix[pos] = ch;
352             
353             if (pos > *max_pos)
354                 *max_pos = pos;
355             if (pos >= init_pos)
356                 range = mc->range;
357             else
358                 range = 0;
359             move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
360             for (d = mc->n; --d >= 0; )
361                 if (Rj1[range*mc->n + d])
362                     break;
363             if (d >= 0)
364             {
365                 Dict_ptr subptr;
366                 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
367                 {
368                     for (d = mc->n; --d >= 0; )
369                         if (Rj1[range*mc->n + d] & mc->match_mask[d])
370                         {
371                             prefix[pos+1] = DICT_EOS;
372                             if ((*userfunc)((char*) prefix,
373                                             info+sizeof(Dict_ptr)+
374                                             sizeof(Dict_char), client))
375                                 return 1;
376                             break;
377                         }
378                 }
379                 memcpy (&subptr, info, sizeof(Dict_ptr));
380                 if (subptr)
381                 {
382                     if (dict_grep (dict, subptr, mc, Rj1, pos+1,
383                                    client, userfunc, prefix, dfa, max_pos,
384                                    init_pos))
385                         return 1;
386                     dict_bf_readp (dict->dbf, ptr, &p);
387                     indxp = (short*) ((char*) p+DICT_pagesize(dict)
388                                       -sizeof(short));
389                 }
390             }
391         }
392         lo++;
393     }
394     return 0;
395 }
396
397 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
398                       int *max_pos, int init_pos,
399                       int (*userfunc)(char *name, const char *info,
400                                       void *client))
401 {
402     MatchWord *Rj;
403     Dict_char prefix[MAX_LENGTH+1];
404     const char *this_pattern = pattern;
405     MatchContext *mc;
406     struct DFA *dfa = dfa_init();
407     int i, d;
408
409     logf (LOG_DEBUG, "dict_lookup_grep '%s' range=%d", pattern, range);
410    
411     dfa_set_cmap (dfa, dict->grep_cmap);
412
413     i = dfa_parse (dfa, &this_pattern);
414     if (i || *this_pattern)
415     {
416         dfa_delete (&dfa);
417         return -1;
418     }
419     dfa_mkstate (dfa);
420
421     mc = mk_MatchContext (dfa, range);
422
423     Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
424
425     set_bit (mc, Rj, 0, 0);
426     for (d = 1; d<=mc->range; d++)
427     {
428         int s;
429         memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
430         for (s = 0; s<dfa->no_states; s++)
431         {
432             if (get_bit (mc, Rj, d-1, s))
433             {
434                 struct DFA_state *state = dfa->states[s];
435                 int i = state->tran_no;
436                 while (--i >= 0)
437                     set_bit (mc, Rj, d, state->trans[i].to);
438             }
439         }
440     }
441     *max_pos = 0;
442     if (dict->head.last > 1)
443         i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
444                        dfa, max_pos, init_pos);
445     else
446         i = 0;
447     logf (LOG_DEBUG, "max_pos = %d", *max_pos);
448     dfa_delete (&dfa);
449     xfree (Rj);
450     rm_MatchContext (&mc);
451     return i;
452 }
453
454 void dict_grep_cmap (Dict dict, char **(*cmap)(const char **from, int len))
455 {
456     dict->grep_cmap = cmap;
457 }