chashmap · Overview
chashmap
NAME
CHASHMAP - overview of the chashmap hash map
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chashmap.h>
DESCRIPTION
A chmap maps keys to values, and an insert, a lookup and a delete take O(1) time on average. When the map is created it picks one of two implementations. It uses open addressing when the key and the value are both integral types of eight bytes or less (long double does not count), and separate chaining for every other combination and for any map with a custom key equality procedure. The API is the same either way, and chashmap_create_full(3) describes the selection, the hashing and how the map resists hash flooding. The API has two layers. The type-inferred macros, such as chmap_construct(3), chmap_insert(3), chmap_get(3), chmap_get_ptr(3) and chmap_remove(3), keep the key type and the value type with the handle, and on a hard error they stop the program through ccol__fatal_err(). The raw functions, chashmap_create_full(3), chashmap_insert_elem(3), chashmap_get_elem_ref(3), chashmap_get_elem_copy(3) and chashmap_remove_elem(3), take every key and value as a cmap_pair and return a ccol_retval. The typed layer also has chmap_destroy(3), chmap_size(3) and chmap_reset(3), short names for chashmap_destroy(3), chashmap_size(3) and chashmap_reset(3).
OWNERSHIP OF KEYS AND VALUES
The map keeps its own copy of every key and value. An insert copies the bytes of the key and the value into storage the map owns, and an update frees the old value. A char * key or value is a NUL-terminated string, and the map copies its contents together with the terminator. Your own string is yours again as soon as the insert returns, so you can change or free it without affecting the map. chmap_get(3), chmap_get_ptr(3), chashmap_get_elem_ref(3) and an iterator give back string pointers into the map's storage, which you must not free. Such a pointer is valid until the next insert, delete, reset or resize of the map, or until the map is destroyed. To replace a stored string, insert the key again. A value of any other pointer type, a struct point * for example, is kept as the pointer itself. The map does not copy, follow or free the object at that address. That object belongs to the caller and must be freed before or during the destroy of the map, and chashmap_destroy_with_dtor(3) can run a destructor on each value during the destroy for that purpose. An array type such as int[2] or char[16] cannot be the key or value type of the type-inferred macros. It is a compile error, and the message says to wrap the array in a struct: 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.
REFERENCES INTO THE MAP
A pointer from chmap_get_ptr(3) or chashmap_get_elem_ref(3) is valid until the map changes, which means an insert, a delete, a reset or a resize. A lookup invalidates nothing, so you can hold pointers to many values at once. With both implementations each such pointer is aligned for the value type.
THREAD SAFETY
A map has no internal lock. You can share one map between threads, but then every call that changes the map must be serialized against all other calls on that map.
SEE ALSO
chmap_construct(3), chashmap_create_full(3), chmap_insert(3), chmap_get_ptr(3), chashmap_get_elem_ref(3), chashmap_destroy_with_dtor(3), chashmap_begin_iter(3), cbstmap(7), citerators(7)