C Collections

chashmap · Section 3

chashmap_get_elem_ref

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

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
ccol_retval chashmap_get_elem_ref(chmap chm,
                                  const cmap_pair *key_pair,
                                  cmap_pair *val_pair);

Finds the value stored for 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, so you can read the value through val_pair->ptr and change
its bytes in place within val_pair->size.
 
The cmap_pair is yours and the map keeps no reference to it. Changing
val_pair->ptr or val_pair->size only changes that copy, never the stored
value; use chashmap_insert_elem(3) to replace a value.

chm        Hash map to search
key_pair   Key to look up
val_pair   The caller's cmap_pair, which the function fills

ccol_success        - The key was found; val_pair->ptr points at the
                      value and val_pair->size is its size
ccol_invalid_args   - chm, key_pair or val_pair is NULL or the key size
                      is 0, or key_pair->size is not exactly the byte
                      size of the key type for any fixed-width numeric
                      key type (char, short, int, long, long long, their
                      unsigned counterparts, float, double, long double,
                      or a pointer type), whichever backend the map uses
ccol_key_not_found  - The key is not in the map
On every failure, if val_pair is not NULL, val_pair->ptr is set to NULL
and val_pair->size to 0.

key_pair->ptr needs no particular alignment, because the map hashes and
compares the key bytes through memcpy, never through a pointer cast to the
key type. So it can point straight at key bytes at any offset in a larger
buffer, such as a field of a packed network frame or a memory-mapped
record, and you don't have to copy them into an aligned local variable
first
Do not free val_pair->ptr, because the value bytes belong to the map
Average O(1) complexity
val_pair->ptr is valid until the next insert, delete or resize. A
lookup of another key never invalidates it, so you can hold results for
different keys at once, each in its own cmap_pair
val_pair->ptr is correctly aligned for any value type, with both
internal backends
For a float or double key, chmap_construct(3) gives the key equality
contract (bitwise, except that -0.0 becomes 0.0)
For a long double key, chmap_construct(3) gives the key equality contract
(by value, unlike every other key type)

#include <ccollections/chashmap.h>
#include <stdio.h>
 
int main(void) {
  chmap m = chashmap_create(16, ccol_int, ccol_long, 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)};
  chashmap_insert_elem(m, &kp, &vp);
 
  cmap_pair found;
  if (chashmap_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);
  }
 
  chmap_destroy(m);
  return 0;
}

chmap_get(3), chmap_get_ptr(3), chashmap_insert_elem(3), chmap_construct(3)