2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1995-11-28 09:06:37 adam
8 * Fixed potential dangling pointer.
10 * Revision 1.12 1995/09/06 10:34:44 adam
11 * Memcpy in clean_page edited to satisfy checkergcc.
13 * Revision 1.11 1995/09/04 12:33:31 adam
14 * Various cleanup. YAZ util used instead.
16 * Revision 1.10 1994/10/05 12:16:48 adam
17 * Pagesize is a resource now.
19 * Revision 1.9 1994/09/16 15:39:13 adam
20 * Initial code of lookup - not tested yet.
22 * Revision 1.8 1994/09/16 12:35:01 adam
23 * New version of split_page which use clean_page for splitting.
25 * Revision 1.7 1994/09/12 08:06:42 adam
26 * Futher development of insert.c
28 * Revision 1.6 1994/09/06 13:05:15 adam
29 * Further development of insertion. Some special cases are
30 * not properly handled yet! assert(0) are put here. The
31 * binary search in each page definitely reduce usr CPU.
33 * Revision 1.5 1994/09/01 17:49:39 adam
34 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
36 * Revision 1.4 1994/09/01 17:44:09 adam
37 * depend include change.
39 * Revision 1.3 1994/08/18 12:40:56 adam
40 * Some development of dictionary. Not finished at all!
42 * Revision 1.2 1994/08/17 13:32:19 adam
43 * Use cache in dict - not in bfile.
45 * Revision 1.1 1994/08/16 16:26:48 adam
59 static int dict_ins (Dict dict, const Dict_char *str,
60 Dict_ptr back_ptr, int userlen, void *userinfo);
61 static void clean_page (Dict dict, Dict_ptr ptr, void *p, Dict_char *out,
62 Dict_ptr subptr, char *userinfo);
65 static Dict_ptr new_page (Dict dict, Dict_ptr back_ptr, void **pp)
68 Dict_ptr ptr = dict->head.free_list;
69 if (dict->head.free_list == dict->head.last)
71 dict->head.free_list++;
72 dict->head.last = dict->head.free_list;
73 dict_bf_newp (dict->dbf, ptr, &p);
77 dict_bf_readp (dict->dbf, dict->head.free_list, &p);
78 dict->head.free_list = DICT_nextptr(p);
79 if (dict->head.free_list == 0)
80 dict->head.free_list = dict->head.last;
84 DICT_backptr(p) = back_ptr;
87 DICT_size(p) = DICT_infoffset;
93 static int split_page (Dict dict, Dict_ptr ptr, void *p)
99 short *indxp, *best_indxp = NULL;
100 Dict_char best_char = 0;
101 Dict_char prev_char = 0;
102 int best_no = -1, no_current = 1;
104 /* determine splitting char... */
105 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
106 for (i = DICT_nodir (p); --i >= 0; --indxp)
108 if (*indxp > 0) /* tail string here! */
112 memcpy (&dc, (char*) p + *indxp, sizeof(dc));
114 { /* first entry met */
115 best_char = prev_char = dc;
118 else if (prev_char == dc)
119 { /* same char prefix. update */
120 if (++no_current > best_no)
121 { /* best entry so far */
122 best_no = no_current;
128 { /* new char prefix. restore */
134 if (best_no < 0) /* we didn't find any tail string entry at all! */
137 j = best_indxp - (short*) p;
138 subptr = new_page (dict, ptr, &subp);
139 /* scan entries to see if there is a string with */
140 /* length 1. info_here indicates if such entry exist */
142 for (i=0; i<best_no; i++, j++)
147 info = (char*) p + ((short*) p)[j];
149 assert (*info == best_char);
150 slen = dict_strlen(info);
156 info_here = info+(slen+1)*sizeof(Dict_char);
160 info1 = info+(1+slen)*sizeof(Dict_char); /* info start */
161 dict_ins (dict, info+sizeof(Dict_char), subptr, *info1, info1+1);
162 dict_bf_readp (dict->dbf, ptr, &p);
165 /* now clean the page ... */
166 clean_page (dict, ptr, p, &best_char, subptr, info_here);
170 static void clean_page (Dict dict, Dict_ptr ptr, void *p, Dict_char *out,
171 Dict_ptr subptr, char *userinfo)
173 char *np = xmalloc (dict->head.page_size);
175 short *indxp1, *indxp2;
178 indxp1 = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
179 indxp2 = (short*) ((char*) np+DICT_pagesize(dict));
180 info2 = (char*) np + DICT_infoffset;
181 for (i = DICT_nodir (p); --i >= 0; --indxp1)
183 if (*indxp1 > 0) /* tail string here! */
185 /* string (Dict_char *) DICT_EOS terminated */
186 /* unsigned char length of information */
187 /* char * information */
189 info1 = (char*) p + *indxp1;
190 if (out && memcmp (out, info1, sizeof(Dict_char)) == 0)
194 *--indxp2 = -(info2 - np);
195 memcpy (info2, &subptr, sizeof(Dict_ptr));
196 info2 += sizeof(Dict_ptr);
197 memcpy (info2, out, sizeof(Dict_char));
198 info2 += sizeof(Dict_char);
201 memcpy (info2, userinfo, *userinfo+1);
202 info2 += *userinfo + 1;
210 *--indxp2 = info2 - np;
211 slen = (dict_strlen(info1)+1)*sizeof(Dict_char);
212 memcpy (info2, info1, slen);
218 /* Dict_ptr subptr */
219 /* Dict_char sub char */
220 /* unsigned char length of information */
221 /* char * information */
223 assert (*indxp1 < 0);
224 *--indxp2 = -(info2 - np);
225 info1 = (char*) p - *indxp1;
226 memcpy (info2, info1, sizeof(Dict_ptr)+sizeof(Dict_char));
227 info1 += sizeof(Dict_ptr)+sizeof(Dict_char);
228 info2 += sizeof(Dict_ptr)+sizeof(Dict_char);
231 memcpy (info2, info1, slen);
236 memcpy ((char*)p+DICT_infoffset,
237 (char*)np+DICT_infoffset,
238 info2 - ((char*)np+DICT_infoffset));
239 memcpy ((char*)p + ((char*)indxp2 - (char*)np),
241 ((char*) np+DICT_pagesize(dict)) - (char*)indxp2);
243 memcpy ((char*)p+DICT_infoffset, (char*)np+DICT_infoffset,
244 DICT_pagesize(dict)-DICT_infoffset);
246 DICT_size(p) = info2 - np;
250 dict_bf_touch (dict->dbf, ptr);
255 /* return 0 if new */
256 /* return 1 if before but change of info */
257 /* return 2 if same as before */
259 static int dict_ins (Dict dict, const Dict_char *str,
260 Dict_ptr back_ptr, int userlen, void *userinfo)
262 int hi, lo, mid, slen, cmp = 1;
263 Dict_ptr ptr = back_ptr;
269 ptr = new_page (dict, back_ptr, &p);
271 dict_bf_readp (dict->dbf, ptr, &p);
277 hi = DICT_nodir(p)-1;
278 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
284 /* string (Dict_char *) DICT_EOS terminated */
285 /* unsigned char length of information */
286 /* char * information */
287 info = (char*)p + indxp[-mid];
288 cmp = dict_strcmp((Dict_char*) info, str);
291 info += (dict_strlen(info)+1)*sizeof(Dict_char);
292 /* consider change of userinfo length... */
293 if (*info == userlen)
295 if (memcmp (info+1, userinfo, userlen))
297 dict_bf_touch (dict->dbf, ptr);
298 memcpy (info+1, userinfo, userlen);
303 else if (*info > userlen)
307 dict_bf_touch (dict->dbf, ptr);
308 memcpy (info+1, userinfo, userlen);
319 /* Dict_ptr subptr */
320 /* Dict_char sub char */
321 /* unsigned char length of information */
322 /* char * information */
323 info = (char*)p - indxp[-mid];
324 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
328 memcpy (&subptr, info, sizeof(Dict_ptr));
329 if (*++str == DICT_EOS)
333 xlen = info[sizeof(Dict_ptr)+sizeof(Dict_char)];
336 if (memcmp (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
339 dict_bf_touch (dict->dbf, ptr);
340 memcpy (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
346 else if (xlen > userlen)
349 info[sizeof(Dict_ptr)+sizeof(Dict_char)] = userlen;
350 memcpy (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
352 dict_bf_touch (dict->dbf, ptr);
355 if (DICT_size(p)+sizeof(Dict_char)+sizeof(Dict_ptr)+
357 DICT_pagesize(dict) - (1+DICT_nodir(p))*sizeof(short))
359 if (DICT_type(p) == 1)
361 clean_page (dict, ptr, p, NULL, 0, NULL);
362 return dict_ins (dict, str-1, ptr,
365 if (split_page (dict, ptr, p))
367 logf (LOG_FATAL, "Unable to split page %d\n", ptr);
370 return dict_ins (dict, str-1, ptr, userlen, userinfo);
374 info = (char*)p + DICT_size(p);
375 memcpy (info, &subptr, sizeof(subptr));
376 memcpy (info+sizeof(Dict_ptr), &dc, sizeof(Dict_char));
377 info[sizeof(Dict_char)+sizeof(Dict_ptr)] = userlen;
378 memcpy (info+sizeof(Dict_char)+sizeof(Dict_ptr)+1,
380 indxp[-mid] = -DICT_size(p);
381 DICT_size(p) += sizeof(Dict_char)+sizeof(Dict_ptr)
384 dict_bf_touch (dict->dbf, ptr);
394 subptr = new_page (dict, ptr, NULL);
395 memcpy (info, &subptr, sizeof(subptr));
396 dict_bf_touch (dict->dbf, ptr);
398 return dict_ins (dict, str, subptr, userlen, userinfo);
408 if (lo>hi && cmp < 0)
410 slen = (dict_strlen(str)+1)*sizeof(Dict_char);
411 if (DICT_size(p)+slen+userlen >=
412 DICT_pagesize(dict) - (1+DICT_nodir(p))*sizeof(short)) /* overflow? */
414 split_page (dict, ptr, p);
415 return dict_ins (dict, str, ptr, userlen, userinfo);
421 indxp1 = (short*)((char*) p + DICT_pagesize(dict)
422 - DICT_nodir(p)*sizeof(short));
423 for (; indxp1 != indxp; indxp1++)
424 indxp1[0] = indxp1[1];
426 indxp1 = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
427 for (i = DICT_nodir (p); --i >= 0; --indxp1)
431 info = (char*)p - *indxp1;
432 assert (info[sizeof(Dict_ptr)] > ' ');
439 info = (char*)p + DICT_size(p);
440 memcpy (info, str, slen);
443 memcpy (info, userinfo, userlen);
446 *indxp = DICT_size(p);
447 DICT_size(p) = info- (char*) p;
448 dict_bf_touch (dict->dbf, ptr);
454 int dict_insert (Dict dict, const Dict_char *str, int userlen, void *userinfo)
456 assert (dict->head.last > 0);
457 if (dict->head.last == 1)
458 return dict_ins (dict, str, 0, userlen, userinfo);
460 return dict_ins (dict, str, 1, userlen, userinfo);