C Collections

chashmap · Section 3

chmap_insert

chmap_insert - insert a key-value pair (type-inferred)

C Collections (libccollections, -lccollections)

#include <ccollections/chashmap.h>
 
chmap_insert(hm_name, key, val);

Type-inferred wrapper for chashmap_insert_elem() that builds the key and
value cmap_pairs from the given expressions. If the map already holds
the key, the value is updated silently. ccol__fatal_err() is called only
on a real failure, which means ccol_not_enough_memory,
ccol_container_full or ccol_invalid_args.
 
key and val are converted through the DECLARED key and value types of
the map. The macro makes a private copy of each argument in that type
with a plain C assignment, so the value is CONVERTED and never
reinterpreted byte for byte. A float of 1.5f going into a map of int
becomes 1, just as in "int x = 1.5f;", and not 1069547520. An argument
whose type has no implicit conversion to the declared type is a compile
error at the call site. cvec_push(3) follows the same rule.
 
Since the macro copies the argument instead of taking its address, an
lvalue, an rvalue, a literal and a computed expression all work.
 
The one exception is a declared key or value type that is a CHARACTER
POINTER, which converts nothing. To this library char *, signed char *
and unsigned char * are a single type. All three classify as
ccol_string, a NUL-terminated string that the macro measures with
strlen. The map compares the bytes of the string and never the pointer
type, so a map keyed on unsigned char * can be reached through a char *
that addresses the same string. An array key, such as a char buf[64] or
a string literal, decays to a pointer as in any other assignment.
 
A macro can't know the length of the buffer a pointer addresses, so a
character pointer is never a binary key. A binary key takes one of two
forms. A fixed-length key such as a digest can be a struct that wraps an
array, such as struct { unsigned char b[32]; }, and the map hashes and
compares all of its bytes, zero bytes included. A key whose length is
known only at run time goes through the raw chashmap_insert_elem(3) layer,
with that length in cmap_pair.size.
 
A NULL character pointer is not a string, so a NULL key or value of a
character-pointer type stops the program with ccol_invalid_args.

hm_name  Hash map to insert into
key      Key to insert
val      Value to associate with key

Stops the program only on a real failure; updating an existing key is
not a failure
Copies the key and the value, so neither has to be addressable
Handles both value types and string types correctly
For a float or double key, equality is bitwise with one exception:
-0.0 and 0.0 are made into the same key. A NaN key is compared bit by
bit, not with IEEE 754 equality (see chmap_construct(3))
For a long double key, equality compares the value (native ==) instead
of the bytes that every other key type compares. Every NaN long double
is the same key (see chmap_construct(3))

chmap_construct(map, int, char*);
int first = 42;
chmap_insert(map, first, "hello");
chmap_insert(map, 100, "world");   /* a literal key is fine */
chmap_destroy(map);

chashmap_insert_elem(3), chmap_get(3), chmap_remove(3)