ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/lookup.c
Revision: 2.15
Committed: Tue May 25 22:04:13 2004 UTC (19 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad3R6, rad3R6P1
Changes since 2.14: +8 -8 lines
Log Message:
Added const modifier to key and other parameters in lookup.h

File Contents

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