1 /* $Header: hash.c,v 4.0 91/03/20 01:57:49 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 4.0 91/03/20 01:57:49 lwall
32 for (s=key, i=0, hash = 0;
34 s++, i++, hash *= 5) {
35 hash += *s * coeff[i];
37 entry = tb->tbl_array[hash & tb->tbl_max];
38 for (; entry; entry = entry->hent_next) {
39 if (entry->hent_hash != hash) /* strings can't be equal */
41 if (strNE(entry->hent_key,key)) /* is this it? */
43 return entry->hent_val;
58 register HENT **oentry;
62 for (s=key, i=0, hash = 0;
64 s++, i++, hash *= 5) {
65 hash += *s * coeff[i];
68 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
71 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
72 if (entry->hent_hash != hash) /* strings can't be equal */
74 if (strNE(entry->hent_key,key)) /* is this it? */
77 safefree((char*)entry->hent_val);
78 entry->hent_val = val;
82 entry = (HENT*) safemalloc(sizeof(HENT));
84 entry->hent_key = savestr(key);
85 entry->hent_val = val;
86 entry->hent_hash = hash;
87 entry->hent_next = *oentry;
90 if (i) { /* initial entry? */
92 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
108 register HENT *entry;
109 register HENT **oentry;
113 for (s=key, i=0, hash = 0;
115 s++, i++, hash *= 5) {
116 hash += *s * coeff[i];
119 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
122 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
123 if (entry->hent_hash != hash) /* strings can't be equal */
125 if (strNE(entry->hent_key,key)) /* is this it? */
127 safefree((char*)entry->hent_val);
128 safefree(entry->hent_key);
129 *oentry = entry->hent_next;
130 safefree((char*)entry);
142 int oldsize = tb->tbl_max + 1;
143 register int newsize = oldsize * 2;
147 register HENT *entry;
148 register HENT **oentry;
150 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
151 bzero((char*)&a[oldsize], oldsize * sizeof(HENT*)); /* zero second half */
152 tb->tbl_max = --newsize;
155 for (i=0; i<oldsize; i++,a++) {
156 if (!*a) /* non-existent */
159 for (oentry = a, entry = *a; entry; entry = *oentry) {
160 if ((entry->hent_hash & newsize) != i) {
161 *oentry = entry->hent_next;
162 entry->hent_next = *b;
169 oentry = &entry->hent_next;
171 if (!*a) /* everything moved */
179 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
181 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
184 hiterinit(tb); /* so each() will start off right */
185 bzero((char*)tb->tbl_array, 8 * sizeof(HENT*));
193 fprintf(stderr,"%5d %4d (%2d%%)\n",
196 tb->tbl_fill * 100 / (tb->tbl_max+1));
204 tb->tbl_eiter = Null(HENT*);
212 register HENT *entry;
214 entry = tb->tbl_eiter;
217 entry = entry->hent_next;
220 if (tb->tbl_riter > tb->tbl_max) {
224 entry = tb->tbl_array[tb->tbl_riter];
228 tb->tbl_eiter = entry;
234 register HENT *entry;
236 return entry->hent_key;
241 register HENT *entry;
243 return entry->hent_val;