C Collections

chashmap · Section 3

chmap_construct_ch_scoped

chmap_construct_ch_scoped - declare and create a hash map with a custom hashing procedure, destroyed on scope exit

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
chmap_construct_ch_scoped(hm_name, KeyT, ValT, custom_hashing_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 hashes its keys with custom_hashing_proc
instead of 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_ch(3).
chmap_construct_scoped(3) is the same macro with the map's own hashing,
chmap_construct_mp_scoped(3) takes an allocator instead of a hashing
procedure, and chmap_construct_full_scoped(3) takes both.

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
custom_hashing_proc  A ccol_hashing_proc, or NULL to use the map's own
                     hashing for KeyT

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".
 
The procedure must stay callable for the map's whole lifetime, which
here ends at the closing brace of the enclosing scope.

Stops the program through ccol__fatal_err() if the creation fails
Needs GCC or Clang, because it uses the cleanup attribute
Uses the default allocator and CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE (16)
buckets
A float or double key is canonicalized before the procedure sees it
A long double key is the one case where a custom procedure changes how
keys are handled; chashmap_create_full(3) says what such a procedure has to
do
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.

unsigned long hash_bytes(const void *p, size_t size);
 
void tally(char **words, size_t n) {
    chmap_construct_ch_scoped(seen, char *, int, hash_bytes);
    int one = 1;
    for (size_t i = 0; i < n; i++) {
        chmap_insert(seen, words[i], one);
    }
}                                     /* seen is destroyed here */

chmap_construct_ch(3), chmap_construct_scoped(3), chmap_construct_mp_scoped(3), chmap_construct_full_scoped(3), chmap_declare_scoped(3), chmap_init_ch(3), chmap_redeclare(3), chmap_destroy(3)