ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/lookup.c
Revision: 2.11
Committed: Wed Jul 30 10:11:06 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.10: +2 -2 lines
Log Message:
Added prototypes submitted by Randolph Fritz.

File Contents

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