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