1
0
Fork
You've already forked hashmap.h
0
A header-only library which implements a hashmap.
  • C 100%
2026年06月07日 22:09:34 +01:00
hashmap.h Initial commit 2026年06月07日 22:09:34 +01:00
LICENSE Initial commit 2026年06月07日 22:09:34 +01:00
README.md Initial commit 2026年06月07日 22:09:34 +01:00

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);
}