3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2002,
4 * 2005 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.
16 char *savestr(char *str);
20 hfetch(register HASH *tb, char *key)
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;
46 hstore(register HASH *tb, char *key, STR *val)
52 register HENT **oentry;
56 for (s=key, i=0, hash = 0;
58 s++, i++, hash *= 5) {
59 hash += *s * coeff[i];
62 oentry = &(tb->tbl_array[hash & tb->tbl_max]);
65 for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
66 if (entry->hent_hash != hash) /* strings can't be equal */
68 if (strNE(entry->hent_key,key)) /* is this it? */
71 safefree(entry->hent_val);
72 entry->hent_val = val;
76 entry = (HENT*) safemalloc(sizeof(HENT));
78 entry->hent_key = savestr(key);
79 entry->hent_val = val;
80 entry->hent_hash = hash;
81 entry->hent_next = *oentry;
84 if (i) { /* initial entry? */
86 if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
96 const int oldsize = tb->tbl_max + 1;
97 register int newsize = oldsize * 2;
101 register HENT *entry;
102 register HENT **oentry;
104 a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
105 memset(&a[oldsize], 0, oldsize * sizeof(HENT*)); /* zero second half */
106 tb->tbl_max = --newsize;
109 for (i=0; i<oldsize; i++,a++) {
110 if (!*a) /* non-existent */
113 for (oentry = a, entry = *a; entry; entry = *oentry) {
114 if ((entry->hent_hash & newsize) != i) {
115 *oentry = entry->hent_next;
116 entry->hent_next = *b;
123 oentry = &entry->hent_next;
125 if (!*a) /* everything moved */
133 register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
135 tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
138 hiterinit(tb); /* so each() will start off right */
139 memset(tb->tbl_array, 0, 8 * sizeof(HENT*));
144 hiterinit(register HASH *tb)
147 tb->tbl_eiter = (HENT*)NULL;