// lesson: put-and-get
Put and Get
Time to assemble the map itself: a heap-allocated array of bucket heads plus a length counter.
struct hashmap {
struct hm_entry **buckets; /* array of nbuckets chain heads */
size_t nbuckets;
size_t len; /* number of stored entries */
};
hm_get is the two helpers from last lesson glued together: hash, index,
walk the chain โ the whole lookup is just this pipeline:
hm_put has more decisions in it:
- Overwrite, don't duplicate. If the key already exists, replace its
value in place;
lendoesn't change. Only a genuinely new key allocates a node and bumpslen. - Own your keys. The caller's string may be a stack buffer that dies at
the next
}. The map must copy the key into memory it owns (allocatestrlen + 1bytes and copy โ this is what non-standardstrdupdoes). - Report allocation failure.
mallocreturns NULL when memory runs out; a library that crashes instead of returning an error is a library you can't ship. Return 0 on success, -1 on failure.
Returning int * (a pointer to the stored value) from hm_get instead of
int kills two birds: NULL cleanly signals "not found", and callers can
update a value through the pointer without a second lookup.
โบ The Core API
15 ptsImplement hm_put and hm_get. The starter gives you working fnv1a,
hm_new, bucket_index, and chain_find so the earlier building blocks
don't need re-typing; new entries may go at the head of their chain (it's
the easy spot).
Log in to submit a solution and earn points.