A Topo object has a topo->exec() function which can operate either as a 'visit' function, a 'cycle' function, or a 'done' function. During execution the function is changed, on-the-fly, to match the situation at an item as follows: if the item has not yet been visited, the function is the 'visit' function, if the neighbors are being visited the function is the 'cycle' function, if all neighbors have been visited the function is the 'done' function. If a 'cycle' function is encountered during execution, a cycle has been found and the program is terminated. If a 'done' function is encountered execution returns to the caller. Otherwise, the 'visit' function is changed to the 'cycle' function, the neighborhood is visited - that is, the topo->exec() functions are invoked for all neighbors, if the neighbor is visited without exiting, the item's identity is output, and finally the 'cycle' function is replaced by the 'done' function.
The classes Visit (visit.h, visit.cc), and Cycle (cycle.h) subclass Topo. Each re-implements topo->exec() as a 'visit' or 'cycle' described above. The 'done' function is obtained using Topo directly. Getting the topo->exec() function to change during execution is accomplished merely by replacing Topo objects - replace a Visit object with a Cycle object then a Topo object. This is seen in the exec() function of visit.cc. Initially, the Topo object is set to a Visit object in the constructor of class Node in node.cc.