chashmap · Section 3
chashmap_create_ch
NAME
chashmap_create_ch - create a hash map with a custom hashing procedure
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chashmap.h>
chmap chashmap_create_ch(size_t initial_bucket_array_size,
ccol_data_type key_type, ccol_data_type val_type,
ccol_hashing_proc custom_hashing_proc, char **err);
DESCRIPTION
Creates a hash map that hashes its keys with the given procedure. It is a thin inline wrapper over chashmap_create_full(3) that passes NULL for the memory management procedures, so the map uses the default allocator. The map picks its implementation from the key and value types, as chashmap_create_full(3) describes, and a hashing procedure does not change that choice.
CUSTOM HASHING
The 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 length is the size that the map stores for that key:
- For a char * key: strlen + 1.
- For a key of a fixed size (an integer, a float, a double, a pointer
or a struct): The sizeof() of the type.
- For a key that goes through the raw layer, for example
chashmap_insert_elem(3): cmap_pair.size.
So the procedure needs no terminator and no rule of its own to find the
end of a key, and it can hash a binary key whose length is known only at
run time.
The result doesn't have to avalanche. The map runs whatever the
procedure returns through its own bit-mixing step before reducing it to
a bucket or a slot, so a procedure that keeps all of its entropy in the
low bits (a sum, a small identifier, a truncated checksum) still
distributes well. The contract is simply any function of the key that
respects equality.
PARAMETERS
initial_bucket_array_size Initial number of buckets. A value of 0 is
refused and the call fails; a value from 1
to 16 gives 16 buckets, and a larger value is
rounded up to the next power of two
key_type Key type, as a ccol_data_type
val_type Value type, as a ccol_data_type
custom_hashing_proc Hashing procedure, or NULL to use the map's own
hashing for the key type
err Optional pointer that receives an error string on
failure, or NULL
RETURN VALUE
A newly created map on success. NULL on failure, with *err set to an error string when err is non-NULL.
NOTES
A failure is reported, not fatal; the chmap_construct_ch(3) macro layer calls ccol__fatal_err() instead
SEE ALSO
chashmap_create(3), chashmap_create_full(3), chmap_construct_ch(3), chmap_init_ch(3), chmap_destroy(3)