cvector · Section 3
cvector_begin_iter
NAME
cvector_begin_iter - create an iterator positioned at a vector's first element
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cvector.h> cmap_iterator *cvector_begin_iter(cvec v, char **err);
DESCRIPTION
Allocates an iterator at element 0 that walks the vector in index order. The iterator's key is the zero-based index of the element and its value is the element itself. You read both with the common accessors ccol_iter_key_ptr(3) and ccol_iter_val_ptr(3), which give typed pointers straight into the vector's backing buffer, so writing through the value pointer updates the element in place. The usual entry point is the generic ccol_begin(3) macro, which picks this function for a cvec. Call it directly only when the container type is already known and you want the error string.
PARAMETERS
v Vector to iterate over, or NULL err Optional pointer to receive an error string on failure, or NULL
RETURN VALUE
A pointer to a newly allocated iterator at the first element. NULL with *err set to NULL when there is nothing to iterate (v is NULL or the vector holds no elements). NULL with *err set to an error string when the iterator itself couldn't be allocated.
NOTES
The function sets *err to NULL on entry, before anything else. To tell an empty result from a failure, test *err rather than the return value. A NULL v iterates as an empty vector instead of being reported as an error. This contract is shared across modules with chashmap_begin_iter(3) and cbstmap_begin_iter(3): A lazily created container field that is still NULL because nothing has been put in it can be iterated as it is, and no caller needs its own NULL guard first. The iterator frees itself when ccol_iter_next(3) steps past the last element and returns NULL. An iteration that stops early has to end with ccol_iter_destroy(3), or you can declare the iterator with ccol_iter_declare(3) so that the exit of the scope destroys it. The iterator is allocated through the vector's own memory management procedures. Any operation that inserts, removes or resizes invalidates it.
EXAMPLES
cvec_construct(vec, int); cvec_push(vec, 10); cvec_push(vec, 20); char *err = NULL; ccol_iter_declare(vec, it); for (it = cvector_begin_iter(vec, &err); it; it = ccol_iter_next(it)) { printf("[%zu] = %d\n", *ccol_iter_key_ptr(it), *ccol_iter_val_ptr(it)); } if (err) { fprintf(stderr, "cannot iterate: %s\n", err); } cvec_destroy(vec);
SEE ALSO
ccol_begin(3), ccol_iter_next(3), ccol_iter_destroy(3), ccol_iter_declare(3), chashmap_begin_iter(3), cbstmap_begin_iter(3)