2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1999-02-02 14:50:22 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.18 1998/03/05 08:17:24 adam
11 * Added a few comments - no code changed.
13 * Revision 1.17 1996/05/14 15:49:09 adam
14 * Bug fix: In function split_page. In rare cases variable best_indxp was
17 * Revision 1.16 1996/02/02 13:43:50 adam
18 * The public functions simply use char instead of Dict_char to represent
19 * search strings. Dict_char is used internally only.
21 * Revision 1.15 1996/02/01 20:39:59 adam
22 * Bug fix: insert didn't work on 8-bit characters due to unsigned char
23 * compares in dict_strcmp (strcmp) and signed Dict_char. Dict_char is
26 * Revision 1.14 1995/12/07 11:48:56 adam
27 * Insert operation obeys DICT_type = 1 (slack in page).
28 * Function dict_open exists if page size or magic aren't right.
30 * Revision 1.13 1995/11/28 09:06:37 adam
31 * Fixed potential dangling pointer.
33 * Revision 1.12 1995/09/06 10:34:44 adam
34 * Memcpy in clean_page edited to satisfy checkergcc.
36 * Revision 1.11 1995/09/04 12:33:31 adam
37 * Various cleanup. YAZ util used instead.
39 * Revision 1.10 1994/10/05 12:16:48 adam
40 * Pagesize is a resource now.
42 * Revision 1.9 1994/09/16 15:39:13 adam
43 * Initial code of lookup - not tested yet.
45 * Revision 1.8 1994/09/16 12:35:01 adam
46 * New version of split_page which use clean_page for splitting.
48 * Revision 1.7 1994/09/12 08:06:42 adam
49 * Futher development of insert.c
51 * Revision 1.6 1994/09/06 13:05:15 adam
52 * Further development of insertion. Some special cases are
53 * not properly handled yet! assert(0) are put here. The
54 * binary search in each page definitely reduce usr CPU.
56 * Revision 1.5 1994/09/01 17:49:39 adam
57 * Removed stupid line. Work on insertion in dictionary. Not finished yet.
59 * Revision 1.4 1994/09/01 17:44:09 adam
60 * depend include change.
62 * Revision 1.3 1994/08/18 12:40:56 adam
63 * Some development of dictionary. Not finished at all!
65 * Revision 1.2 1994/08/17 13:32:19 adam
66 * Use cache in dict - not in bfile.
68 * Revision 1.1 1994/08/16 16:26:48 adam
82 static int dict_ins (Dict dict, const Dict_char *str,
83 Dict_ptr back_ptr, int userlen, void *userinfo);
84 static void clean_page (Dict dict, Dict_ptr ptr, void *p, Dict_char *out,
85 Dict_ptr subptr, char *userinfo);
88 static Dict_ptr new_page (Dict dict, Dict_ptr back_ptr, void **pp)
91 Dict_ptr ptr = dict->head.free_list;
92 if (dict->head.free_list == dict->head.last)
94 dict->head.free_list++;
95 dict->head.last = dict->head.free_list;
96 dict_bf_newp (dict->dbf, ptr, &p);
100 dict_bf_readp (dict->dbf, dict->head.free_list, &p);
101 dict->head.free_list = DICT_nextptr(p);
102 if (dict->head.free_list == 0)
103 dict->head.free_list = dict->head.last;
107 DICT_backptr(p) = back_ptr;
110 DICT_size(p) = DICT_infoffset;
116 static int split_page (Dict dict, Dict_ptr ptr, void *p)
122 short *indxp, *best_indxp = NULL;
123 Dict_char best_char = 0;
124 Dict_char prev_char = 0;
125 int best_no = -1, no_current = 1;
127 /* determine splitting char... */
128 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
129 for (i = DICT_nodir (p); --i >= 0; --indxp)
131 if (*indxp > 0) /* tail string here! */
135 memcpy (&dc, (char*) p + *indxp, sizeof(dc));
137 { /* first entry met */
138 best_char = prev_char = dc;
142 else if (prev_char == dc)
143 { /* same char prefix. update */
144 if (++no_current > best_no)
145 { /* best entry so far */
146 best_no = no_current;
152 { /* new char prefix. restore */
158 if (best_no < 0) /* we didn't find any tail string entry at all! */
161 j = best_indxp - (short*) p;
162 subptr = new_page (dict, ptr, &subp);
163 /* scan entries to see if there is a string with */
164 /* length 1. info_here indicates if such entry exist */
166 for (i=0; i<best_no; i++, j++)
172 info = (char*) p + ((short*) p)[j];
174 memcpy (&dc, info, sizeof(dc));
175 assert (dc == best_char);
176 slen = 1+dict_strlen((Dict_char*) info);
182 info_here = info+slen*sizeof(Dict_char);
186 info1 = info+slen*sizeof(Dict_char); /* info start */
187 dict_ins (dict, (Dict_char*) (info+sizeof(Dict_char)),
188 subptr, *info1, info1+1);
189 dict_bf_readp (dict->dbf, ptr, &p);
192 /* now clean the page ... */
193 clean_page (dict, ptr, p, &best_char, subptr, info_here);
197 static void clean_page (Dict dict, Dict_ptr ptr, void *p, Dict_char *out,
198 Dict_ptr subptr, char *userinfo)
200 char *np = xmalloc (dict->head.page_size);
202 short *indxp1, *indxp2;
205 indxp1 = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
206 indxp2 = (short*) ((char*) np+DICT_pagesize(dict));
207 info2 = (char*) np + DICT_infoffset;
208 for (i = DICT_nodir (p); --i >= 0; --indxp1)
210 if (*indxp1 > 0) /* tail string here! */
212 /* string (Dict_char *) DICT_EOS terminated */
213 /* unsigned char length of information */
214 /* char * information */
216 info1 = (char*) p + *indxp1;
217 if (out && memcmp (out, info1, sizeof(Dict_char)) == 0)
221 *--indxp2 = -(info2 - np);
222 memcpy (info2, &subptr, sizeof(Dict_ptr));
223 info2 += sizeof(Dict_ptr);
224 memcpy (info2, out, sizeof(Dict_char));
225 info2 += sizeof(Dict_char);
228 memcpy (info2, userinfo, *userinfo+1);
229 info2 += *userinfo + 1;
237 *--indxp2 = info2 - np;
238 slen = (dict_strlen((Dict_char*) info1)+1)*sizeof(Dict_char);
239 memcpy (info2, info1, slen);
245 /* Dict_ptr subptr */
246 /* Dict_char sub char */
247 /* unsigned char length of information */
248 /* char * information */
250 assert (*indxp1 < 0);
251 *--indxp2 = -(info2 - np);
252 info1 = (char*) p - *indxp1;
253 memcpy (info2, info1, sizeof(Dict_ptr)+sizeof(Dict_char));
254 info1 += sizeof(Dict_ptr)+sizeof(Dict_char);
255 info2 += sizeof(Dict_ptr)+sizeof(Dict_char);
258 memcpy (info2, info1, slen);
263 memcpy ((char*)p+DICT_infoffset,
264 (char*)np+DICT_infoffset,
265 info2 - ((char*)np+DICT_infoffset));
266 memcpy ((char*)p + ((char*)indxp2 - (char*)np),
268 ((char*) np+DICT_pagesize(dict)) - (char*)indxp2);
270 memcpy ((char*)p+DICT_infoffset, (char*)np+DICT_infoffset,
271 DICT_pagesize(dict)-DICT_infoffset);
273 DICT_size(p) = info2 - np;
277 dict_bf_touch (dict->dbf, ptr);
282 /* return 0 if new */
283 /* return 1 if before but change of info */
284 /* return 2 if same as before */
286 static int dict_ins (Dict dict, const Dict_char *str,
287 Dict_ptr back_ptr, int userlen, void *userinfo)
289 int hi, lo, mid, slen, cmp = 1;
290 Dict_ptr ptr = back_ptr;
296 ptr = new_page (dict, back_ptr, &p);
298 dict_bf_readp (dict->dbf, ptr, &p);
304 hi = DICT_nodir(p)-1;
305 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
311 /* string (Dict_char *) DICT_EOS terminated */
312 /* unsigned char length of information */
313 /* char * information */
314 info = (char*)p + indxp[-mid];
315 cmp = dict_strcmp((Dict_char*) info, str);
318 info += (dict_strlen((Dict_char*) info)+1)*sizeof(Dict_char);
319 /* consider change of userinfo length... */
320 if (*info == userlen)
322 /* change of userinfo ? */
323 if (memcmp (info+1, userinfo, userlen))
325 dict_bf_touch (dict->dbf, ptr);
326 memcpy (info+1, userinfo, userlen);
332 else if (*info > userlen)
334 /* room for new userinfo */
337 dict_bf_touch (dict->dbf, ptr);
338 memcpy (info+1, userinfo, userlen);
349 /* Dict_ptr subptr */
350 /* Dict_char sub char */
351 /* unsigned char length of information */
352 /* char * information */
353 info = (char*)p - indxp[-mid];
354 memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
358 memcpy (&subptr, info, sizeof(Dict_ptr));
359 if (*++str == DICT_EOS)
361 /* finish of string. Store userinfo here... */
363 int xlen = info[sizeof(Dict_ptr)+sizeof(Dict_char)];
366 if (memcmp (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
369 dict_bf_touch (dict->dbf, ptr);
370 memcpy (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
376 else if (xlen > userlen)
379 info[sizeof(Dict_ptr)+sizeof(Dict_char)] = userlen;
380 memcpy (info+sizeof(Dict_ptr)+sizeof(Dict_char)+1,
382 dict_bf_touch (dict->dbf, ptr);
385 /* xlen < userlen, expanding needed ... */
386 if (DICT_size(p)+sizeof(Dict_char)+sizeof(Dict_ptr)+
388 DICT_pagesize(dict) - (1+DICT_nodir(p))*sizeof(short))
390 /* not enough room - split needed ... */
391 if (DICT_type(p) == 1)
393 clean_page (dict, ptr, p, NULL, 0, NULL);
394 return dict_ins (dict, str-1, ptr,
397 if (split_page (dict, ptr, p))
399 logf (LOG_FATAL, "Unable to split page %d\n", ptr);
402 return dict_ins (dict, str-1, ptr, userlen, userinfo);
405 { /* enough room - no split needed ... */
406 info = (char*)p + DICT_size(p);
407 memcpy (info, &subptr, sizeof(subptr));
408 memcpy (info+sizeof(Dict_ptr), &dc, sizeof(Dict_char));
409 info[sizeof(Dict_char)+sizeof(Dict_ptr)] = userlen;
410 memcpy (info+sizeof(Dict_char)+sizeof(Dict_ptr)+1,
412 indxp[-mid] = -DICT_size(p);
413 DICT_size(p) += sizeof(Dict_char)+sizeof(Dict_ptr)
416 dict_bf_touch (dict->dbf, ptr);
426 subptr = new_page (dict, ptr, NULL);
427 memcpy (info, &subptr, sizeof(subptr));
428 dict_bf_touch (dict->dbf, ptr);
430 return dict_ins (dict, str, subptr, userlen, userinfo);
440 if (lo>hi && cmp < 0)
442 slen = (dict_strlen(str)+1)*sizeof(Dict_char);
443 if (DICT_size(p)+slen+userlen >=
444 DICT_pagesize(dict) - (1+DICT_nodir(p))*sizeof(short)) /* overflow? */
448 clean_page (dict, ptr, p, NULL, 0, NULL);
449 return dict_ins (dict, str, ptr, userlen, userinfo);
451 split_page (dict, ptr, p);
452 return dict_ins (dict, str, ptr, userlen, userinfo);
458 indxp1 = (short*)((char*) p + DICT_pagesize(dict)
459 - DICT_nodir(p)*sizeof(short));
460 for (; indxp1 != indxp; indxp1++)
461 indxp1[0] = indxp1[1];
463 indxp1 = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
464 for (i = DICT_nodir (p); --i >= 0; --indxp1)
468 info = (char*)p - *indxp1;
469 assert (info[sizeof(Dict_ptr)] > ' ');
476 info = (char*)p + DICT_size(p);
477 memcpy (info, str, slen);
480 memcpy (info, userinfo, userlen);
483 *indxp = DICT_size(p);
484 DICT_size(p) = info- (char*) p;
485 dict_bf_touch (dict->dbf, ptr);
491 int dict_insert (Dict dict, const char *str, int userlen, void *userinfo)
493 assert (dict->head.last > 0);
494 if (dict->head.last == 1)
495 return dict_ins (dict, (const Dict_char *) str, 0, userlen, userinfo);
497 return dict_ins (dict, (const Dict_char *) str, 1, userlen, userinfo);