1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.18 |
static const char RCSid[] = "$Id: lookup.c,v 2.17 2010/05/01 21:49:42 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Table lookup routines |
6 |
|
|
*/ |
7 |
greg |
2.6 |
|
8 |
greg |
2.1 |
#include <stdio.h> |
9 |
greg |
2.6 |
#include <stdlib.h> |
10 |
schorsch |
2.16 |
#include <string.h> |
11 |
schorsch |
2.9 |
|
12 |
greg |
2.1 |
#include "lookup.h" |
13 |
|
|
|
14 |
|
|
|
15 |
greg |
2.18 |
int |
16 |
schorsch |
2.13 |
lu_init( /* initialize tbl for at least nel elements */ |
17 |
|
|
register LUTAB *tbl, |
18 |
|
|
int nel |
19 |
|
|
) |
20 |
greg |
2.1 |
{ |
21 |
|
|
static int hsiztab[] = { |
22 |
gregl |
2.3 |
31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, |
23 |
|
|
32749, 65521, 131071, 262139, 524287, 1048573, 2097143, |
24 |
|
|
4194301, 8388593, 0 |
25 |
greg |
2.1 |
}; |
26 |
|
|
register int *hsp; |
27 |
|
|
|
28 |
|
|
nel += nel>>1; /* 66% occupancy */ |
29 |
|
|
for (hsp = hsiztab; *hsp; hsp++) |
30 |
|
|
if (*hsp > nel) |
31 |
|
|
break; |
32 |
|
|
if (!(tbl->tsiz = *hsp)) |
33 |
|
|
tbl->tsiz = nel*2 + 1; /* not always prime */ |
34 |
|
|
tbl->tabl = (LUENT *)calloc(tbl->tsiz, sizeof(LUENT)); |
35 |
|
|
if (tbl->tabl == NULL) |
36 |
|
|
tbl->tsiz = 0; |
37 |
|
|
tbl->ndel = 0; |
38 |
|
|
return(tbl->tsiz); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
|
42 |
greg |
2.18 |
unsigned long |
43 |
schorsch |
2.13 |
lu_shash( /* hash a nul-terminated string */ |
44 |
greg |
2.18 |
const char *s |
45 |
schorsch |
2.13 |
) |
46 |
greg |
2.1 |
{ |
47 |
gregl |
2.3 |
static unsigned char shuffle[256] = { |
48 |
|
|
0, 157, 58, 215, 116, 17, 174, 75, 232, 133, 34, |
49 |
|
|
191, 92, 249, 150, 51, 208, 109, 10, 167, 68, 225, |
50 |
|
|
126, 27, 184, 85, 242, 143, 44, 201, 102, 3, 160, |
51 |
|
|
61, 218, 119, 20, 177, 78, 235, 136, 37, 194, 95, |
52 |
|
|
252, 153, 54, 211, 112, 13, 170, 71, 228, 129, 30, |
53 |
|
|
187, 88, 245, 146, 47, 204, 105, 6, 163, 64, 221, |
54 |
|
|
122, 23, 180, 81, 238, 139, 40, 197, 98, 255, 156, |
55 |
|
|
57, 214, 115, 16, 173, 74, 231, 132, 33, 190, 91, |
56 |
|
|
248, 149, 50, 207, 108, 9, 166, 67, 224, 125, 26, |
57 |
|
|
183, 84, 241, 142, 43, 200, 101, 2, 159, 60, 217, |
58 |
|
|
118, 19, 176, 77, 234, 135, 36, 193, 94, 251, 152, |
59 |
|
|
53, 210, 111, 12, 169, 70, 227, 128, 29, 186, 87, |
60 |
|
|
244, 145, 46, 203, 104, 5, 162, 63, 220, 121, 22, |
61 |
|
|
179, 80, 237, 138, 39, 196, 97, 254, 155, 56, 213, |
62 |
|
|
114, 15, 172, 73, 230, 131, 32, 189, 90, 247, 148, |
63 |
|
|
49, 206, 107, 8, 165, 66, 223, 124, 25, 182, 83, |
64 |
|
|
240, 141, 42, 199, 100, 1, 158, 59, 216, 117, 18, |
65 |
|
|
175, 76, 233, 134, 35, 192, 93, 250, 151, 52, 209, |
66 |
|
|
110, 11, 168, 69, 226, 127, 28, 185, 86, 243, 144, |
67 |
|
|
45, 202, 103, 4, 161, 62, 219, 120, 21, 178, 79, |
68 |
|
|
236, 137, 38, 195, 96, 253, 154, 55, 212, 113, 14, |
69 |
|
|
171, 72, 229, 130, 31, 188, 89, 246, 147, 48, 205, |
70 |
|
|
106, 7, 164, 65, 222, 123, 24, 181, 82, 239, 140, |
71 |
|
|
41, 198, 99 |
72 |
|
|
}; |
73 |
greg |
2.1 |
register int i = 0; |
74 |
gwlarson |
2.5 |
register unsigned long h = 0; |
75 |
greg |
2.15 |
register unsigned const char *t = (unsigned const char *)s; |
76 |
greg |
2.1 |
|
77 |
gregl |
2.3 |
while (*t) |
78 |
gwlarson |
2.5 |
h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf); |
79 |
gregl |
2.3 |
|
80 |
greg |
2.1 |
return(h); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
|
84 |
greg |
2.18 |
LUENT * |
85 |
schorsch |
2.13 |
lu_find( /* find a table entry */ |
86 |
|
|
register LUTAB *tbl, |
87 |
greg |
2.15 |
const char *key |
88 |
schorsch |
2.13 |
) |
89 |
greg |
2.1 |
{ |
90 |
gwlarson |
2.5 |
unsigned long hval; |
91 |
gregl |
2.3 |
int i, n; |
92 |
greg |
2.1 |
register int ndx; |
93 |
gregl |
2.3 |
register LUENT *le; |
94 |
greg |
2.1 |
/* look up object */ |
95 |
gwlarson |
2.5 |
if (tbl->tsiz == 0 && !lu_init(tbl, 1)) |
96 |
|
|
return(NULL); |
97 |
greg |
2.1 |
hval = (*tbl->hashf)(key); |
98 |
|
|
tryagain: |
99 |
gregl |
2.3 |
ndx = hval % tbl->tsiz; |
100 |
|
|
for (i = 0, n = 1; i < tbl->tsiz; i++, n += 2) { |
101 |
gwlarson |
2.5 |
le = &tbl->tabl[ndx]; |
102 |
gregl |
2.3 |
if (le->key == NULL) { |
103 |
|
|
le->hval = hval; |
104 |
|
|
return(le); |
105 |
greg |
2.1 |
} |
106 |
gregl |
2.3 |
if (le->hval == hval && |
107 |
|
|
(tbl->keycmp == NULL || !(*tbl->keycmp)(le->key, key))) { |
108 |
|
|
return(le); |
109 |
|
|
} |
110 |
gwlarson |
2.5 |
if ((ndx += n) >= tbl->tsiz) /* this happens rarely */ |
111 |
gregl |
2.3 |
ndx = ndx % tbl->tsiz; |
112 |
greg |
2.1 |
} |
113 |
|
|
/* table is full, reallocate */ |
114 |
gregl |
2.3 |
le = tbl->tabl; |
115 |
greg |
2.1 |
ndx = tbl->tsiz; |
116 |
|
|
i = tbl->ndel; |
117 |
greg |
2.2 |
if (!lu_init(tbl, ndx-i+1)) { /* no more memory! */ |
118 |
gregl |
2.3 |
tbl->tabl = le; |
119 |
greg |
2.1 |
tbl->tsiz = ndx; |
120 |
|
|
tbl->ndel = i; |
121 |
|
|
return(NULL); |
122 |
|
|
} |
123 |
|
|
/* |
124 |
|
|
* The following code may fail if the user has reclaimed many |
125 |
|
|
* deleted entries and the system runs out of memory in a |
126 |
|
|
* recursive call to lu_find(). |
127 |
|
|
*/ |
128 |
|
|
while (ndx--) |
129 |
schorsch |
2.10 |
if (le[ndx].key != NULL) { |
130 |
gregl |
2.3 |
if (le[ndx].data != NULL) |
131 |
schorsch |
2.10 |
*lu_find(tbl,le[ndx].key) = le[ndx]; |
132 |
greg |
2.1 |
else if (tbl->freek != NULL) |
133 |
gregl |
2.3 |
(*tbl->freek)(le[ndx].key); |
134 |
schorsch |
2.10 |
} |
135 |
greg |
2.6 |
free((void *)le); |
136 |
greg |
2.1 |
goto tryagain; /* should happen only once! */ |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
|
140 |
greg |
2.18 |
void |
141 |
schorsch |
2.13 |
lu_delete( /* delete a table entry */ |
142 |
|
|
register LUTAB *tbl, |
143 |
greg |
2.15 |
const char *key |
144 |
schorsch |
2.13 |
) |
145 |
greg |
2.1 |
{ |
146 |
|
|
register LUENT *le; |
147 |
|
|
|
148 |
|
|
if ((le = lu_find(tbl, key)) == NULL) |
149 |
|
|
return; |
150 |
|
|
if (le->key == NULL || le->data == NULL) |
151 |
|
|
return; |
152 |
|
|
if (tbl->freed != NULL) |
153 |
|
|
(*tbl->freed)(le->data); |
154 |
|
|
le->data = NULL; |
155 |
|
|
tbl->ndel++; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
|
159 |
greg |
2.18 |
int |
160 |
schorsch |
2.13 |
lu_doall( /* loop through all valid table entries */ |
161 |
greg |
2.15 |
register const LUTAB *tbl, |
162 |
greg |
2.17 |
/* int (*f)(const LUENT *) */ |
163 |
greg |
2.14 |
lut_doallf_t *f, |
164 |
|
|
void *p |
165 |
schorsch |
2.13 |
) |
166 |
gwlarson |
2.4 |
{ |
167 |
|
|
int rval = 0; |
168 |
greg |
2.15 |
register const LUENT *tp; |
169 |
gwlarson |
2.4 |
|
170 |
|
|
for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; ) |
171 |
schorsch |
2.10 |
if (tp->data != NULL) { |
172 |
greg |
2.14 |
if (f != NULL) { |
173 |
|
|
int r = (*f)(tp, p); |
174 |
|
|
if (r < 0) |
175 |
|
|
return(-1); |
176 |
|
|
rval += r; |
177 |
|
|
} else |
178 |
gwlarson |
2.4 |
rval++; |
179 |
schorsch |
2.10 |
} |
180 |
gwlarson |
2.4 |
return(rval); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
|
184 |
greg |
2.18 |
void |
185 |
schorsch |
2.13 |
lu_done( /* free table and contents */ |
186 |
|
|
register LUTAB *tbl |
187 |
|
|
) |
188 |
greg |
2.1 |
{ |
189 |
|
|
register LUENT *tp; |
190 |
|
|
|
191 |
|
|
if (!tbl->tsiz) |
192 |
|
|
return; |
193 |
|
|
for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; ) |
194 |
|
|
if (tp->key != NULL) { |
195 |
|
|
if (tbl->freek != NULL) |
196 |
|
|
(*tbl->freek)(tp->key); |
197 |
|
|
if (tp->data != NULL && tbl->freed != NULL) |
198 |
|
|
(*tbl->freed)(tp->data); |
199 |
|
|
} |
200 |
greg |
2.6 |
free((void *)tbl->tabl); |
201 |
greg |
2.1 |
tbl->tabl = NULL; |
202 |
|
|
tbl->tsiz = 0; |
203 |
|
|
tbl->ndel = 0; |
204 |
|
|
} |