| 1 | /* |
| 2 | * Generic hashmap manipulation functions |
| 3 | * |
| 4 | * Originally by Elliot C Back - http://elliottback.com/wp/hashmap-implementation-in-c/ |
| 5 | * |
| 6 | * Modified by Pete Warden to fix a serious performance problem, support strings as keys |
| 7 | * and removed thread synchronization - http://petewarden.typepad.com |
| 8 | */ |
| 9 | #pragma once |
| 10 | |
| 11 | #define MAP_MISSING -3 /* No such element */ |
| 12 | #define MAP_FULL -2 /* Hashmap is full */ |
| 13 | #define MAP_OMEM -1 /* Out of Memory */ |
| 14 | #define MAP_OK 0 /* OK */ |
| 15 | |
| 16 | /* |
| 17 | * any_t is a pointer. This allows you to put arbitrary structures in |
| 18 | * the hashmap. |
| 19 | */ |
| 20 | typedef void *any_t; |
| 21 | |
| 22 | /* |
| 23 | * PFany is a pointer to a function that can take two any_t arguments |
| 24 | * and return an integer. Returns status code.. |
| 25 | */ |
| 26 | typedef int (*PFany)(any_t, any_t); |
| 27 | |
| 28 | /* |
| 29 | * map_t is a pointer to an internally maintained data structure. |
| 30 | * Clients of this package do not need to know how hashmaps are |
| 31 | * represented. They see and manipulate only map_t's. |
| 32 | */ |
| 33 | typedef struct hashmap_map *map_t; |
| 34 | |
| 35 | /* |
| 36 | * Return an empty hashmap. Returns NULL if empty. |
| 37 | */ |
| 38 | map_t hashmap_new(void); |
| 39 | |
| 40 | /* |
| 41 | * Perform a copy of the hashmap, with optional callback to perform |
| 42 | * per-element copies. Leave NULL to perform a shallow value-only |
| 43 | * copy. |
| 44 | */ |
| 45 | map_t hashmap_copy(const map_t in, any_t (*callback)(any_t)); |
| 46 | |
| 47 | /* |
| 48 | * Iteratively call f with argument (item, data) for |
| 49 | * each element data in the hashmap. The function must |
| 50 | * return a map status code. If it returns anything other |
| 51 | * than MAP_OK the traversal is terminated. f must |
| 52 | * not reenter any hashmap functions, or deadlock may arise. |
| 53 | */ |
| 54 | int hashmap_iterate(map_t in, PFany f, any_t item); |
| 55 | int hashmap_iterate_keys(map_t in, PFany f, any_t item); |
| 56 | int hashmap_iterate_keys_sorted(map_t in, PFany f, any_t item); |
| 57 | |
| 58 | /* |
| 59 | * Add an element to the hashmap. Return MAP_OK or MAP_OMEM. |
| 60 | */ |
| 61 | int hashmap_put(map_t in, const char* key, any_t value); |
| 62 | |
| 63 | /* |
| 64 | * Get an element from the hashmap. Return MAP_OK or MAP_MISSING. |
| 65 | */ |
| 66 | int hashmap_get(const map_t in, const char* key, any_t *arg); |
| 67 | |
| 68 | /* |
| 69 | * Remove an element from the hashmap. Return MAP_OK or MAP_MISSING. |
| 70 | */ |
| 71 | int hashmap_remove(map_t in, char* key); |
| 72 | |
| 73 | /* |
| 74 | * Get any element. Return MAP_OK or MAP_MISSING. |
| 75 | */ |
| 76 | int hashmap_get_one(map_t m, any_t *arg); |
| 77 | int hashmap_get_one_key(map_t m, any_t *arg); |
| 78 | |
| 79 | /* |
| 80 | * Remove all elements, with a custom callback to each element, so that they |
| 81 | * may be deallocated by the callback |
| 82 | */ |
| 83 | void hashmap_clear(map_t in, void(*callback)(any_t)); |
| 84 | |
| 85 | /* |
| 86 | * Free the hashmap |
| 87 | */ |
| 88 | void hashmap_free(map_t in); |
| 89 | |
| 90 | /* |
| 91 | * Free the hashmap, as well as a custom callback to each element, so that they |
| 92 | * may be deallocated by the callback |
| 93 | * It is a shortcut to hashmap_iterate with a deallocation function followed by |
| 94 | * hashmap_free. |
| 95 | */ |
| 96 | void hashmap_destroy(map_t in, void (*callback)(any_t)); |
| 97 | |
| 98 | /* |
| 99 | * Get the current size of a hashmap |
| 100 | */ |
| 101 | int hashmap_length(map_t in); |