C Collections

cbstmap · Section 3

cbmap_declare

cbmap_declare - declare an ordered map handle and its companion type variables

C Collections (libccollections, -lccollections)

#include <ccollections/cbstmap.h>
 
cbmap_declare(hm_name, KeyT, ValT);

Declares an ordered map handle named hm_name with the companion type
variables that carry KeyT and ValT to every later type-inferred macro.
Nothing is created, and the handle holds no map until one of the init
macros runs.
 
This is the declare half of a declare-then-create pair; cbmap_init(3),
cbmap_init_mp(3), cbmap_init_cc(3) and cbmap_init_full(3) are the create
half. cbmap_construct(3) and its variants do both steps at once, which is
the shorter way to write the common case. Keeping them apart helps when
one place declares the handle and another creates the map, for example
when the comparison procedure depends on a condition, or when the map is
created only after the enclosing function has validated its arguments.
 
The handle and the companion type variables are left uninitialized, so
until an init macro assigns it the handle holds no usable value, not even
NULL. cbmap_declare_scoped(3) initializes the handle to NULL instead and
also destroys the map when the scope exits.

hm_name  Name for the map handle to declare
KeyT    Key type. Every later macro reads a key through it, and the init
         macro takes the order of the tree from it
ValT    Value type. Only the type-inferred macros read it; the library
         never passes it to the container

None. The macro expands to declarations, not to an expression.

Because the macro is a sequence of declarations, put it where a
declaration is legal and end it with a semicolon of its own.
 
The container is an AVL tree, so the key type given here also fixes the
order in which a walk visits the elements, as cbstmap_begin_iter(3)
describes.
 
The companion type variables follow the scope of the handle, so a function
that receives the handle needs cbmap_redeclare(3) there.
 
The macro never destroys anything; destroy the map in this handle with
cbmap_destroy(3).
 
An array key type or value type, such as int[2] or char[16], is a compile
error whose message says to wrap the array in a struct. The macros copy
keys and values by assignment and C can't assign an array, so the map
would otherwise store the address of the caller's array instead of its
elements. A struct that holds the array works.

Every other type-inferred macro reads the handle, so calling one of them
between this declaration and an init macro reads an uninitialized
variable.

cbmap_declare(sorted, int, char *);
if (use_custom_allocator) {
    cbmap_init_mp(sorted, &my_procs);
} else {
    cbmap_init(sorted);
}
int k = 42;
cbmap_insert(sorted, k, "hello");
cbmap_destroy(sorted);

cbmap_declare_scoped(3), cbmap_init(3), cbmap_init_mp(3), cbmap_init_cc(3), cbmap_init_full(3), cbmap_construct(3), cbmap_redeclare(3), cbstmap_begin_iter(3), cbmap_destroy(3)