ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/lookup.c
Revision: 2.5
Committed: Thu Sep 3 14:22:23 1998 UTC (25 years, 8 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.4: +16 -15 lines
Log Message:
changed lookup routines to use unsigned long hash values

File Contents

# User Rev Content
1 gwlarson 2.5 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2 greg 2.1
3     #ifndef lint
4 gregl 2.3 static char SCCSid[] = "$SunId$ SGI";
5 greg 2.1 #endif
6    
7     /*
8     * Table lookup routines
9     */
10    
11     #include <stdio.h>
12     #include "lookup.h"
13    
14 gwlarson 2.5 #ifdef NOSTRUCTASS
15     #define copystruct(d,s) bcopy((char *)(s),(char *)(d),sizeof(*(d)))
16     #else
17     #define copystruct(d,s) (*(d) = *(s))
18     #endif
19    
20 greg 2.1 #define MEM_PTR char *
21    
22     extern MEM_PTR calloc();
23    
24    
25     int
26     lu_init(tbl, nel) /* initialize tbl for at least nel elements */
27     register LUTAB *tbl;
28     int nel;
29     {
30     static int hsiztab[] = {
31 gregl 2.3 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381,
32     32749, 65521, 131071, 262139, 524287, 1048573, 2097143,
33     4194301, 8388593, 0
34 greg 2.1 };
35     register int *hsp;
36    
37     nel += nel>>1; /* 66% occupancy */
38     for (hsp = hsiztab; *hsp; hsp++)
39     if (*hsp > nel)
40     break;
41     if (!(tbl->tsiz = *hsp))
42     tbl->tsiz = nel*2 + 1; /* not always prime */
43     tbl->tabl = (LUENT *)calloc(tbl->tsiz, sizeof(LUENT));
44     if (tbl->tabl == NULL)
45     tbl->tsiz = 0;
46     tbl->ndel = 0;
47     return(tbl->tsiz);
48     }
49    
50    
51 gwlarson 2.5 unsigned long
52 greg 2.1 lu_shash(s) /* hash a nul-terminated string */
53 gregl 2.3 char *s;
54 greg 2.1 {
55 gregl 2.3 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 greg 2.1 register int i = 0;
82 gwlarson 2.5 register unsigned long h = 0;
83 gregl 2.3 register unsigned char *t = (unsigned char *)s;
84 greg 2.1
85 gregl 2.3 while (*t)
86 gwlarson 2.5 h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf);
87 gregl 2.3
88 greg 2.1 return(h);
89     }
90    
91    
92     LUENT *
93     lu_find(tbl, key) /* find a table entry */
94     register LUTAB *tbl;
95     char *key;
96     {
97 gwlarson 2.5 unsigned long hval;
98 gregl 2.3 int i, n;
99 greg 2.1 register int ndx;
100 gregl 2.3 register LUENT *le;
101 greg 2.1 /* look up object */
102 gwlarson 2.5 if (tbl->tsiz == 0 && !lu_init(tbl, 1))
103     return(NULL);
104 greg 2.1 hval = (*tbl->hashf)(key);
105     tryagain:
106 gregl 2.3 ndx = hval % tbl->tsiz;
107     for (i = 0, n = 1; i < tbl->tsiz; i++, n += 2) {
108 gwlarson 2.5 le = &tbl->tabl[ndx];
109 gregl 2.3 if (le->key == NULL) {
110     le->hval = hval;
111     return(le);
112 greg 2.1 }
113 gregl 2.3 if (le->hval == hval &&
114     (tbl->keycmp == NULL || !(*tbl->keycmp)(le->key, key))) {
115     return(le);
116     }
117 gwlarson 2.5 if ((ndx += n) >= tbl->tsiz) /* this happens rarely */
118 gregl 2.3 ndx = ndx % tbl->tsiz;
119 greg 2.1 }
120     /* table is full, reallocate */
121 gregl 2.3 le = tbl->tabl;
122 greg 2.1 ndx = tbl->tsiz;
123     i = tbl->ndel;
124 greg 2.2 if (!lu_init(tbl, ndx-i+1)) { /* no more memory! */
125 gregl 2.3 tbl->tabl = le;
126 greg 2.1 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 gregl 2.3 if (le[ndx].key != NULL)
137     if (le[ndx].data != NULL)
138 gwlarson 2.5 copystruct(lu_find(tbl,le[ndx].key), &le[ndx]);
139 greg 2.1 else if (tbl->freek != NULL)
140 gregl 2.3 (*tbl->freek)(le[ndx].key);
141     free((MEM_PTR)le);
142 greg 2.1 goto tryagain; /* should happen only once! */
143     }
144    
145    
146     void
147     lu_delete(tbl, key) /* delete a table entry */
148     register LUTAB *tbl;
149     char *key;
150     {
151     register LUENT *le;
152    
153     if ((le = lu_find(tbl, key)) == NULL)
154     return;
155     if (le->key == NULL || le->data == NULL)
156     return;
157     if (tbl->freed != NULL)
158     (*tbl->freed)(le->data);
159     le->data = NULL;
160     tbl->ndel++;
161     }
162    
163    
164 gwlarson 2.4 int
165     lu_doall(tbl, f) /* loop through all valid table entries */
166     register LUTAB *tbl;
167     int (*f)();
168     {
169     int rval = 0;
170     register LUENT *tp;
171    
172     for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; )
173     if (tp->data != NULL)
174     if (f != NULL)
175     rval += (*f)(tp);
176     else
177     rval++;
178     return(rval);
179     }
180    
181    
182 greg 2.1 void
183     lu_done(tbl) /* free table and contents */
184     register LUTAB *tbl;
185     {
186     register LUENT *tp;
187    
188     if (!tbl->tsiz)
189     return;
190     for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; )
191     if (tp->key != NULL) {
192     if (tbl->freek != NULL)
193     (*tbl->freek)(tp->key);
194     if (tp->data != NULL && tbl->freed != NULL)
195     (*tbl->freed)(tp->data);
196     }
197     free((MEM_PTR)tbl->tabl);
198     tbl->tabl = NULL;
199     tbl->tsiz = 0;
200     tbl->ndel = 0;
201     }