[8] | 1 | /**CHeaderFile***************************************************************** |
---|
| 2 | |
---|
| 3 | FileName [st.h] |
---|
| 4 | |
---|
| 5 | PackageName [st] |
---|
| 6 | |
---|
| 7 | Synopsis [Symbol table package.] |
---|
| 8 | |
---|
| 9 | Description [The st library provides functions to create, maintain, |
---|
| 10 | and query symbol tables.] |
---|
| 11 | |
---|
| 12 | SeeAlso [] |
---|
| 13 | |
---|
| 14 | Author [] |
---|
| 15 | |
---|
| 16 | Copyright [] |
---|
| 17 | |
---|
| 18 | Revision [$Id: st.h,v 1.4 2005/04/13 05:02:20 fabio Exp $] |
---|
| 19 | |
---|
| 20 | ******************************************************************************/ |
---|
| 21 | |
---|
| 22 | #ifndef ST_INCLUDED |
---|
| 23 | #define ST_INCLUDED |
---|
| 24 | |
---|
| 25 | /*---------------------------------------------------------------------------*/ |
---|
| 26 | /* Nested includes */ |
---|
| 27 | /*---------------------------------------------------------------------------*/ |
---|
| 28 | |
---|
| 29 | #ifdef __cplusplus |
---|
| 30 | extern "C" { |
---|
| 31 | #endif |
---|
| 32 | |
---|
| 33 | /*---------------------------------------------------------------------------*/ |
---|
| 34 | /* Constant declarations */ |
---|
| 35 | /*---------------------------------------------------------------------------*/ |
---|
| 36 | |
---|
| 37 | #define ST_DEFAULT_MAX_DENSITY 5 |
---|
| 38 | #define ST_DEFAULT_INIT_TABLE_SIZE 11 |
---|
| 39 | #define ST_DEFAULT_GROW_FACTOR 2.0 |
---|
| 40 | #define ST_DEFAULT_REORDER_FLAG 0 |
---|
| 41 | #define ST_OUT_OF_MEM -10000 |
---|
| 42 | |
---|
| 43 | /*---------------------------------------------------------------------------*/ |
---|
| 44 | /* Stucture declarations */ |
---|
| 45 | /*---------------------------------------------------------------------------*/ |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | /*---------------------------------------------------------------------------*/ |
---|
| 49 | /* Type declarations */ |
---|
| 50 | /*---------------------------------------------------------------------------*/ |
---|
| 51 | |
---|
| 52 | typedef struct st_table_entry st_table_entry; |
---|
| 53 | struct st_table_entry { |
---|
| 54 | char *key; |
---|
| 55 | char *record; |
---|
| 56 | st_table_entry *next; |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | typedef struct st_table st_table; |
---|
| 60 | struct st_table { |
---|
| 61 | int (*compare)(const char *, const char *); |
---|
| 62 | int (*hash)(char *, int); |
---|
| 63 | int num_bins; |
---|
| 64 | int num_entries; |
---|
| 65 | int max_density; |
---|
| 66 | int reorder_flag; |
---|
| 67 | double grow_factor; |
---|
| 68 | st_table_entry **bins; |
---|
| 69 | }; |
---|
| 70 | |
---|
| 71 | typedef struct st_generator st_generator; |
---|
| 72 | struct st_generator { |
---|
| 73 | st_table *table; |
---|
| 74 | st_table_entry *entry; |
---|
| 75 | int index; |
---|
| 76 | }; |
---|
| 77 | |
---|
| 78 | enum st_retval {ST_CONTINUE, ST_STOP, ST_DELETE}; |
---|
| 79 | |
---|
| 80 | typedef enum st_retval (*ST_PFSR)(char *, char *, char *); |
---|
| 81 | |
---|
| 82 | typedef int (*ST_PFICPCP)(const char *, const char *); /* type for comparison function */ |
---|
| 83 | |
---|
| 84 | typedef int (*ST_PFICPI)(char *, int); /* type for hash function */ |
---|
| 85 | |
---|
| 86 | /*---------------------------------------------------------------------------*/ |
---|
| 87 | /* Variable declarations */ |
---|
| 88 | /*---------------------------------------------------------------------------*/ |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | /*---------------------------------------------------------------------------*/ |
---|
| 92 | /* Macro declarations */ |
---|
| 93 | /*---------------------------------------------------------------------------*/ |
---|
| 94 | |
---|
| 95 | /**Macro*********************************************************************** |
---|
| 96 | |
---|
| 97 | Synopsis [Checks whethere `key' is in `table'.] |
---|
| 98 | |
---|
| 99 | Description [Returns 1 if there is an entry under `key' in `table', 0 |
---|
| 100 | otherwise.] |
---|
| 101 | |
---|
| 102 | SideEffects [None] |
---|
| 103 | |
---|
| 104 | SeeAlso [st_lookup] |
---|
| 105 | |
---|
| 106 | ******************************************************************************/ |
---|
| 107 | #define st_is_member(table,key) st_lookup(table,key,(char **) 0) |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | /**Macro*********************************************************************** |
---|
| 111 | |
---|
| 112 | Synopsis [Returns the number of entries in the table `table'.] |
---|
| 113 | |
---|
| 114 | Description [Returns the number of entries in the table `table'.] |
---|
| 115 | |
---|
| 116 | SideEffects [None] |
---|
| 117 | |
---|
| 118 | SeeAlso [] |
---|
| 119 | |
---|
| 120 | ******************************************************************************/ |
---|
| 121 | #define st_count(table) ((table)->num_entries) |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | /**Macro*********************************************************************** |
---|
| 125 | |
---|
| 126 | Synopsis [Iteration macro.] |
---|
| 127 | |
---|
| 128 | Description [An iteration macro which loops over all the entries in |
---|
| 129 | `table', setting `key' to point to the key and `value' to the |
---|
| 130 | associated value (if it is not nil). `gen' is a generator variable |
---|
| 131 | used internally. Sample usage: |
---|
| 132 | <pre> |
---|
| 133 | char *key, *value; |
---|
| 134 | </pre> |
---|
| 135 | <pre> |
---|
| 136 | st_generator *gen; |
---|
| 137 | </pre> |
---|
| 138 | <pre> |
---|
| 139 | |
---|
| 140 | st_foreach_item(table, gen, &key, &value) { |
---|
| 141 | </pre> |
---|
| 142 | <pre> |
---|
| 143 | process_item(value); |
---|
| 144 | </pre> |
---|
| 145 | <pre> |
---|
| 146 | } |
---|
| 147 | </pre> |
---|
| 148 | ] |
---|
| 149 | |
---|
| 150 | SideEffects [None] |
---|
| 151 | |
---|
| 152 | SeeAlso [st_foreach_item_int st_foreach] |
---|
| 153 | |
---|
| 154 | ******************************************************************************/ |
---|
| 155 | #define st_foreach_item(table, gen, key, value) \ |
---|
| 156 | for(gen=st_init_gen(table); st_gen(gen,key,value) || (st_free_gen(gen),0);) |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | /**Macro*********************************************************************** |
---|
| 160 | |
---|
| 161 | Synopsis [Iteration macro.] |
---|
| 162 | |
---|
| 163 | Description [An iteration macro which loops over all the entries in |
---|
| 164 | `table', setting `key' to point to the key and `value' to the |
---|
| 165 | associated value (if it is not nil). `value' is assumed to be a |
---|
| 166 | pointer to an integer. `gen' is a generator variable used |
---|
| 167 | internally. Sample usage: |
---|
| 168 | <pre> |
---|
| 169 | char *key; |
---|
| 170 | </pre> |
---|
| 171 | <pre> |
---|
| 172 | int value; |
---|
| 173 | </pre> |
---|
| 174 | <pre> |
---|
| 175 | st_generator *gen; |
---|
| 176 | </pre> |
---|
| 177 | <pre> |
---|
| 178 | |
---|
| 179 | st_foreach_item_int(table, gen, &key, &value) { |
---|
| 180 | </pre> |
---|
| 181 | <pre> |
---|
| 182 | process_item(value); |
---|
| 183 | </pre> |
---|
| 184 | <pre> |
---|
| 185 | } |
---|
| 186 | </pre> |
---|
| 187 | ] |
---|
| 188 | |
---|
| 189 | SideEffects [None] |
---|
| 190 | |
---|
| 191 | SeeAlso [st_foreach_item st_foreach] |
---|
| 192 | |
---|
| 193 | ******************************************************************************/ |
---|
| 194 | #define st_foreach_item_int(table, gen, key, value) \ |
---|
| 195 | for(gen=st_init_gen(table); st_gen_int(gen,key,value) || (st_free_gen(gen),0);) |
---|
| 196 | |
---|
| 197 | /**AutomaticStart*************************************************************/ |
---|
| 198 | |
---|
| 199 | /*---------------------------------------------------------------------------*/ |
---|
| 200 | /* Function prototypes */ |
---|
| 201 | /*---------------------------------------------------------------------------*/ |
---|
| 202 | |
---|
| 203 | extern st_table *st_init_table_with_params (ST_PFICPCP, ST_PFICPI, int, int, double, int); |
---|
| 204 | extern st_table *st_init_table (ST_PFICPCP, ST_PFICPI); |
---|
| 205 | extern void st_free_table (st_table *); |
---|
| 206 | extern int st_lookup (st_table *, void *, void *); |
---|
| 207 | extern int st_lookup_int (st_table *, void *, int *); |
---|
| 208 | extern int st_insert (st_table *, void *, void *); |
---|
| 209 | extern int st_add_direct (st_table *, void *, void *); |
---|
| 210 | extern int st_find_or_add (st_table *, void *, void *); |
---|
| 211 | extern int st_find (st_table *, void *, void *); |
---|
| 212 | extern st_table *st_copy (st_table *); |
---|
| 213 | extern int st_delete (st_table *, void *, void *); |
---|
| 214 | extern int st_delete_int (st_table *, void *, int *); |
---|
| 215 | extern int st_foreach (st_table *, ST_PFSR, char *); |
---|
| 216 | extern int st_strhash (char *, int); |
---|
| 217 | extern int st_numhash (char *, int); |
---|
| 218 | extern int st_ptrhash (char *, int); |
---|
| 219 | extern int st_numcmp (const char *, const char *); |
---|
| 220 | extern int st_ptrcmp (const char *, const char *); |
---|
| 221 | extern st_generator *st_init_gen (st_table *); |
---|
| 222 | extern int st_gen (st_generator *, void *, void *); |
---|
| 223 | extern int st_gen_int (st_generator *, void *, int *); |
---|
| 224 | extern void st_free_gen (st_generator *); |
---|
| 225 | |
---|
| 226 | /**AutomaticEnd***************************************************************/ |
---|
| 227 | |
---|
| 228 | #ifdef __cplusplus |
---|
| 229 | } /* end of extern "C" */ |
---|
| 230 | #endif |
---|
| 231 | |
---|
| 232 | #endif /* ST_INCLUDED */ |
---|