C Collections

citerators · Overview

citerators

CITERATORS - overview of the citerators API

C Collections (libccollections, -lccollections)

#include <ccollections/cvector.h>
#include <ccollections/chashmap.h>
#include <ccollections/cbstmap.h>

citerators is a single iteration API shared by cvec, chmap and cbmap.
cvector.h, chashmap.h and cbstmap.h each include it, so including any one
of those headers gives you the whole API with no other #include.
 
Most code uses ccol_for_each(3), which declares the iterator, walks the
container and destroys the iterator when the loop ends. If you want to
write the loop yourself, declare the iterator with ccol_iter_declare(3),
start it with ccol_begin(3) and move it on with ccol_iter_next(3).
ccol_iter_key_ptr(3) and ccol_iter_val_ptr(3) give typed pointers to the
current key and value.
 
ccol_begin(3) picks the begin function of the container from its type at
compile time, and any type other than the three containers is a compile
error. A NULL container handle iterates as an empty container.

Container        ccol_iter_key_ptr(3)        ccol_iter_val_ptr(3)
cvec of T        const size_t * (the index)  T *
chmap of K, V    const K *                   V *, or V const * when V
                                             is a character pointer
cbmap of K, V    const K *                   V *, or V const * when V
                                             is a character pointer

A cvec is walked by index from 0 up and a cbmap in ascending key order.
A chmap has no defined order; chashmap_begin_iter(3) describes what it
does.

When ccol_iter_next(3) moves past the last element, it destroys the
iterator and returns NULL. If a loop stops earlier, the iterator stays
live until ccol_iter_destroy(3) or until the end of the scope of
ccol_iter_declare(3). The key and value pointers are valid only while the
iterator is on that element and the container does not change. Inserting
into the container or removing from it during an iteration invalidates
the iterator, and the behavior is undefined from then on.

The macros use GNU C statement expressions and the cleanup attribute, so
they need GCC or Clang.

ccol_for_each(3), ccol_iter_declare(3), ccol_begin(3), ccol_iter_next(3), ccol_iter_key_ptr(3), ccol_iter_val_ptr(3), ccol_iter_destroy(3), cvector_begin_iter(3), chashmap_begin_iter(3), cbstmap_begin_iter(3)