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