C Collections

chashmap · Section 3

chmap_redeclare

chmap_redeclare - re-establish the companion type variables for a hash map handle from another scope

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
chmap_redeclare(hm_name, KeyT, ValT);

Declares the companion type variables that carry KeyT and ValT for a
hash map handle that already exists. It declares no handle and neither
creates nor destroys a map; the only thing it declares again is the type
information that the type-inferred macros need.
 
Every type-inferred macro learns the key and value types of a map from
two ordinary local variables named after the handle, which
chmap_construct(3) and chmap_declare(3) create next to it. Since they are
local declarations, they don't travel with the handle into another
function. In that function, chmap_insert(hm_name, ...) and the other
macros refer to variables that are not declared there, and the
translation unit doesn't compile. This macro declares them again, so any
code that passes a chmap across a function boundary needs it.
 
The types given here must be the ones the map was created with. The
macros use them to read a key, to size a value and to decide whether a
value is a char * that the map stores by contents.

hm_name  Name of the existing map handle
KeyT    Key type the map was created with
ValT    Value type the map was created with

None. The macro expands to declarations and is not an expression.

Expands to a sequence of declarations, so put it where a declaration is
allowed; it takes a trailing semicolon of its own
The declared variables are pointers that nothing dereferences at run
time. They are only there so that typeof and the type-introspection
macros can read the types from them, and they cost nothing in the
generated code
Since it only declares variables, it is also how you use a handle that
lives in a struct field or in a global: Name it in each function that
works on that handle
An array key or value type, such as int[2] or char[16], is a compile
error, and the message says to wrap the array in a struct. The macros copy
a key or a value by assignment and C cannot assign an array, so the map
would store the address of the caller's array instead of its elements. A
struct that holds the array works

Do not declare a value type whose width differs from the stored one.
chmap_get(3) and chmap_get_ptr(3) catch such a width at run time, describe
the size mismatch and stop the program through ccol__fatal_err(). A
mismatch between two types of the same width goes unnoticed, so the
types here are a promise that you keep yourself and the compiler does
not check.
A key of the wrong width is reported instead of accepted, and the raw
entry points return ccol_invalid_args for it, because the hash of a key
and its equality test mean something only at the width the map was
created for. chmap_get(3) and chmap_insert(3) turn that error into
ccol__fatal_err(). chmap_get_ptr(3) and chmap_remove(3) report it
without stopping: chmap_get_ptr(3) gives a NULL result and
chmap_remove(3) gives the return code.

void bump(chmap scores, const char *who) {
    chmap_redeclare(scores, char *, int);
    int *slot = chmap_get_ptr(scores, who);
    if (slot) {
        (*slot)++;
    }
}
 
chmap_construct(scores, char *, int);
int score = 42;
chmap_insert(scores, "alice", score);
bump(scores, "alice");
chmap_destroy(scores);

chmap_declare(3), chmap_construct(3), chmap_get(3), chmap_get_ptr(3), chmap_insert(3)