willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | This File is copied from |
| 3 | |
| 4 | http://www.oreilly.com/catalog/masteralgoc/index.html |
| 5 | Mastering Algorithms with C |
| 6 | By Kyle Loudon |
| 7 | ISBN: 1-56592-453-3 |
| 8 | Publishd by O'Reilly |
| 9 | |
| 10 | */ |
| 11 | |
| 12 | /***************************************************************************** |
| 13 | * * |
| 14 | * ------------------------------- chtbl.c -------------------------------- * |
| 15 | * * |
| 16 | *****************************************************************************/ |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame^] | 21 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #include <common/list.h> |
| 23 | #include <common/chtbl.h> |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 24 | |
| 25 | /***************************************************************************** |
| 26 | * * |
| 27 | * ------------------------------ chtbl_init ------------------------------ * |
| 28 | * * |
| 29 | *****************************************************************************/ |
| 30 | |
| 31 | int chtbl_init(CHTbl *htbl, int buckets, int (*h)(const void *key), int |
| 32 | (*match)(const void *key1, const void *key2), void (*destroy)(void*data)) { |
| 33 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | int i; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 35 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | /***************************************************************************** |
| 37 | * * |
| 38 | * Allocate space for the hash table. * |
| 39 | * * |
| 40 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 41 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | if ((htbl->table = (List *)malloc(buckets * sizeof(List))) == NULL) |
| 43 | return -1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 44 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | /***************************************************************************** |
| 46 | * * |
| 47 | * Initialize the buckets. * |
| 48 | * * |
| 49 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 50 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | htbl->buckets = buckets; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 52 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | for (i = 0; i < htbl->buckets; i++) |
| 54 | list_init(&htbl->table[i], destroy); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 55 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | /***************************************************************************** |
| 57 | * * |
| 58 | * Encapsulate the functions. * |
| 59 | * * |
| 60 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 61 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | htbl->h = h; |
| 63 | htbl->match = match; |
| 64 | htbl->destroy = destroy; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 65 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | /***************************************************************************** |
| 67 | * * |
| 68 | * Initialize the number of elements in the table. * |
| 69 | * * |
| 70 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 71 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | htbl->size = 0; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 73 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | return 0; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 75 | } /* end chtbl_init () */ |
| 76 | |
| 77 | /***************************************************************************** |
| 78 | * * |
| 79 | * ---------------------------- chtbl_destroy ----------------------------- * |
| 80 | * * |
| 81 | *****************************************************************************/ |
| 82 | |
| 83 | void chtbl_destroy(CHTbl *htbl) { |
| 84 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 85 | int i; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 86 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | /***************************************************************************** |
| 88 | * * |
| 89 | * Destroy each bucket. * |
| 90 | * * |
| 91 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 92 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | for (i = 0; i < htbl->buckets; i++) { |
| 94 | list_destroy(&htbl->table[i]); |
| 95 | } /* end for () */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 96 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | /***************************************************************************** |
| 98 | * * |
| 99 | * Free the storage allocated for the hash table. * |
| 100 | * * |
| 101 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 102 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | free(htbl->table); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 104 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | /***************************************************************************** |
| 106 | * * |
| 107 | * No operations are allowed now, but clear the structure as a precaution. * |
| 108 | * * |
| 109 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 110 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | memset(htbl, 0, sizeof(CHTbl)); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 112 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | return; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 114 | } /* end chtbl_destroy() */ |
| 115 | |
| 116 | /***************************************************************************** |
| 117 | * * |
| 118 | * ----------------------------- chtbl_insert ----------------------------- * |
| 119 | * * |
| 120 | *****************************************************************************/ |
| 121 | |
| 122 | int chtbl_insert(CHTbl *htbl, const void *data) { |
| 123 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | void *temp; |
| 125 | int bucket,retval; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 126 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 127 | /***************************************************************************** |
| 128 | * * |
| 129 | * Do nothing if the data is already in the table. * |
| 130 | * * |
| 131 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 132 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 133 | temp = (void *)data; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 134 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | if (chtbl_lookup(htbl, &temp) == 0) |
| 136 | return 1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 137 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 138 | /***************************************************************************** |
| 139 | * * |
| 140 | * Hash the key. * |
| 141 | * * |
| 142 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 143 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 144 | bucket = htbl->h(data) % htbl->buckets; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 145 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 146 | /***************************************************************************** |
| 147 | * * |
| 148 | * Insert the data into the bucket. * |
| 149 | * * |
| 150 | *****************************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 151 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 152 | if ((retval = list_ins_next(&htbl->table[bucket], NULL, data)) == 0) |
| 153 | htbl->size++; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 154 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 155 | return retval; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 156 | } /* end chtbl_insert() */ |
| 157 | |
| 158 | /***************************************************************************** |
| 159 | * * |
| 160 | * ----------------------------- chtbl_remove ----------------------------- * |
| 161 | * * |
| 162 | *****************************************************************************/ |
| 163 | |
| 164 | int chtbl_remove(CHTbl *htbl, void **data) { |
| 165 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 166 | ListElmt *element, *prev; |
| 167 | int bucket; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 168 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | /********************************************************************* |
| 170 | * * |
| 171 | * Hash the key. * |
| 172 | * * |
| 173 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 174 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | bucket = htbl->h(*data) % htbl->buckets; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 176 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 177 | /********************************************************************* |
| 178 | * * |
| 179 | * Search for the data in the bucket. * |
| 180 | * * |
| 181 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 182 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 183 | prev = NULL; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 184 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 185 | for (element = list_head(&htbl->table[bucket]); |
| 186 | element != NULL; element = list_next(element)) { |
| 187 | if (htbl->match(*data, list_data(element))) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 188 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 189 | /***************************************************** |
| 190 | * * |
| 191 | * Remove the data from the bucket. * |
| 192 | * * |
| 193 | *****************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 194 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 195 | if (list_rem_next(&htbl->table[bucket], prev, data) == 0) { |
| 196 | htbl->size--; |
| 197 | return 0; |
| 198 | } /* end if() */ |
| 199 | else { |
| 200 | return -1; |
| 201 | }/* end else */ |
| 202 | }/* end if (htbl->match(*data, list_data(element))) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 203 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 204 | prev = element; |
| 205 | }/* end for() */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 206 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 207 | /********************************************************************* |
| 208 | * * |
| 209 | * Return that the data was not found. * |
| 210 | * * |
| 211 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 212 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 213 | return -1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 214 | } /* end int chtbl_remove(CHTbl *htbl, void **data) */ |
| 215 | |
| 216 | /***************************************************************************** |
| 217 | * * |
| 218 | * ----------------------------- chtbl_lookup ----------------------------- * |
| 219 | * * |
| 220 | *****************************************************************************/ |
| 221 | |
| 222 | int chtbl_lookup(const CHTbl *htbl, void **data) { |
| 223 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | ListElmt *element; |
| 225 | int bucket; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 226 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 227 | /********************************************************************* |
| 228 | * * |
| 229 | * Hash the key. * |
| 230 | * * |
| 231 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 232 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 233 | bucket = htbl->h(*data) % htbl->buckets; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 234 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 235 | /********************************************************************* |
| 236 | * * |
| 237 | * Search for the data in the bucket. * |
| 238 | * * |
| 239 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 240 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 241 | for (element = list_head(&htbl->table[bucket]); |
| 242 | element != NULL; element = list_next(element)) { |
| 243 | if (htbl->match(*data, list_data(element))) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 245 | /***************************************************** |
| 246 | * * |
| 247 | * Pass back the data from the table. * |
| 248 | * * |
| 249 | *****************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 250 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 251 | *data = list_data(element); |
| 252 | return 0; |
| 253 | }/* end if() */ |
| 254 | }/* end for() */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 255 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 256 | /********************************************************************* |
| 257 | * * |
| 258 | * Return that the data was not found. * |
| 259 | * * |
| 260 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 261 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 262 | return -1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 263 | } /* end chtbl_lookup() */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * Local variables: |
| 267 | * c-indent-level: 8 |
| 268 | * c-basic-offset: 8 |
| 269 | * End: |
| 270 | */ |