C Collections

cbstmap · Section 3

cbmap_init_full

cbmap_init_full - create an ordered map with a custom allocator and comparison into an already declared handle

C Collections (libccollections, -lccollections)

#include <ccollections/cbstmap.h>
 
cbmap_init_full(hm_name, mmgmt_procs, custom_comparison_proc);

Creates an ordered map that takes all of its memory from mmgmt_procs and
orders its keys with custom_comparison_proc, then assigns it to hm_name.
Declare the handle first, with cbmap_declare(3) or
cbmap_declare_scoped(3). The key type and the value type are not passed to
this macro; it reads them from the companion type variables that the
declare macro created.
 
This is the widest of the init macros. cbmap_init(3) is the same thing
with the default allocator and the default order, cbmap_init_mp(3) passes
only an allocator and cbmap_init_cc(3) only a comparison procedure. Either
argument can be NULL to select the matching default, so this one macro
covers every combination.
 
The one-step equivalents are cbmap_construct_full(3) and
cbmap_construct_full_scoped(3).

hm_name                 The map handle that the caller declared earlier,
                        which receives the new map
mmgmt_procs             Pointer to a ccol_memmgmt_procs, or NULL for the
                        default allocator
custom_comparison_proc  A ccol_comparison_proc, or NULL for the
                        default order for the declared key type

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

The procedure receives pointers to two keys and returns a negative value
when the first key orders before the second, a positive value when it
orders after the second, and zero when the map must treat the two as the
same key. A zero makes the call an update instead of an insert, so the
procedure defines key equality together with key order.
 
It must give a strict weak order over the key type that doesn't change
while the map holds elements. With two bare pointers and no lengths it can
only read a width that the declared key type fixes, and the map checks the
width of an incoming key against that type and aligns its stored keys for
it. cbstmap_create_full(3) describes the default orders that a procedure
replaces.

The macro stops the program through ccol__fatal_err() if the creation
fails.
 
If the caller passes a ccol_memmgmt_procs, none of its four members may
be NULL. The map copies the procedures into itself, but the procedures and
the comparison procedure must stay callable for the whole life of the map.
 
A walk visits the elements in ascending key order, by the order that the
map was given; cbstmap_begin_iter(3) describes that contract.
 
Only the key type reaches the container; the declared value type is read
by the type-inferred macros alone.

ccol_memmgmt_procs procs = {
    .malloc = my_malloc, .calloc = my_calloc,
    .realloc = my_realloc, .free = my_free,
};
int version_cmp(const void *a, const void *b);
 
cbmap_declare(releases, version, char *);
cbmap_init_full(releases, &procs, version_cmp);
cbmap_destroy(releases);

cbmap_init(3), cbmap_init_mp(3), cbmap_init_cc(3), cbmap_declare(3), cbmap_construct_full(3), cbstmap_begin_iter(3), cbstmap_create_full(3), cbmap_destroy(3)