1 /* $RCSfile: hash.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:20 $
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2002,
4 * by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
18 char *savestr(char *str);
22 hfetch(register HASH *tb, char *key)
31 for (s=key, i=0, hash = 0;
33 s++, i++, hash *= 5) {
34 hash += *s * coeff[i];
36 entry = tb->tbl_array[hash & tb->tbl_max];
37 for (; entry; entry = entry->hent_next) {
38 if (entry->hent_hash != hash) /* strings can't be equal */
40 if (strNE(entry->hent_key,key)) /* is this it? */
42 return entry->hent_val;
48 hstore(register HASH *tb, char *key, STR *val)
54 register HENT **oentry;
58 for (s=key, i=0, hash = 0;
60 s++, i++, hash *= 5) {
61 hash += *s * coeff[i];
64 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
67 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
68 if (entry->hent_hash != hash) /* strings can't be equal */
70 if (strNE(entry->hent_key,key)) /* is this it? */
73 safefree(entry->hent_val);
74 entry->hent_val = val;
78 entry = (HENT*) safemalloc(sizeof(HENT));
80 entry->hent_key = savestr(key);
81 entry->hent_val = val;
82 entry->hent_hash = hash;
83 entry->hent_next = *oentry;
86 if (i) { /* initial entry? */
88 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
97 hdelete(register HASH *tb, char *key)
102 register HENT *entry;
103 register HENT **oentry;
107 for (s=key, i=0, hash = 0;
109 s++, i++, hash *= 5) {
110 hash += *s * coeff[i];
113 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
116 for (; entry; i=0, oentry = &entry->hent_next, entry = entry->hent_next) {
117 if (entry->hent_hash != hash) /* strings can't be equal */
119 if (strNE(entry->hent_key,key)) /* is this it? */
121 safefree((char*)entry->hent_val);
122 safefree(entry->hent_key);
123 *oentry = entry->hent_next;
124 safefree((char*)entry);
136 int oldsize = tb->tbl_max + 1;
137 register int newsize = oldsize * 2;
141 register HENT *entry;
142 register HENT **oentry;
144 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
145 memset(&a[oldsize], 0, oldsize * sizeof(HENT*)); /* zero second half */
146 tb->tbl_max = --newsize;
149 for (i=0; i<oldsize; i++,a++) {
150 if (!*a) /* non-existent */
153 for (oentry = a, entry = *a; entry; entry = *oentry) {
154 if ((entry->hent_hash & newsize) != i) {
155 *oentry = entry->hent_next;
156 entry->hent_next = *b;
163 oentry = &entry->hent_next;
165 if (!*a) /* everything moved */
173 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
175 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
178 hiterinit(tb); /* so each() will start off right */
179 memset(tb->tbl_array, 0, 8 * sizeof(HENT*));
184 hshow(register HASH *tb)
186 fprintf(stderr,"%5d %4d (%2d%%)\n",
189 tb->tbl_fill * 100 / (tb->tbl_max+1));
194 hiterinit(register HASH *tb)
197 tb->tbl_eiter = Null(HENT*);
202 hiternext(register HASH *tb)
204 register HENT *entry;
206 entry = tb->tbl_eiter;
209 entry = entry->hent_next;
212 if (tb->tbl_riter > tb->tbl_max) {
216 entry = tb->tbl_array[tb->tbl_riter];
220 tb->tbl_eiter = entry;
225 hiterkey(register HENT *entry)
227 return entry->hent_key;
231 hiterval(register HENT *entry)
233 return entry->hent_val;