--- ray/src/common/lookup.h 1994/07/06 15:14:16 2.1 +++ ray/src/common/lookup.h 2004/03/28 20:33:12 2.12 @@ -1,29 +1,36 @@ -/* Copyright (c) 1994 Regents of the University of California */ - -/* SCCSid "$SunId$ LBL" */ - +/* RCSid $Id: lookup.h,v 2.12 2004/03/28 20:33:12 schorsch Exp $ */ /* * Header file for general associative table lookup routines */ +#ifndef _RAD_LOOKUP_H_ +#define _RAD_LOOKUP_H_ +#include /* strcmp() */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void lut_free_t(void *p); +typedef unsigned long lut_hashf_t(void*); +typedef int lut_keycmpf_t(const void*, const void*); + typedef struct { - char *key; /* key name */ - long hval; /* key hash value (for efficiency) */ - char *data; /* pointer to client data */ + char *key; /* key name */ + unsigned long hval; /* key hash value (for efficiency) */ + char *data; /* pointer to client data */ } LUENT; typedef struct { - long (*hashf)(); /* key hash function */ - int (*keycmp)(); /* key comparison function */ - void (*freek)(); /* free a key */ - void (*freed)(); /* free the data */ - int tsiz; /* current table size */ - LUENT *tabl; /* table, if allocated */ - int ndel; /* number of deleted entries */ + lut_hashf_t *hashf; /* key hash function */ + lut_keycmpf_t *keycmp; /* key comparison function */ + lut_free_t *freek; /* free a key */ + lut_free_t *freed; /* free the data */ + int tsiz; /* current table size */ + LUENT *tabl; /* table, if allocated */ + int ndel; /* number of deleted entries */ } LUTAB; -#define LU_SINIT(fk,fd) {lu_shash,strcmp,(void (*)())(fk),\ - (void (*)())(fd),0,NULL,0} /* * The lu_init routine is called to initialize a table. The number of @@ -56,7 +63,15 @@ typedef struct { * The lu_delete routine frees an entry's data (if any) by calling * the freed member function, but does not free the key field. This * will be freed later during (or instead of) table reallocation. + * It is therefore an error to reuse or do anything with the key + * field after calling lu_delete. * + * The lu_doall routine loops through every filled table entry, calling + * the given function once on each entry. If a NULL pointer is passed + * for this function, then lu_doall simply returns the total number of + * active entries. Otherwise, it returns the sum of all the function + * evaluations. + * * The lu_done routine calls the given free function once for each * assigned table entry (i.e. each entry with an assigned key value). * The user must define these routines to free the key and the data @@ -64,10 +79,20 @@ typedef struct { * allocated table itself. */ -extern int lu_init(); -extern LUENT *lu_find(); -extern void lu_delete(); -extern void lu_done(); -extern long lu_shash(); +typedef int lut_doallf_t(LUENT *p); -extern int strcmp(); +extern lut_keycmpf_t lu_strcmp; +extern int lu_init(LUTAB *tbl, int nel); +extern unsigned long lu_shash(void *s); +extern LUENT *lu_find(LUTAB *tbl, char *key); +extern void lu_delete(LUTAB *tbl, char *key); +extern int lu_doall(LUTAB *tbl, lut_doallf_t *f); +extern void lu_done(LUTAB *tbl); + +#define LU_SINIT(fk,fd) {lu_shash,lu_strcmp,fk,fd,0,NULL,0} + +#ifdef __cplusplus +} +#endif +#endif /* _RAD_LOOKUP_H_ */ +