For many (container) types LEDA provides iteration macros. These macros can be used to iterate over the elements of lists, sets and dictionaries or the nodes and edges of a graph. Iteration macros can be used similarly to the C++ for statement. Examples are
forall_items(it, D) { the items of D are successively assigned to variable it }
forall_rev_items(it, D) { the items of D are assigned to it in reverse order }
forall(x, L) { the elements of L are successively assigned to x}
forall_rev(x, L) { the elements of L are assigned to x in reverse order}
forall_nodes(v, G) { the nodes of G are successively assigned to v}
forall_edges(e, G) { the edges of G are successively assigned to e}
forall_adj_edges(e, v) { all edges adjacent to v are successively assigned to e}
PLEASE NOTE:
Inside the body of a forall loop insertions into or deletions from the
corresponding container are not allowed, with one exception,
the current item or object of the iteration may be removed, as in
forall_edges(e,G) { if (source(e) == target(e)) G.del_edge(e); } // remove selfloops
The item based data types list
, array
, and dictionary
provide now also an STL compatible iteration scheme. The following example
shows STL iteration on lists. Note that not all LEDA supported compilers allow
the usage of this feature.
list<int> L; // fill list somehow list<int>::iterator it; for ( it = L.begin(); it != L.end(); it++ ) cout << *it << endl;
list<int>::iterator
defines the iterator type, begin()
delivers
access to the first list item via an iterator. end()
is the past the end
iterator and serves as an end marker. The increment operator ++
moves
the iterator one position to the next item, and *it
delivers the
content of the item to which the iterator is pointing. For more information
on STL please refer to the standard literature about STL.
For a more flexible access to the LEDA graph data type there are graph iterators which extent the STL paradigm to more complex container types. To make use of these features please refer to Graph Iterators.