C Collections

citerators · Section 3

ccol_iter_declare

ccol_iter_declare - declare a typed iterator variable with RAII cleanup

C Collections (libccollections, -lccollections)

#include <ccollections/citerators.h>   /* or any container header */
 
ccol_iter_declare(container, it);

ccol_iter_declare declares a cmap_iterator pointer named it that carries
the type information of container. The declaration has
__attribute__((cleanup)), so the iterator is destroyed at the end of its
scope and you don't need an explicit ccol_iter_destroy() call.
 
It also declares two hidden companion type variables. Those are what let
ccol_iter_key_ptr() and ccol_iter_val_ptr() return pointers of the right
type for container.

container  Container variable, declared beforehand with a *_declare or
           *_construct macro. It must be a cvec, a chmap or a cbmap.
it         Name for the iterator variable.

container must be in scope, and its companion type variables must be
visible too, in the same scope or in a parent one.
The macro uses __attribute__((cleanup)), so it needs GCC or Clang.
When a loop leaves early, the iterator stays live until the end of the
scope. You can destroy the container before that point, since the
cleanup frees the iterator without reading the container, but do not
advance or dereference the iterator once its container is gone.

ccol_iter_declare(my_map, it);
for (it = ccol_begin(my_map); it != NULL; it = ccol_iter_next(it)) {
    printf("%d\n", *ccol_iter_val_ptr(it));
}

ccol_begin(3), ccol_iter_next(3), ccol_for_each(3), ccol_iter_destroy(3)