diff --git a/src/main/java/grafos/CFG.java b/src/main/java/grafos/CFG.java index 24dc65c..3b301c3 100644 --- a/src/main/java/grafos/CFG.java +++ b/src/main/java/grafos/CFG.java @@ -7,7 +7,7 @@ import java.util.*; public class CFG { private final List nodes = new ArrayList(); - private final Map> edges = new HashMap>(); + private final List> edges = new LinkedList<>(); private Node beginNode, endNode; public void addNode(Node stmt) { @@ -20,13 +20,7 @@ public class CFG { } public void connect(Node begin, Node end) { - if (edges.containsKey(begin) && !edges.get(begin).contains(end)) { - edges.get(begin).add(end); - } else { - List dest = new ArrayList(); - dest.add(end); - edges.put(begin, dest); - } + edges.add(new HashMap.SimpleEntry<>(begin, end)); } public void connect(Collection begin, Node end) { @@ -52,10 +46,8 @@ public class CFG { public List toStringList() { List res = new LinkedList(); - for (Map.Entry> e : edges.entrySet()) { - for (Node dest : e.getValue()) { - res.add(edge2str(e.getKey(), dest)); - } + for (Map.Entry e : edges) { + res.add(edge2str(e.getKey(), e.getValue())); } return res; } @@ -69,7 +61,16 @@ public class CFG { return "Start"; if (s == endNode) return "Stop"; - int index = nodes.indexOf(s); - return "\"(" + index + ") " + s.toString().replace("\"", "\\\"") + "\""; + int index = -1; + for (int i = 0; i < nodes.size(); i++) { + if (nodes.get(i) == s) { + index = i; + break; + } + } + if (index == -1) + throw new RuntimeException("Internal error, can't find node"); + return "\"(" + index + ", " + s.getRange().get().begin.line + ") " + + s.toString().replace("\"", "\\\"") + "\""; } }