1 |
schorsch |
1.11 |
/* RCSid: $Id: lookup.h,v 1.10 2003/02/28 20:11:29 greg Exp $ */ |
2 |
greg |
1.1 |
/* |
3 |
greg |
1.4 |
* Header file for general associative table lookup routines |
4 |
greg |
1.1 |
*/ |
5 |
schorsch |
1.11 |
#ifndef _MGF_LOOKUP_H_ |
6 |
|
|
#define _MGF_LOOKUP_H_ |
7 |
|
|
#ifdef __cplusplus |
8 |
|
|
extern "C" { |
9 |
|
|
#endif |
10 |
|
|
|
11 |
greg |
1.1 |
|
12 |
|
|
typedef struct { |
13 |
greg |
1.4 |
char *key; /* key name */ |
14 |
|
|
long hval; /* key hash value (for efficiency) */ |
15 |
greg |
1.1 |
char *data; /* pointer to client data */ |
16 |
|
|
} LUENT; |
17 |
|
|
|
18 |
greg |
1.6 |
typedef struct { |
19 |
gwlarson |
1.9 |
unsigned long (*hashf)(char *); /* key hash function */ |
20 |
greg |
1.6 |
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 |
greg |
1.1 |
|
28 |
greg |
1.4 |
#define LU_SINIT(fk,fd) {lu_shash,strcmp,(void (*)())(fk),\ |
29 |
|
|
(void (*)())(fd),0,NULL,0} |
30 |
greg |
1.2 |
|
31 |
greg |
1.1 |
/* |
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 |
greg |
1.4 |
* 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 |
greg |
1.1 |
* |
46 |
greg |
1.4 |
* 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 |
greg |
1.1 |
* The lu_find routine returns the entry corresponding to the given |
52 |
greg |
1.4 |
* 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 |
greg |
1.1 |
* |
59 |
greg |
1.2 |
* 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 |
greg |
1.5 |
* It is therefore an error to reuse or do anything with the key |
63 |
|
|
* field after calling lu_delete. |
64 |
greg |
1.2 |
* |
65 |
greg |
1.1 |
* 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 |
greg |
1.3 |
* The user must define these routines to free the key and the data |
68 |
greg |
1.2 |
* in the LU_TAB structure. The final action of lu_done is to free the |
69 |
greg |
1.1 |
* allocated table itself. |
70 |
|
|
*/ |
71 |
|
|
|
72 |
|
|
extern int lu_init(LUTAB *, int); |
73 |
|
|
extern LUENT *lu_find(LUTAB *, char *); |
74 |
greg |
1.2 |
extern void lu_delete(LUTAB *, char *); |
75 |
|
|
extern void lu_done(LUTAB *); |
76 |
gwlarson |
1.9 |
extern unsigned long lu_shash(char *); |
77 |
schorsch |
1.11 |
|
78 |
|
|
|
79 |
|
|
#ifdef __cplusplus |
80 |
|
|
} |
81 |
greg |
1.1 |
#endif |
82 |
schorsch |
1.11 |
#endif /* _MGF_LOOKUP_H_ */ |
83 |
|
|
|