@foreach
Description:
Calls the given function for each of the key/value pairs in the Tree.
The function is passed the key and value of each pair, and the given data
parameter. The tree is traversed in sorted order.
The tree may not be modified while iterating over it (you can't add/remove items). To remove all items matching a predicate, you need to add each item to a list in your TraverseFunc as you walk over the tree, then walk the list and remove each item.
Example: Apply a function to each element:
public static int main (string[] args) {
Tree<string, string> tree = new Tree<string, string>.full ((a, b) => { return strcmp (a, b); }, free, free);
tree.insert ("key1", "val1");
tree.insert ("key2", "val2");
tree.insert ("key3", "val3");
// Output:
// ``key=key1, value=val1``
// ``key=key2, value=val2``
// ``key=key3, value=val3``
tree.@foreach ((_key, _val) => {
unowned string key = (string) _key; // void* _key
unowned string val = (string) _val; // void* _val
print ("key=%s, value=%s\n", key, val);
return false; // return true to stop foreach
});
return 0;
}
valac --pkg glib-2.0 GLib.Tree.foreach.vala
Parameters:
this |
a Tree |
func |
the function to call for each node visited. If this function returns true, the traversal is stopped. |
user_data |
user data to pass to the function |