1 |
/* RCSid: $Id: lookup.h,v 1.10 2003/02/28 20:11:29 greg Exp $ */ |
2 |
/* |
3 |
* Header file for general associative table lookup routines |
4 |
*/ |
5 |
#ifndef _MGF_LOOKUP_H_ |
6 |
#define _MGF_LOOKUP_H_ |
7 |
#ifdef __cplusplus |
8 |
extern "C" { |
9 |
#endif |
10 |
|
11 |
|
12 |
typedef struct { |
13 |
char *key; /* key name */ |
14 |
long hval; /* key hash value (for efficiency) */ |
15 |
char *data; /* pointer to client data */ |
16 |
} LUENT; |
17 |
|
18 |
typedef struct { |
19 |
unsigned long (*hashf)(char *); /* key hash function */ |
20 |
int (*keycmp)(const char *, const char *); /* key comparison function */ |
21 |
void (*freek)(char *); /* free a key */ |
22 |
void (*freed)(char *); /* free the data */ |
23 |
int tsiz; /* current table size */ |
24 |
LUENT *tabl; /* table, if allocated */ |
25 |
int ndel; /* number of deleted entries */ |
26 |
} LUTAB; |
27 |
|
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_done routine calls the given free function once for each |
66 |
* assigned table entry (i.e. each entry with an assigned key value). |
67 |
* The user must define these routines to free the key and the data |
68 |
* in the LU_TAB structure. The final action of lu_done is to free the |
69 |
* allocated table itself. |
70 |
*/ |
71 |
|
72 |
extern int lu_init(LUTAB *, int); |
73 |
extern LUENT *lu_find(LUTAB *, char *); |
74 |
extern void lu_delete(LUTAB *, char *); |
75 |
extern void lu_done(LUTAB *); |
76 |
extern unsigned long lu_shash(char *); |
77 |
|
78 |
|
79 |
#ifdef __cplusplus |
80 |
} |
81 |
#endif |
82 |
#endif /* _MGF_LOOKUP_H_ */ |
83 |
|