| 1 | /* |
| 2 | Authors: |
| 3 | John Dennis <jdennis.redhat.com> |
| 4 | Esmond Pitt <[email protected]> |
| 5 | |
| 6 | This implementation was based on a 3/7/1989 public domain submission |
| 7 | to comp.sources.misc by Esmond Pitt <[email protected]>. |
| 8 | |
| 9 | Copyright (C) 2009 Red Hat |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify |
| 12 | it under the terms of the GNU Lesser General Public License as published by |
| 13 | the Free Software Foundation; either version 3 of the License, or |
| 14 | (at your option) any later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU Lesser General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU Lesser General Public License |
| 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | */ |
| 24 | |
| 25 | #ifndef DHASH_H |
| 26 | #define DHASH_H |
| 27 | |
| 28 | /*****************************************************************************/ |
| 29 | /******************************** Documentation ******************************/ |
| 30 | /*****************************************************************************/ |
| 31 | |
| 32 | #if 0 |
| 33 | |
| 34 | Dynamic hash table implementation based on article in CACM April 1988 pp. |
| 35 | 446-457, by Per-Ake Larson. |
| 36 | |
| 37 | This implementation was based on a 3/7/1989 submission to comp.sources.misc by |
| 38 | Esmond Pitt (ejp@ausmelb.oz.AU) that emulated the hsearch(3) interface. The interface |
| 39 | was reworked to be much more flexible and significant new functionality was |
| 40 | added by John Dennis (jdennis@sharpeye.com). |
| 41 | |
| 42 | A hash table maintains a set of <key,value> pairs. Lookups are performed by |
| 43 | locating the key in the table and returning its value. |
| 44 | |
| 45 | A dynamic hash table keeps the number of hash collisions constant |
| 46 | independent of the number of entries in the hash table. |
| 47 | |
| 48 | Both keys and values may be of different types. Two different key types are |
| 49 | supported, strings and unsigned longs. If the key type is a string the hash |
| 50 | library will automatically allocate memory to hold the hash key string and |
| 51 | will automatically free the memory for the key string when the hash entry |
| 52 | is destroyed. Items in the hash table only match when their key types match |
| 53 | AND the keys themselves match. For example if there were two hash entries, |
| 54 | one whose key type was an unsigned long equal to 1 and one whose key type was |
| 55 | a string equal to "1" they would not match, these are considered two |
| 56 | distinct entries. |
| 57 | |
| 58 | The value of the key may be a undefined, pointer, an int, an unsigned int, a |
| 59 | long, an unsigned long, a float, or a double. The hash library does nothing |
| 60 | with user pointers (value.type == HASH_VALUE_PTR). Its the user responsibility |
| 61 | to free items pointed to by these pointers when a hash entry is deleted or the |
| 62 | hash table is destroyed (see hash_delete_callback and/or hash_destroy). |
| 63 | |
| 64 | See dhash_example.c for an illustration of how one might use the API. It does not |
| 65 | represent complete API coverage nor the optimal way to do things in all cases, |
| 66 | it is just a general example. |
| 67 | |
| 68 | Copyright 2009-1010 John Dennis |
| 69 | This program is distributed under the terms of the GNU Lesser General Public |
| 70 | License (LGPL). See the included COPYING and COPYING.lesser files for the |
| 71 | terms of this license. |
| 72 | |
| 73 | #endif |
| 74 | |
| 75 | /*****************************************************************************/ |
| 76 | /******************************* Include Files *******************************/ |
| 77 | /*****************************************************************************/ |
| 78 | |
| 79 | #include <stdbool.h> |
| 80 | #include <stddef.h> |
| 81 | |
| 82 | /*****************************************************************************/ |
| 83 | /*********************************** Defines *********************************/ |
| 84 | /*****************************************************************************/ |
| 85 | |
| 86 | #if 1 |
| 87 | #define HASH_STATISTICS |
| 88 | #endif |
| 89 | |
| 90 | #define HASH_DEFAULT_DIRECTORY_BITS 5 |
| 91 | #define HASH_DEFAULT_SEGMENT_BITS 5 |
| 92 | #define HASH_DEFAULT_MIN_LOAD_FACTOR 1 |
| 93 | #define HASH_DEFAULT_MAX_LOAD_FACTOR 5 |
| 94 | |
| 95 | #define HASH_ERROR_BASE -2000 |
| 96 | #define HASH_ERROR_LIMIT (HASH_ERROR_BASE+20) |
| 97 | #define IS_HASH_ERROR(error) (((error) >= HASH_ERROR_BASE) && ((error) < HASH_ERROR_LIMIT)) |
| 98 | |
| 99 | #define HASH_SUCCESS 0 |
| 100 | #define HASH_ERROR_BAD_KEY_TYPE (HASH_ERROR_BASE + 1) |
| 101 | #define HASH_ERROR_BAD_VALUE_TYPE (HASH_ERROR_BASE + 2) |
| 102 | #define HASH_ERROR_NO_MEMORY (HASH_ERROR_BASE + 3) |
| 103 | #define HASH_ERROR_KEY_NOT_FOUND (HASH_ERROR_BASE + 4) |
| 104 | #define HASH_ERROR_BAD_TABLE (HASH_ERROR_BASE + 5) |
| 105 | |
| 106 | /*****************************************************************************/ |
| 107 | /******************************* Type Definitions ****************************/ |
| 108 | /*****************************************************************************/ |
| 109 | |
| 110 | struct hash_table_str; |
| 111 | typedef struct hash_table_str hash_table_t; |
| 112 | |
| 113 | typedef enum { |
| 114 | HASH_KEY_STRING, |
| 115 | HASH_KEY_ULONG, |
| 116 | HASH_KEY_CONST_STRING |
| 117 | } hash_key_enum; |
| 118 | |
| 119 | typedef enum |
| 120 | { |
| 121 | HASH_VALUE_UNDEF, |
| 122 | HASH_VALUE_PTR, |
| 123 | HASH_VALUE_INT, |
| 124 | HASH_VALUE_UINT, |
| 125 | HASH_VALUE_LONG, |
| 126 | HASH_VALUE_ULONG, |
| 127 | HASH_VALUE_FLOAT, |
| 128 | HASH_VALUE_DOUBLE |
| 129 | } hash_value_enum; |
| 130 | |
| 131 | typedef enum |
| 132 | { |
| 133 | HASH_TABLE_DESTROY, |
| 134 | HASH_ENTRY_DESTROY |
| 135 | } hash_destroy_enum; |
| 136 | |
| 137 | typedef struct hash_key_t { |
| 138 | hash_key_enum type; |
| 139 | union { |
| 140 | char *str; |
| 141 | const char *c_str; |
| 142 | unsigned long ul; |
| 143 | }; |
| 144 | } hash_key_t; |
| 145 | |
| 146 | typedef struct hash_value_t { |
| 147 | hash_value_enum type; |
| 148 | union { |
| 149 | void *ptr; |
| 150 | int i; |
| 151 | unsigned int ui; |
| 152 | long l; |
| 153 | unsigned long ul; |
| 154 | float f; |
| 155 | double d; |
| 156 | }; |
| 157 | } hash_value_t; |
| 158 | |
| 159 | typedef struct hash_entry_t { |
| 160 | hash_key_t key; |
| 161 | hash_value_t value; |
| 162 | } hash_entry_t; |
| 163 | |
| 164 | #ifdef HASH_STATISTICS |
| 165 | typedef struct hash_statistics_t { |
| 166 | unsigned long hash_accesses; |
| 167 | unsigned long hash_collisions; |
| 168 | unsigned long table_expansions; |
| 169 | unsigned long table_contractions; |
| 170 | } hash_statistics_t; |
| 171 | #endif |
| 172 | |
| 173 | |
| 174 | /* typedef's for callback based iteration */ |
| 175 | typedef bool(*hash_iterate_callback)(hash_entry_t *item, void *user_data); |
| 176 | typedef void (hash_delete_callback)(hash_entry_t *item, |
| 177 | hash_destroy_enum type, void *pvt); |
| 178 | |
| 179 | /* typedef's for iteration object based iteration */ |
| 180 | struct hash_iter_context_t; |
| 181 | typedef hash_entry_t *(*hash_iter_next_t)(struct hash_iter_context_t *iter); |
| 182 | struct hash_iter_context_t { |
| 183 | hash_iter_next_t next; |
| 184 | }; |
| 185 | |
| 186 | /* typedef for hash_create_ex() */ |
| 187 | typedef void *(hash_alloc_func)(size_t size, void *pvt); |
| 188 | typedef void (hash_free_func)(void *ptr, void *pvt); |
| 189 | |
| 190 | /*****************************************************************************/ |
| 191 | /************************* External Global Variables ***********************/ |
| 192 | /*****************************************************************************/ |
| 193 | |
| 194 | /*****************************************************************************/ |
| 195 | /**************************** Exported Functions ***************************/ |
| 196 | /*****************************************************************************/ |
| 197 | |
| 198 | /* |
| 199 | * Given an error code returned by a hash function, map it to a error string. |
| 200 | * Returns NULL if the error code is unrecognized. |
| 201 | */ |
| 202 | const char* hash_error_string(int error); |
| 203 | |
| 204 | /* |
| 205 | * Create a new hash table with room for n initial entries. hash_create returns |
| 206 | * an opaque pointer to the new hash table in the table parameter. Functions |
| 207 | * operating on the hash table pass in this hash table pointer. This means you |
| 208 | * may have as many concurrent hash tables as you want. |
| 209 | |
| 210 | * If the table creation is successful tbl is set to the new table and |
| 211 | * HASH_SUCCESS is returned, otherwise tbl will be set to NULL and the |
| 212 | * return value will be a HASH error code. |
| 213 | * |
| 214 | * count |
| 215 | * Expected number of entries in the table. This is a tuning |
| 216 | * parameter because a dynamic hash table dynamically adjusts it's |
| 217 | * internal data strucuture to optimize for the actual number of |
| 218 | * entries. The count parameter allows for some more optimization, |
| 219 | * however it's not critical to get it right, the table will still |
| 220 | * perform well. If you have no idea of how many entries to expect |
| 221 | * then pass 0, a reasonable default will be selected. |
| 222 | * tbl |
| 223 | * Pointer to a hash_table_t variable which is initialized to the |
| 224 | * new hash table (i.e. the returned table). If the table fails to |
| 225 | * be created due to errors the tbl parameter will be set to NULL |
| 226 | * and the return value will be a HASH error code. |
| 227 | * delete_callback |
| 228 | * Pointer to a function which will be called just prior to a hash |
| 229 | * entry being deleted. This is useful when the hash value has |
| 230 | * items which may need to be disposed of. The delete_callback may |
| 231 | * be NULL. |
| 232 | * delete_private_data |
| 233 | * Void pointer passed to the delete_callback, this way delete |
| 234 | * callbacks can have a private context to reach data they need to |
| 235 | * use before performning their operations. delete_private_data |
| 236 | * may be NULL. |
| 237 | * |
| 238 | * hash_create is identical to calling: |
| 239 | * |
| 240 | * hash_create_ex(count, tbl, 0, 0, 0, 0, NULL, NULL, NULL, |
| 241 | * delete_callback, delete_private_data); |
| 242 | */ |
| 243 | int hash_create(unsigned long count, hash_table_t **tbl, |
| 244 | hash_delete_callback *delete_callback, |
| 245 | void *delete_private_data); |
| 246 | |
| 247 | /* |
| 248 | * Create a new hash table and fine tune it's configurable parameters. |
| 249 | * Refer to CACM article for explanation of parameters. See |
| 250 | * hash_create for other parameters and return values. |
| 251 | * |
| 252 | * directory_bits |
| 253 | * Number of address bits allocated to top level directory array. |
| 254 | * If directory_bits or segment_bits is zero then directory_bits |
| 255 | * and segment_bits will be computed based on the count parameter. |
| 256 | * segment_bits |
| 257 | * number of address bits allocated to segment array. |
| 258 | * min_load_factor |
| 259 | * The table contracted when the ratio of entry count to bucket count |
| 260 | * is less than the min_load_factor the table is contracted. |
| 261 | * max_load_factor |
| 262 | * The table expanded when the ratio of entry count to bucket count |
| 263 | * is greater than the max_load_factor the table is expanded. |
| 264 | * alloc_func |
| 265 | * Function pointer for allocation. |
| 266 | * free_func |
| 267 | * Function pointer for freeing memory allocated with alloc_func. |
| 268 | * alloc_private_data |
| 269 | * Data pointer passed to the alloc and free functions so that |
| 270 | * custom functions can refernce other private data they may need |
| 271 | * during their execution without having to use global variables. |
| 272 | * |
| 273 | * Note directory_bits + segment_bits must be <= number of bits in |
| 274 | * unsigned long |
| 275 | */ |
| 276 | int hash_create_ex(unsigned long count, hash_table_t **tbl, |
| 277 | unsigned int directory_bits, |
| 278 | unsigned int segment_bits, |
| 279 | unsigned long min_load_factor, |
| 280 | unsigned long max_load_factor, |
| 281 | hash_alloc_func *alloc_func, |
| 282 | hash_free_func *free_func, |
| 283 | void *alloc_private_data, |
| 284 | hash_delete_callback *delete_callback, |
| 285 | void *delete_private_data); |
| 286 | |
| 287 | #ifdef HASH_STATISTICS |
| 288 | /* |
| 289 | * Return statistics for the table. |
| 290 | */ |
| 291 | int hash_get_statistics(hash_table_t *table, hash_statistics_t *statistics); |
| 292 | #endif |
| 293 | |
| 294 | /* |
| 295 | * hash_destroy deletes all entries in the hash table, freeing all memory used |
| 296 | * in implementing the hash table. Some hash entries may have values which are |
| 297 | * pointers to user data structures. User pointers are not free by hash_destroy, |
| 298 | * they must be free by the caller. This may be done by iterating over all the |
| 299 | * entries in the table using hash_iterate() and freeing the values or by |
| 300 | * registering a delete callback when the table is created with |
| 301 | * hash_create(). Note, hash keys which are strings will be automatically freed |
| 302 | * by hash_destroy, the caller does not need to free the key strings. |
| 303 | */ |
| 304 | int hash_destroy(hash_table_t *table); |
| 305 | |
| 306 | /* |
| 307 | * Enter or update an item in the table referenced by key. If the key does not |
| 308 | * exist yet the item will be created and inserted into the table with the |
| 309 | * value, otherwise the value for the existing key is updated. The return value |
| 310 | * may be HASH_ERROR_BAD_KEY_TYPE or HASH_ERROR_BAD_VALUE_TYPE if the key or |
| 311 | * value type respectively is invalid. This function might also return |
| 312 | * HASH_ERROR_NO_MEMORY. |
| 313 | */ |
| 314 | int hash_enter(hash_table_t *table, hash_key_t *key, hash_value_t *value); |
| 315 | |
| 316 | /* |
| 317 | * Using the key lookup the value associated with it in the table. If the key is |
| 318 | * not in the table HASH_ERROR_KEY_NOT_FOUND is returned. If the type of key is |
| 319 | * invalid HASH_ERROR_BAD_KEY_TYPE is returned. Otherwise HASH_SUCCESS is |
| 320 | * returned. If the result is anything other than HASH_SUCCESS the value pointer |
| 321 | * is not updated. |
| 322 | */ |
| 323 | int hash_lookup(hash_table_t *table, hash_key_t *key, hash_value_t *value); |
| 324 | |
| 325 | /* |
| 326 | * Like hash_lookup() except if the key is not in the table then it is entered |
| 327 | * into the table and assigned the default_value. Thus it is not possible for |
| 328 | * hash_get_default() to return HASH_ERROR_KEY_NOT_FOUND. |
| 329 | */ |
| 330 | int hash_get_default(hash_table_t *table, hash_key_t *key, hash_value_t *value, hash_value_t *default_value); |
| 331 | |
| 332 | /* |
| 333 | * Delete the item from the table. The key and its type are specified in the key |
| 334 | * parameter which are passed by reference. If the key was in the table |
| 335 | * HASH_SUCCESS is returned otherwise HASH_ERROR_KEY_NOT_FOUND is |
| 336 | * returned. Memory allocated to hold the key if it was a string is free by the |
| 337 | * hash library, but values which are pointers to user data must be freed by the |
| 338 | * caller (see delete_callback). |
| 339 | */ |
| 340 | int hash_delete(hash_table_t *table, hash_key_t *key); |
| 341 | |
| 342 | /* |
| 343 | * Often it is useful to operate on every key and/or value in the hash |
| 344 | * table. The hash_iterate function will invoke the users callback on every item |
| 345 | * in the table as long as the callback returns a non-zero value. Returning a |
| 346 | * zero from the callback can be used to halt the iteration prematurely if some |
| 347 | * condition is met. The user_data parameter is passed to the callback |
| 348 | * function. It can be used for any purpose the caller wants. The callback |
| 349 | * parameter list is: |
| 350 | * |
| 351 | * bool callback(hash_entry_t *item, void *user_data); |
| 352 | * |
| 353 | * WARNING: Do not modify the contents of the table during an iteration it will |
| 354 | * produce undefined results. If you need to visit each item in the table and |
| 355 | * potentially delete or insert some entries the proper way to do this is to |
| 356 | * obtain a list of keys or items using hash_keys() or hash_items() which |
| 357 | * returns a copy of the keys or items. You may then loop on the list returned |
| 358 | * and safely update the table (don't forget to free the list when done). |
| 359 | */ |
| 360 | int hash_iterate(hash_table_t *table, hash_iterate_callback callback, void *user_data); |
| 361 | |
| 362 | /* |
| 363 | * This is another method to iterate on every key/value in the hash table. |
| 364 | * However, unlike hash_iterate which requires a callback this function returns |
| 365 | * an iterator object which contains a next() function pointer. Each time |
| 366 | * next() is invoked it returns a pointer to the next hash entry in the table, |
| 367 | * then NULL when all entries have been visited. In some circumstances it's more |
| 368 | * convenient than having to define a callback. Like hash_iterate() one must |
| 369 | * never modify the table contents during iteration. If you intend to modify the |
| 370 | * table during iteration use the functions which return an indepent list of |
| 371 | * keys, values, or items instead and iterate on the list. The iterator object |
| 372 | * must be free'ed when you're done iterating by calling free() on it. |
| 373 | * |
| 374 | * Example: |
| 375 | * |
| 376 | * struct hash_iter_context_t *iter; |
| 377 | * hash_entry_t *entry; |
| 378 | * |
| 379 | * iter = new_hash_iter_context(table); |
| 380 | * while ((entry = iter->next(iter)) != NULL) { |
| 381 | * do_something(entry); |
| 382 | * } |
| 383 | * free(iter); |
| 384 | */ |
| 385 | struct hash_iter_context_t *new_hash_iter_context(hash_table_t *table); |
| 386 | |
| 387 | /* |
| 388 | * Return a count of how many items are currently in the table. |
| 389 | */ |
| 390 | unsigned long hash_count(hash_table_t *table); |
| 391 | |
| 392 | /* |
| 393 | * Get an array of all the keys in the table returned through the keys |
| 394 | * parameter. The number of elements in the array is returned in the count |
| 395 | * parameter. Each key in the array is a copy of the key in the table. Any |
| 396 | * pointers in the key will be shared with the table entry thus both the item in |
| 397 | * the array and the item in the table point to the same object. The array |
| 398 | * should be freed by calling free(). The function may return |
| 399 | * HASH_ERROR_NO_MEMORY, otherwise HASH_SUCCESS. |
| 400 | */ |
| 401 | int hash_keys(hash_table_t *table, unsigned long *count, hash_key_t **keys); |
| 402 | |
| 403 | /* |
| 404 | * Get an array of all the values in the table returned through the values |
| 405 | * parameter. The number of elements in the array is returned in the count |
| 406 | * parameter. Each value in the array is a copy of the value in the table. Any |
| 407 | * pointers in the value will be shared with the table entry thus both the item in |
| 408 | * the array and the item in the table point to the same object. The array |
| 409 | * should be freed by calling free(). The function may return |
| 410 | * HASH_ERROR_NO_MEMORY, otherwise HASH_SUCCESS. |
| 411 | */ |
| 412 | int hash_values(hash_table_t *table, unsigned long *count, hash_value_t **values); |
| 413 | |
| 414 | |
| 415 | /* |
| 416 | * Get an array of all the entries in the table returned through the entries |
| 417 | * parameter. The number of elements in the array is returned in the count |
| 418 | * parameter. Each entry in the array is a copy of the entry in the table. Any |
| 419 | * pointers in the entry will be shared with the table entry thus both the item in |
| 420 | * the array and the item in the table point to the same object. The array |
| 421 | * should be freed by calling free(). The function may return |
| 422 | * HASH_ERROR_NO_MEMORY, otherwise HASH_SUCCESS. |
| 423 | */ |
| 424 | int hash_entries(hash_table_t *table, unsigned long *count, hash_entry_t **entries); |
| 425 | |
| 426 | /* |
| 427 | * Return boolean if the key is in the table. |
| 428 | */ |
| 429 | bool hash_has_key(hash_table_t *table, hash_key_t *key); |
| 430 | |
| 431 | #endif |