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