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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines