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.6 by greg, Sat Feb 22 02:07:22 2003 UTC vs.
Revision 2.19 by greg, Sun Feb 9 00:08:18 2014 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
5   * Table lookup routines
6   */
7  
8 /* ====================================================================
9 * The Radiance Software License, Version 1.0
10 *
11 * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 * through Lawrence Berkeley National Laboratory.   All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * 1. Redistributions of source code must retain the above copyright
19 *         notice, this list of conditions and the following disclaimer.
20 *
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *       notice, this list of conditions and the following disclaimer in
23 *       the documentation and/or other materials provided with the
24 *       distribution.
25 *
26 * 3. The end-user documentation included with the redistribution,
27 *           if any, must include the following acknowledgment:
28 *             "This product includes Radiance software
29 *                 (http://radsite.lbl.gov/)
30 *                 developed by the Lawrence Berkeley National Laboratory
31 *               (http://www.lbl.gov/)."
32 *       Alternately, this acknowledgment may appear in the software itself,
33 *       if and wherever such third-party acknowledgments normally appear.
34 *
35 * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 *       and "The Regents of the University of California" must
37 *       not be used to endorse or promote products derived from this
38 *       software without prior written permission. For written
39 *       permission, please contact [email protected].
40 *
41 * 5. Products derived from this software may not be called "Radiance",
42 *       nor may "Radiance" appear in their name, without prior written
43 *       permission of Lawrence Berkeley National Laboratory.
44 *
45 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 * ====================================================================
58 *
59 * This software consists of voluntary contributions made by many
60 * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
61 * information on Lawrence Berkeley National Laboratory, please see
62 * <http://www.lbl.gov/>.
63 */
64
8   #include <stdio.h>
9   #include <stdlib.h>
10 + #include <string.h>
11 +
12   #include "lookup.h"
13  
69 #ifdef  NOSTRUCTASS
70 #define  copystruct(d,s)        bcopy((char *)(s),(char *)(d),sizeof(*(d)))
71 #else
72 #define  copystruct(d,s)        (*(d) = *(s))
73 #endif
14  
75
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 >        LUTAB   *tbl,
18 >        int     nel
19 > )
20   {
21          static int  hsiztab[] = {
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;
26 >        int  *hsp;
27  
28          nel += nel>>1;                  /* 66% occupancy */
29          for (hsp = hsiztab; *hsp; hsp++)
# Line 100 | 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 129 | Line 70 | char   *s;
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;
73 >        int                     i = 0;
74 >        unsigned long           h = 0;
75 >        unsigned const char     *t = (unsigned const char *)s;
76  
77          while (*t)
78                  h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf);
# Line 141 | 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 >        LUTAB   *tbl,
87 >        const char      *key
88 > )
89   {
90          unsigned long   hval;
91          int     i, n;
92 <        register int    ndx;
93 <        register LUENT  *le;
92 >        int     ndx;
93 >        LUENT   *le;
94                                          /* look up object */
95          if (tbl->tsiz == 0 && !lu_init(tbl, 1))
96                  return(NULL);
# Line 184 | 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 +                }
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 >        LUTAB *tbl,
143 >        const char *key
144 > )
145   {
146 <        register LUENT  *le;
146 >        LUENT   *le;
147  
148          if ((le = lu_find(tbl, key)) == NULL)
149                  return;
# Line 213 | 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 >        const LUTAB *tbl,
162 >        /* int  (*f)(const LUENT *, void *) */
163 >        lut_doallf_t *f,
164 >        void *p
165 > )
166   {
167          int     rval = 0;
168 <        register LUENT  *tp;
168 >        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 >        LUTAB   *tbl
187 > )
188   {
189 <        register LUENT  *tp;
189 >        LUENT   *tp;
190  
191          if (!tbl->tsiz)
192                  return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines