package edu.cmu.cs.l3.flowgraph;

import edu.cmu.cs.l3.graph.*;
import edu.cmu.cs.l3.assem.*;
import java.util.*;
import edu.cmu.cs.l3.temp.*;

public class FlowGraph extends DirectedGraph {

    public FlowGraph() {

    }

    public Vertex createVertex() {
        Vertex v = new FlowVertex(new OperInstruction(" ",null,null));
        addVertex(v);

        return v;
    }

    public Vertex createVertex(Instruction instr) {
        Vertex v = new FlowVertex(instr);
        addVertex(v);

        return v;
    }

    public FlowVertex getStartNode() {
        return (FlowVertex)m_vertices.get(0);
    }

    public FlowVertex getExitNode() throws GraphException {
        int i;
        for (i = 0; i < m_vertices.size(); i++) {
            FlowVertex v = (FlowVertex) m_vertices.get(i);
            if (getChildren(v).size() == 0) {
                return v;
            }
        }
        return null;
    }

    public String toString() {

       int i,j;
       List verts = getVertices();
       String str="";

       // Verts
       for (i = 0; i < verts.size(); i++) {
           FlowVertex v = (FlowVertex) verts.get(i);
           str += v.toString() + ": Edges: ";

           List vlist = (List)m_adjlists.get(v.getID());
           for (j = 0; j < vlist.size(); j++) {
               FlowVertex u = (FlowVertex) vlist.get(j);
               str += "(" + u.toString() + "),";
           }
           str += "\n";
       }

       return str;
    }
}