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.
17 hfetch(register HASH *tb, char *key)
26 for (s=key, i=0, hash = 0;
28 s++, i++, hash *= 5) {
29 hash += *s * coeff[i];
31 entry = tb->tbl_array[hash & tb->tbl_max];
32 for (; entry; entry = entry->hent_next) {
33 if (entry->hent_hash != hash) /* strings can't be equal */
35 if (strNE(entry->hent_key,key)) /* is this it? */
37 return entry->hent_val;
43 hstore(register HASH *tb, char *key, STR *val)
49 register HENT **oentry;
53 for (s=key, i=0, hash = 0;
55 s++, i++, hash *= 5) {
56 hash += *s * coeff[i];
59 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
62 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
63 if (entry->hent_hash != hash) /* strings can't be equal */
65 if (strNE(entry->hent_key,key)) /* is this it? */
68 safefree(entry->hent_val);
69 entry->hent_val = val;
73 entry = (HENT*) safemalloc(sizeof(HENT));
75 entry->hent_key = savestr(key);
76 entry->hent_val = val;
77 entry->hent_hash = hash;
78 entry->hent_next = *oentry;
81 if (i) { /* initial entry? */
83 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
92 hdelete(register HASH *tb, char *key)
98 register HENT **oentry;
102 for (s=key, i=0, hash = 0;
104 s++, i++, hash *= 5) {
105 hash += *s * coeff[i];
108 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
111 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
112 if (entry->hent_hash != hash) /* strings can't be equal */
114 if (strNE(entry->hent_key,key)) /* is this it? */
116 safefree((char*)entry->hent_val);
117 safefree(entry->hent_key);
118 *oentry = entry->hent_next;
119 safefree((char*)entry);
131 int oldsize = tb->tbl_max + 1;
132 register int newsize = oldsize * 2;
136 register HENT *entry;
137 register HENT **oentry;
139 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
140 bzero((char*)&a[oldsize], oldsize * sizeof(HENT*)); /* zero second half */
141 tb->tbl_max = --newsize;
144 for (i=0; i<oldsize; i++,a++) {
145 if (!*a) /* non-existent */
148 for (oentry = a, entry = *a; entry; entry = *oentry) {
149 if ((entry->hent_hash & newsize) != i) {
150 *oentry = entry->hent_next;
151 entry->hent_next = *b;
158 oentry = &entry->hent_next;
160 if (!*a) /* everything moved */
168 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
170 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
173 hiterinit(tb); /* so each() will start off right */
174 bzero((char*)tb->tbl_array, 8 * sizeof(HENT*));
179 hshow(register HASH *tb)
181 fprintf(stderr,"%5d %4d (%2d%%)\n",
184 tb->tbl_fill * 100 / (tb->tbl_max+1));
189 hiterinit(register HASH *tb)
192 tb->tbl_eiter = Null(HENT*);
197 hiternext(register HASH *tb)
199 register HENT *entry;
201 entry = tb->tbl_eiter;
204 entry = entry->hent_next;
207 if (tb->tbl_riter > tb->tbl_max) {
211 entry = tb->tbl_array[tb->tbl_riter];
215 tb->tbl_eiter = entry;
220 hiterkey(register HENT *entry)
222 return entry->hent_key;
226 hiterval(register HENT *entry)
228 return entry->hent_val;