ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/lookup.c
Revision: 2.16
Committed: Wed Jun 7 17:52:03 2006 UTC (17 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R0, rad3R8, rad3R9
Changes since 2.15: +2 -1 lines
Log Message:
Eliminated some compiler warnings.

File Contents

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