chashmap · Section 3
chmap_construct
NAME
chmap_construct - type-inferred hash map construction
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chashmap.h> chmap_construct(hm_name, key_type, val_type);
DESCRIPTION
Type-inferred macro that creates a hash map with the default settings. It works out the key and value types and also creates type marker variables, which is how the other type-dispatching macros know the declared key and value types at each call site.
PARAMETERS
hm_name Name of the hash map variable to create key_type Type of keys val_type Type of values
NOTES
Stops the program if the allocation fails Creates hidden type marker variables that the other type-dispatching macros read, and chmap_redeclare(3) creates them again in another scope Uses CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE (16) buckets Uses the default memory management and the default hashing 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
FLOAT AND DOUBLE KEY EQUALITY
For a float or double key_type, two keys are equal when their bits are equal, and IEEE 754 == plays no part, with one exception: -0.0 and 0.0 are made into the same key, which matches -0.0 == 0.0 in C. Every other bit pattern, NaN included, is compared bit by bit, so a NaN key is always found again by the exact bit pattern it was inserted with (in C, NaN == NaN is always false). Two different NaN payloads are two different keys.
LONG DOUBLE KEY EQUALITY
For a long double key_type, key equality compares the numeric value (native ==) instead of the bits. -0.0L and 0.0L are the same key, and two keys with the same value stay one key even when their platform-defined padding bits differ. Every NaN long double is the same key, unlike float and double, where each distinct NaN payload is a different key. None of this value-based handling applies when a custom hashing function is given at creation time; see chashmap_create_full(3).
STRUCT KEY EQUALITY
For a key type the map doesn't know, such as a struct, key equality compares every byte of the key, padding included, and the default hash reads every byte too. With GCC 11 or later the macros zero the padding of their own copy of a key. With any other compiler that padding is unspecified, so a padded struct key needs a custom hashing function and a custom key equality function, which you give through chmap_construct_full(3). See chashmap_create_full(3).
EXAMPLES
chmap_construct(map, int, char*); int key = 42; chmap_insert(map, key, "hello"); chmap_destroy(map);