1 /* $Header: hash.c,v 1.0 87/12/18 13:07:18 root Exp $
4 * Revision 1.0 87/12/18 13:07:18 root
27 for (s=key, i=0, hash = 0;
29 s++, i++, hash *= 5) {
30 hash += *s * coeff[i];
32 entry = tb->tbl_array[hash & tb->tbl_max];
33 for (; entry; entry = entry->hent_next) {
34 if (entry->hent_hash != hash) /* strings can't be equal */
36 if (strNE(entry->hent_key,key)) /* is this it? */
38 return entry->hent_val;
53 register HENT **oentry;
57 for (s=key, i=0, hash = 0;
59 s++, i++, hash *= 5) {
60 hash += *s * coeff[i];
63 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
66 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
67 if (entry->hent_hash != hash) /* strings can't be equal */
69 if (strNE(entry->hent_key,key)) /* is this it? */
71 safefree((char*)entry->hent_val);
72 entry->hent_val = val;
75 entry = (HENT*) safemalloc(sizeof(HENT));
77 entry->hent_key = savestr(key);
78 entry->hent_val = val;
79 entry->hent_hash = hash;
80 entry->hent_next = *oentry;
83 if (i) { /* initial entry? */
85 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
101 register HENT *entry;
102 register HENT **oentry;
106 for (s=key, i=0, hash = 0;
108 s++, i++, hash *= 5) {
109 hash += *s * coeff[i];
112 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
115 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
116 if (entry->hent_hash != hash) /* strings can't be equal */
118 if (strNE(entry->hent_key,key)) /* is this it? */
120 safefree((char*)entry->hent_val);
121 safefree(entry->hent_key);
122 *oentry = entry->hent_next;
123 safefree((char*)entry);
135 int oldsize = tb->tbl_max + 1;
136 register int newsize = oldsize * 2;
140 register HENT *entry;
141 register HENT **oentry;
143 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
144 bzero((char*)&a[oldsize], oldsize * sizeof(HENT*)); /* zero second half */
145 tb->tbl_max = --newsize;
148 for (i=0; i<oldsize; i++,a++) {
149 if (!*a) /* non-existent */
152 for (oentry = a, entry = *a; entry; entry = *oentry) {
153 if ((entry->hent_hash & newsize) != i) {
154 *oentry = entry->hent_next;
155 entry->hent_next = *b;
162 oentry = &entry->hent_next;
164 if (!*a) /* everything moved */
172 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
174 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
177 hiterinit(tb); /* so each() will start off right */
178 bzero((char*)tb->tbl_array, 8 * sizeof(HENT*));
186 fprintf(stderr,"%5d %4d (%2d%%)\n",
189 tb->tbl_fill * 100 / (tb->tbl_max+1));
197 tb->tbl_eiter = Null(HENT*);
205 register HENT *entry;
207 entry = tb->tbl_eiter;
210 entry = entry->hent_next;
213 if (tb->tbl_riter > tb->tbl_max) {
217 entry = tb->tbl_array[tb->tbl_riter];
221 tb->tbl_eiter = entry;
227 register HENT *entry;
229 return entry->hent_key;
234 register HENT *entry;
236 return entry->hent_val;