C Collections

chashmap · Section 3

chmap_construct_full

chmap_construct_full - declare and create a hash map with custom allocation, hashing and key equality

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
chmap_construct_full(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 the map. This is the widest of the construct macros.
chmap_construct(3) is this macro with the default allocator and the
map's own hashing, chmap_construct_mp(3) gives only an allocator, and
chmap_construct_ch(3) gives only a hashing procedure.
 
If the creation fails, the program stops through ccol__fatal_err(). When
a failure has to be handled instead of being fatal, use
chmap_init_full(3) on a handle that is already declared.
 
The map picks its implementation from the key and value types, 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
ValT                Value type
mmgmt_procs          Pointer to 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 key type
custom_key_equality_proc
                     A ccol_key_equality_proc, or NULL to use the map's
                     own key comparison

A custom procedure gets 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 (a sum, a small identifier, a truncated checksum) still
distributes well. That is why the procedure's only contract is "any
function of the key that respects equality", with no rule about which
end of the result carries the entropy.

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).

None. The macro declares a variable and is not an expression.

chmap_destroy(3) destroys the handle; use chmap_construct_full_scoped(3)
when leaving the scope should destroy it instead
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

unsigned long my_hash(const void *key, size_t len);
 
chmap_construct_full(cache, char *, int, &my_procs, my_hash, NULL);
int score = 42;
chmap_insert(cache, "alice", score);
chmap_destroy(cache);

chmap_construct(3), chmap_construct_mp(3), chmap_construct_ch(3), chmap_construct_full_scoped(3), chmap_init_full(3), chashmap_create_full(3), chmap_destroy(3)