cbstmap · Overview
cbstmap
NAME
CBSTMAP - overview of the cbstmap ordered map
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cbstmap.h>
DESCRIPTION
A cbmap maps keys to values and keeps its keys sorted. Underneath it is a self-balancing AVL tree, so an insert, a lookup and a delete each take O(log n) time in the worst case, whatever order the keys arrive in. An iteration visits the entries in ascending key order, and that order is part of the contract (see cbstmap_begin_iter(3)). The key type picks the default order: - char: The native order of char. - The signed integer types: A signed order. - The unsigned integer types and pointers: An unsigned order. - The floating types: A numeric order, in which a NaN sorts above all other values. - A string: The strcmp order. - Every other key type, a struct for example: A raw memcmp of the key bytes. A custom comparison procedure replaces this order. cbstmap_create_full(3) gives the complete rules, and cbmap_construct_cc(3) the contract of a comparison procedure. There are two layers in the API. The type-inferred macros, such as cbmap_construct(3), cbmap_insert(3), cbmap_get(3), cbmap_get_ptr(3) and cbmap_remove(3), keep the key type and the value type with the handle and stop the program through ccol__fatal_err() on a hard error. The raw functions, cbstmap_create_full(3), cbstmap_insert_elem(3), cbstmap_get_elem_ref(3), cbstmap_get_elem_copy(3) and cbstmap_remove_elem(3), take each key and value as a cmap_pair and return a ccol_retval. The typed layer also has cbmap_destroy(3), cbmap_size(3) and cbmap_reset(3), short names for cbstmap_destroy(3), cbstmap_size(3) and cbstmap_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 that the map owns, and an update frees the old value. A char * key or value is a NUL-terminated string. The map copies the contents of the string together with its terminator, and the caller's string stays the caller's, free to change or be freed as soon as the insert returns. cbmap_get(3), cbmap_get_ptr(3), cbstmap_get_elem_ref(3) and an iterator give back string pointers into the map's own storage, so don't free them. Such a pointer is valid until the next insert, the next delete, a reset or the destroy of the map. To replace a stored string, insert the key again. A value of any other pointer type is stored as the pointer itself. The map doesn't copy, follow or free the object at that address; the caller must free those objects, for example by walking over the map and freeing them right before cbmap_destroy(3). An array type, such as int[2] or char[16], can't be the key type or the value type of the type-inferred macros. It is a compile error whose message says to wrap the array in a struct, because C cannot assign an array and the map would otherwise store the address of the caller's array instead of its elements. A struct that holds the array works.
THREAD SAFETY
A map has no internal lock. One map can be shared between threads, but then the caller must serialize every call that changes the map against all other calls on that map.
SEE ALSO
cbmap_construct(3), cbmap_construct_cc(3), cbstmap_create_full(3), cbmap_insert(3), cbmap_get_ptr(3), cbstmap_begin_iter(3), chashmap(7), citerators(7)