2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1995-09-06 10:34:44 adam
8 * Memcpy in clean_page edited to satisfy checkergcc.
10 * Revision 1.11 1995/09/04 12:33:31 adam
11 * Various cleanup. YAZ util used instead.
13 * Revision 1.10 1994/10/05 12:16:48 adam
14 * Pagesize is a resource now.
16 * Revision 1.9 1994/09/16 15:39:13 adam
17 * Initial code of lookup - not tested yet.
19 * Revision 1.8 1994/09/16 12:35:01 adam
20 * New version of split_page which use clean_page for splitting.
22 * Revision 1.7 1994/09/12 08:06:42 adam
23 * Futher development of insert.c
25 * Revision 1.6 1994/09/06 13:05:15 adam
26 * Further development of insertion. Some special cases are
27 * not properly handled yet! assert(0) are put here. The
28 * binary search in each page definitely reduce usr CPU.
30 * Revision 1.5 1994/09/01 17:49:39 adam
31 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
33 * Revision 1.4 1994/09/01 17:44:09 adam
34 * depend include change.
36 * Revision 1.3 1994/08/18 12:40:56 adam
37 * Some development of dictionary. Not finished at all!
39 * Revision 1.2 1994/08/17 13:32:19 adam
40 * Use cache in dict - not in bfile.
42 * Revision 1.1 1994/08/16 16:26:48 adam
56 static int dict_ins (Dict dict, const Dict_char *str,
57 Dict_ptr back_ptr, int userlen, void *userinfo);
58 static void clean_page (Dict dict, Dict_ptr ptr, void *p, Dict_char *out,
59 Dict_ptr subptr, char *userinfo);
62 static Dict_ptr new_page (Dict dict, Dict_ptr back_ptr, void **pp)
65 Dict_ptr ptr = dict->head.free_list;
66 if (dict->head.free_list == dict->head.last)
68 dict->head.free_list++;
69 dict->head.last = dict->head.free_list;
70 dict_bf_newp (dict->dbf, ptr, &p);
74 dict_bf_readp (dict->dbf, dict->head.free_list, &p);
75 dict->head.free_list = DICT_nextptr(p);
76 if (dict->head.free_list == 0)
77 dict->head.free_list = dict->head.last;
81 DICT_backptr(p) = back_ptr;
84 DICT_size(p) = DICT_infoffset;
90 static int split_page (Dict dict, Dict_ptr ptr, void *p)
96 short *indxp, *best_indxp = NULL;
97 Dict_char best_char = 0;
98 Dict_char prev_char = 0;
99 int best_no = -1, no_current = 1;
101 /* determine splitting char... */
102 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
103 for (i = DICT_nodir (p); --i >= 0; --indxp)
105 if (*indxp > 0) /* tail string here! */
109 memcpy (&dc, (char*) p + *indxp, sizeof(dc));
111 { /* first entry met */
112 best_char = prev_char = dc;
115 else if (prev_char == dc)
116 { /* same char prefix. update */
117 if (++no_current > best_no)
118 { /* best entry so far */
119 best_no = no_current;
125 { /* new char prefix. restore */
131 if (best_no < 0) /* we didn't find any tail string entry at all! */
134 subptr = new_page (dict, ptr, &subp);
135 /* scan entries to see if there is a string with */
136 /* length 1. info_here indicates if such entry exist */
138 for (indxp=best_indxp, i=0; i<best_no; i++, indxp++)
145 info = (char*) p + *indxp; /* entry start */
146 assert (*info == best_char);
147 slen = dict_strlen(info);
153 info_here = info+(slen+1)*sizeof(Dict_char);
157 info1 = info+(1+slen)*sizeof(Dict_char); /* info start */
158 dict_ins (dict, info+sizeof(Dict_char), subptr, *info1, info1+1);
159 dict_bf_readp (dict->dbf, ptr, &p);
162 /* now clean the page ... */
163 clean_page (dict, ptr, p, &best_char, subptr, info_here);
167 static void clean_page (Dict dict, Dict_ptr ptr, void *p, Dict_char *out,
168 Dict_ptr subptr, char *userinfo)
170 char *np = xmalloc (dict->head.page_size);
172 short *indxp1, *indxp2;
175 indxp1 = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
176 indxp2 = (short*) ((char*) np+DICT_pagesize(dict));
177 info2 = (char*) np + DICT_infoffset;
178 for (i = DICT_nodir (p); --i >= 0; --indxp1)
180 if (*indxp1 > 0) /* tail string here! */
182 /* string (Dict_char *) DICT_EOS terminated */
183 /* unsigned char length of information */
184 /* char * information */
186 info1 = (char*) p + *indxp1;
187 if (out && memcmp (out, info1, sizeof(Dict_char)) == 0)
191 *--indxp2 = -(info2 - np);
192 memcpy (info2, &subptr, sizeof(Dict_ptr));
193 info2 += sizeof(Dict_ptr);
194 memcpy (info2, out, sizeof(Dict_char));
195 info2 += sizeof(Dict_char);
198 memcpy (info2, userinfo, *userinfo+1);
199 info2 += *userinfo + 1;
207 *--indxp2 = info2 - np;
208 slen = (dict_strlen(info1)+1)*sizeof(Dict_char);
209 memcpy (info2, info1, slen);
215 /* Dict_ptr subptr */
216 /* Dict_char sub char */
217 /* unsigned char length of information */
218 /* char * information */
220 assert (*indxp1 < 0);
221 *--indxp2 = -(info2 - np);
222 info1 = (char*) p - *indxp1;
223 memcpy (info2, info1, sizeof(Dict_ptr)+sizeof(Dict_char));
224 info1 += sizeof(Dict_ptr)+sizeof(Dict_char);
225 info2 += sizeof(Dict_ptr)+sizeof(Dict_char);
228 memcpy (info2, info1, slen);
233 memcpy ((char*)p+DICT_infoffset,
234 (char*)np+DICT_infoffset,
235 info2 - ((char*)np+DICT_infoffset));
236 memcpy ((char*)p + ((char*)indxp2 - (char*)np),
238 ((char*) np+DICT_pagesize(dict)) - (char*)indxp2);
240 memcpy ((char*)p+DICT_infoffset, (char*)np+DICT_infoffset,
241 DICT_pagesize(dict)-DICT_infoffset);
243 DICT_size(p) = info2 - np;
247 dict_bf_touch (dict->dbf, ptr);
252 /* return 0 if new */
253 /* return 1 if before but change of info */
254 /* return 2 if same as before */
256 static int dict_ins (Dict dict, const Dict_char *str,
257 Dict_ptr back_ptr, int userlen, void *userinfo)
259 int hi, lo, mid, slen, cmp = 1;
260 Dict_ptr ptr = back_ptr;
266 ptr = new_page (dict, back_ptr, &p);
268 dict_bf_readp (dict->dbf, ptr, &p);
274 hi = DICT_nodir(p)-1;
275 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
281 /* string (Dict_char *) DICT_EOS terminated */
282 /* unsigned char length of information */
283 /* char * information */
284 info = (char*)p + indxp[-mid];
285 cmp = dict_strcmp((Dict_char*) info, str);
288 info += (dict_strlen(info)+1)*sizeof(Dict_char);
289 /* consider change of userinfo length... */
290 if (*info == userlen)
292 if (memcmp (info+1, userinfo, userlen))
294 dict_bf_touch (dict->dbf, ptr);
295 memcpy (info+1, userinfo, userlen);
300 else if (*info > userlen)
304 dict_bf_touch (dict->dbf, ptr);
305 memcpy (info+1, userinfo, userlen);
316 /* Dict_ptr subptr */
317 /* Dict_char sub char */
318 /* unsigned char length of information */
319 /* char * information */
320 info = (char*)p - indxp[-mid];
321 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
325 memcpy (&subptr, info, sizeof(Dict_ptr));
326 if (*++str == DICT_EOS)
330 xlen = info[sizeof(Dict_ptr)+sizeof(Dict_char)];
333 if (memcmp (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
336 dict_bf_touch (dict->dbf, ptr);
337 memcpy (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
343 else if (xlen > userlen)
346 info[sizeof(Dict_ptr)+sizeof(Dict_char)] = userlen;
347 memcpy (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
349 dict_bf_touch (dict->dbf, ptr);
352 if (DICT_size(p)+sizeof(Dict_char)+sizeof(Dict_ptr)+
354 DICT_pagesize(dict) - (1+DICT_nodir(p))*sizeof(short))
356 if (DICT_type(p) == 1)
358 clean_page (dict, ptr, p, NULL, 0, NULL);
359 return dict_ins (dict, str-1, ptr,
362 if (split_page (dict, ptr, p))
364 logf (LOG_FATAL, "Unable to split page %d\n", ptr);
367 return dict_ins (dict, str-1, ptr, userlen, userinfo);
371 info = (char*)p + DICT_size(p);
372 memcpy (info, &subptr, sizeof(subptr));
373 memcpy (info+sizeof(Dict_ptr), &dc, sizeof(Dict_char));
374 info[sizeof(Dict_char)+sizeof(Dict_ptr)] = userlen;
375 memcpy (info+sizeof(Dict_char)+sizeof(Dict_ptr)+1,
377 indxp[-mid] = -DICT_size(p);
378 DICT_size(p) += sizeof(Dict_char)+sizeof(Dict_ptr)
381 dict_bf_touch (dict->dbf, ptr);
391 subptr = new_page (dict, ptr, NULL);
392 memcpy (info, &subptr, sizeof(subptr));
393 dict_bf_touch (dict->dbf, ptr);
395 return dict_ins (dict, str, subptr, userlen, userinfo);
405 if (lo>hi && cmp < 0)
407 slen = (dict_strlen(str)+1)*sizeof(Dict_char);
408 if (DICT_size(p)+slen+userlen >=
409 DICT_pagesize(dict) - (1+DICT_nodir(p))*sizeof(short)) /* overflow? */
411 split_page (dict, ptr, p);
412 return dict_ins (dict, str, ptr, userlen, userinfo);
418 indxp1 = (short*)((char*) p + DICT_pagesize(dict)
419 - DICT_nodir(p)*sizeof(short));
420 for (; indxp1 != indxp; indxp1++)
421 indxp1[0] = indxp1[1];
423 indxp1 = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
424 for (i = DICT_nodir (p); --i >= 0; --indxp1)
428 info = (char*)p - *indxp1;
429 assert (info[sizeof(Dict_ptr)] > ' ');
436 info = (char*)p + DICT_size(p);
437 memcpy (info, str, slen);
440 memcpy (info, userinfo, userlen);
443 *indxp = DICT_size(p);
444 DICT_size(p) = info- (char*) p;
445 dict_bf_touch (dict->dbf, ptr);
451 int dict_insert (Dict dict, const Dict_char *str, int userlen, void *userinfo)
453 assert (dict->head.last > 0);
454 if (dict->head.last == 1)
455 return dict_ins (dict, str, 0, userlen, userinfo);
457 return dict_ins (dict, str, 1, userlen, userinfo);