C Collections

chashmap · Section 3

chmap_init_ch

chmap_init_ch - create a hash map with a custom hashing procedure into an already declared handle

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
chmap_init_ch(hm_name, custom_hashing_proc);

Creates a hash map that hashes its keys with custom_hashing_proc instead
of the map's own hashing for the declared key type, 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.
 
It is chmap_init(3) with a hashing procedure added, and it uses the
default allocator. chmap_init_mp(3) swaps the allocator instead, and
chmap_init_full(3) takes both.
 
A custom procedure only changes how a key is turned into a number. The
implementation the map picks stays the same, and so does the set of keys
the map considers equal.
 
The one-step equivalents are chmap_construct_ch(3) and
chmap_construct_ch_scoped(3).

hm_name              The already declared map handle to create the map into
custom_hashing_proc  A ccol_hashing_proc, or NULL to use the map's own
                     hashing for the declared key type

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. For a fixed-width key type the length is
the map's fixed key width, and for a string or other buffer-like key it
is the byte length the caller gave. So the procedure can always hash
exactly that many bytes and needs no NUL terminator or length prefix of
its own.
 
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, like a sum, a small identifier or a truncated checksum, still
spreads across the table. The only contract is "any function of the key
that respects the map's notion of key equality", with no rule about
which end of the result carries the entropy.

Stops the program through ccol__fatal_err() if the creation fails
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, so
a custom procedure never gets a raw -0.0
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
The procedure must stay callable for the whole life of the map

unsigned long hash_bytes(const void *p, size_t size);
 
chmap_declare(scores, char *, int);
chmap_init_ch(scores, hash_bytes);
int score = 42;
chmap_insert(scores, "alice", score);
chmap_destroy(scores);

chmap_init(3), chmap_init_mp(3), chmap_init_full(3), chmap_declare(3), chmap_construct_ch(3), chashmap_create_full(3), chmap_destroy(3)