cbstmap · Section 3
cbstmap_insert_elem
NAME
cbstmap_insert_elem - insert a key-value pair into the BST map
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cbstmap.h>
ccol_retval cbstmap_insert_elem(cbmap cbm,
const cmap_pair *key_pair,
const cmap_pair *val_pair);
DESCRIPTION
Inserts a key-value pair into the BST map, or replaces the old value when the key is already there, and then rebalances the tree with AVL rotations.
PARAMETERS
cbm BST map to insert into key_pair Key to insert (the function copies it into the map) val_pair Value for that key (the function copies it into the map)
RETURN VALUE
ccol_success - A new key was inserted
ccol_key_already_present - The key was already in the map, and its
value was updated
ccol_container_full - The map holds ccol_max_elem_count elements
ccol_not_enough_memory - A memory allocation failed
ccol_invalid_args - key_pair or val_pair is NULL, one of the
two pairs 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
NOTES
The ptr of a key_pair can have any alignment, because the comparison reads the key bytes with memcpy and never through a pointer cast to the key type. key_pair->ptr may point straight at key bytes at an arbitrary offset in a larger buffer, such as a field sliced out of a packed network frame or a memory-mapped record, with no copy into an aligned local first. The insert is O(log n), and the key data and the value data are copied into the map. When the key is already present, the old value is freed and the new one takes its place. The function asserts if cbm is NULL. A NULL key_pair or val_pair is reported as ccol_invalid_args, as chashmap_insert_elem(3) does. A 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. A size of 0 is valid for the value, and for the key of a map with ccol_other_types keys, with or without a ptr, and stores an empty value or an empty key. A string key (ccol_string) counts its terminating NUL, so its size is at least 1, and a key_pair of size 0 on such a map is ccol_invalid_args, as in chashmap_insert_elem(3). chashmap_insert_elem(3) also refuses a value, and a key of any type, of size zero.
SEE ALSO
cbmap_insert(3), cbstmap_remove_elem(3), cbstmap_get_elem_ref(3)