C Collections

chashmap · Section 3

chmap_construct_full_scoped

chmap_construct_full_scoped - declare and create a hash map with custom allocation, hashing and key equality, destroyed on scope exit

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
chmap_construct_full_scoped(hm_name, KeyT, ValT, mmgmt_procs,
                            custom_hashing_proc,
                            custom_key_equality_proc);

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 a map that takes its memory from mmgmt_procs, hashes
its keys with custom_hashing_proc and compares them with
custom_key_equality_proc. The handle gets the cleanup attribute, which
destroys the map on every exit from the enclosing scope, an early return
included.
 
It is chmap_construct_full(3) with that cleanup attribute, and the
widest of the scoped construct macros. chmap_construct_scoped(3) is this
macro with the default allocator and the map's own hashing,
chmap_construct_mp_scoped(3) gives only an allocator, and
chmap_construct_ch_scoped(3) gives only a hashing procedure. Any of the
three arguments can be NULL to get the matching default, so this one
macro covers every combination.
 
The map picks its implementation from KeyT and ValT, as
chashmap_create_full(3) describes. A custom hashing procedure leaves that
choice alone, but a custom key equality procedure selects separate
chaining.

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
mmgmt_procs          Pointer to a ccol_memmgmt_procs, or NULL for the
                     default allocator
custom_hashing_proc  A ccol_hashing_proc, or NULL to use the map's own
                     hashing for KeyT
custom_key_equality_proc
                     A ccol_key_equality_proc, or NULL to use the map's
                     own key comparison

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

A custom procedure gets a pointer to the key bytes and their length and
returns an unsigned long. It must return the same value for any two keys
that the map considers equal, and for a char * key equal means equal
contents, not an equal pointer.
 
The result doesn't have to avalanche. The map runs whatever a custom
procedure returns through its own bit-mixing step before reducing it to
a slot or a bucket, so a procedure that keeps all of its entropy in the
low bits still spreads across the table. The only contract is "any
function of the key that respects the map's notion of key equality".

custom_key_equality_proc receives both keys and both sizes and returns
true when they are the same key. The map calls it instead of its
built-in comparison. It must agree with custom_hashing_proc, so two keys
that it reports as equal must get the same hash. A key equality
procedure without a hashing procedure makes the creation fail, and this
macro then stops the program. A struct key with padding bytes needs both
procedures; see chashmap_create_full(3).

Stops the program through ccol__fatal_err() if the creation fails; use
chmap_declare_scoped(3) with chmap_init_full(3) when a failure has to be
handled
Needs GCC or Clang, because it uses the cleanup attribute
When you pass a ccol_memmgmt_procs, all four of its members must be
non-NULL. The macro copies the procedures into the map, and like the
hashing and key equality procedures they must stay callable for the
whole life of the map, which here ends at the closing brace of the
enclosing scope
Uses 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
The companion type variables live in the scope of the handle, so a
function that receives the handle needs chmap_redeclare(3) inside it
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.

ccol_memmgmt_procs procs = {
    .malloc = my_malloc, .calloc = my_calloc,
    .realloc = my_realloc, .free = my_free,
};
unsigned long hash_bytes(const void *p, size_t size);
 
void tally(char **words, size_t n) {
    chmap_construct_full_scoped(seen, char *, int, &procs, hash_bytes,
                                NULL);
    int one = 1;
    for (size_t i = 0; i < n; i++) {
        chmap_insert(seen, words[i], one);
    }
}                                     /* seen is destroyed here */

chmap_construct_full(3), chmap_construct_scoped(3), chmap_construct_mp_scoped(3), chmap_construct_ch_scoped(3), chmap_declare_scoped(3), chmap_init_full(3), chmap_redeclare(3), chashmap_create_full(3), chmap_destroy(3)