cbstmap · Section 3
cbmap_construct_scoped
NAME
cbmap_construct_scoped - declare and create an ordered map that is destroyed on scope exit
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cbstmap.h> cbmap_construct_scoped(hm_name, KeyT, ValT);
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 the map with the default allocator and 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 attribute is the only difference from cbmap_construct(3). This macro fits a map whose whole useful life is one block; use cbmap_construct(3) when your code returns the handle, stores it, or destroys it somewhere the compiler can't see. The two-step form is cbmap_declare_scoped(3) followed by cbmap_init(3), for a map whose configuration comes from inside the scope rather than from the declaration.
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 the order of the tree from it
ValT Value type. Only the type-inferred macros read it; the library
never passes it to the container
RETURN VALUE
None. The macro declares variables and doesn't expand to an expression.
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(3). The cleanup attribute means it needs GCC or Clang. The map keeps its elements in key order and a walk visits them in ascending key order. cbstmap_begin_iter(3) describes that contract and cbstmap_create_full(3) the order that each key type gets. For a struct KeyT the default order is a raw comparison of the key's representation, so use cbmap_construct_cc_scoped(3) for such a key. 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
void report(int *ids, size_t n) { cbmap_construct_scoped(seen, int, int); int one = 1; for (size_t i = 0; i < n; i++) { cbmap_insert(seen, ids[i], one); } /* The walk visits the smallest id first */ ccol_iter_declare(seen, it); for (it = ccol_begin(seen); it; it = ccol_iter_next(it)) { print_id(*ccol_iter_key_ptr(it)); } } /* The macro destroys seen here */
SEE ALSO
cbmap_construct(3), cbmap_construct_mp_scoped(3), cbmap_construct_cc_scoped(3), cbmap_construct_full_scoped(3), cbmap_declare_scoped(3), cbmap_init(3), cbmap_redeclare(3), cbstmap_begin_iter(3), cbmap_destroy(3)