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

Comparing ray/src/common/lookup.c (file contents):
Revision 2.4 by gwlarson, Tue Jun 2 15:01:43 1998 UTC vs.
Revision 2.15 by greg, Tue May 25 22:04:13 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
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 +
11   #include "lookup.h"
12  
13 < #define MEM_PTR         char *
13 > extern int
14 > lu_strcmp(
15 >        const void *s1,
16 >        const void *s2
17 > )
18 > {
19 >        return strcmp((const char*)s1,(const char*)s2);
20 > }
21  
22 < extern MEM_PTR  calloc();
23 <
24 <
25 < int
26 < lu_init(tbl, nel)               /* initialize tbl for at least nel elements */
21 < register LUTAB  *tbl;
22 < int     nel;
22 > extern int
23 > lu_init(                /* initialize tbl for at least nel elements */
24 >        register LUTAB  *tbl,
25 >        int     nel
26 > )
27   {
28          static int  hsiztab[] = {
29                  31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381,
# Line 42 | Line 46 | int    nel;
46   }
47  
48  
49 < long
50 < lu_shash(s)                     /* hash a nul-terminated string */
51 < char    *s;
49 > extern unsigned long
50 > lu_shash(                       /* hash a nul-terminated string */
51 >        const void      *s
52 > )
53   {
54          static unsigned char shuffle[256] = {
55                  0, 157, 58, 215, 116, 17, 174, 75, 232, 133, 34,
# Line 73 | Line 78 | char   *s;
78                  41, 198, 99
79          };
80          register int    i = 0;
81 <        register long   h = 0;
82 <        register unsigned char *t = (unsigned char *)s;
81 >        register unsigned long  h = 0;
82 >        register unsigned const char *t = (unsigned const char *)s;
83  
84          while (*t)
85 <                h ^= (long)shuffle[*t++] << ((i+=11) & 0xf);
85 >                h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf);
86  
87          return(h);
88   }
89  
90  
91 < LUENT *
92 < lu_find(tbl, key)               /* find a table entry */
93 < register LUTAB  *tbl;
94 < char    *key;
91 > extern LUENT *
92 > lu_find(                /* find a table entry */
93 >        register LUTAB  *tbl,
94 >        const char      *key
95 > )
96   {
97 <        long    hval;
97 >        unsigned long   hval;
98          int     i, n;
99          register int    ndx;
100          register LUENT  *le;
101                                          /* look up object */
102 <        if (tbl->tsiz == 0)
103 <                lu_init(tbl, 1);
102 >        if (tbl->tsiz == 0 && !lu_init(tbl, 1))
103 >                return(NULL);
104          hval = (*tbl->hashf)(key);
105   tryagain:
106          ndx = hval % tbl->tsiz;
101        le = &tbl->tabl[ndx];
107          for (i = 0, n = 1; i < tbl->tsiz; i++, n += 2) {
108 +                le = &tbl->tabl[ndx];
109                  if (le->key == NULL) {
110                          le->hval = hval;
111                          return(le);
# Line 108 | Line 114 | tryagain:
114                        (tbl->keycmp == NULL || !(*tbl->keycmp)(le->key, key))) {
115                          return(le);
116                  }
117 <                le += n;
112 <                if ((ndx += n) >= tbl->tsiz) {  /* this happens rarely */
117 >                if ((ndx += n) >= tbl->tsiz)    /* this happens rarely */
118                          ndx = ndx % tbl->tsiz;
114                        le = &tbl->tabl[ndx];
115                }
119          }
120                                          /* table is full, reallocate */
121          le = tbl->tabl;
# Line 124 | Line 127 | tryagain:
127                  tbl->ndel = i;
128                  return(NULL);
129          }
127        if (!ndx)
128                goto tryagain;
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 <                if (le[ndx].key != NULL)
136 >                if (le[ndx].key != NULL) {
137                          if (le[ndx].data != NULL)
138 <                                *lu_find(tbl, le[ndx].key) = le[ndx];
138 >                                *lu_find(tbl,le[ndx].key) = le[ndx];
139                          else if (tbl->freek != NULL)
140                                  (*tbl->freek)(le[ndx].key);
141 <        free((MEM_PTR)le);
141 >                }
142 >        free((void *)le);
143          goto tryagain;                  /* should happen only once! */
144   }
145  
146  
147 < void
148 < lu_delete(tbl, key)             /* delete a table entry */
149 < register LUTAB  *tbl;
150 < char    *key;
147 > extern void
148 > lu_delete(              /* delete a table entry */
149 >        register LUTAB  *tbl,
150 >        const char      *key
151 > )
152   {
153          register LUENT  *le;
154  
# Line 160 | Line 163 | char   *key;
163   }
164  
165  
166 < int
167 < lu_doall(tbl, f)                /* loop through all valid table entries */
168 < register LUTAB  *tbl;
169 < int     (*f)();
166 > extern int
167 > lu_doall(               /* loop through all valid table entries */
168 >        register const LUTAB    *tbl,
169 >        //int   (*f)(const LUENT *)
170 >        lut_doallf_t *f,
171 >        void *p
172 > )
173   {
174          int     rval = 0;
175 <        register LUENT  *tp;
175 >        register const LUENT    *tp;
176  
177          for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; )
178 <                if (tp->data != NULL)
179 <                        if (f != NULL)
180 <                                rval += (*f)(tp);
181 <                        else
178 >                if (tp->data != NULL) {
179 >                        if (f != NULL) {
180 >                                int     r = (*f)(tp, p);
181 >                                if (r < 0)
182 >                                        return(-1);
183 >                                rval += r;
184 >                        } else
185                                  rval++;
186 +                }
187          return(rval);
188   }
189  
190  
191 < void
192 < lu_done(tbl)                    /* free table and contents */
193 < register LUTAB  *tbl;
191 > extern void
192 > lu_done(                        /* free table and contents */
193 >        register LUTAB  *tbl
194 > )
195   {
196          register LUENT  *tp;
197  
# Line 193 | Line 204 | register LUTAB *tbl;
204                          if (tp->data != NULL && tbl->freed != NULL)
205                                  (*tbl->freed)(tp->data);
206                  }
207 <        free((MEM_PTR)tbl->tabl);
207 >        free((void *)tbl->tabl);
208          tbl->tabl = NULL;
209          tbl->tsiz = 0;
210          tbl->ndel = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines