C Collections

cyaml · Section 3

cyaml_dictionary_first

cyaml_dictionary_first - start an iteration over the members of a dictionary node

C Collections (libccollections, -lccollections)

#include <ccollections/cyaml.h>
 
typedef struct cyaml_dictionary_iter {
    const char *key;
    cyaml       value;
    /* private fields, whose names begin with cyaml__ */
} cyaml_dictionary_iter;
 
bool cyaml_dictionary_first(cyaml dict, cyaml_dictionary_iter *it);

Fills the cursor it and puts it on the first member of the
dictionary node dict, and cyaml_dictionary_next(3) then moves it to
each later member.  After a call that returns true, it->key is the
member's key and it->value is its value, a borrowed reference with
the same rules as for cyaml_dictionary_get(3).
 
Members come in insertion order, the order in which each key first
entered the dictionary.  For a parsed document that is the order of
the members in the text, and for a dictionary the program builds it
is the order of the cyaml_dictionary_set(3) and cyaml_set(3) calls
that added each key.  A key whose value is replaced keeps its place,
so a key that a document repeats has the place of its first
occurrence and the value of its last.  A key that is removed and
then set again goes after every other member.
 
The members that a merge key ("<<") brings in take the place of the
first merge key that brings their key, after the explicit members in
front of it and before the explicit members after it.  They come
source by source in the order the merge key lists them, and each
source in its own member order.  A key that the mapping already
holds keeps its own place and value, and cyaml(7) gives the
precedence between several merge keys.
 
cyaml_serialize(3), cyaml_serialize_flow(3) and cyaml_clone(3) use
the same order, so parsing and then serializing keeps the key order
of the source document.
 
The cursor is a plain struct that the caller owns, usually on its
stack.  Iterating allocates nothing and cannot fail, so there is
nothing to release and the caller can stop at any point.

dict  The dictionary node to walk.
it    The cursor to fill.  It must not be NULL.

true when it stands on the first member.
 
false when dict has no members, is NULL or is not a
CYAML_DICTIONARY, and the function then sets it->key and it->value
to NULL.  Also false when it is NULL.

it->key points at the copy of the key that the dictionary stores, and
that copy stays valid until the member is removed or the dictionary
is destroyed.
 
The cursor holds the member that the next step reads, which is the
successor of the member that the last successful call gave.  It stays
valid across every change to the dictionary that leaves that
successor in place:
 
  - a replacement of the value of any member, the current one
    included, through cyaml_dictionary_set(3) or cyaml_set(3); the
    member keeps its place, so the iteration goes on in the same
    order;
  - the removal of the current member, or of any member other than
    the successor (to remove the current member, pass it->key to
    cyaml_dictionary_remove(3));
  - an insert of a new key; the new member goes after every other
    member, and it is unspecified whether this iteration reaches it.
 
Removing the successor, through cyaml_dictionary_remove(3),
cyaml_remove(3) or any other call, makes the cursor invalid, and so
does destroying the dictionary.  A later cyaml_dictionary_next(3) on
an invalid cursor is undefined behavior, so start again with
cyaml_dictionary_first() after such a change.
 
it->key and it->value describe the current member as it was when the
cursor stepped onto it.  Replacing the value of the current member
through cyaml_dictionary_set(3) destroys the old value, so it->value
then names freed memory; read the new value with
cyaml_dictionary_get(3) and it->key.  cyaml_set(3) on the other hand
updates the existing node in place, so it->value stays the same node.

A cyaml tree is not thread safe, but several threads can walk the
same dictionary at once, each with its own cursor, as long as no
thread changes the dictionary.

cyaml doc = cyaml_parse("b: 1\na: 2\n", NULL);
cyaml_dictionary_iter it;
for (bool ok = cyaml_dictionary_first(doc, &it); ok;
     ok = cyaml_dictionary_next(&it)) {
    printf("%s = %lld\n", it.key, cyaml_int_val(it.value));
}
/* prints "b = 1" and then "a = 2" */
 
/* Remove every member whose value is null, in one pass. */
for (bool ok = cyaml_dictionary_first(doc, &it); ok;
     ok = cyaml_dictionary_next(&it)) {
    if (cyaml_type(it.value) == CYAML_NULL)
        cyaml_dictionary_remove(doc, it.key);
}
cyaml_destroy(doc);

cyaml_dictionary_next(3), cyaml_dictionary_get(3), cyaml_dictionary_remove(3), cyaml_dictionary_size(3), cyaml_serialize(3)