1 /* $RCSfile: hash.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:20 $
3 * Copyright (c) 1991-2003, 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 char *savestr(char *str);
21 hfetch(register HASH *tb, char *key)
30 for (s=key, i=0, hash = 0;
32 s++, i++, hash *= 5) {
33 hash += *s * coeff[i];
35 entry = tb->tbl_array[hash & tb->tbl_max];
36 for (; entry; entry = entry->hent_next) {
37 if (entry->hent_hash != hash) /* strings can't be equal */
39 if (strNE(entry->hent_key,key)) /* is this it? */
41 return entry->hent_val;
47 hstore(register HASH *tb, char *key, STR *val)
53 register HENT **oentry;
57 for (s=key, i=0, hash = 0;
59 s++, i++, hash *= 5) {
60 hash += *s * coeff[i];
63 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
66 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
67 if (entry->hent_hash != hash) /* strings can't be equal */
69 if (strNE(entry->hent_key,key)) /* is this it? */
72 safefree(entry->hent_val);
73 entry->hent_val = val;
77 entry = (HENT*) safemalloc(sizeof(HENT));
79 entry->hent_key = savestr(key);
80 entry->hent_val = val;
81 entry->hent_hash = hash;
82 entry->hent_next = *oentry;
85 if (i) { /* initial entry? */
87 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
96 hdelete(register HASH *tb, char *key)
101 register HENT *entry;
102 register HENT **oentry;
106 for (s=key, i=0, hash = 0;
108 s++, i++, hash *= 5) {
109 hash += *s * coeff[i];
112 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
115 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
116 if (entry->hent_hash != hash) /* strings can't be equal */
118 if (strNE(entry->hent_key,key)) /* is this it? */
120 safefree((char*)entry->hent_val);
121 safefree(entry->hent_key);
122 *oentry = entry->hent_next;
123 safefree((char*)entry);
135 int oldsize = tb->tbl_max + 1;
136 register int newsize = oldsize * 2;
140 register HENT *entry;
141 register HENT **oentry;
143 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
144 memset(&a[oldsize], 0, oldsize * sizeof(HENT*)); /* zero second half */
145 tb->tbl_max = --newsize;
148 for (i=0; i<oldsize; i++,a++) {
149 if (!*a) /* non-existent */
152 for (oentry = a, entry = *a; entry; entry = *oentry) {
153 if ((entry->hent_hash & newsize) != i) {
154 *oentry = entry->hent_next;
155 entry->hent_next = *b;
162 oentry = &entry->hent_next;
164 if (!*a) /* everything moved */
172 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
174 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
177 hiterinit(tb); /* so each() will start off right */
178 memset(tb->tbl_array, 0, 8 * sizeof(HENT*));
183 hshow(register HASH *tb)
185 fprintf(stderr,"%5d %4d (%2d%%)\n",
188 tb->tbl_fill * 100 / (tb->tbl_max+1));
193 hiterinit(register HASH *tb)
196 tb->tbl_eiter = Null(HENT*);
201 hiternext(register HASH *tb)
203 register HENT *entry;
205 entry = tb->tbl_eiter;
208 entry = entry->hent_next;
211 if (tb->tbl_riter > tb->tbl_max) {
215 entry = tb->tbl_array[tb->tbl_riter];
219 tb->tbl_eiter = entry;
224 hiterkey(register HENT *entry)
226 return entry->hent_key;
230 hiterval(register HENT *entry)
232 return entry->hent_val;