chashmap · Section 3
chmap_construct_ch
NAME
chmap_construct_ch - declare and create a hash map that hashes its keys with a custom procedure
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chashmap.h> chmap_construct_ch(hm_name, KeyT, ValT, custom_hashing_proc);
DESCRIPTION
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. It is chmap_construct(3) with a hashing procedure added, and it uses the default allocator. chmap_construct_mp(3) swaps the allocator instead, chmap_construct_full(3) takes both, and chmap_construct_ch_scoped(3) is this macro with a cleanup attribute that destroys the map at scope exit. A custom procedure only changes how a key is turned into a number. The implementation that the map picks from KeyT and ValT stays the same, and so does the set of keys the map considers equal. The two-step form is chmap_declare(3) followed by chmap_init_ch(3). Use it when a creation failure has to be handled instead of being fatal.
PARAMETERS
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
RETURN VALUE
None. The macro declares variables and is not an expression.
CUSTOM HASHING
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.
NOTES
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 chmap_destroy(3) destroys the map. 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
EXAMPLES
unsigned long hash_bytes(const void *p, size_t size); chmap_construct_ch(scores, char *, int, hash_bytes); int score = 42; chmap_insert(scores, "alice", score); chmap_destroy(scores);
SEE ALSO
chmap_construct(3), chmap_construct_ch_scoped(3), chmap_construct_mp(3), chmap_construct_full(3), chmap_init_ch(3), chmap_declare(3), chmap_redeclare(3), chashmap_create_full(3), chmap_destroy(3)