1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/* $Header: hash.c,v 2.0 88/06/05 00:09:06 root Exp $
*
* $Log: hash.c,v $
* Revision 2.0 88/06/05 00:09:06 root
* Baseline version 2.0.
*
*/
#include "EXTERN.h"
#include "perl.h"
STR *
hfetch(tb,key)
register HASH *tb;
char *key;
{
register char *s;
register int i;
register int hash;
register HENT *entry;
if (!tb)
return Nullstr;
for (s=key, i=0, hash = 0;
/* while */ *s && i < COEFFSIZE;
s++, i++, hash *= 5) {
hash += *s * coeff[i];
}
entry = tb->tbl_array[hash & tb->tbl_max];
for (; entry; entry = entry->hent_next) {
if (entry->hent_hash != hash) /* strings can't be equal */
continue;
if (strNE(entry->hent_key,key)) /* is this it? */
continue;
return entry->hent_val;
}
return Nullstr;
}
bool
hstore(tb,key,val)
register HASH *tb;
char *key;
STR *val;
{
register char *s;
register int i;
register int hash;
register HENT *entry;
register HENT **oentry;
if (!tb)
return FALSE;
for (s=key, i=0, hash = 0;
/* while */ *s && i < COEFFSIZE;
s++, i++, hash *= 5) {
hash += *s * coeff[i];
}
oentry = &(tb->tbl_array[hash & tb->tbl_max]);
i = 1;
for (entry = *oentry; entry; i=0, entry = entry->hent_next) {
if (entry->hent_hash != hash) /* strings can't be equal */
continue;
if (strNE(entry->hent_key,key)) /* is this it? */
continue;
safefree((char*)entry->hent_val);
entry->hent_val = val;
return TRUE;
}
entry = (HENT*) safemalloc(sizeof(HENT));
entry->hent_key = savestr(key);
entry->hent_val = val;
entry->hent_hash = hash;
entry->hent_next = *oentry;
*oentry = entry;
if (i) { /* initial entry? */
tb->tbl_fill++;
if ((tb->tbl_fill * 100 / (tb->tbl_max + 1)) > FILLPCT)
hsplit(tb);
}
return FALSE;
}
STR *
hdelete(tb,key)
register HASH *tb;
char *key;
{
register char *s;
register int i;
register int hash;
register HENT *entry;
register HENT **oentry;
STR *str;
if (!tb)
return Nullstr;
for (s=key, i=0, hash = 0;
/* while */ *s && i < COEFFSIZE;
s++, i++, hash *= 5) {
hash += *s * coeff[i];
}
oentry = &(tb->tbl_array[hash & tb->tbl_max]);
entry = *oentry;
i = 1;
for (; entry; i=0, oentry = &entry->hent_next, entry = *oentry) {
if (entry->hent_hash != hash) /* strings can't be equal */
continue;
if (strNE(entry->hent_key,key)) /* is this it? */
continue;
*oentry = entry->hent_next;
str = str_static(entry->hent_val);
hentfree(entry);
if (i)
tb->tbl_fill--;
return str;
}
return Nullstr;
}
hsplit(tb)
HASH *tb;
{
int oldsize = tb->tbl_max + 1;
register int newsize = oldsize * 2;
register int i;
register HENT **a;
register HENT **b;
register HENT *entry;
register HENT **oentry;
a = (HENT**) saferealloc((char*)tb->tbl_array, newsize * sizeof(HENT*));
bzero((char*)&a[oldsize], oldsize * sizeof(HENT*)); /* zero second half */
tb->tbl_max = --newsize;
tb->tbl_array = a;
for (i=0; i<oldsize; i++,a++) {
if (!*a) /* non-existent */
continue;
b = a+oldsize;
for (oentry = a, entry = *a; entry; entry = *oentry) {
if ((entry->hent_hash & newsize) != i) {
*oentry = entry->hent_next;
entry->hent_next = *b;
if (!*b)
tb->tbl_fill++;
*b = entry;
continue;
}
else
oentry = &entry->hent_next;
}
if (!*a) /* everything moved */
tb->tbl_fill--;
}
}
HASH *
hnew()
{
register HASH *tb = (HASH*)safemalloc(sizeof(HASH));
tb->tbl_array = (HENT**) safemalloc(8 * sizeof(HENT*));
tb->tbl_fill = 0;
tb->tbl_max = 7;
hiterinit(tb); /* so each() will start off right */
bzero((char*)tb->tbl_array, 8 * sizeof(HENT*));
return tb;
}
void
hentfree(hent)
register HENT *hent;
{
if (!hent)
return;
str_free(hent->hent_val);
safefree(hent->hent_key);
safefree((char*)hent);
}
void
hclear(tb)
register HASH *tb;
{
register HENT *hent;
register HENT *ohent = Null(HENT*);
if (!tb)
return;
hiterinit(tb);
while (hent = hiternext(tb)) { /* concise but not very efficient */
hentfree(ohent);
ohent = hent;
}
hentfree(ohent);
tb->tbl_fill = 0;
bzero((char*)tb->tbl_array, (tb->tbl_max + 1) * sizeof(HENT*));
}
#ifdef NOTUSED
void
hfree(tb)
HASH *tb;
{
if (!tb)
return
hiterinit(tb);
while (hent = hiternext(tb)) {
hentfree(ohent);
ohent = hent;
}
hentfree(ohent);
safefree((char*)tb->tbl_array);
safefree((char*)tb);
}
#endif
#ifdef NOTUSED
hshow(tb)
register HASH *tb;
{
fprintf(stderr,"%5d %4d (%2d%%)\n",
tb->tbl_max+1,
tb->tbl_fill,
tb->tbl_fill * 100 / (tb->tbl_max+1));
}
#endif
hiterinit(tb)
register HASH *tb;
{
tb->tbl_riter = -1;
tb->tbl_eiter = Null(HENT*);
return tb->tbl_fill;
}
HENT *
hiternext(tb)
register HASH *tb;
{
register HENT *entry;
entry = tb->tbl_eiter;
do {
if (entry)
entry = entry->hent_next;
if (!entry) {
tb->tbl_riter++;
if (tb->tbl_riter > tb->tbl_max) {
tb->tbl_riter = -1;
break;
}
entry = tb->tbl_array[tb->tbl_riter];
}
} while (!entry);
tb->tbl_eiter = entry;
return entry;
}
char *
hiterkey(entry)
register HENT *entry;
{
return entry->hent_key;
}
STR *
hiterval(entry)
register HENT *entry;
{
return entry->hent_val;
}
|