C Collections

cbstmap · Section 3

cbstmap_get_elem_ref

cbstmap_get_elem_ref - get the location and the size of the value of a key

C Collections (libccollections, -lccollections)

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

Finds the value of the given key and fills the caller's cmap_pair with its
location and size: val_pair->ptr points at the stored value bytes inside
the map and val_pair->size is the size of the stored value. The caller can
read the value through val_pair->ptr and change its bytes in place, within
val_pair->size.
 
The cmap_pair belongs to the caller and the map keeps no reference to it.
Changing val_pair->ptr or val_pair->size changes only that copy, never the
stored value; to replace a value, use cbstmap_insert_elem(3).

cbm        BST map to search
key_pair   Key to look up
val_pair   A cmap_pair of the caller that the function fills

ccol_success        - The key was found; val_pair->ptr points at the
                      value and val_pair->size is its size
ccol_key_not_found  - The key is not in the map
ccol_invalid_args   - key_pair or val_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
On every failure, when val_pair is not NULL, the function sets
val_pair->ptr to NULL and val_pair->size to 0.

Since the comparison reads key bytes with memcpy and never through a
pointer cast to the key type, the ptr of a key_pair can have any
alignment. You can point key_pair->ptr directly at key bytes at an
arbitrary offset in a larger buffer, for example a field sliced out of a
packed network frame or a memory-mapped record, without copying them into
an aligned local.
 
Don't free val_pair->ptr; the map owns the value bytes.
 
The lookup is O(log n). val_pair->ptr stays valid until the next insert or
delete. A lookup never invalidates it, so you can hold results for
different keys at the same time, each in its own cmap_pair.
 
The function asserts if cbm is NULL. A NULL key_pair or val_pair is
reported as ccol_invalid_args, as chashmap_get_elem_ref(3) does, and so is a
key_pair whose ptr is NULL while its size is not zero, because it promises
bytes that 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.

#include <ccollections/cbstmap.h>
#include <stdio.h>
 
int main(void) {
  cbmap m = cbstmap_create(ccol_int, NULL);
  if (!m) return 1;
 
  int key = 7;
  long value = 70;
  cmap_pair kp = {.ptr = &key, .size = sizeof(key)};
  cmap_pair vp = {.ptr = &value, .size = sizeof(value)};
  cbstmap_insert_elem(m, &kp, &vp);
 
  cmap_pair found;
  if (cbstmap_get_elem_ref(m, &kp, &found) == ccol_success) {
    *(long *)found.ptr += 1;  /* change the stored value in place */
    printf("%ld (%zu bytes)\n", *(const long *)found.ptr, found.size);
  }
 
  cbmap_destroy(m);
  return 0;
}

cbmap_get(3), cbmap_get_ptr(3), cbstmap_insert_elem(3)