CFG: moved to list of tuples to avoid implicit comparisons with .equals() and improved index detection

This commit is contained in:
Carlos Galindo 2019-03-27 00:15:04 +01:00
parent 6be592fbd1
commit 8b62733a44
Signed by untrusted user who does not match committer: kauron
GPG key ID: 83E68706DEE119A3

View file

@ -7,7 +7,7 @@ import java.util.*;
public class CFG { public class CFG {
private final List<Node> nodes = new ArrayList<Node>(); private final List<Node> nodes = new ArrayList<Node>();
private final Map<Node, List<Node>> edges = new HashMap<Node, List<Node>>(); private final List<Map.Entry<Node, Node>> edges = new LinkedList<>();
private Node beginNode, endNode; private Node beginNode, endNode;
public void addNode(Node stmt) { public void addNode(Node stmt) {
@ -20,13 +20,7 @@ public class CFG {
} }
public void connect(Node begin, Node end) { public void connect(Node begin, Node end) {
if (edges.containsKey(begin) && !edges.get(begin).contains(end)) { edges.add(new HashMap.SimpleEntry<>(begin, end));
edges.get(begin).add(end);
} else {
List<Node> dest = new ArrayList<Node>();
dest.add(end);
edges.put(begin, dest);
}
} }
public void connect(Collection<Node> begin, Node end) { public void connect(Collection<Node> begin, Node end) {
@ -52,10 +46,8 @@ public class CFG {
public List<String> toStringList() { public List<String> toStringList() {
List<String> res = new LinkedList<String>(); List<String> res = new LinkedList<String>();
for (Map.Entry<Node, List<Node>> e : edges.entrySet()) { for (Map.Entry<Node, Node> e : edges) {
for (Node dest : e.getValue()) { res.add(edge2str(e.getKey(), e.getValue()));
res.add(edge2str(e.getKey(), dest));
}
} }
return res; return res;
} }
@ -69,7 +61,16 @@ public class CFG {
return "Start"; return "Start";
if (s == endNode) if (s == endNode)
return "Stop"; return "Stop";
int index = nodes.indexOf(s); int index = -1;
return "\"(" + index + ") " + s.toString().replace("\"", "\\\"") + "\""; 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("\"", "\\\"") + "\"";
} }
} }