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