ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/lookup.c
(Generate patch)

Comparing ray/src/cv/mgflib/lookup.c (file contents):
Revision 1.1 by greg, Tue Jun 21 14:45:45 1994 UTC vs.
Revision 1.10 by schorsch, Sat Nov 15 17:54:06 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Table lookup routines
6   */
7  
8 + #include "copyright.h"
9 +
10   #include <stdio.h>
11 + #include <stdlib.h>
12 +
13   #include "lookup.h"
14  
14 #ifndef MEM_PTR
15 #define MEM_PTR         void *
16 #endif
15  
18 extern MEM_PTR  calloc();
19
20
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 <                31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, 0
22 >                31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381,
23 >                32749, 65521, 131071, 262139, 524287, 1048573, 2097143,
24 >                4194301, 8388593, 0
25          };
26          register int  *hsp;
27  
28 <        nel += nel>>2;                  /* 75% occupancy */
28 >        nel += nel>>1;                  /* 66% occupancy */
29          for (hsp = hsiztab; *hsp; hsp++)
30                  if (*hsp > nel)
31                          break;
# Line 37 | Line 34 | int    nel;
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 < int
43 < lu_hash(s)                      /* hash a nul-terminated string */
44 < register char  *s;
42 > unsigned long
43 > lu_shash(s)                     /* hash a nul-terminated string */
44 > char    *s;
45   {
46 <        register int  h = 0;
46 >        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 >        register int    i = 0;
73 >        register unsigned long  h = 0;
74 >        register unsigned char *t = (unsigned char *)s;
75  
76 <        while (*s)
77 <                h = (h<<1 & 0x7fff) ^ (*s++ & 0xff);
76 >        while (*t)
77 >                h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf);
78 >
79          return(h);
80   }
81  
# Line 58 | Line 85 | lu_find(tbl, key)              /* find a table entry */
85   register LUTAB  *tbl;
86   char    *key;
87   {
88 <        int  hval, i;
89 <        register int  ndx;
88 >        unsigned long   hval;
89 >        int     i, n;
90 >        register int    ndx;
91          register LUENT  *le;
64        LUENT  *oldtabl;
92                                          /* look up object */
93 <        hval = lu_hash(key);
93 >        if (tbl->tsiz == 0 && !lu_init(tbl, 1))
94 >                return(NULL);
95 >        hval = (*tbl->hashf)(key);
96   tryagain:
97 <        for (i = 0; i < tbl->tsiz; i++) {
98 <                ndx = (hval + i*i) % tbl->tsiz;
99 <                if (tbl->tabl[ndx].key == NULL ||
100 <                                !strcmp(tbl->tabl[ndx].key, key))
101 <                        return(&tbl->tabl[ndx]);
97 >        ndx = hval % tbl->tsiz;
98 >        for (i = 0, n = 1; i < tbl->tsiz; i++, n += 2) {
99 >                le = &tbl->tabl[ndx];
100 >                if (le->key == NULL) {
101 >                        le->hval = hval;
102 >                        return(le);
103 >                }
104 >                if (le->hval == hval &&
105 >                      (tbl->keycmp == NULL || !(*tbl->keycmp)(le->key, key))) {
106 >                        return(le);
107 >                }
108 >                if ((ndx += n) >= tbl->tsiz)    /* this happens rarely */
109 >                        ndx = ndx % tbl->tsiz;
110          }
111                                          /* table is full, reallocate */
112 <        oldtabl = tbl->tabl;
112 >        le = tbl->tabl;
113          ndx = tbl->tsiz;
114 <        if (!lu_init(tbl, ndx)) {       /* no more memory! */
115 <                tbl->tabl = oldtabl;
114 >        i = tbl->ndel;
115 >        if (!lu_init(tbl, ndx-i+1)) {   /* no more memory! */
116 >                tbl->tabl = le;
117                  tbl->tsiz = ndx;
118 +                tbl->ndel = i;
119                  return(NULL);
120          }
121 <        if (!ndx)
122 <                goto tryagain;
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 <                if (oldtabl[ndx].key != NULL) {
128 <                        le = lu_find(tbl, oldtabl[ndx].key);
129 <                        le->key = oldtabl[ndx].key;
130 <                        le->data = oldtabl[ndx].data;
127 >                if (le[ndx].key != NULL) {
128 >                        if (le[ndx].data != NULL)
129 >                                *lu_find(tbl,le[ndx].key) = le[ndx];
130 >                        else if (tbl->freek != NULL)
131 >                                (*tbl->freek)(le[ndx].key);
132                  }
133 <        free((MEM_PTR)oldtabl);
133 >        free((void *)le);
134          goto tryagain;                  /* should happen only once! */
135   }
136  
137  
138   void
139 < lu_done(tbl, f)                 /* free table and contents */
140 < LUTAB   *tbl;
141 < int     (*f)();
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 + int
157 + lu_doall(tbl, f)                /* loop through all valid table entries */
158 + register LUTAB  *tbl;
159 + int     (*f)(LUENT *);
160 + {
161 +        int     rval = 0;
162          register LUENT  *tp;
163  
164 +        for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; )
165 +                if (tp->data != NULL) {
166 +                        if (f != NULL)
167 +                                rval += (*f)(tp);
168 +                        else
169 +                                rval++;
170 +                }
171 +        return(rval);
172 + }
173 +
174 +
175 + 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 <        if (f != NULL)
184 <                for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; )
185 <                        if (tp->key != NULL)
186 <                                (*f)(tp);
187 <        free((MEM_PTR)tbl->tabl);
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 >        free((void *)tbl->tabl);
191          tbl->tabl = NULL;
192          tbl->tsiz = 0;
193 +        tbl->ndel = 0;
194   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines