1 /* $RCSfile: hash.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:20 $
3 * Copyright (c) 1991-1997, 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.
28 for (s=key, i=0, hash = 0;
30 s++, i++, hash *= 5) {
31 hash += *s * coeff[i];
33 entry = tb->tbl_array[hash & tb->tbl_max];
34 for (; entry; entry = entry->hent_next) {
35 if (entry->hent_hash != hash) /* strings can't be equal */
37 if (strNE(entry->hent_key,key)) /* is this it? */
39 return entry->hent_val;
54 register HENT **oentry;
58 for (s=key, i=0, hash = 0;
60 s++, i++, hash *= 5) {
61 hash += *s * coeff[i];
64 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
67 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
68 if (entry->hent_hash != hash) /* strings can't be equal */
70 if (strNE(entry->hent_key,key)) /* is this it? */
73 Safefree(entry->hent_val);
74 entry->hent_val = val;
78 entry = (HENT*) safemalloc(sizeof(HENT));
80 entry->hent_key = savestr(key);
81 entry->hent_val = val;
82 entry->hent_hash = hash;
83 entry->hent_next = *oentry;
86 if (i) { /* initial entry? */
88 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
104 register HENT *entry;
105 register HENT **oentry;
109 for (s=key, i=0, hash = 0;
111 s++, i++, hash *= 5) {
112 hash += *s * coeff[i];
115 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
118 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
119 if (entry->hent_hash != hash) /* strings can't be equal */
121 if (strNE(entry->hent_key,key)) /* is this it? */
123 safefree((char*)entry->hent_val);
124 safefree(entry->hent_key);
125 *oentry = entry->hent_next;
126 safefree((char*)entry);
139 int oldsize = tb->tbl_max + 1;
140 register int newsize = oldsize * 2;
144 register HENT *entry;
145 register HENT **oentry;
147 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
148 bzero((char*)&a[oldsize], oldsize * sizeof(HENT*)); /* zero second half */
149 tb->tbl_max = --newsize;
152 for (i=0; i<oldsize; i++,a++) {
153 if (!*a) /* non-existent */
156 for (oentry = a, entry = *a; entry; entry = *oentry) {
157 if ((entry->hent_hash & newsize) != i) {
158 *oentry = entry->hent_next;
159 entry->hent_next = *b;
166 oentry = &entry->hent_next;
168 if (!*a) /* everything moved */
176 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
178 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
181 hiterinit(tb); /* so each() will start off right */
182 bzero((char*)tb->tbl_array, 8 * sizeof(HENT*));
190 fprintf(stderr,"%5d %4d (%2d%%)\n",
193 tb->tbl_fill * 100 / (tb->tbl_max+1));
202 tb->tbl_eiter = Null(HENT*);
210 register HENT *entry;
212 entry = tb->tbl_eiter;
215 entry = entry->hent_next;
218 if (tb->tbl_riter > tb->tbl_max) {
222 entry = tb->tbl_array[tb->tbl_riter];
226 tb->tbl_eiter = entry;
232 register HENT *entry;
234 return entry->hent_key;
239 register HENT *entry;
241 return entry->hent_val;