cbstmap · Section 3
cbmap_construct_full
NAME
cbmap_construct_full - declare and create an ordered map with custom allocation and comparison
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cbstmap.h>
cbmap_construct_full(hm_name, KeyT, ValT, mmgmt_procs,
custom_comparison_proc);
DESCRIPTION
Declares an ordered map handle named hm_name with the companion type variables that carry KeyT and ValT to every later type-inferred macro, then creates a map that takes its memory from mmgmt_procs and orders its keys with custom_comparison_proc. This is the widest of the construct macros. cbmap_construct(3) is the same thing with the default allocator and the default order, cbmap_construct_mp(3) passes only an allocator and cbmap_construct_cc(3) only a comparison procedure. Either argument can be NULL to select the matching default, so this one macro covers every combination. If the creation fails the macro stops the program through ccol__fatal_err(). When the caller has to handle a failure without it being fatal, declare the handle first and use cbmap_init_full(3). cbmap_construct_full_scoped(3) is the same macro with a cleanup attribute that destroys the map when the scope exits.
PARAMETERS
hm_name Name for the map handle to declare
KeyT Key type. Every later macro reads a key through
it, and the container takes from it the width to
expect and the alignment of a stored key
ValT Value type. Only the type-inferred macros read
it; the library never passes it to the container
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 KeyT
RETURN VALUE
None. The macro declares variables and doesn't expand to an expression.
CUSTOM COMPARISON
The procedure receives pointers to two keys. It returns a negative value when the first key orders before the second, a positive value when the first orders after the second, and zero when the map must treat them as the same key. A zero makes the call an update rather than an insert, so the procedure defines key equality together with key order. The order it gives must be a strict weak order over KeyT that doesn't change while the map holds elements: One pair of keys always compares the same way, the reverse comparison gives the opposite sign, and the order is transitive. An inconsistent procedure breaks the placement invariant of the tree, after which the map can't find a key that it holds. The procedure gets two bare pointers and no lengths, so it can only read a width that KeyT fixes. cbstmap_create_full(3) describes the default orders that a procedure replaces, and the order that a struct key type gets when there is no procedure.
NOTES
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. Every tree node of the map is allocated through these procedures, and everything the map frees goes back through them. A walk visits the elements in ascending key order, by the order that the map was given; cbstmap_begin_iter(3) describes that contract. Destroy the map with cbmap_destroy(3), or use cbmap_construct_full_scoped(3) to have the exit of the scope destroy it. The companion type variables follow the scope of the handle, so a function that receives the handle needs cbmap_redeclare(3) there. An array key type or value type, such as int[2] or char[16], gives a compile error whose message says to wrap the array in a struct. The macros copy keys and values by assignment, C cannot assign an array, and the map would otherwise store the address of the caller's array rather than its elements. A struct holding the array works.
EXAMPLES
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_construct_full(releases, version, char *, &procs, version_cmp);
cbmap_destroy(releases);
SEE ALSO
cbmap_construct(3), cbmap_construct_mp(3), cbmap_construct_cc(3), cbmap_construct_full_scoped(3), cbmap_init_full(3), cbmap_declare(3), cbmap_redeclare(3), cbstmap_begin_iter(3), cbstmap_create_full(3), cbmap_destroy(3)