2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1994-09-22 14:43:57 adam
8 * First functional version of lookup with error correction. A 'range'
9 * specified the maximum number of insertions+deletions+substitutions.
11 * Revision 1.1 1994/09/22 10:43:44 adam
12 * Two versions of depend. Type 1 is the tail-type compatible with
13 * all make programs. Type 2 is the GNU make with include facility.
14 * Type 2 is default. depend rule chooses current rule.
25 typedef unsigned MatchWord;
32 #define SH(x) (((x)<<1)+1)
34 int dict_look_ec (Dict dict, Dict_ptr ptr, MatchInfo *mi, MatchWord *ri_base,
35 int pos, int (*userfunc)(Dict_char *),
36 int range, Dict_char *prefix)
42 MatchWord match_mask = 1<<(mi->m-1);
44 dict_bf_readp (dict->dbf, ptr, &p);
47 indxp = (short*) ((char*) p+DICT_PAGESIZE-sizeof(short));
52 /* string (Dict_char *) DICT_EOS terminated */
53 /* unsigned char length of information */
54 /* char * information */
55 MatchWord *ri = ri_base, sc;
57 info = (char*)p + indxp[-lo];
62 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
66 if (ri[range] & match_mask)
70 if (j+pos >= mi->m+range)
73 ri[1+range] = SH(ri[0]) & sc;
74 for (i=1; i<=range; i++)
75 ri[i+1+range] = (SH(ri[i]) & sc) | SH(ri[i-1])
76 | SH(ri[i+range]) | ri[i-1];
78 if (!(ri[range] & (1<<(pos+j))))
85 MatchWord *ri = ri_base, sc;
89 /* Dict_char sub char */
90 /* unsigned char length of information */
91 /* char * information */
92 info = (char*)p - indxp[-lo];
93 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
97 ri[1+range] = SH(ri[0]) & sc;
98 for (i=1; i<=range; i++)
99 ri[i+1+range] = (SH(ri[i]) & sc) | SH(ri[i-1])
100 | SH(ri[i+range]) | ri[i-1];
102 if (ri[range] & (1<<pos))
105 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)] &&
106 (ri[range] & match_mask))
108 prefix[pos+1] = DICT_EOS;
111 memcpy (&subptr, info, sizeof(Dict_ptr));
114 dict_look_ec (dict, subptr, mi, ri, pos+1,
115 userfunc, range, prefix);
116 dict_bf_readp (dict->dbf, ptr, &p);
117 indxp = (short*) ((char*) p+DICT_PAGESIZE-sizeof(short));
126 static MatchInfo *prepare_match (Dict_char *pattern)
132 mi = xmalloc (sizeof(*mi));
133 mi->m = dict_strlen (pattern);
134 mi->s = s = xmalloc (sizeof(*s)*256); /* 256 !!! */
135 for (i=0; i<256; i++)
137 for (i=0; pattern[i]; i++)
138 s[pattern[i]&255] += 1<<i;
142 int dict_lookup_ec (Dict dict, Dict_char *pattern, int range,
143 int (*userfunc)(Dict_char *name))
148 Dict_char prefix[2048];
150 if (dict->head.last == 1)
153 mi = prepare_match (pattern);
156 ri = xmalloc ((dict_strlen(pattern)+range+2)*(range+1)*sizeof(*ri));
158 ri = xmalloc (2048 * (range+1) * sizeof(*ri));
161 for (i=0; i<=range; i++)
164 i = dict_look_ec (dict, 1, mi, ri, 0, userfunc, range, prefix);