cbstmap · Section 3
cbmap_construct_cc_scoped
NAME
cbmap_construct_cc_scoped - declare and create an ordered map with a custom comparison procedure, destroyed on scope exit
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cbstmap.h> cbmap_construct_cc_scoped(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. The handle also gets the cleanup attribute, which destroys the map on every exit from the enclosing scope, an early return included. That cleanup attribute is the only difference from cbmap_construct_cc(3). cbmap_construct_scoped(3) is the same macro with the default order, cbmap_construct_mp_scoped(3) takes an allocator where this one takes a comparison procedure, and cbmap_construct_full_scoped(3) takes both. Use this scoped variant for a struct KeyT, because a key type that the map doesn't recognize is ordered by default by a raw comparison of the key's representation.
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 and returns a negative value when the first key orders before the second, a positive value when it orders after it, 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 both key equality and key order. It must give a strict weak order over KeyT, and that order must not change while the map holds elements. With only two bare pointers and no lengths, the procedure can read no more than the width that KeyT fixes, so the map checks the width of an incoming key against KeyT and aligns its stored keys for KeyT. The procedure must stay callable for the whole life of the map, which in this case ends at the closing brace of the enclosing scope.
NOTES
The macro stops the program through ccol__fatal_err() if the creation fails. It needs GCC or Clang because of the cleanup attribute, and it uses the default allocator. A walk visits the elements in ascending key order, by the order of this procedure; cbstmap_begin_iter(3) describes that contract. When the scope exits the map is destroyed and the handle cleared, so an earlier cbmap_destroy(3) inside the scope is also fine. The companion type variables follow the scope of the handle, and 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], doesn't compile; 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 without that error the map would store 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 and don't store it anywhere that lives longer than the scope, because the cleanup attribute frees its map when the scope exits and you'd be handing out a handle to freed memory.
EXAMPLES
typedef struct { int major; int minor; } version; int version_cmp(const void *a, const void *b); void summarize(version *v, size_t n) { cbmap_construct_cc_scoped(releases, version, int, 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_cc(3), cbmap_construct_scoped(3), cbmap_construct_mp_scoped(3), cbmap_construct_full_scoped(3), cbmap_declare_scoped(3), cbmap_init_cc(3), cbmap_redeclare(3), cbstmap_begin_iter(3), cbmap_destroy(3)