cbstmap · Section 3
cbmap_construct_full_scoped
NAME
cbmap_construct_full_scoped - declare and create an ordered map with custom allocation and comparison, destroyed on scope exit
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cbstmap.h>
cbmap_construct_full_scoped(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. The handle also gets the cleanup attribute, so the map is destroyed on every exit from the enclosing scope, an early return included. It is cbmap_construct_full(3) with that cleanup attribute added, and the widest of the scoped construct macros. cbmap_construct_scoped(3) is the same with the default allocator and the default order, cbmap_construct_mp_scoped(3) passes only an allocator, and cbmap_construct_cc_scoped(3) only a comparison procedure. Either argument can be NULL to select the matching default, which lets this one macro cover every combination.
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 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. Because a zero turns an insert into an update, the procedure defines key equality as well as key order. It must give a strict weak order over KeyT that doesn't change while the map holds elements. It 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.
NOTES
The macro stops the program through ccol__fatal_err() if the creation fails. When the caller has to handle a failure instead, use cbmap_declare_scoped(3) with cbmap_init_full(3). The cleanup attribute means the macro needs GCC or Clang. 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, which here ends at the closing brace of the enclosing scope. A walk visits the elements in ascending key order, by the order that the map was given; cbstmap_begin_iter(3) describes that contract. The exit of the scope destroys the map and clears the handle, so an explicit cbmap_destroy(3) earlier in the scope is fine too. 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], is a compile error, and the message says to wrap the array in a struct. Keys and values are copied by assignment and C can't assign an array, so the map would otherwise keep the address of the caller's array instead of its elements. A struct that holds the array works.
WARNINGS
The handle must not outlive its scope. Don't return it or store it anywhere that lives longer than the scope: The cleanup attribute frees the map when the scope exits, and the stored handle would then name a freed map.
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);
void summarize(version *v, size_t n) {
cbmap_construct_full_scoped(releases, version, int, &procs, version_cmp);
int one = 1;
for (size_t i = 0; i < n; i++) {
cbmap_insert(releases, v[i], one);
}
} /* The macro destroys the releases map here */
SEE ALSO
cbmap_construct_full(3), cbmap_construct_scoped(3), cbmap_construct_mp_scoped(3), cbmap_construct_cc_scoped(3), cbmap_declare_scoped(3), cbmap_init_full(3), cbmap_redeclare(3), cbstmap_begin_iter(3), cbmap_destroy(3)