cbstmap · Section 3
cbmap_construct_cc
NAME
cbmap_construct_cc - declare and create an ordered map ordered by a custom comparison procedure
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cbstmap.h> cbmap_construct_cc(hm_name, KeyT, ValT, 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 whose keys are ordered by custom_comparison_proc instead of the default order for KeyT. Think of it as cbmap_construct(3) with a comparison procedure added; the allocator is the default one. cbmap_construct_mp(3) replaces the allocator instead, and cbmap_construct_full(3) takes both. cbmap_construct_cc_scoped(3) is the same macro with a cleanup attribute that destroys the map when the scope exits. A struct KeyT needs this variant. Without a procedure the map orders a key type that it doesn't recognize by a raw comparison of the key's representation, which reads the padding bytes and orders a pointer member by its address. The two-step form is cbmap_declare(3) followed by cbmap_init_cc(3). Use it when the caller has to handle a creation failure and that failure must not be fatal.
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
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 it orders after the second, and zero when the map must treat the two as the same key. Since a zero turns an insert into an update, the procedure defines key equality as well as key order. The procedure must give a strict weak order over KeyT, and that order must not change while the map holds elements: The same pair of keys always compares the same way, swapping the two gives the opposite sign, and the order is transitive. An inconsistent procedure breaks the placement invariant of the tree, and after that the map can't find keys that it holds. The procedure gets two bare pointers and no lengths, so it can only read a width that KeyT fixes. That is why the map checks the width of an incoming key against KeyT and aligns its stored keys for KeyT; the procedure decides the order of the keys and nothing else.
NOTES
The macro stops the program through ccol__fatal_err() if the creation fails, and it uses the default allocator. The procedure must stay callable for the whole life of the map. A walk visits the elements in ascending key order, by the order of this procedure; cbstmap_begin_iter(3) describes that contract. Destroy the map with cbmap_destroy(3). The companion type variables follow the scope of the handle, so a function that receives the handle has to call cbmap_redeclare(3) first. 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.
EXAMPLES
typedef struct { int major; int minor; } version; int version_cmp(const void *a, const void *b) { version x, y; memcpy(&x, a, sizeof(x)); /* a key can be at any address */ memcpy(&y, b, sizeof(y)); if (x.major != y.major) { return (x.major > y.major) - (x.major < y.major); } return (x.minor > y.minor) - (x.minor < y.minor); } cbmap_construct_cc(releases, version, char *, version_cmp); cbmap_destroy(releases);
SEE ALSO
cbmap_construct(3), cbmap_construct_cc_scoped(3), cbmap_construct_mp(3), cbmap_construct_full(3), cbmap_init_cc(3), cbmap_declare(3), cbmap_redeclare(3), cbstmap_begin_iter(3), cbstmap_create_full(3), cbmap_destroy(3)