| 1 | schorsch | 2.11 | /* RCSid $Id: lookup.h,v 2.10 2003/07/14 22:23:59 schorsch Exp $ */ | 
| 2 | greg | 2.1 | /* | 
| 3 |  |  | * Header file for general associative table lookup routines | 
| 4 |  |  | */ | 
| 5 | schorsch | 2.8 | #ifndef _RAD_LOOKUP_H_ | 
| 6 |  |  | #define _RAD_LOOKUP_H_ | 
| 7 | schorsch | 2.10 |  | 
| 8 |  |  | #include <string.h> /* strcmp() */ | 
| 9 |  |  |  | 
| 10 | schorsch | 2.8 | #ifdef __cplusplus | 
| 11 |  |  | extern "C" { | 
| 12 |  |  | #endif | 
| 13 | greg | 2.6 |  | 
| 14 | schorsch | 2.11 | typedef void lut_free_t(void*); | 
| 15 |  |  |  | 
| 16 | greg | 2.1 | typedef struct { | 
| 17 | gwlarson | 2.5 | char    *key;                   /* key name */ | 
| 18 |  |  | unsigned long   hval;           /* key hash value (for efficiency) */ | 
| 19 |  |  | char    *data;                  /* pointer to client data */ | 
| 20 | greg | 2.1 | } LUENT; | 
| 21 |  |  |  | 
| 22 |  |  | typedef struct { | 
| 23 | schorsch | 2.11 | unsigned long (*hashf)(char*);  /* key hash function */ | 
| 24 |  |  | int (*keycmp)(const char*, const char*);        /* key comparison function */ | 
| 25 |  |  | lut_free_t *freek;              /* free a key */ | 
| 26 |  |  | lut_free_t *freed;              /* free the data */ | 
| 27 | gwlarson | 2.5 | int     tsiz;                   /* current table size */ | 
| 28 |  |  | LUENT   *tabl;                  /* table, if allocated */ | 
| 29 |  |  | int     ndel;                   /* number of deleted entries */ | 
| 30 | greg | 2.1 | } LUTAB; | 
| 31 |  |  |  | 
| 32 | gregl | 2.3 | #undef strcmp | 
| 33 | schorsch | 2.11 | #define LU_SINIT(fk,fd)         {lu_shash,strcmp,fk,\ | 
| 34 |  |  | fd,0,NULL,0} | 
| 35 | greg | 2.1 |  | 
| 36 |  |  | /* | 
| 37 |  |  | * The lu_init routine is called to initialize a table.  The number of | 
| 38 |  |  | * elements passed is not a limiting factor, as a table can grow to | 
| 39 |  |  | * any size permitted by memory.  However, access will be more efficient | 
| 40 |  |  | * if this number strikes a reasonable balance between default memory use | 
| 41 |  |  | * and the expected (minimum) table size.  The value returned is the | 
| 42 |  |  | * actual allocated table size (or zero if there was insufficient memory). | 
| 43 |  |  | * | 
| 44 |  |  | * The hashf, keycmp, freek and freed member functions must be assigned | 
| 45 |  |  | * separately.  If the hash value is sufficient to guarantee equality | 
| 46 |  |  | * between keys, then the keycmp pointer may be NULL.  Otherwise, it | 
| 47 |  |  | * should return 0 if the two passed keys match.  If it is not necessary | 
| 48 |  |  | * (or possible) to free the key and/or data values, then the freek and/or | 
| 49 |  |  | * freed member functions may be NULL. | 
| 50 |  |  | * | 
| 51 |  |  | * It isn't fully necessary to call lu_init to initialize the LUTAB structure. | 
| 52 |  |  | * If tsiz is 0, then the first call to lu_find will allocate a minimal table. | 
| 53 |  |  | * The LU_SINIT macro provides a convenient static declaration for character | 
| 54 |  |  | * string keys. | 
| 55 |  |  | * | 
| 56 |  |  | * The lu_find routine returns the entry corresponding to the given | 
| 57 |  |  | * key.  If the entry does not exist, the corresponding key field will | 
| 58 |  |  | * be NULL.  If the entry has been previously deleted but not yet freed, | 
| 59 |  |  | * then only the data field will be NULL.  It is the caller's | 
| 60 |  |  | * responsibility to (allocate and) assign the key and data fields when | 
| 61 |  |  | * creating a new entry.  The only case where lu_find returns NULL is when | 
| 62 |  |  | * the system has run out of memory. | 
| 63 |  |  | * | 
| 64 |  |  | * The lu_delete routine frees an entry's data (if any) by calling | 
| 65 |  |  | * the freed member function, but does not free the key field.  This | 
| 66 |  |  | * will be freed later during (or instead of) table reallocation. | 
| 67 | greg | 2.2 | * It is therefore an error to reuse or do anything with the key | 
| 68 |  |  | * field after calling lu_delete. | 
| 69 | greg | 2.1 | * | 
| 70 | gwlarson | 2.4 | * The lu_doall routine loops through every filled table entry, calling | 
| 71 |  |  | * the given function once on each entry.  If a NULL pointer is passed | 
| 72 |  |  | * for this function, then lu_doall simply returns the total number of | 
| 73 |  |  | * active entries.  Otherwise, it returns the sum of all the function | 
| 74 |  |  | * evaluations. | 
| 75 |  |  | * | 
| 76 | greg | 2.1 | * The lu_done routine calls the given free function once for each | 
| 77 |  |  | * assigned table entry (i.e. each entry with an assigned key value). | 
| 78 |  |  | * The user must define these routines to free the key and the data | 
| 79 |  |  | * in the LU_TAB structure.  The final action of lu_done is to free the | 
| 80 |  |  | * allocated table itself. | 
| 81 |  |  | */ | 
| 82 | greg | 2.6 |  | 
| 83 |  |  | extern int      lu_init(LUTAB *tbl, int nel); | 
| 84 |  |  | extern unsigned long    lu_shash(char *s); | 
| 85 |  |  | extern LUENT    *lu_find(LUTAB *tbl, char *key); | 
| 86 |  |  | extern void     lu_delete(LUTAB *tbl, char *key); | 
| 87 | schorsch | 2.11 | extern int      lu_doall(LUTAB *tbl, int (*f)(LUENT*)); | 
| 88 | greg | 2.6 | extern void     lu_done(LUTAB *tbl); | 
| 89 |  |  |  | 
| 90 | schorsch | 2.8 |  | 
| 91 |  |  | #ifdef __cplusplus | 
| 92 |  |  | } | 
| 93 | greg | 2.6 | #endif | 
| 94 | schorsch | 2.8 | #endif /* _RAD_LOOKUP_H_ */ | 
| 95 |  |  |  |