C Collections

chashmap · Section 3

chmap_init_full

chmap_init_full - create a hash map with a custom allocator, hashing procedure and key equality procedure into an already declared handle

C Collections (libccollections, -lccollections)

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

Creates a fully customized hash map and assigns it to hm_name. The
handle has to be declared first, with chmap_declare(3) or
chmap_declare_scoped(3). The key and value types are not arguments,
because the macro reads them from the companion type variables that the
declare macro created.
 
This is the widest of the init macros. chmap_init(3) is this macro with
the default allocator and the map's own hashing, chmap_init_mp(3) gives
only an allocator, and chmap_init_ch(3) gives only a hashing procedure.
Any of the three arguments can be NULL to get the matching default, so
the same macro also serves a call site that chooses them at run time.
 
The one-step equivalents are chmap_construct_full(3) and
chmap_construct_full_scoped(3).

hm_name              The already declared map handle to create the map into
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 the declared key type
custom_key_equality_proc
                     A ccol_key_equality_proc, or NULL to use the map's
                     own key comparison

None. The macro expands to a statement 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.

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; call
chashmap_create_full(3) directly when a failure has to be handled instead
of being fatal
Uses CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE (16) buckets; call
chashmap_create_full(3) for a different initial bucket count
The macro copies the memory management procedures into the map, so your
own ccol_memmgmt_procs doesn't need to outlive the call. Those
procedures, the hashing procedure and the key equality procedure must
stay callable for the whole life of the map
A custom hashing procedure leaves the implementation that the map picks
from the declared key and value types alone, but a custom key equality
procedure selects separate chaining

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);
 
chmap_declare(scores, char *, int);
chmap_init_full(scores, &procs, hash_bytes, NULL);
int score = 42;
chmap_insert(scores, "alice", score);
chmap_destroy(scores);

chmap_init(3), chmap_init_mp(3), chmap_init_ch(3), chmap_declare(3), chmap_construct_full(3), chmap_construct_full_scoped(3), chashmap_create_full(3), chmap_destroy(3)