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.
29 for (s=key, i=0, hash = 0;
31 s++, i++, hash *= 5) {
32 hash += *s * coeff[i];
34 entry = tb->tbl_array[hash & tb->tbl_max];
35 for (; entry; entry = entry->hent_next) {
36 if (entry->hent_hash != hash) /* strings can't be equal */
38 if (strNE(entry->hent_key,key)) /* is this it? */
40 return entry->hent_val;
55 register HENT **oentry;
59 for (s=key, i=0, hash = 0;
61 s++, i++, hash *= 5) {
62 hash += *s * coeff[i];
65 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
68 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
69 if (entry->hent_hash != hash) /* strings can't be equal */
71 if (strNE(entry->hent_key,key)) /* is this it? */
74 safefree((char*)entry->hent_val);
75 entry->hent_val = val;
79 entry = (HENT*) safemalloc(sizeof(HENT));
81 entry->hent_key = savestr(key);
82 entry->hent_val = val;
83 entry->hent_hash = hash;
84 entry->hent_next = *oentry;
87 if (i) { /* initial entry? */
89 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
105 register HENT *entry;
106 register HENT **oentry;
110 for (s=key, i=0, hash = 0;
112 s++, i++, hash *= 5) {
113 hash += *s * coeff[i];
116 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
119 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
120 if (entry->hent_hash != hash) /* strings can't be equal */
122 if (strNE(entry->hent_key,key)) /* is this it? */
124 safefree((char*)entry->hent_val);
125 safefree(entry->hent_key);
126 *oentry = entry->hent_next;
127 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));
201 tb->tbl_eiter = Null(HENT*);
209 register HENT *entry;
211 entry = tb->tbl_eiter;
214 entry = entry->hent_next;
217 if (tb->tbl_riter > tb->tbl_max) {
221 entry = tb->tbl_array[tb->tbl_riter];
225 tb->tbl_eiter = entry;
231 register HENT *entry;
233 return entry->hent_key;
238 register HENT *entry;
240 return entry->hent_val;