56 end = (ub4)1<<(t->logsize);
58 printf(
"error: end %lu apos %lu\n", end, t->apos);
63 for (h=t->table[t->apos]; h && h != t->ipos; h = h->next)
66 printf(
"error:ipos not in apos, apos is %lu\n", t->apos);
71 for (counter=0, i=0; i<end; ++i)
72 for (h=t->table[i]; h; h=h->next)
74 if (counter != t->count)
75 printf(
"error: counter %lu t->count %lu\n", counter, t->count);
89 register ub4 newsize = (ub4)1<<(++t->logsize);
90 register ub4 newmask = newsize-1;
92 register hitem **oldtab = t->table;
96 for (i=0; i<newsize; ++i) newtab[i] = (
hitem *)0;
101 for (i=newsize>>1; i--;)
103 register hitem *
this, *that;
104 for (
this = oldtab[i];
this;)
106 register hitem **newplace;
109 newplace = &newtab[(that->hval & newmask)];
110 that->next = *newplace;
119 free((
char *)oldtab);
124htab *hcreate(logsize)
130 len = ((ub4)1<<logsize);
131 t->table = (
hitem **)malloc(
sizeof(
hitem *)*(ub4)len);
132 for (i=0; i<len; ++i) t->table[i] = (
hitem *)0;
133 t->logsize = logsize;
137 t->ipos = (
hitem *)0;
138 t->space = remkroot(
sizeof(
hitem));
148 free((
char *)t->table);
158word hfind( t, key, keyl )
164 ub4 x = lookup(key,keyl,0);
166 for (h = t->table[y=(x&t->mask)]; h; h = h->next)
168 if ((x == h->hval) &&
170 !memcmp(key, h->key, keyl))
184word hadd( t, key, keyl, stuff)
190 register hitem *h,**hp;
191 register ub4 y, x = lookup(key,keyl,0);
194 for (h = t->table[(y=(x&t->mask))]; h; h = h->next)
196 if ((x == h->hval) &&
198 !memcmp(key, h->key, keyl))
207 h = (
hitem *)renew(t->space);
210 if (++t->count > (ub4)1<<(t->logsize))
242 if (!(h = t->ipos))
return FALSE;
245 for (ip = &t->table[t->apos]; *ip != h; ip = &(*ip)->next)
251 if (!(t->ipos = h->next)) hnbucket(t);
269 return (t->ipos != (
hitem *)0);
281 ub4 oldapos = t->apos;
282 ub4 end = (ub4)1<<(t->logsize);
286 for (i=oldapos+1; i<end; ++i)
288 if (t->table[i&t->mask])
291 t->ipos = t->table[i];
297 for (i=0; i<=oldapos; ++i)
302 t->ipos = t->table[i];
320 for (i=0; i<=t->mask; ++i)
322 for (h=t->table[i], j=0; h; ++j, h=h->next)
324 for (walk=stat; walk && (walk->keyl != j); walk=walk->next)
332 walk = (
hitem *)renew(t->space);
335 if (!stat || stat->keyl > j) {walk->next=stat; stat=walk;}
339 walk2->next && (walk2->next->keyl<j);
342 walk->next = walk2->next;
349 for (walk=stat; walk; walk=walk->next)
351 total+=(double)walk->hval*(
double)walk->keyl*(double)walk->keyl;
353 if (t->count) total /= (double)t->count;
354 else total = (double)0;
358 for (walk=stat; walk; walk=walk->next)
360 printf(
"items %ld: %ld buckets\n", walk->keyl, walk->hval);
362 printf(
"\nbuckets: %lu items: %ld existing: %g\n\n",
363 ((ub4)1<<t->logsize), t->count, total);
369 redel(t->space, stat);