--- ray/src/common/lookup.c 2003/02/25 02:47:21 2.7 +++ ray/src/common/lookup.c 2014/02/09 00:08:18 2.19 @@ -1,34 +1,29 @@ #ifndef lint -static const char RCSid[] = "$Id: lookup.c,v 2.7 2003/02/25 02:47:21 greg Exp $"; +static const char RCSid[] = "$Id: lookup.c,v 2.19 2014/02/09 00:08:18 greg Exp $"; #endif /* * Table lookup routines */ -#include "copyright.h" - #include #include +#include + #include "lookup.h" -#ifdef NOSTRUCTASS -#define copystruct(d,s) bcopy((char *)(s),(char *)(d),sizeof(*(d))) -#else -#define copystruct(d,s) (*(d) = *(s)) -#endif - int -lu_init(tbl, nel) /* initialize tbl for at least nel elements */ -register LUTAB *tbl; -int nel; +lu_init( /* initialize tbl for at least nel elements */ + LUTAB *tbl, + int nel +) { static int hsiztab[] = { 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262139, 524287, 1048573, 2097143, 4194301, 8388593, 0 }; - register int *hsp; + int *hsp; nel += nel>>1; /* 66% occupancy */ for (hsp = hsiztab; *hsp; hsp++) @@ -45,8 +40,9 @@ int nel; unsigned long -lu_shash(s) /* hash a nul-terminated string */ -char *s; +lu_shash( /* hash a nul-terminated string */ + const char *s +) { static unsigned char shuffle[256] = { 0, 157, 58, 215, 116, 17, 174, 75, 232, 133, 34, @@ -74,9 +70,9 @@ char *s; 106, 7, 164, 65, 222, 123, 24, 181, 82, 239, 140, 41, 198, 99 }; - register int i = 0; - register unsigned long h = 0; - register unsigned char *t = (unsigned char *)s; + int i = 0; + unsigned long h = 0; + unsigned const char *t = (unsigned const char *)s; while (*t) h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf); @@ -86,14 +82,15 @@ char *s; LUENT * -lu_find(tbl, key) /* find a table entry */ -register LUTAB *tbl; -char *key; +lu_find( /* find a table entry */ + LUTAB *tbl, + const char *key +) { unsigned long hval; int i, n; - register int ndx; - register LUENT *le; + int ndx; + LUENT *le; /* look up object */ if (tbl->tsiz == 0 && !lu_init(tbl, 1)) return(NULL); @@ -129,22 +126,24 @@ tryagain: * recursive call to lu_find(). */ while (ndx--) - if (le[ndx].key != NULL) + if (le[ndx].key != NULL) { if (le[ndx].data != NULL) - copystruct(lu_find(tbl,le[ndx].key), &le[ndx]); + *lu_find(tbl,le[ndx].key) = le[ndx]; else if (tbl->freek != NULL) (*tbl->freek)(le[ndx].key); + } free((void *)le); goto tryagain; /* should happen only once! */ } void -lu_delete(tbl, key) /* delete a table entry */ -register LUTAB *tbl; -char *key; +lu_delete( /* delete a table entry */ + LUTAB *tbl, + const char *key +) { - register LUENT *le; + LUENT *le; if ((le = lu_find(tbl, key)) == NULL) return; @@ -158,28 +157,36 @@ char *key; int -lu_doall(tbl, f) /* loop through all valid table entries */ -register LUTAB *tbl; -int (*f)(); +lu_doall( /* loop through all valid table entries */ + const LUTAB *tbl, + /* int (*f)(const LUENT *, void *) */ + lut_doallf_t *f, + void *p +) { int rval = 0; - register LUENT *tp; + const LUENT *tp; for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; ) - if (tp->data != NULL) - if (f != NULL) - rval += (*f)(tp); - else + if (tp->data != NULL) { + if (f != NULL) { + int r = (*f)(tp, p); + if (r < 0) + return(-1); + rval += r; + } else rval++; + } return(rval); } void -lu_done(tbl) /* free table and contents */ -register LUTAB *tbl; +lu_done( /* free table and contents */ + LUTAB *tbl +) { - register LUENT *tp; + LUENT *tp; if (!tbl->tsiz) return;