The following Graph agent is designed to find an existing edge in the graph.
Gedge *Graph::findEdge(Gnode *from,Gnode *to)
{
for(GedgePtrBagIter it(d_edges);it;++it){
if((Gnode *)it()->from()==from && (Gnode *)it()->to()==to){
return it();
}
}
return 0;
}
Again an iterator is used. This time it's the edge iterator for the graph (d_edges is a GedgePtrBag). So the iterator steps through the linked list stored in the GedgePtrBagLink. Each edge has from and to nodes associated with it. Thus the iterator steps through the edges and compares the from and to nodes of each edge with the from and to nodes which are passed to the argument. If a match is found, a pointer to that edge is returned. Otherwise, a null is returned.