54 end = (ub4)1 << (t->logsize);
56 printf(
"error: end %ld apos %ld\n", end, t->apos);
60 for (h = t->table[t->apos]; h && h != t->ipos; h = h->next)
63 printf(
"error:ipos not in apos, apos is %ld\n", t->apos);
68 for (counter = 0, i = 0; i < end; ++i)
69 for (h = t->table[i]; h; h = h->next)
71 if (counter != t->count)
72 printf(
"error: counter %ld t->count %ld\n", counter, t->count);
85 register ub4 newsize = (ub4)1 << (++t->logsize);
86 register ub4 newmask = newsize - 1;
88 register hitem **oldtab = t->table;
89 register hitem **newtab = (hitem **)malloc(newsize *
sizeof(hitem *));
92 for (i = 0; i < newsize; ++i)
93 newtab[i] = (hitem *)0;
98 for (i = newsize >> 1; i--;) {
99 register hitem *
this, *that, **newplace;
100 for (
this = oldtab[i];
this;) {
103 newplace = &newtab[(that->hval & newmask)];
104 that->next = *newplace;
113 free((
char *)oldtab);
128 htab *t = (htab *)malloc(
sizeof(htab));
130 len = ((ub4)1 << logsize);
131 t->table = (hitem **)malloc(
sizeof(hitem *) * (ub4)len);
132 for (i = 0; i < len; ++i)
133 t->table[i] = (hitem *)0;
134 t->logsize = logsize;
138 t->ipos = (hitem *)0;
139 t->space = remkroot(
sizeof(hitem));
159 free((
char *)t->table);
184 ub4 x = lookup(key, keyl, 0);
186 for (h = t->table[y = (x & t->mask)]; h; h = h->next) {
187 if ((x == h->hval) && (keyl == h->keyl) && !memcmp(key, h->key, keyl)) {
213 register hitem *h, **hp;
214 register ub4 y, x = lookup(key, keyl, 0);
216 for (h = t->table[(y = (x & t->mask))]; h; h = h->next) {
217 if ((x == h->hval) && (keyl == h->keyl) && !memcmp(key, h->key, keyl)) {
225 h = (hitem *)renew(t->space);
228 if (++t->count > (ub4)1 << (t->logsize)) {
235 h->key = (ub1 *)malloc(keyl);
236 memcpy(h->key, key, keyl);
265 for (ip = &t->table[t->apos]; *ip != h; ip = &(*ip)->next)
271 if (!(t->ipos = h->next))
297 return (t->ipos != (hitem *)0);
309 ub4 oldapos = t->apos;
310 ub4 end = (ub4)1 << (t->logsize);
314 for (i = oldapos + 1; i < end; ++i) {
315 if (t->table[i & t->mask]) {
317 t->ipos = t->table[i];
323 for (i = 0; i <= oldapos; ++i) {
326 t->ipos = t->table[i];
340 hitem *walk, *walk2, *stat = (hitem *)0;
343 for (i = 0; i <= t->mask; ++i) {
344 for (h = t->table[i], j = 0; h; ++j, h = h->next)
346 for (walk = stat; walk && (walk->keyl != j); walk = walk->next)
351 walk = (hitem *)renew(t->space);
354 if (!stat || stat->keyl > j) {
358 for (walk2 = stat; walk2->next && (walk2->next->keyl < j); walk2 = walk2->next)
360 walk->next = walk2->next;
367 for (walk = stat; walk; walk = walk->next) {
368 total += (double)walk->hval * (
double)walk->keyl * (double)walk->keyl;
371 total /= (double)t->count;
377 for (walk = stat; walk; walk = walk->next) {
378 printf(
"items %ld: %ld buckets\n", walk->keyl, walk->hval);
380 printf(
"\nbuckets: %ld items: %ld existing: %g\n\n", ((ub4)1 << t->logsize), t->count, total);
385 redel(t->space, stat);
word hfirst(htab *t)
Position the hash table on the first element.
htab * hcreate(word logsize)
Create a hash table.
word hadd(htab *t, ub1 *key, ub4 keyl, void *stuff)
Add an item to the hash table.
void hdestroy(htab *t)
Destroy the hash table and free all its memory.
word hfind(htab *t, ub1 *key, ub4 keyl)
Find an item with a given key in the hash table.