C Collections

chashmap · Section 3

chashmap_get_elem_copy

chashmap_get_elem_copy - copy the value associated with a key out of a hash map

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
ccol_retval chashmap_get_elem_copy(chmap chm, const cmap_pair *key_pair,
                                     void *target_buf,
                                     size_t target_buf_size);

Looks key_pair up in the map and copies min(value_size,
target_buf_size) bytes of the value into target_buf, so a buffer that is
too small gets a partial copy and not an error. When target_buf_size is
larger than the stored value, the rest of target_buf is set to zero.
cbstmap_get_elem_copy(3) is stricter and needs the sizes to match
exactly.

chm              Hash 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

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   - A pointer is NULL, a size is 0, or key_pair's
                      size does not match the size of a fixed-width
                      key type (see chashmap_create_full(3))

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
O(1) average complexity
Open addressing: O(n) worst case for linear probing
Separate chaining: O(n) worst case per bucket, where n is chain length
Copies min(actual_value_size, target_buf_size) bytes
Safe with an undersized buffer, which gets a partial copy
Bytes of target_buf beyond the copied value are zeroed

chashmap_get_elem_ref(3), chashmap_insert_elem(3), cbstmap_get_elem_copy(3)