2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1994-09-26 16:31:06 adam
10 * Revision 1.2 1994/09/22 14:43:57 adam
11 * First functional version of lookup with error correction. A 'range'
12 * specified the maximum number of insertions+deletions+substitutions.
14 * Revision 1.1 1994/09/22 10:43:44 adam
15 * Two versions of depend. Type 1 is the tail-type compatible with
16 * all make programs. Type 2 is the GNU make with include facility.
17 * Type 2 is default. depend rule chooses current rule.
27 typedef unsigned MatchWord;
34 #define SH(x) (((x)<<1)+1)
36 int dict_look_ec (Dict dict, Dict_ptr ptr, MatchInfo *mi, MatchWord *ri_base,
37 int pos, int (*userfunc)(Dict_char *),
38 int range, Dict_char *prefix)
44 MatchWord match_mask = 1<<(mi->m-1);
46 dict_bf_readp (dict->dbf, ptr, &p);
49 indxp = (short*) ((char*) p+DICT_PAGESIZE-sizeof(short));
54 /* string (Dict_char *) DICT_EOS terminated */
55 /* unsigned char length of information */
56 /* char * information */
57 MatchWord *ri = ri_base, sc;
59 info = (char*)p + indxp[-lo];
64 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
68 if (ri[range] & match_mask)
72 if (j+pos >= mi->m+range)
75 ri[1+range] = SH(ri[0]) & sc;
76 for (i=1; i<=range; i++)
77 ri[i+1+range] = (SH(ri[i]) & sc) | SH(ri[i-1])
78 | SH(ri[i+range]) | ri[i-1];
80 if (!(ri[range] & (1<<(pos+j))))
87 MatchWord *ri = ri_base, sc;
91 /* Dict_char sub char */
92 /* unsigned char length of information */
93 /* char * information */
94 info = (char*)p - indxp[-lo];
95 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
99 ri[1+range] = SH(ri[0]) & sc;
100 for (i=1; i<=range; i++)
101 ri[i+1+range] = (SH(ri[i]) & sc) | SH(ri[i-1])
102 | SH(ri[i+range]) | ri[i-1];
104 if (ri[range] & (1<<pos))
107 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)] &&
108 (ri[range] & match_mask))
110 prefix[pos+1] = DICT_EOS;
113 memcpy (&subptr, info, sizeof(Dict_ptr));
116 dict_look_ec (dict, subptr, mi, ri, pos+1,
117 userfunc, range, prefix);
118 dict_bf_readp (dict->dbf, ptr, &p);
119 indxp = (short*) ((char*) p+DICT_PAGESIZE-sizeof(short));
128 static MatchInfo *prepare_match (Dict_char *pattern)
134 mi = xmalloc (sizeof(*mi));
135 mi->m = dict_strlen (pattern);
136 mi->s = s = xmalloc (sizeof(*s)*256); /* 256 !!! */
137 for (i=0; i<256; i++)
139 for (i=0; pattern[i]; i++)
140 s[pattern[i]&255] += 1<<i;
144 int dict_lookup_ec (Dict dict, Dict_char *pattern, int range,
145 int (*userfunc)(Dict_char *name))
150 Dict_char prefix[2048];
152 if (dict->head.last == 1)
155 mi = prepare_match (pattern);
158 ri = xmalloc ((dict_strlen(pattern)+range+2)*(range+1)*sizeof(*ri));
160 ri = xmalloc (2048 * (range+1) * sizeof(*ri));
163 for (i=0; i<=range; i++)
166 i = dict_look_ec (dict, 1, mi, ri, 0, userfunc, range, prefix);