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