CFG: moved to list of tuples to avoid implicit comparisons with .equals() and improved index detection
This commit is contained in:
parent
6be592fbd1
commit
8b62733a44
1 changed files with 15 additions and 14 deletions
|
@ -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("\"", "\\\"") + "\"";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue