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.5 by gwlarson, Thu Sep 3 14:22:23 1998 UTC vs.
Revision 2.18 by greg, Fri Feb 18 00:40:25 2011 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1998 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 + #include <string.h>
11 +
12   #include "lookup.h"
13  
14 #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
14  
20 #define MEM_PTR         char *
21
22 extern MEM_PTR  calloc();
23
24
15   int
16 < lu_init(tbl, nel)               /* initialize tbl for at least nel elements */
17 < register LUTAB  *tbl;
18 < int     nel;
16 > lu_init(                /* initialize tbl for at least nel elements */
17 >        register LUTAB  *tbl,
18 >        int     nel
19 > )
20   {
21          static int  hsiztab[] = {
22                  31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381,
# Line 49 | Line 40 | int    nel;
40  
41  
42   unsigned long
43 < lu_shash(s)                     /* hash a nul-terminated string */
44 < char    *s;
43 > lu_shash(                       /* hash a nul-terminated string */
44 >        const char      *s
45 > )
46   {
47          static unsigned char shuffle[256] = {
48                  0, 157, 58, 215, 116, 17, 174, 75, 232, 133, 34,
# Line 80 | Line 72 | char   *s;
72          };
73          register int    i = 0;
74          register unsigned long  h = 0;
75 <        register unsigned char *t = (unsigned char *)s;
75 >        register unsigned const char *t = (unsigned const char *)s;
76  
77          while (*t)
78                  h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf);
# Line 90 | Line 82 | char   *s;
82  
83  
84   LUENT *
85 < lu_find(tbl, key)               /* find a table entry */
86 < register LUTAB  *tbl;
87 < char    *key;
85 > lu_find(                /* find a table entry */
86 >        register LUTAB  *tbl,
87 >        const char      *key
88 > )
89   {
90          unsigned long   hval;
91          int     i, n;
# Line 133 | Line 126 | tryagain:
126           * recursive call to lu_find().
127           */
128          while (ndx--)
129 <                if (le[ndx].key != NULL)
129 >                if (le[ndx].key != NULL) {
130                          if (le[ndx].data != NULL)
131 <                                copystruct(lu_find(tbl,le[ndx].key), &le[ndx]);
131 >                                *lu_find(tbl,le[ndx].key) = le[ndx];
132                          else if (tbl->freek != NULL)
133                                  (*tbl->freek)(le[ndx].key);
134 <        free((MEM_PTR)le);
134 >                }
135 >        free((void *)le);
136          goto tryagain;                  /* should happen only once! */
137   }
138  
139  
140   void
141 < lu_delete(tbl, key)             /* delete a table entry */
142 < register LUTAB  *tbl;
143 < char    *key;
141 > lu_delete(              /* delete a table entry */
142 >        register LUTAB  *tbl,
143 >        const char      *key
144 > )
145   {
146          register LUENT  *le;
147  
# Line 162 | Line 157 | char   *key;
157  
158  
159   int
160 < lu_doall(tbl, f)                /* loop through all valid table entries */
161 < register LUTAB  *tbl;
162 < int     (*f)();
160 > lu_doall(               /* loop through all valid table entries */
161 >        register const LUTAB    *tbl,
162 >        /* int  (*f)(const LUENT *) */
163 >        lut_doallf_t *f,
164 >        void *p
165 > )
166   {
167          int     rval = 0;
168 <        register LUENT  *tp;
168 >        register const LUENT    *tp;
169  
170          for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; )
171 <                if (tp->data != NULL)
172 <                        if (f != NULL)
173 <                                rval += (*f)(tp);
174 <                        else
171 >                if (tp->data != NULL) {
172 >                        if (f != NULL) {
173 >                                int     r = (*f)(tp, p);
174 >                                if (r < 0)
175 >                                        return(-1);
176 >                                rval += r;
177 >                        } else
178                                  rval++;
179 +                }
180          return(rval);
181   }
182  
183  
184   void
185 < lu_done(tbl)                    /* free table and contents */
186 < register LUTAB  *tbl;
185 > lu_done(                        /* free table and contents */
186 >        register LUTAB  *tbl
187 > )
188   {
189          register LUENT  *tp;
190  
# Line 194 | Line 197 | register LUTAB *tbl;
197                          if (tp->data != NULL && tbl->freed != NULL)
198                                  (*tbl->freed)(tp->data);
199                  }
200 <        free((MEM_PTR)tbl->tabl);
200 >        free((void *)tbl->tabl);
201          tbl->tabl = NULL;
202          tbl->tsiz = 0;
203          tbl->ndel = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines