diff options
Diffstat (limited to 'hashmap.h')
| -rw-r--r-- | hashmap.h | 262 |
1 files changed, 211 insertions, 51 deletions
@@ -1,6 +1,8 @@ #ifndef HASHMAP_H #define HASHMAP_H +#include "hash.h" + /* * Generic implementation of hash-based key-value mappings. * @@ -11,7 +13,7 @@ * * struct hashmap map; * struct long2string { - * struct hashmap_entry ent; // must be the first member! + * struct hashmap_entry ent; * long key; * char value[FLEX_ARRAY]; // be careful with allocating on stack! * }; @@ -19,12 +21,16 @@ * #define COMPARE_VALUE 1 * * static int long2string_cmp(const void *hashmap_cmp_fn_data, - * const struct long2string *e1, - * const struct long2string *e2, + * const struct hashmap_entry *eptr, + * const struct hashmap_entry *entry_or_key, * const void *keydata) * { * const char *string = keydata; * unsigned flags = *(unsigned *)hashmap_cmp_fn_data; + * const struct long2string *e1, *e2; + * + * e1 = container_of(eptr, const struct long2string, ent); + * e2 = container_of(entry_or_key, const struct long2string, ent); * * if (flags & COMPARE_VALUE) * return e1->key != e2->key || @@ -39,54 +45,58 @@ * char value[255], action[32]; * unsigned flags = 0; * - * hashmap_init(&map, (hashmap_cmp_fn) long2string_cmp, &flags, 0); + * hashmap_init(&map, long2string_cmp, &flags, 0); * * while (scanf("%s %ld %s", action, &key, value)) { * * if (!strcmp("add", action)) { * struct long2string *e; * FLEX_ALLOC_STR(e, value, value); - * hashmap_entry_init(e, memhash(&key, sizeof(long))); + * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long))); * e->key = key; - * hashmap_add(&map, e); + * hashmap_add(&map, &e->ent); * } * * if (!strcmp("print_all_by_key", action)) { * struct long2string k, *e; - * hashmap_entry_init(&k, memhash(&key, sizeof(long))); + * hashmap_entry_init(&k.ent, memhash(&key, sizeof(long))); * k.key = key; * * flags &= ~COMPARE_VALUE; - * e = hashmap_get(&map, &k, NULL); + * e = hashmap_get_entry(&map, &k, ent, NULL); * if (e) { * printf("first: %ld %s\n", e->key, e->value); - * while ((e = hashmap_get_next(&map, e))) + * while ((e = hashmap_get_next_entry(&map, e, + * struct long2string, ent))) { * printf("found more: %ld %s\n", e->key, e->value); + * } * } * } * * if (!strcmp("has_exact_match", action)) { * struct long2string *e; * FLEX_ALLOC_STR(e, value, value); - * hashmap_entry_init(e, memhash(&key, sizeof(long))); + * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long))); * e->key = key; * * flags |= COMPARE_VALUE; - * printf("%sfound\n", hashmap_get(&map, e, NULL) ? "" : "not "); + * printf("%sfound\n", + * hashmap_get(&map, &e->ent, NULL) ? "" : "not "); * free(e); * } * * if (!strcmp("has_exact_match_no_heap_alloc", action)) { * struct long2string k; - * hashmap_entry_init(&k, memhash(&key, sizeof(long))); + * hashmap_entry_init(&k.ent, memhash(&key, sizeof(long))); * k.key = key; * * flags |= COMPARE_VALUE; - * printf("%sfound\n", hashmap_get(&map, &k, value) ? "" : "not "); + * printf("%sfound\n", + * hashmap_get(&map, &k.ent, value) ? "" : "not "); * } * * if (!strcmp("end", action)) { - * hashmap_free(&map, 1); + * hashmap_clear_and_free(&map, struct long2string, ent); * break; * } * } @@ -104,11 +114,11 @@ * `memihash_cont` is a variant of `memihash` that allows a computation to be * continued with another chunk of data. */ -extern unsigned int strhash(const char *buf); -extern unsigned int strihash(const char *buf); -extern unsigned int memhash(const void *buf, size_t len); -extern unsigned int memihash(const void *buf, size_t len); -extern unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len); +unsigned int strhash(const char *buf); +unsigned int strihash(const char *buf); +unsigned int memhash(const void *buf, size_t len); +unsigned int memihash(const void *buf, size_t len); +unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len); /* * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code @@ -118,20 +128,20 @@ extern unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_ * the results will be different on big-endian and little-endian * platforms, so they should not be stored or transferred over the net. */ -static inline unsigned int sha1hash(const unsigned char *sha1) +static inline unsigned int oidhash(const struct object_id *oid) { /* - * Equivalent to 'return *(unsigned int *)sha1;', but safe on + * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on * platforms that don't support unaligned reads. */ unsigned int hash; - memcpy(&hash, sha1, sizeof(hash)); + memcpy(&hash, oid->hash, sizeof(hash)); return hash; } /* * struct hashmap_entry is an opaque structure representing an entry in the - * hash table, which must be used as first member of user data structures. + * hash table. * Ideally it should be followed by an int-sized member to prevent unused * memory on 64-bit systems due to alignment. */ @@ -158,7 +168,7 @@ struct hashmap_entry { * argument `keydata`, respectively. Otherwise, `keydata` is NULL. * * When it is too expensive to allocate a user entry (either because it is - * large or varialbe sized, such that it is not on the stack), then the + * large or variable sized, such that it is not on the stack), then the * relevant data to check for equality should be passed via `keydata`. * In this case `key` can be a stripped down version of the user key data * or even just a hashmap_entry having the correct hash. @@ -166,7 +176,8 @@ struct hashmap_entry { * The `hashmap_cmp_fn_data` entry is the pointer given in the init function. */ typedef int (*hashmap_cmp_fn)(const void *hashmap_cmp_fn_data, - const void *entry, const void *entry_or_key, + const struct hashmap_entry *entry, + const struct hashmap_entry *entry_or_key, const void *keydata); /* @@ -199,6 +210,9 @@ struct hashmap { /* hashmap functions */ +#define HASHMAP_INIT(fn, data) { .cmpfn = fn, .cmpfn_data = data, \ + .do_count_items = 1 } + /* * Initializes a hashmap structure. * @@ -216,18 +230,73 @@ struct hashmap { * parameter may be used to preallocate a sufficiently large table and thus * prevent expensive resizing. If 0, the table is dynamically resized. */ -extern void hashmap_init(struct hashmap *map, - hashmap_cmp_fn equals_function, - const void *equals_function_data, - size_t initial_size); +void hashmap_init(struct hashmap *map, + hashmap_cmp_fn equals_function, + const void *equals_function_data, + size_t initial_size); + +/* internal functions for clearing or freeing hashmap */ +void hashmap_partial_clear_(struct hashmap *map, ssize_t offset); +void hashmap_clear_(struct hashmap *map, ssize_t offset); + +/* + * Frees a hashmap structure and allocated memory for the table, but does not + * free the entries nor anything they point to. + * + * Usage note: + * + * Many callers will need to iterate over all entries and free the data each + * entry points to; in such a case, they can free the entry itself while at it. + * Thus, you might see: + * + * hashmap_for_each_entry(map, hashmap_iter, e, hashmap_entry_name) { + * free(e->somefield); + * free(e); + * } + * hashmap_clear(map); + * + * instead of + * + * hashmap_for_each_entry(map, hashmap_iter, e, hashmap_entry_name) { + * free(e->somefield); + * } + * hashmap_clear_and_free(map, struct my_entry_struct, hashmap_entry_name); + * + * to avoid the implicit extra loop over the entries. However, if there are + * no special fields in your entry that need to be freed beyond the entry + * itself, it is probably simpler to avoid the explicit loop and just call + * hashmap_clear_and_free(). + */ +#define hashmap_clear(map) hashmap_clear_(map, -1) + +/* + * Similar to hashmap_clear(), except that the table is no deallocated; it + * is merely zeroed out but left the same size as before. If the hashmap + * will be reused, this avoids the overhead of deallocating and + * reallocating map->table. As with hashmap_clear(), you may need to free + * the entries yourself before calling this function. + */ +#define hashmap_partial_clear(map) hashmap_partial_clear_(map, -1) + +/* + * Similar to hashmap_clear() but also frees all entries. @type is the + * struct type of the entry where @member is the hashmap_entry struct used + * to associate with @map. + * + * See usage note above hashmap_clear(). + */ +#define hashmap_clear_and_free(map, type, member) \ + hashmap_clear_(map, offsetof(type, member)) /* - * Frees a hashmap structure and allocated memory. + * Similar to hashmap_partial_clear() but also frees all entries. @type is + * the struct type of the entry where @member is the hashmap_entry struct + * used to associate with @map. * - * If `free_entries` is true, each hashmap_entry in the map is freed as well - * using stdlibs free(). + * See usage note above hashmap_clear(). */ -extern void hashmap_free(struct hashmap *map, int free_entries); +#define hashmap_partial_clear_and_free(map, type, member) \ + hashmap_partial_clear_(map, offsetof(type, member)) /* hashmap_entry functions */ @@ -242,9 +311,9 @@ extern void hashmap_free(struct hashmap *map, int free_entries); * your structure was allocated with xmalloc(), you can just free(3) it, * and if it is on stack, you can just let it go out of scope). */ -static inline void hashmap_entry_init(void *entry, unsigned int hash) +static inline void hashmap_entry_init(struct hashmap_entry *e, + unsigned int hash) { - struct hashmap_entry *e = entry; e->hash = hash; e->next = NULL; } @@ -284,8 +353,9 @@ static inline unsigned int hashmap_get_size(struct hashmap *map) * If an entry with matching hash code is found, `key` and `keydata` are passed * to `hashmap_cmp_fn` to decide whether the entry matches the key. */ -extern void *hashmap_get(const struct hashmap *map, const void *key, - const void *keydata); +struct hashmap_entry *hashmap_get(const struct hashmap *map, + const struct hashmap_entry *key, + const void *keydata); /* * Returns the hashmap entry for the specified hash code and key data, @@ -299,9 +369,10 @@ extern void *hashmap_get(const struct hashmap *map, const void *key, * `entry_or_key` parameter of `hashmap_cmp_fn` points to a hashmap_entry * structure that should not be used in the comparison. */ -static inline void *hashmap_get_from_hash(const struct hashmap *map, - unsigned int hash, - const void *keydata) +static inline struct hashmap_entry *hashmap_get_from_hash( + const struct hashmap *map, + unsigned int hash, + const void *keydata) { struct hashmap_entry key; hashmap_entry_init(&key, hash); @@ -316,7 +387,8 @@ static inline void *hashmap_get_from_hash(const struct hashmap *map, * `entry` is the hashmap_entry to start the search from, obtained via a previous * call to `hashmap_get` or `hashmap_get_next`. */ -extern void *hashmap_get_next(const struct hashmap *map, const void *entry); +struct hashmap_entry *hashmap_get_next(const struct hashmap *map, + const struct hashmap_entry *entry); /* * Adds a hashmap entry. This allows to add duplicate entries (i.e. @@ -325,7 +397,7 @@ extern void *hashmap_get_next(const struct hashmap *map, const void *entry); * `map` is the hashmap structure. * `entry` is the entry to add. */ -extern void hashmap_add(struct hashmap *map, void *entry); +void hashmap_add(struct hashmap *map, struct hashmap_entry *entry); /* * Adds or replaces a hashmap entry. If the hashmap contains duplicate @@ -335,7 +407,20 @@ extern void hashmap_add(struct hashmap *map, void *entry); * `entry` is the entry to add or replace. * Returns the replaced entry, or NULL if not found (i.e. the entry was added). */ -extern void *hashmap_put(struct hashmap *map, void *entry); +struct hashmap_entry *hashmap_put(struct hashmap *map, + struct hashmap_entry *entry); + +/* + * Adds or replaces a hashmap entry contained within @keyvar, + * where @keyvar is a pointer to a struct containing a + * "struct hashmap_entry" @member. + * + * Returns the replaced pointer which is of the same type as @keyvar, + * or NULL if not found. + */ +#define hashmap_put_entry(map, keyvar, member) \ + container_of_or_null_offset(hashmap_put(map, &(keyvar)->member), \ + OFFSETOF_VAR(keyvar, member)) /* * Removes a hashmap entry matching the specified key. If the hashmap contains @@ -344,8 +429,24 @@ extern void *hashmap_put(struct hashmap *map, void *entry); * * Argument explanation is the same as in `hashmap_get`. */ -extern void *hashmap_remove(struct hashmap *map, const void *key, - const void *keydata); +struct hashmap_entry *hashmap_remove(struct hashmap *map, + const struct hashmap_entry *key, + const void *keydata); + +/* + * Removes a hashmap entry contained within @keyvar, + * where @keyvar is a pointer to a struct containing a + * "struct hashmap_entry" @member. + * + * See `hashmap_get` for an explanation of @keydata + * + * Returns the replaced pointer which is of the same type as @keyvar, + * or NULL if not found. + */ +#define hashmap_remove_entry(map, keyvar, member, keydata) \ + container_of_or_null_offset( \ + hashmap_remove(map, &(keyvar)->member, keydata), \ + OFFSETOF_VAR(keyvar, member)) /* * Returns the `bucket` an entry is stored in. @@ -365,20 +466,79 @@ struct hashmap_iter { }; /* Initializes a `hashmap_iter` structure. */ -extern void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter); +void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter); /* Returns the next hashmap_entry, or NULL if there are no more entries. */ -extern void *hashmap_iter_next(struct hashmap_iter *iter); +struct hashmap_entry *hashmap_iter_next(struct hashmap_iter *iter); /* Initializes the iterator and returns the first entry, if any. */ -static inline void *hashmap_iter_first(struct hashmap *map, - struct hashmap_iter *iter) +static inline struct hashmap_entry *hashmap_iter_first(struct hashmap *map, + struct hashmap_iter *iter) { hashmap_iter_init(map, iter); return hashmap_iter_next(iter); } /* + * returns the first entry in @map using @iter, where the entry is of + * @type (e.g. "struct foo") and @member is the name of the + * "struct hashmap_entry" in @type + */ +#define hashmap_iter_first_entry(map, iter, type, member) \ + container_of_or_null(hashmap_iter_first(map, iter), type, member) + +/* internal macro for hashmap_for_each_entry */ +#define hashmap_iter_next_entry_offset(iter, offset) \ + container_of_or_null_offset(hashmap_iter_next(iter), offset) + +/* internal macro for hashmap_for_each_entry */ +#define hashmap_iter_first_entry_offset(map, iter, offset) \ + container_of_or_null_offset(hashmap_iter_first(map, iter), offset) + +/* + * iterate through @map using @iter, @var is a pointer to a type + * containing a @member which is a "struct hashmap_entry" + */ +#define hashmap_for_each_entry(map, iter, var, member) \ + for (var = NULL, /* for systems without typeof */ \ + var = hashmap_iter_first_entry_offset(map, iter, \ + OFFSETOF_VAR(var, member)); \ + var; \ + var = hashmap_iter_next_entry_offset(iter, \ + OFFSETOF_VAR(var, member))) + +/* + * returns a pointer of type matching @keyvar, or NULL if nothing found. + * @keyvar is a pointer to a struct containing a + * "struct hashmap_entry" @member. + */ +#define hashmap_get_entry(map, keyvar, member, keydata) \ + container_of_or_null_offset( \ + hashmap_get(map, &(keyvar)->member, keydata), \ + OFFSETOF_VAR(keyvar, member)) + +#define hashmap_get_entry_from_hash(map, hash, keydata, type, member) \ + container_of_or_null(hashmap_get_from_hash(map, hash, keydata), \ + type, member) +/* + * returns the next equal pointer to @var, or NULL if not found. + * @var is a pointer of any type containing "struct hashmap_entry" + * @member is the name of the "struct hashmap_entry" field + */ +#define hashmap_get_next_entry(map, var, member) \ + container_of_or_null_offset(hashmap_get_next(map, &(var)->member), \ + OFFSETOF_VAR(var, member)) + +/* + * iterate @map starting from @var, where @var is a pointer of @type + * and @member is the name of the "struct hashmap_entry" field in @type + */ +#define hashmap_for_each_entry_from(map, var, member) \ + for (; \ + var; \ + var = hashmap_get_next_entry(map, var, member)) + +/* * Disable item counting and automatic rehashing when adding/removing items. * * Normally, the hashmap keeps track of the number of items in the map @@ -394,7 +554,7 @@ static inline void hashmap_disable_item_counting(struct hashmap *map) } /* - * Re-enable item couting when adding/removing items. + * Re-enable item counting when adding/removing items. * If counting is currently disabled, it will force count them. * It WILL NOT automatically rehash them. */ @@ -429,7 +589,7 @@ static inline void hashmap_enable_item_counting(struct hashmap *map) * * Uses a hashmap to store the pool of interned strings. */ -extern const void *memintern(const void *data, size_t len); +const void *memintern(const void *data, size_t len); static inline const char *strintern(const char *string) { return memintern(string, strlen(string)); |
