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.2 by greg, Tue Apr 11 13:33:37 1995 UTC vs.
Revision 2.3 by gregl, Wed Dec 3 10:59:30 1997 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines