Memory management of keys and data pointers is the resposibility of the user although del_fn function pointers (defined in allocator(3m)) may be specified with some hashmap functions to assist the user with this task.
Hashmap Definitions
typedef unsigned long (*hash_fn)(const void *object, void *context); typedef int (*cmp_fn)(const void *object1, const void *object2, void *context); unsigned long hash_text(const void *text, void *context); unsigned long cmp_text(const void *text1, const void *text2, void *context);The hash_text function is a suitable hash_fn for character strings. This function is actually a macro for either hash_str or hash_wcs that accept multi-byte or wide character strings depending on wheather or not USE_WCHAR is defined.
The cmp_text function is a suitable cmp_fn for character strings. This function is actually a macro for either cmp_str or cmp_wcs that accept multi-byte or wide character strings depending on wheather or not USE_WCHAR is defined.
The hashmap_init function
Description
#include <mba/hashmap.h> int hashmap_init(struct hashmap *h, unsigned int load_factor, hash_fn hash, cmp_fn cmp, void *context, struct allocator *al);
If the hash parameter is not NULL it will be used to generate a hash value given a key and the specified context object. Given a set of keys the hash function should generate an even distribution of values. If the hash parameter is NULL a key's memory address will be used as it's hash value.
If the cmp parameter is not NULL it will used to compare two keys for equality. This function should return 0 if two keys are equal and non-zero if they are not. If the cmp parameter is NULL the memory addresses of the two keys will be compared.
The al parameter is an allocator(3m) from which all memory associated with this hashmap should be allocated. As with the allocator functions, a NULL allocator indicates the stdlib_allocator should be used.
The following example illustrates how to initialize a hashmap and use it to store the object data associated with the character string "name".
struct hashmap hm; struct foo data, *out; hashmap_init(&hm, 0, /* default load factor of 75 */ hash_text, /* default text hash function */ cmp_text, /* default text compare function */ NULL, /* hash_fn and cmp_fn function do not require context */ NULL); /* use the stdlib_allocator */ hashmap_put(&hm, "name", &data); out = hashmap_get(&hm, "name"); /* out now points to data */Returns
The hashmap_deinit function
Description
#include <mba/hashmap.h> int hashmap_deinit(struct hashmap *h, del_fn key_del, del_fn data_del, void *context);
The hashmap_new function
Description
#include <mba/hashmap.h> struct hashmap *hashmap_new(hash_fn hash, cmp_fn cmp, void *context, struct allocator *al);
The hashmap_del function
Description
#include <mba/hashmap.h> int hashmap_del(struct hashmap *h, del_fn key_del, del_fn data_del, void *context);
The hashmap_clear function
Description
#include <mba/hashmap.h> int hashmap_clear(struct hashmap *h, del_fn key_del, del_fn data_del, void *context);
The hashmap_clean function
Description
#include <mba/hashmap.h> int hashmap_clean(struct hashmap *h);
The hashmap_put function
Description
#include <mba/hashmap.h> int hashmap_put(struct hashmap *h, void *key, void *data);
The hashmap_get function
Description
#include <mba/hashmap.h> void *hashmap_get(const struct hashmap *h, const void *key);
The hashmap_is_empty function
Description
#include <mba/hashmap.h> int hashmap_is_empty(struct hashmap *h);
The hashmap_size function
Description
#include <mba/hashmap.h> unsigned int hashmap_size(struct hashmap *h);
The hashmap_iterate function
Description
#include <mba/hashmap.h> void hashmap_iterate(void *h, iter_t *iter);
Modifying the map during the enumeration is permitted however should adding or removing data cause the table to be resized, not all keys may be enumerated and some keys may be returned more than once. Therefore, to make multiple modifications during the enumeration it may be desirable to first create a snapshot of the keys in an array or list.
The hashmap_next function
Returns
#include <mba/hashmap.h> void *hashmap_next(void *h, iter_t *iter);
The hashmap_remove function
Description
#include <mba/hashmap.h> int hashmap_remove(struct hashmap *h, void **key, void **data);
The following is an example of removing an element from a hashmap.
char *key = name; struct foo *data; hashmap_remove(hm, (void **)&key, (void **)&data); /* free data if necessary */Returns