C Collections

cbstmap · Section 3

cbstmap_get_elem_copy

cbstmap_get_elem_copy - copy the value associated with a key out of a BST map

C Collections (libccollections, -lccollections)

#include <ccollections/cbstmap.h>
 
ccol_retval cbstmap_get_elem_copy(cbmap cbm, const cmap_pair *key_pair,
                                    void *target_buf, size_t target_buf_size);

Looks key_pair up in the map and copies its value into target_buf. The
buffer's size has to match the size of the stored value exactly. The
function never makes a partial copy, so a value is never truncated
without a report. That is stricter than chashmap_get_elem_copy(3), which
copies min(value_size, target_buf_size) bytes.

cbm              BST map to search
key_pair         Key to look up
target_buf       Buffer that receives the copy of the value
target_buf_size  Size of target_buf, which must equal the size of the
                 stored value

ccol_success        - The key was found and its value copied into
                      target_buf
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, target_buf is NULL
                      with a non-zero target_buf_size,
                      target_buf_size doesn't
                      match the size of the stored value, 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 comparison reads the key bytes with memcpy, never through a pointer
cast to the key type, so the ptr of a key_pair needs no particular
alignment. key_pair->ptr can point straight at key bytes at an arbitrary
offset in a larger buffer, such as a field of a packed network frame or of
a memory-mapped record, with no copy into an aligned local first.
 
The lookup is O(log n). The two value sizes must match exactly, which
chashmap_get_elem_copy(3) doesn't require. On any failure target_buf is left
untouched. The function asserts if cbm is NULL.
 
A NULL key_pair is reported as ccol_invalid_args, as in
chashmap_get_elem_copy(3), and so is a key_pair whose ptr is NULL while its
size is not zero, since it promises bytes it has no storage for.
 
On a map with ccol_other_types keys a key_pair of size zero is valid, with
or without a ptr, and looks up the empty key.

cbstmap_get_elem_ref(3), cbstmap_insert_elem(3), cbstmap_create_full(3)