Skip to content

hadd

Category: hash
GitHub: cute_hashtable.h


Add's a {key, item} pair.

#define hadd(h, k, ...) cf_hashtable_add(h, k, (__VA_ARGS__))
Parameters Description
h The hashtable. Can be NULL. Needs to be a pointer to the type of items in the table.
k The key for lookups. Each {key, item} pair must be unique. Keys are always typecasted to uint64_t e.g. you can use pointers as keys.
... An item to place in the table.

Return Value

Returns a pointer to the item set into the table.

Code Example

Set and get a few elements from a hashtable.

htbl int table = NULL;
hadd(table, 0, 5);
hadd(table, 1, 12);
CF_ASSERT(hget(table, 0) == 5);
CF_ASSERT(hget(table, 1) == 12);
hfree(table);

Remarks

This function works the same as hset. If the item already exists in the table, it's simply updated to a new value. The pointer returned is not stable. Internally the table can be resized, invalidating all pointers to any elements within the table. Therefor, no items may store pointers to themselves or other items. Indices however, are totally fine.

htbl
hset
hfree
hget
hfind
hget_ptr
hfind_ptr
hhas
hdel
hclear
hkeys
hitems
hswap
hsize
hcount