1 /* $RCSfile: hash.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:20 $
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.1 92/08/07 18:29:20 lwall
11 * Revision 4.0.1.1 91/06/07 12:15:55 lwall
12 * patch4: new copyright notice
14 * Revision 4.0 91/03/20 01:57:49 lwall
37 for (s=key, i=0, hash = 0;
39 s++, i++, hash *= 5) {
40 hash += *s * coeff[i];
42 entry = tb->tbl_array[hash & tb->tbl_max];
43 for (; entry; entry = entry->hent_next) {
44 if (entry->hent_hash != hash) /* strings can't be equal */
46 if (strNE(entry->hent_key,key)) /* is this it? */
48 return entry->hent_val;
63 register HENT **oentry;
67 for (s=key, i=0, hash = 0;
69 s++, i++, hash *= 5) {
70 hash += *s * coeff[i];
73 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
76 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
77 if (entry->hent_hash != hash) /* strings can't be equal */
79 if (strNE(entry->hent_key,key)) /* is this it? */
82 safefree((char*)entry->hent_val);
83 entry->hent_val = val;
87 entry = (HENT*) safemalloc(sizeof(HENT));
89 entry->hent_key = savestr(key);
90 entry->hent_val = val;
91 entry->hent_hash = hash;
92 entry->hent_next = *oentry;
95 if (i) { /* initial entry? */
97 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
113 register HENT *entry;
114 register HENT **oentry;
118 for (s=key, i=0, hash = 0;
120 s++, i++, hash *= 5) {
121 hash += *s * coeff[i];
124 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
127 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
128 if (entry->hent_hash != hash) /* strings can't be equal */
130 if (strNE(entry->hent_key,key)) /* is this it? */
132 safefree((char*)entry->hent_val);
133 safefree(entry->hent_key);
134 *oentry = entry->hent_next;
135 safefree((char*)entry);
147 int oldsize = tb->tbl_max + 1;
148 register int newsize = oldsize * 2;
152 register HENT *entry;
153 register HENT **oentry;
155 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
156 bzero((char*)&a[oldsize], oldsize * sizeof(HENT*)); /* zero second half */
157 tb->tbl_max = --newsize;
160 for (i=0; i<oldsize; i++,a++) {
161 if (!*a) /* non-existent */
164 for (oentry = a, entry = *a; entry; entry = *oentry) {
165 if ((entry->hent_hash & newsize) != i) {
166 *oentry = entry->hent_next;
167 entry->hent_next = *b;
174 oentry = &entry->hent_next;
176 if (!*a) /* everything moved */
184 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
186 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
189 hiterinit(tb); /* so each() will start off right */
190 bzero((char*)tb->tbl_array, 8 * sizeof(HENT*));
198 fprintf(stderr,"%5d %4d (%2d%%)\n",
201 tb->tbl_fill * 100 / (tb->tbl_max+1));
209 tb->tbl_eiter = Null(HENT*);
217 register HENT *entry;
219 entry = tb->tbl_eiter;
222 entry = entry->hent_next;
225 if (tb->tbl_riter > tb->tbl_max) {
229 entry = tb->tbl_array[tb->tbl_riter];
233 tb->tbl_eiter = entry;
239 register HENT *entry;
241 return entry->hent_key;
246 register HENT *entry;
248 return entry->hent_val;