C Collections

chashmap · Section 3

chmap_construct_scoped

chmap_construct_scoped - declare and create a hash map that is destroyed on scope exit

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
chmap_construct_scoped(hm_name, KeyT, ValT);

Declares a hash map handle named hm_name, along with the companion type
variables that pass KeyT and ValT on to every later type-inferred
macro, and creates the map with the default allocator and the map's own
hashing for KeyT. The handle gets the cleanup attribute, which destroys
the map on every exit from the enclosing scope, an early return
included.
 
The cleanup attribute is the only difference from chmap_construct(3);
the allocator, the hashing and the implementation chosen from KeyT and
ValT are the same. Use this macro when the whole useful life of the map
is one block, and chmap_construct(3) when the handle is returned, stored
or destroyed somewhere the compiler can't see.
 
The two-step form is chmap_declare_scoped(3) followed by chmap_init(3),
for a map whose configuration is decided inside the scope and not at the
declaration.

hm_name  Name for the map handle to declare
KeyT    Key type; it tells every later macro how to read a key and, with
         ValT, which implementation strategy the map uses
ValT    Value type

None. The macro declares variables and is not an expression.

Stops the program through ccol__fatal_err() if the creation fails; use
chmap_declare_scoped(3) with chmap_init(3) when a failure has to be
handled
Needs GCC or Clang, because it uses the cleanup attribute
Uses the default allocator and CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE (16)
buckets
Leaving the scope destroys the map and clears the handle, so an
explicit chmap_destroy(3) earlier in the scope is fine as well, and the
cleanup at scope exit then does nothing
The companion type variables live in the scope of the handle, so a
function that receives the handle needs chmap_redeclare(3) inside it
Destroying the map frees its own copies of the keys and values, but not
anything that a stored pointer value points at
An array key 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. The macros copy
a key or a value by assignment and C cannot assign an array, so the map
would store the address of the caller's array instead of its elements. A
struct that holds the array works

The handle must not outlive its scope. Do not return it and do not store
it anywhere that outlives the scope, because either way you hand out a
handle whose map is freed when the scope exits.

int count_unique(char **words, size_t n) {
    chmap_construct_scoped(seen, char *, int);
    int one = 1;
    for (size_t i = 0; i < n; i++) {
        chmap_insert(seen, words[i], one);
    }
    return (int)chashmap_size(seen);   /* seen is destroyed here */
}

chmap_construct(3), chmap_construct_mp_scoped(3), chmap_construct_ch_scoped(3), chmap_construct_full_scoped(3), chmap_declare_scoped(3), chmap_init(3), chmap_redeclare(3), chmap_destroy(3)