A header-only library which implements a hashmap.
| hashmap.h | Initial commit | |
| LICENSE | Initial commit | |
| README.md | Initial commit | |
hashmap.h
A header-only library which implements a hashmap.
Example
#include <assert.h>
#define HASHMAP_IMPL
#include "hashmap.h"
#define ARRAY_LEN(x) (sizeof(x) / sizeof(*x))
#define HASHMAP_PUT(M, K, V) \
assert(HashMap_put((M), (K), (void *) (V)) == true)
int main(void) {
size_t value = 0;
HashMap map = {0};
char *keys[] = {
"apples", "pears", "oranges", "bananas",
"grapes", "peaches", "strawberries", "blackberries",
};
size_t values[] = {
34, 12, 45, 928,
123, 548, 2382, 1232,
};
HashMap_init(
&map,
HashMap_key_hash_djb2,
HashMap_key_equal_str
);
for (size_t i = 0; i < ARRAY_LEN(keys); i++) {
HASHMAP_PUT(&map, keys[i], values[i]);
}
for (size_t i = 0; i < ARRAY_LEN(keys); i++) {
assert(
HashMap_get(map, keys[i], (void **) &value)
== true
);
printf("%s => %zu\n", keys[i], value);
}
assert(
HashMap_get(map, "Not in the map", (void **) &value)
== false
);
HashMap_free(&map);
}