-- File graphg.adb -- This is a generic graph package with an active iterator. The package -- will be used in a larger program that performs graph traversals using -- the iterator (lab6). For this purpose the iterator is not sufficient, -- in that the *iterator* marks nodes as visited when they are returned. -- This makes it impossible to correctly perform depth-first traversals. -- The flaws will be fixed in a subsequent version that students -- implement as a child package of this package. package body Graphg is procedure Clear (AGraph: in out Graph) is begin for i in 1..AGraph.Size loop AGraph.InGraph (i) := False; AGraph.Visited (i) := False; for j in 1..AGraph.Size loop AGraph.Edges (i, j) := False; end loop; end loop; end Clear; procedure Add (ANode: in positive; WithValue: in Element; ToGraph: in out Graph) is begin ToGraph.InGraph (ANode) := True; ToGraph.Values (ANode) := WithValue; end Add; procedure InsertEdge (FromNode: in positive; ToNode: in positive; InGraph: in out Graph) is begin InGraph.Edges (FromNode, ToNode) := True; end InsertEdge; procedure Initialize (AnIterator: out Iterator; ForGraph: in out Graph; ToNode: in positive) is begin for i in 1..ForGraph.Size loop ForGraph.Visited (i) := False; end loop; ForGraph.Visited (ToNode) := True; AnIterator := Iterator (ToNode); end Initialize; procedure GetNext (IteratorNode: in out Iterator; ForGraph: in out Graph) is row: positive := integer (IteratorNode); begin for i in 1..ForGraph.Size loop if ForGraph.Edges (row, i) then if not ForGraph.Visited (i) then ForGraph.Visited (i) := True; IteratorNode := Iterator (i); return; end if; end if; end loop; end GetNext; function ValueOf (IteratorNode: in Iterator; ForGraph: in Graph) return Element is begin return ForGraph.Values (integer (IteratorNode)); end ValueOf; function Done (IteratorNode: in Iterator; ForGraph: in Graph) return Boolean is -- return True if all nodes adjacent to that specified by -- IteratorNode have been visited row: positive := integer (IteratorNode); begin for i in 1..ForGraph.Size loop if ForGraph.Edges (row, i) then if not ForGraph.Visited (i) then return False; end if; end if; end loop; return True; end Done; end graphg;