2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1994-10-05 12:16:48 adam
8 * Pagesize is a resource now.
10 * Revision 1.9 1994/09/16 15:39:13 adam
11 * Initial code of lookup - not tested yet.
13 * Revision 1.8 1994/09/16 12:35:01 adam
14 * New version of split_page which use clean_page for splitting.
16 * Revision 1.7 1994/09/12 08:06:42 adam
17 * Futher development of insert.c
19 * Revision 1.6 1994/09/06 13:05:15 adam
20 * Further development of insertion. Some special cases are
21 * not properly handled yet! assert(0) are put here. The
22 * binary search in each page definitely reduce usr CPU.
24 * Revision 1.5 1994/09/01 17:49:39 adam
25 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
27 * Revision 1.4 1994/09/01 17:44:09 adam
28 * depend include change.
30 * Revision 1.3 1994/08/18 12:40:56 adam
31 * Some development of dictionary. Not finished at all!
33 * Revision 1.2 1994/08/17 13:32:19 adam
34 * Use cache in dict - not in bfile.
36 * Revision 1.1 1994/08/16 16:26:48 adam
50 static int dict_ins (Dict dict, const Dict_char *str,
51 Dict_ptr back_ptr, int userlen, void *userinfo);
52 static void clean_page (Dict dict, Dict_ptr ptr, void *p, Dict_char *out,
53 Dict_ptr subptr, char *userinfo);
56 static Dict_ptr new_page (Dict dict, Dict_ptr back_ptr, void **pp)
59 Dict_ptr ptr = dict->head.free_list;
60 if (dict->head.free_list == dict->head.last)
62 dict->head.free_list++;
63 dict->head.last = dict->head.free_list;
64 dict_bf_newp (dict->dbf, ptr, &p);
68 dict_bf_readp (dict->dbf, dict->head.free_list, &p);
69 dict->head.free_list = DICT_nextptr(p);
70 if (dict->head.free_list == 0)
71 dict->head.free_list = dict->head.last;
75 DICT_backptr(p) = back_ptr;
78 DICT_size(p) = DICT_infoffset;
84 static int split_page (Dict dict, Dict_ptr ptr, void *p)
90 short *indxp, *best_indxp = NULL;
91 Dict_char best_char = 0;
92 Dict_char prev_char = 0;
93 int best_no = -1, no_current = 1;
95 /* determine splitting char... */
96 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
97 for (i = DICT_nodir (p); --i >= 0; --indxp)
99 if (*indxp > 0) /* tail string here! */
103 memcpy (&dc, (char*) p + *indxp, sizeof(dc));
105 { /* first entry met */
106 best_char = prev_char = dc;
109 else if (prev_char == dc)
110 { /* same char prefix. update */
111 if (++no_current > best_no)
112 { /* best entry so far */
113 best_no = no_current;
119 { /* new char prefix. restore */
125 if (best_no < 0) /* we didn't find any tail string entry at all! */
128 subptr = new_page (dict, ptr, &subp);
129 /* scan entries to see if there is a string with */
130 /* length 1. info_here indicates if such entry exist */
132 for (indxp=best_indxp, i=0; i<best_no; i++, indxp++)
139 info = (char*) p + *indxp; /* entry start */
140 assert (*info == best_char);
141 slen = dict_strlen(info);
147 info_here = info+(slen+1)*sizeof(Dict_char);
151 info1 = info+(1+slen)*sizeof(Dict_char); /* info start */
152 dict_ins (dict, info+sizeof(Dict_char), subptr, *info1, info1+1);
153 dict_bf_readp (dict->dbf, ptr, &p);
156 /* now clean the page ... */
157 clean_page (dict, ptr, p, &best_char, subptr, info_here);
161 static void clean_page (Dict dict, Dict_ptr ptr, void *p, Dict_char *out,
162 Dict_ptr subptr, char *userinfo)
164 char *np = xmalloc (dict->head.page_size);
166 short *indxp1, *indxp2;
169 indxp1 = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
170 indxp2 = (short*) ((char*) np+DICT_pagesize(dict));
171 info2 = (char*) np + DICT_infoffset;
172 for (i = DICT_nodir (p); --i >= 0; --indxp1)
174 if (*indxp1 > 0) /* tail string here! */
176 /* string (Dict_char *) DICT_EOS terminated */
177 /* unsigned char length of information */
178 /* char * information */
180 info1 = (char*) p + *indxp1;
181 if (out && memcmp (out, info1, sizeof(Dict_char)) == 0)
185 *--indxp2 = -(info2 - np);
186 memcpy (info2, &subptr, sizeof(Dict_ptr));
187 info2 += sizeof(Dict_ptr);
188 memcpy (info2, out, sizeof(Dict_char));
189 info2 += sizeof(Dict_char);
192 memcpy (info2, userinfo, *userinfo+1);
193 info2 += *userinfo + 1;
201 *--indxp2 = info2 - np;
202 slen = (dict_strlen(info1)+1)*sizeof(Dict_char);
203 memcpy (info2, info1, slen);
209 /* Dict_ptr subptr */
210 /* Dict_char sub char */
211 /* unsigned char length of information */
212 /* char * information */
214 assert (*indxp1 < 0);
215 *--indxp2 = -(info2 - np);
216 info1 = (char*) p - *indxp1;
217 memcpy (info2, info1, sizeof(Dict_ptr)+sizeof(Dict_char));
218 info1 += sizeof(Dict_ptr)+sizeof(Dict_char);
219 info2 += sizeof(Dict_ptr)+sizeof(Dict_char);
222 memcpy (info2, info1, slen);
226 memcpy ((char*)p+DICT_infoffset, (char*)np+DICT_infoffset,
227 DICT_pagesize(dict)-DICT_infoffset);
228 DICT_size(p) = info2 - np;
232 dict_bf_touch (dict->dbf, ptr);
237 /* return 0 if new */
238 /* return 1 if before but change of info */
239 /* return 2 if same as before */
241 static int dict_ins (Dict dict, const Dict_char *str,
242 Dict_ptr back_ptr, int userlen, void *userinfo)
244 int hi, lo, mid, slen, cmp = 1;
245 Dict_ptr ptr = back_ptr;
251 ptr = new_page (dict, back_ptr, &p);
253 dict_bf_readp (dict->dbf, ptr, &p);
259 hi = DICT_nodir(p)-1;
260 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
266 /* string (Dict_char *) DICT_EOS terminated */
267 /* unsigned char length of information */
268 /* char * information */
269 info = (char*)p + indxp[-mid];
270 cmp = dict_strcmp((Dict_char*) info, str);
273 info += (dict_strlen(info)+1)*sizeof(Dict_char);
274 /* consider change of userinfo length... */
275 if (*info == userlen)
277 if (memcmp (info+1, userinfo, userlen))
279 dict_bf_touch (dict->dbf, ptr);
280 memcpy (info+1, userinfo, userlen);
285 else if (*info > userlen)
289 dict_bf_touch (dict->dbf, ptr);
290 memcpy (info+1, userinfo, userlen);
301 /* Dict_ptr subptr */
302 /* Dict_char sub char */
303 /* unsigned char length of information */
304 /* char * information */
305 info = (char*)p - indxp[-mid];
306 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
310 memcpy (&subptr, info, sizeof(Dict_ptr));
311 if (*++str == DICT_EOS)
315 xlen = info[sizeof(Dict_ptr)+sizeof(Dict_char)];
318 if (memcmp (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
321 dict_bf_touch (dict->dbf, ptr);
322 memcpy (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
328 else if (xlen > userlen)
331 info[sizeof(Dict_ptr)+sizeof(Dict_char)] = userlen;
332 memcpy (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
334 dict_bf_touch (dict->dbf, ptr);
337 if (DICT_size(p)+sizeof(Dict_char)+sizeof(Dict_ptr)+
339 DICT_pagesize(dict) - (1+DICT_nodir(p))*sizeof(short))
341 if (DICT_type(p) == 1)
343 clean_page (dict, ptr, p, NULL, 0, NULL);
344 return dict_ins (dict, str-1, ptr,
347 if (split_page (dict, ptr, p))
349 log (LOG_FATAL, "Unable to split page %d\n", ptr);
352 return dict_ins (dict, str-1, ptr, userlen, userinfo);
356 info = (char*)p + DICT_size(p);
357 memcpy (info, &subptr, sizeof(subptr));
358 memcpy (info+sizeof(Dict_ptr), &dc, sizeof(Dict_char));
359 info[sizeof(Dict_char)+sizeof(Dict_ptr)] = userlen;
360 memcpy (info+sizeof(Dict_char)+sizeof(Dict_ptr)+1,
362 indxp[-mid] = -DICT_size(p);
363 DICT_size(p) += sizeof(Dict_char)+sizeof(Dict_ptr)
366 dict_bf_touch (dict->dbf, ptr);
376 subptr = new_page (dict, ptr, NULL);
377 memcpy (info, &subptr, sizeof(subptr));
378 dict_bf_touch (dict->dbf, ptr);
380 return dict_ins (dict, str, subptr, userlen, userinfo);
390 if (lo>hi && cmp < 0)
392 slen = (dict_strlen(str)+1)*sizeof(Dict_char);
393 if (DICT_size(p)+slen+userlen >=
394 DICT_pagesize(dict) - (1+DICT_nodir(p))*sizeof(short)) /* overflow? */
396 split_page (dict, ptr, p);
397 return dict_ins (dict, str, ptr, userlen, userinfo);
403 indxp1 = (short*)((char*) p + DICT_pagesize(dict)
404 - DICT_nodir(p)*sizeof(short));
405 for (; indxp1 != indxp; indxp1++)
406 indxp1[0] = indxp1[1];
408 indxp1 = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
409 for (i = DICT_nodir (p); --i >= 0; --indxp1)
413 info = (char*)p - *indxp1;
414 assert (info[sizeof(Dict_ptr)] > ' ');
421 info = (char*)p + DICT_size(p);
422 memcpy (info, str, slen);
425 memcpy (info, userinfo, userlen);
428 *indxp = DICT_size(p);
429 DICT_size(p) = info- (char*) p;
430 dict_bf_touch (dict->dbf, ptr);
436 int dict_insert (Dict dict, const Dict_char *str, int userlen, void *userinfo)
438 assert (dict->head.last > 0);
439 if (dict->head.last == 1)
440 return dict_ins (dict, str, 0, userlen, userinfo);
442 return dict_ins (dict, str, 1, userlen, userinfo);