C Collections

chashmap · Section 3

chmap_get

chmap_get - get value by key (type-inferred, returns value)

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
value = chmap_get(hm_name, key);

Type-inferred wrapper for chashmap_get_elem_ref() that returns the value
itself. If the key is not found or the sizes don't match, it calls
ccol__fatal_err().
 
The lookup key is converted through the DECLARED key type of the map,
the same way chmap_insert(3) converts it, so a key stored through that
conversion is only found again by a lookup that makes the same
conversion. A literal and a computed expression both work.

hm_name  Hash map to search
key      Key to look up

The value stored for key

Stops the program when the map does not hold the key
Stops the program with ccol_invalid_args when key is a NULL character
pointer, since that is not a string
Stops the program when the size of the stored value does not match
the size of the declared value type
Returns the value, not a pointer to it
For a string, returns the char* itself
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)

chmap_construct(map, int, char*);
int key = 42;
chmap_insert(map, key, "hello");
char* val = chmap_get(map, key); // val is "hello"
chmap_destroy(map);  // val points into the map, so do not use val after this

chmap_get_ptr(3), chmap_insert(3), chmap_construct(3)