cbstmap · Section 3
cbmap_init_cc
NAME
cbmap_init_cc - create an ordered map with a custom comparison procedure into an already declared handle
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cbstmap.h> cbmap_init_cc(hm_name, custom_comparison_proc);
DESCRIPTION
Creates an ordered map whose keys are ordered by custom_comparison_proc instead of 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; the macro reads them from the companion type variables that the declare macro created. This is cbmap_init(3) with a comparison procedure, and the allocator is the default one. cbmap_init_mp(3) replaces the allocator instead, and cbmap_init_full(3) takes both. A struct key type needs this variant. Without a procedure the map orders a key type 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 one-step equivalents are cbmap_construct_cc(3) and cbmap_construct_cc_scoped(3).
PARAMETERS
hm_name The map handle that the caller declared earlier,
which receives the new map
custom_comparison_proc A ccol_comparison_proc, or NULL for the
default order for the declared key type
RETURN VALUE
None. The macro expands to a statement, not 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 instead of an insert, which is why the procedure defines key equality as well as key order. The order must be a strict weak order over the key type that doesn't change while the map holds elements. A given pair of keys always compares the same way, the reverse comparison gives the opposite sign, and the order is transitive. If the procedure is inconsistent the placement invariant of the tree breaks, and 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 the declared key type fixes. For that reason the map checks the width of an incoming key against that type and aligns its stored keys for it; 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 follows this procedure: It visits the elements in ascending key order, by the order that the map was given, as cbstmap_begin_iter(3) describes.
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_declare(releases, version, char *); cbmap_init_cc(releases, version_cmp); cbmap_destroy(releases);
SEE ALSO
cbmap_init(3), cbmap_init_mp(3), cbmap_init_full(3), cbmap_declare(3), cbmap_construct_cc(3), cbstmap_begin_iter(3), cbstmap_create_full(3), cbmap_destroy(3)