1 /* $Header: hash.c,v 2.0 88/06/05 00:09:06 root Exp $
4 * Revision 2.0 88/06/05 00:09:06 root
5 * Baseline version 2.0.
24 for (s=key, i=0, hash = 0;
25 /* while */ *s && i < COEFFSIZE;
26 s++, i++, hash *= 5) {
27 hash += *s * coeff[i];
29 entry = tb->tbl_array[hash & tb->tbl_max];
30 for (; entry; entry = entry->hent_next) {
31 if (entry->hent_hash != hash) /* strings can't be equal */
33 if (strNE(entry->hent_key,key)) /* is this it? */
35 return entry->hent_val;
50 register HENT **oentry;
54 for (s=key, i=0, hash = 0;
55 /* while */ *s && i < COEFFSIZE;
56 s++, i++, hash *= 5) {
57 hash += *s * coeff[i];
60 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
63 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
64 if (entry->hent_hash != hash) /* strings can't be equal */
66 if (strNE(entry->hent_key,key)) /* is this it? */
68 safefree((char*)entry->hent_val);
69 entry->hent_val = val;
72 entry = (HENT*) safemalloc(sizeof(HENT));
74 entry->hent_key = savestr(key);
75 entry->hent_val = val;
76 entry->hent_hash = hash;
77 entry->hent_next = *oentry;
80 if (i) { /* initial entry? */
82 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
98 register HENT **oentry;
103 for (s=key, i=0, hash = 0;
104 /* while */ *s && i < COEFFSIZE;
105 s++, i++, hash *= 5) {
106 hash += *s * coeff[i];
109 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
112 for (; entry; i=0, oentry = &entry->hent_next, entry = *oentry) {
113 if (entry->hent_hash != hash) /* strings can't be equal */
115 if (strNE(entry->hent_key,key)) /* is this it? */
117 *oentry = entry->hent_next;
118 str = str_static(entry->hent_val);
130 int oldsize = tb->tbl_max + 1;
131 register int newsize = oldsize * 2;
135 register HENT *entry;
136 register HENT **oentry;
138 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
139 bzero((char*)&a[oldsize], oldsize * sizeof(HENT*)); /* zero second half */
140 tb->tbl_max = --newsize;
143 for (i=0; i<oldsize; i++,a++) {
144 if (!*a) /* non-existent */
147 for (oentry = a, entry = *a; entry; entry = *oentry) {
148 if ((entry->hent_hash & newsize) != i) {
149 *oentry = entry->hent_next;
150 entry->hent_next = *b;
157 oentry = &entry->hent_next;
159 if (!*a) /* everything moved */
167 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
169 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
172 hiterinit(tb); /* so each() will start off right */
173 bzero((char*)tb->tbl_array, 8 * sizeof(HENT*));
183 str_free(hent->hent_val);
184 safefree(hent->hent_key);
185 safefree((char*)hent);
193 register HENT *ohent = Null(HENT*);
198 while (hent = hiternext(tb)) { /* concise but not very efficient */
204 bzero((char*)tb->tbl_array, (tb->tbl_max + 1) * sizeof(HENT*));
215 while (hent = hiternext(tb)) {
220 safefree((char*)tb->tbl_array);
229 fprintf(stderr,"%5d %4d (%2d%%)\n",
232 tb->tbl_fill * 100 / (tb->tbl_max+1));
240 tb->tbl_eiter = Null(HENT*);
248 register HENT *entry;
250 entry = tb->tbl_eiter;
253 entry = entry->hent_next;
256 if (tb->tbl_riter > tb->tbl_max) {
260 entry = tb->tbl_array[tb->tbl_riter];
264 tb->tbl_eiter = entry;
270 register HENT *entry;
272 return entry->hent_key;
277 register HENT *entry;
279 return entry->hent_val;