Skip to content

CF_MAP

Category: map
GitHub: cute_map.h


Declares a map (hashtable) type for values of type T.

#define CF_MAP(T) CK_MAP(T)
Parameters Description
T The value type stored in the map.

Code Example

Showcase of basic map features.

CF_MAP(CF_V2) pts = NULL;
map_set(pts, 0, cf_v2(3, 5));   // Constructs a new table on-the-spot. The table is *hidden* behind `pts`.
map_set(pts, 10, cf_v2(-1, -1));
map_set(pts, 2, cf_v2(0, 0));

// Use `map_get` to fetch values.
CF_V2 a = map_get(pts, 0);
CF_V2 b = map_get(pts, 10);
CF_V2 c = map_get(pts, 2);

// Loop over {key, item} pairs like so:
uint64_t* keys = map_keys(pts);
for (int i = 0; i < map_size(pts); ++i) {
    uint64_t key = keys[i];
    CF_V2 v = pts[i];
    // ...
}

map_free(pts);

Remarks

CF_MAP(T) resolves to T*. The map data structure is hidden just before the pointer in memory (stretchy-buffer style). An empty/uninitialized map is simply NULL. One downside of the C-macro API is the opaque nature of the pointer type. Since the macros use polymorphism on typed pointers, there's no actual map struct type visible. CF_MAP(T) helps visually indicate a type is a map, not just a plain pointer. Keys are always uint64_t. Use sintern for string keys (casting the pointer to uint64_t).

cf_map_set
cf_map_get
cf_map_get_ptr
cf_map_has
cf_map_del
cf_map_clear
cf_map_keys
cf_map_items
cf_map_swap
cf_map_size
cf_map_free