chashmap · Section 3
chashmap_begin_iter
NAME
chashmap_begin_iter - create an iterator positioned at a hash map's first entry
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chashmap.h> cmap_iterator *chashmap_begin_iter(chmap chm, char **err);
DESCRIPTION
Allocates an iterator on the map's first entry. Each step gives the key and the value of one entry, which you read with the unified accessors ccol_iter_key_ptr(3) and ccol_iter_val_ptr(3). The value pointer points at the map's own storage, so writing through it updates the stored value in place. The generic ccol_begin(3) macro picks this function for a chmap and is the normal way in. Call this function directly only when you already know the container type and want the error string.
PARAMETERS
chm Hash map to iterate over, or NULL err Optional pointer that receives an error string on failure, or NULL
RETURN VALUE
A pointer to a newly allocated iterator on the first entry. NULL with *err set to NULL when there is nothing to iterate (chm is NULL or the map holds no entries). NULL with *err set to an error string when the iterator itself could not be allocated.
ITERATION ORDER
The order depends on the map's implementation, which chashmap_create_full(3) describes. With either implementation the order is not part of the map's contract, so do not depend on it. Separate chaining keeps a list of every live entry and links each new entry at its head, so the iterator starts at the newest entry and works back to the oldest one. Open addressing has no such list. The iterator walks the slot array from the first slot to the last, so the order follows where each key hashed to and not when it was inserted. The hash depends on the mode of the map, and the keyed mode depends on a secret of the process, so that order changes when the map switches to the keyed mode and from then on differs from one process to the next. See chashmap_create_full(3).
NOTES
The function sets *err to NULL first thing on entry. To tell an empty result from a failure, test *err and not the return value. A NULL chm iterates as an empty map and reports no error. This contract is shared on purpose with cvector_begin_iter(3) and cbstmap_begin_iter(3), so you can iterate a lazily created map field that is still NULL because nothing was put in it yet, without a NULL check of your own. The iterator frees itself when ccol_iter_next(3) steps past the last entry and returns NULL. An iteration that stops early must end with ccol_iter_destroy(3), or you can declare the iterator with ccol_iter_declare(3) so that leaving the scope destroys it. The iterator is allocated through the map's own memory management procedures. For a map whose values need a destructor on each entry, chashmap_destroy_with_dtor(3) is the better way to destroy it, since it walks the map without allocating anything while an iterator doing the same job can fail under memory pressure. An insert into the map or a removal from it invalidates an open iterator. Changing a value in place through ccol_iter_val_ptr(3) does not.
EXAMPLES
chmap_construct(scores, char *, int); int score = 42; chmap_insert(scores, "alice", score); char *err = NULL; ccol_iter_declare(scores, it); for (it = chashmap_begin_iter(scores, &err); it; it = ccol_iter_next(it)) { printf("%s = %d\n", *ccol_iter_key_ptr(it), *ccol_iter_val_ptr(it)); } if (err) { fprintf(stderr, "cannot iterate: %s\n", err); } chmap_destroy(scores);
SEE ALSO
ccol_begin(3), ccol_iter_next(3), ccol_iter_destroy(3), ccol_iter_declare(3), chashmap_destroy_with_dtor(3), cbstmap_begin_iter(3), cvector_begin_iter(3)