3 * $Id: sym.c,v 1.1 1998/06/17 23:44:42 mdw Exp $
5 * Symbol table management
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with mLib; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1998/06/17 23:44:42 mdw
37 /*----- Header files ------------------------------------------------------*/
39 /* --- ANSI headers --- */
45 /* --- Local headers --- */
54 /*----- Tuning parameters -------------------------------------------------*/
56 /* --- Initial hash table size --- *
58 * This is the initial @mask@ value. It must be of the form %$2^n - 1$%,
59 * so that it can be used to mask of the bottom bits of a hash value.
62 #define SYM_INITSZ 255 /* Size of a new hash table */
64 /* --- Maximum load factor --- *
66 * This parameter controls how much the table has to be loaded before the
67 * table is extended. The number of elements %$n$%, the number of bins %$b$%
68 * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is
69 * added to the table and this relation is found to be false, the table is
72 * The current function gives %$l = {3n \over 4}$%, which appears to be
73 * reasonable on the face of things.
76 #define SYM_LIMIT(n) (((n) * 3) >> 2) /* Load factor for growing table */
78 /*----- Useful macros -----------------------------------------------------*/
80 #define SYM_NAME(sy) \
81 ((sy)->len > SYM_BUFSZ ? (sy)->name.p : (sy)->name.b)
83 /*----- Main code ---------------------------------------------------------*/
85 /* --- @sym_createTable@ --- *
87 * Arguments: @sym_table *t@ = symbol table to initialise
91 * Use: Initialises the given symbol table. Raises @EXC_NOMEM@ if
92 * there isn't enough memory.
95 void sym_createTable(sym_table *t)
99 TRACK_CTX("symbol table creation");
102 t->mask = SYM_INITSZ;
103 t->c = SYM_LIMIT(SYM_INITSZ);
104 t->a = xmalloc((t->mask + 1) * sizeof(sym_base *));
106 for (i = 0; i < SYM_INITSZ + 1; i++)
112 /* --- @sym_destroyTable@ --- *
114 * Arguments: @sym_table *t@ = pointer to symbol table in question
118 * Use: Destroys a symbol table, freeing all the memory it used to
122 void sym_destroyTable(sym_table *t)
127 TRACK_CTX("symbol table deletion");
130 for (i = 0; i <= t->mask; i++) {
134 if (p->len > SYM_BUFSZ)
135 sub_free(p->name.p, p->len);
145 /* --- @sym_find@ --- *
147 * Arguments: @sym_table *t@ = pointer to symbol table in question
148 * @const char *n@ = pointer to symbol table to look up
149 * @long l@ = length of the name string or negative to measure
150 * @size_t sz@ = size of desired symbol object, or zero
151 * @unsigned *f@ = pointer to a flag, or null.
153 * Returns: The address of a @sym_base@ structure, or null if not found
156 * Use: Looks up a symbol in a given symbol table. The name is
157 * passed by the address of its first character. The length
158 * may be given, in which case the name may contain arbitrary
159 * binary data, or it may be given as a negative number, in
160 * which case the length of the name is calculated as
163 * The return value is the address of a pointer to a @sym_base@
164 * block (which may have other things on the end, as above). If
165 * the symbol could be found, the return value points to the
166 * symbol block. If the symbol wasn't there, then if @sz@ is
167 * nonzero, a new symbol is created and its address is returned;
168 * otherwise a null pointer is returned. The exception
169 * @EXC_NOMEM@ is raised if the block can't be allocated.
171 * The value of @*f@ indicates whether a new symbol entry was
172 * created: a nonzero value indicates that an old value was
176 void *sym_find(sym_table *t, const char *n, long l, size_t sz, unsigned *f)
178 unsigned long hash; /* Hash value for user's name */
179 size_t len = l < 0 ? strlen(n) + 1 : l; /* Find length of user's name */
180 sym_base *bin; /* Bin containing our item */
181 sym_base *p, *q; /* Pointer wandering through list */
183 /* --- Find the correct bin --- */
185 CRC32(hash, 0, n, len); /* Find hash value for this name */
186 bin = p = (sym_base *)(t->a + (hash & t->mask));
188 /* --- Search the bin list --- */
191 if (hash == p->next->hash && /* Check full hash values first */
192 len == p->next->len && /* Then compare string lengths */
193 !memcmp(n, SYM_NAME(p->next), len)) /* And finally compare names */
195 /* --- Found a match --- *
197 * As a minor, and probably pointless, tweak, move the item to the
198 * front of its bin list.
201 q = p->next; /* Find the actual symbol block */
202 p->next = q->next; /* Extract block from bin list */
203 q->next = bin->next; /* Set up symbol's next pointer */
204 bin->next = q; /* And reinsert the block */
206 /* --- Return the block --- */
208 if (f) *f = 1; /* Didn't fail to find the item */
209 return (q); /* And return the block */
212 p = p->next; /* Move onto the next item */
215 /* --- Couldn't find the item there --- */
217 if (f) *f = 0; /* Failed to find the block */
218 if (!sz) return (0); /* Return zero if not creating */
220 /* --- Create a new symbol block and initialise it --- */
223 TRACK_CTX("new symbol creation");
226 p = xmalloc(sz); /* Create a new symbol block */
227 p->next = bin->next; /* Set up the next pointer */
228 p->hash = hash; /* Set up the hash value too */
229 p->len = len; /* And set up the string length */
230 if (len <= SYM_BUFSZ)
231 memcpy(p->name.b, n, len); /* And copy the string over */
234 p->name.p = sub_alloc(len); /* Allocate a block for the name */
235 memcpy(p->name.p, n, len); /* And copy the string over */
246 bin->next = p; /* Put the pointer into the bin */
248 /* --- Consider growing the array --- */
251 unsigned long m = t->mask + 1; /* Next set bit in has word */
252 sym_base *p, *q, *r; /* More useful pointers */
253 size_t i, lim; /* Loop counter and limit */
255 TRACK_CTX("symbol table extension");
258 /* --- Update values in the anchor block --- */
261 t->a = xrealloc(t->a, (t->mask + 1) * 2 * sizeof(sym_base *));
262 } CATCH switch (exc_type) {
271 t->c = SYM_LIMIT(t->mask + 1); /* Set load value */
272 t->mask = (t->mask + 1) * 2 - 1; /* Set the new mask value */
274 /* --- Now wander through the table rehashing things --- *
276 * This loop is very careful to avoid problems with aliasing. The items
277 * are dealt with from the end backwards to avoid overwriting bins before
278 * they've been processed.
281 lim = (t->mask + 1) >> 1;
282 for (i = 0; i < lim; i++) {
284 /* --- Some initialisation --- */
286 r = t->a[i]; /* Find the list we're dissecting */
287 p = (sym_base *)(t->a + i); /* Find bit-clear list */
288 q = (sym_base *)(t->a + i + lim); /* And the bit-set lsit */
290 /* --- Now go through the @r@ list --- */
293 if (r->hash & m) /* Is the next bit set? */
294 q = q->next = r; /* Yes, so fit up previous link */
296 p = p->next = r; /* No, so fit up previous link */
297 r = r->next; /* Move onto the next item */
299 p->next = q->next = 0; /* Null terminate the new lists */
305 /* --- Finished that, so return the new symbol block --- */
310 /* --- @sym_remove@ --- *
312 * Arguments: @sym_table *i@ = pointer to a symbol table object
313 * @void *b@ = pointer to symbol table entry
317 * Use: Removes the object from the symbol table. The space occupied
318 * by the object and its name is freed; anything else attached
319 * to the entry should already be gone by this point.
322 void sym_remove(sym_table *t, void *b)
324 /* --- A quick comment --- *
326 * Since the @sym_base@ block contains the hash, finding the element in the
327 * bin list is really quick -- it's not worth bothering with things like
328 * doubly linked lists.
332 sym_base *bin = (sym_base *)(t->a + (p->hash & t->mask));
334 /* --- Find the item in the bin list --- */
344 /* --- Now just remove the item from the list and free it --- *
346 * Oh, and bump the load counter.
350 if (p->len > SYM_BUFSZ)
351 sub_free(p->name.p, p->len);
356 /* --- @sym_createIter@ --- *
358 * Arguments: @sym_iter *i@ = pointer to an iterator object
359 * @sym_table *t@ = pointer to a symbol table object
363 * Use: Creates a new symbol table iterator which may be used to
364 * iterate through a symbol table.
367 void sym_createIter(sym_iter *i, sym_table *t)
374 /* --- @sym_next@ --- *
376 * Arguments: @sym_iter *i@ = pointer to iterator object
378 * Returns: Pointer to the next symbol found, or null when finished.
380 * Use: Returns the next symbol from the table. Symbols are not
381 * returned in any particular order.
384 void *sym_next(sym_iter *i)
388 /* --- Find the next item --- */
391 if (i->i > i->t->mask)
393 i->n = i->t->a[i->i++];
396 /* --- Update the iterator block --- */
406 /*----- Symbol table test code --------------------------------------------*/
413 typedef struct sym_word {
419 /* --- What it does --- *
421 * Reads the file /usr/dict/words (change to some other file full of
422 * interesting and appropriate bits of text to taste) into a big buffer and
423 * picks apart into lines. Then picks lines at random and enters them into
429 char *buff, *p, *lim;
438 /* --- Initialise for reading the file --- */
441 buff = xmalloc(sz + 1);
445 if ((fp = fopen("/usr/dict/words", "r")) == 0)
446 fprintf(stderr, "buggered ;-( (%s)\n", strerror(errno));
448 /* --- Read buffers of text --- *
450 * Read a buffer. If more to come, double the buffer size and try again.
451 * This is the method I recommended to comp.lang.c, so I may as well try
456 i = fread(buff + done, 1, sz - done, fp);
461 buff = xrealloc(buff, sz + 1);
464 /* --- Count the lines --- */
469 for (p = buff; p < lim; p++)
470 if (*p == '\n') sz++;
472 /* --- Build a table of line starts --- */
474 line = xmalloc(sz * sizeof(char *));
477 for (p = buff; p < lim; p++)
478 if (*p == '\n') *p = 0, line[i++] = p + 1;
481 /* --- Build a table of lines --- *
483 * This reverses the mapping which the symbol table performs, so that its
484 * accuracy can be tested.
487 flag = xmalloc(sz * sizeof(sym_word *));
488 for (i = 0; i < sz; i++)
492 sym_createTable(&tbl);
495 i = (unsigned)rand() % sz;
502 printf("find `%s'\n", line[i]);
503 if ((rand() & 1023) == 0) {
504 putchar('.'); fflush(stdout);
507 w = sym_find(&tbl, line[i], -1, 0, 0);
509 printf("*** error: find `%s' gave %p not %p\n",
510 line[i], (void *)w, (void *)flag[i]);
511 else if (w && w->i != i)
512 printf("*** error: find sym for `%s' gives index %i not %i\n",
520 printf("create `%s'\n", line[i]);
521 if ((rand() & 1023) == 0) {
522 putchar('+'); fflush(stdout);
525 w = sym_find(&tbl, line[i], -1, sizeof(sym_word), &f);
529 printf("*** error: create `%s' gave %p not %p\n",
530 line[i], (void *)w, (void *)flag[i]);
531 else if (w && w->i != i)
532 printf("*** error: create sym for `%s' gives index %i not %i\n",
538 printf("*** error: create `%s' gave new block, should be %p\n",
539 line[i], (void *)flag[i]);
555 v = (rand() % entries) == 0;
558 printf("\niterated %i entries\n", entries);
563 ntbl = xmalloc(sz * sizeof(sym_word *));
564 memcpy(ntbl, flag, sz * sizeof(sym_word *));
565 sym_createIter(&it, &tbl);
567 while ((w = sym_next(&it)) != 0) {
569 printf("*** error: iterate returned duff item %i\n", w->i);
574 for (i = 0; i < sz; i++)
575 if (ntbl[i]) printf("*** error: iterate didn't return item %i\n",
582 int v = rand() & 255 ? 0 : 1;
587 for (i = 0; i <= tbl.mask; i++) {
588 if (!tbl.a[i]) continue;
589 if (v) printf(" %i: ", i);
592 if ((b->hash & tbl.mask) != i)
593 printf("*** error: bad hash value found");
594 if (v) printf("`%s'(%08lx:%lu) ",
595 line[((sym_word *)b)->i],
600 if (v) putchar('\n');
606 printf("remove `%s'\n", SYM_NAME(&flag[i]->base));
607 if ((rand() & 1023) == 0) {
608 putchar('-'); fflush(stdout);
610 sym_remove(&tbl, flag[i]);
624 /*----- That's all, folks -------------------------------------------------*/