1 /* $Header: hash.c,v 3.0 89/10/18 15:34:50 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 3.0 89/10/18 15:34:50 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? */
76 safefree((char*)entry->hent_val);
77 entry->hent_val = val;
80 entry = (HENT*) safemalloc(sizeof(HENT));
82 entry->hent_key = savestr(key);
83 entry->hent_val = val;
84 entry->hent_hash = hash;
85 entry->hent_next = *oentry;
88 if (i) { /* initial entry? */
90 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
106 register HENT *entry;
107 register HENT **oentry;
111 for (s=key, i=0, hash = 0;
113 s++, i++, hash *= 5) {
114 hash += *s * coeff[i];
117 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
120 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
121 if (entry->hent_hash != hash) /* strings can't be equal */
123 if (strNE(entry->hent_key,key)) /* is this it? */
125 safefree((char*)entry->hent_val);
126 safefree(entry->hent_key);
127 *oentry = entry->hent_next;
128 safefree((char*)entry);
140 int oldsize = tb->tbl_max + 1;
141 register int newsize = oldsize * 2;
145 register HENT *entry;
146 register HENT **oentry;
148 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
149 bzero((char*)&a[oldsize], oldsize * sizeof(HENT*)); /* zero second half */
150 tb->tbl_max = --newsize;
153 for (i=0; i<oldsize; i++,a++) {
154 if (!*a) /* non-existent */
157 for (oentry = a, entry = *a; entry; entry = *oentry) {
158 if ((entry->hent_hash & newsize) != i) {
159 *oentry = entry->hent_next;
160 entry->hent_next = *b;
167 oentry = &entry->hent_next;
169 if (!*a) /* everything moved */
177 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
179 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
182 hiterinit(tb); /* so each() will start off right */
183 bzero((char*)tb->tbl_array, 8 * sizeof(HENT*));
191 fprintf(stderr,"%5d %4d (%2d%%)\n",
194 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;