1 /* $RCSfile: hash.c,v $$Revision: 4.0.1.1 $$Date: 91/06/07 12:15:55 $
3 * Copyright (c) 1991, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
9 * Revision 4.0.1.1 91/06/07 12:15:55 lwall
10 * patch4: new copyright notice
12 * Revision 4.0 91/03/20 01:57:49 lwall
35 for (s=key, i=0, hash = 0;
37 s++, i++, hash *= 5) {
38 hash += *s * coeff[i];
40 entry = tb->tbl_array[hash & tb->tbl_max];
41 for (; entry; entry = entry->hent_next) {
42 if (entry->hent_hash != hash) /* strings can't be equal */
44 if (strNE(entry->hent_key,key)) /* is this it? */
46 return entry->hent_val;
61 register HENT **oentry;
65 for (s=key, i=0, hash = 0;
67 s++, i++, hash *= 5) {
68 hash += *s * coeff[i];
71 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
74 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
75 if (entry->hent_hash != hash) /* strings can't be equal */
77 if (strNE(entry->hent_key,key)) /* is this it? */
80 safefree((char*)entry->hent_val);
81 entry->hent_val = val;
85 entry = (HENT*) safemalloc(sizeof(HENT));
87 entry->hent_key = savestr(key);
88 entry->hent_val = val;
89 entry->hent_hash = hash;
90 entry->hent_next = *oentry;
93 if (i) { /* initial entry? */
95 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
111 register HENT *entry;
112 register HENT **oentry;
116 for (s=key, i=0, hash = 0;
118 s++, i++, hash *= 5) {
119 hash += *s * coeff[i];
122 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
125 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
126 if (entry->hent_hash != hash) /* strings can't be equal */
128 if (strNE(entry->hent_key,key)) /* is this it? */
130 safefree((char*)entry->hent_val);
131 safefree(entry->hent_key);
132 *oentry = entry->hent_next;
133 safefree((char*)entry);
145 int oldsize = tb->tbl_max + 1;
146 register int newsize = oldsize * 2;
150 register HENT *entry;
151 register HENT **oentry;
153 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
154 bzero((char*)&a[oldsize], oldsize * sizeof(HENT*)); /* zero second half */
155 tb->tbl_max = --newsize;
158 for (i=0; i<oldsize; i++,a++) {
159 if (!*a) /* non-existent */
162 for (oentry = a, entry = *a; entry; entry = *oentry) {
163 if ((entry->hent_hash & newsize) != i) {
164 *oentry = entry->hent_next;
165 entry->hent_next = *b;
172 oentry = &entry->hent_next;
174 if (!*a) /* everything moved */
182 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
184 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
187 hiterinit(tb); /* so each() will start off right */
188 bzero((char*)tb->tbl_array, 8 * sizeof(HENT*));
196 fprintf(stderr,"%5d %4d (%2d%%)\n",
199 tb->tbl_fill * 100 / (tb->tbl_max+1));
207 tb->tbl_eiter = Null(HENT*);
215 register HENT *entry;
217 entry = tb->tbl_eiter;
220 entry = entry->hent_next;
223 if (tb->tbl_riter > tb->tbl_max) {
227 entry = tb->tbl_array[tb->tbl_riter];
231 tb->tbl_eiter = entry;
237 register HENT *entry;
239 return entry->hent_key;
244 register HENT *entry;
246 return entry->hent_val;