Practice ยท 1 of 1
The Chaining Map
Implement a string-keyed map with chaining. The boilerplate declares:
``c
#define HMAP_BUCKETS 8
typedef struct HNode {
const char *key; /* borrowed */
int value;
struct HNode *next;
} HNode;
typedef struct { HNode *buckets[HMAP_BUCKETS]; size_t count; } HMap;
void hmap_init(HMap *m);
/* 0 inserted, 1 updated, -1 bad args. key is borrowed. */
int hmap_put(HMap *m, const char *key, int value);
int hmap_get(const HMap *m, const char *key, int *out); /* 0 found, -1 absent */
int hmap_del(HMap *m, const char *key); /* 1 removed, 0 absent */
size_t hmap_size(const HMap *m);
/* longest chain length (collision health metric) */
size_t hmap_max_chain(const HMap *m);
void hmap_destroy(HMap *m);
``
Hash: FNV-1a over the key's bytes, mod buckets.
Difficulty: intermediate
Back to lesson: Practice: Chaining & Generic Values Gym