C Collections

cbstmap · Section 3

cbmap_insert

cbmap_insert - insert a key-value pair (type-inferred)

C Collections (libccollections, -lccollections)

#include <ccollections/cbstmap.h>
 
cbmap_insert(hm_name, key, val);

Type-inferred wrapper for cbstmap_insert_elem() that builds the key's
cmap_pair and the value's cmap_pair from the caller's expressions. When
the key is already in the map the value is updated and nothing is
reported; ccol__fatal_err() is called only on a real failure, which means
ccol_not_enough_memory or ccol_container_full. The tree is rebalanced
after the insert.
 
key and val are converted through the declared key type and value type of
the map, as NOTES describes.

hm_name  BST map to insert into
key      Key to insert
val      Value for that key

key and val go through the DECLARED key type and value type of the tree.
The macro makes a private copy of each argument with that type by a plain
C assignment, so the value is CONVERTED and never reinterpreted byte for
byte: 1.5f inserted into a map of int becomes 1, the same as
"int x = 1.5f;", and not 1069547520. An argument whose type has no
implicit conversion to the declared type is a compile error at the call
site. cvec_push(3) follows the same rule.
 
Since the macro copies instead of taking an address, it accepts an lvalue,
an rvalue, a literal and a computed expression alike.
 
The one exception, which converts nothing, is a declared key or value
type that is a CHARACTER POINTER. char *, signed char * and
unsigned char * are a single type to this library: All three classify as
ccol_string, a NUL-terminated string that the macro measures with strlen,
and the tree compares the bytes of the string, never the pointer type. A
map keyed on unsigned char * can therefore be reached through a char *
that addresses the same string. An array key, such as a char buf[64] or a
string literal, decays to a pointer as in any other assignment.
 
A macro can't know the length of the buffer behind a pointer, so a
character pointer is never a binary key. A binary key takes one of two
forms. A key of fixed length, such as a digest, can be a struct that wraps
an array, such as
struct { unsigned char b[32]; }; the tree orders by all of its bytes,
zero bytes included. A key whose length is only known at run time goes
through the raw cbstmap_insert_elem(3) layer, with that length in
cmap_pair.size.
 
A NULL character pointer is not a string, so a NULL key or value of a
character-pointer type stops the program with ccol_invalid_args.
 
Both value types and string types are handled correctly.

cbstmap_insert_elem(3), cbstmap_create_full(3), cbmap_get(3), cbmap_remove(3)