C Collections

cbstmap · Section 3

cbstmap_remove_elem

cbstmap_remove_elem - remove a key-value pair from the BST map

C Collections (libccollections, -lccollections)

#include <ccollections/cbstmap.h>
 
ccol_retval cbstmap_remove_elem(cbmap cbm, const cmap_pair *key_pair);

Removes the key-value pair with the given key from the BST map, frees the
memory of its key and value, and rebalances the tree with AVL rotations.

cbm        BST map to remove from
key_pair   Key to remove

ccol_success        - The key was removed
ccol_key_not_found  - The key is not in the map
ccol_invalid_args   - key_pair is NULL, key_pair carries a NULL ptr
                      with a non-zero size, the size of key_pair
                      doesn't match the size of a fixed-width key type
                      (see cbstmap_create_full(3)), or the map has
                      ccol_string keys and the size of key_pair is 0

The ptr of a key_pair needs no particular alignment, because the
comparison reads the key bytes with memcpy and never through a pointer
cast to the key type. You can point key_pair->ptr straight at key bytes
at any offset inside a larger buffer, for example a field sliced out of a
packed network frame or a memory-mapped record, without first copying
them into an aligned local.
 
The removal is O(log n). The function asserts if cbm is NULL.
 
A NULL key_pair is reported as ccol_invalid_args, as chashmap_remove_elem(3)
does. A key_pair whose ptr is NULL and whose size is not zero promises
bytes it has no storage for, so it is ccol_invalid_args too.
 
On a map with ccol_other_types keys a key_pair of size zero is valid,
with or without a ptr, and removes the empty key.

cbmap_remove(3), cbstmap_insert_elem(3)