C Collections

chashmap · Section 3

chashmap_remove_elem

chashmap_remove_elem - remove a key-value pair from the hash map

C Collections (libccollections, -lccollections)

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

Removes the key-value pair with the given key from the hash map and
frees the memory of both the key and the value. When the load factor
falls below a threshold, the map shrinks.

chm        Hash map to remove from
key_pair   Key to remove

ccol_success        - The key was removed
ccol_invalid_args   - chm or key_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

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
Average O(1) complexity
Frees the memory of the key and of the value
Open addressing: Shrinks when the load factor < 0.25 (scale 0.5x). A
grow on insert always leaves the live load above 0.25, so a delete
never shrinks a table straight back after an insert has grown it
Open addressing: The delete moves the later entries of the same
cluster back into the gap (backward-shift deletion) and leaves no
marker for a deleted slot. Entries of that cluster can change slot, so
every pointer from chashmap_get_elem_ref(3) or chmap_get_ptr(3) is
invalid after the delete, as after any other change to the map
Separate chaining: Shrinks when elem_count < (bucket_count + 1) / 8 (scale 0.25x)
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_remove(3), chashmap_insert_elem(3), chmap_construct(3)