C Collections

cbstmap · Section 3

cbmap_init

cbmap_init - create an ordered map with defaults into an already declared handle

C Collections (libccollections, -lccollections)

#include <ccollections/cbstmap.h>
 
cbmap_init(hm_name);

Creates an ordered map with the default allocator and the default order
for the declared key type, and assigns it to hm_name. The caller must
declare the handle first, with cbmap_declare(3) or
cbmap_declare_scoped(3). The key type and the value type are not arguments
here; the macro reads them from the companion type variables that the
declare macro created.
 
It is the narrowest of the init macros. cbmap_init_mp(3) adds an
allocator, cbmap_init_cc(3) adds a comparison procedure, and
cbmap_init_full(3) takes both. The one-step equivalents are
cbmap_construct(3) and cbmap_construct_scoped(3).

hm_name  The map handle that the caller declared earlier, which receives
         the new map

None. The macro expands to a statement, not to an expression.

The container is an AVL tree, so it keeps its elements in key order and a
walk visits them in ascending key order, as cbstmap_begin_iter(3)
describes. The declared key type picks the order: A signed integer
comparison for the signed integer types, an unsigned comparison for the
unsigned types and pointers, a numeric comparison for the floating-point
types and a lexicographic byte comparison for a string key.
cbstmap_create_full(3) spells all of these out, including where a NaN key
sorts.
 
A key type that the map doesn't recognize, such as a struct, is compared
by the raw representation of the key, which reads the padding bytes and
orders a pointer member by its address. Use cbmap_init_cc(3) for such a
key type.

The macro stops the program through ccol__fatal_err() if the creation
fails, and it uses the default allocator.
 
Only the key type reaches the container; the declared value type is read
by the type-inferred macros alone.

cbmap_declare(sorted, int, char *);
cbmap_init(sorted);
int k = 42;
cbmap_insert(sorted, k, "hello");
k = 10;
cbmap_insert(sorted, k, "world");    /* The walk visits 10 before 42 */
cbmap_destroy(sorted);

cbmap_init_mp(3), cbmap_init_cc(3), cbmap_init_full(3), cbmap_declare(3), cbmap_declare_scoped(3), cbmap_construct(3), cbstmap_begin_iter(3), cbstmap_create_full(3), cbmap_destroy(3)