ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/lookup.c
Revision: 1.7
Committed: Fri Sep 4 09:05:09 1998 UTC (25 years, 8 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 1.6: +7 -12 lines
Log Message:
changed lookup routines to use unsigned long hash values

File Contents

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