forked from jasder/antlr
use v4 not v3 Graph.
This commit is contained in:
parent
0a961645be
commit
8908641dca
|
@ -29,7 +29,6 @@
|
|||
|
||||
package org.antlr.v4;
|
||||
|
||||
import org.antlr.misc.Graph;
|
||||
import org.antlr.runtime.ANTLRFileStream;
|
||||
import org.antlr.runtime.ANTLRStringStream;
|
||||
import org.antlr.runtime.CharStream;
|
||||
|
@ -41,6 +40,7 @@ import org.antlr.v4.automata.ATNFactory;
|
|||
import org.antlr.v4.automata.LexerATNFactory;
|
||||
import org.antlr.v4.automata.ParserATNFactory;
|
||||
import org.antlr.v4.codegen.CodeGenPipeline;
|
||||
import org.antlr.v4.misc.Graph;
|
||||
import org.antlr.v4.parse.ANTLRLexer;
|
||||
import org.antlr.v4.parse.ANTLRParser;
|
||||
import org.antlr.v4.parse.GrammarASTAdaptor;
|
||||
|
|
|
@ -27,84 +27,85 @@
|
|||
*/
|
||||
package org.antlr.v4.misc;
|
||||
|
||||
import org.antlr.v4.runtime.misc.OrderedHashSet;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/** A generic graph with edges; Each node as a single Object payload.
|
||||
* This is only used to topologically sort a list of file dependencies
|
||||
* at the moment.
|
||||
*/
|
||||
public class Graph {
|
||||
public class Graph<T> {
|
||||
|
||||
public static class Node {
|
||||
Object payload;
|
||||
List<Node> edges; // points at which nodes?
|
||||
public static class Node<T> {
|
||||
T payload;
|
||||
List<Node<T>> edges; // points at which nodes?
|
||||
|
||||
public Node(Object payload) { this.payload = payload; }
|
||||
public Node(T payload) { this.payload = payload; }
|
||||
|
||||
public void addEdge(Node n) {
|
||||
if ( edges==null ) edges = new ArrayList<Node>();
|
||||
if ( !edges.contains(n) ) edges.add(n);
|
||||
}
|
||||
public void addEdge(Node<T> n) {
|
||||
if ( edges==null ) edges = new ArrayList<Node<T>>();
|
||||
if ( !edges.contains(n) ) edges.add(n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() { return payload.toString(); }
|
||||
}
|
||||
@Override
|
||||
public String toString() { return payload.toString(); }
|
||||
}
|
||||
|
||||
/** Map from node payload to node containing it */
|
||||
protected Map<Object,Node> nodes = new HashMap<Object,Node>();
|
||||
/** Map from node payload to node containing it */
|
||||
protected Map<T,Node<T>> nodes = new HashMap<T,Node<T>>();
|
||||
|
||||
public void addEdge(Object a, Object b) {
|
||||
//System.out.println("add edge "+a+" to "+b);
|
||||
Node a_node = getNode(a);
|
||||
Node b_node = getNode(b);
|
||||
a_node.addEdge(b_node);
|
||||
}
|
||||
public void addEdge(T a, T b) {
|
||||
//System.out.println("add edge "+a+" to "+b);
|
||||
Node<T> a_node = getNode(a);
|
||||
Node<T> b_node = getNode(b);
|
||||
a_node.addEdge(b_node);
|
||||
}
|
||||
|
||||
protected Node getNode(Object a) {
|
||||
Node existing = nodes.get(a);
|
||||
if ( existing!=null ) return existing;
|
||||
Node n = new Node(a);
|
||||
nodes.put(a, n);
|
||||
return n;
|
||||
}
|
||||
protected Node<T> getNode(T a) {
|
||||
Node<T> existing = nodes.get(a);
|
||||
if ( existing!=null ) return existing;
|
||||
Node<T> n = new Node<T>(a);
|
||||
nodes.put(a, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
/** DFS-based topological sort. A valid sort is the reverse of
|
||||
* the post-order DFA traversal. Amazingly simple but true.
|
||||
* For sorting, I'm not following convention here since ANTLR
|
||||
* needs the opposite. Here's what I assume for sorting:
|
||||
*
|
||||
* If there exists an edge u -> v then u depends on v and v
|
||||
* must happen before u.
|
||||
*
|
||||
* So if this gives nonreversed postorder traversal, I get the order
|
||||
* I want.
|
||||
*/
|
||||
public List<Object> sort() {
|
||||
Set<Node> visited = new OrderedHashSet<Node>();
|
||||
ArrayList<Object> sorted = new ArrayList<Object>();
|
||||
while ( visited.size() < nodes.size() ) {
|
||||
// pick any unvisited node, n
|
||||
Node n = null;
|
||||
for (Iterator it = nodes.values().iterator(); it.hasNext();) {
|
||||
n = (Node)it.next();
|
||||
if ( !visited.contains(n) ) break;
|
||||
}
|
||||
DFS(n, visited, sorted);
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
/** DFS-based topological sort. A valid sort is the reverse of
|
||||
* the post-order DFA traversal. Amazingly simple but true.
|
||||
* For sorting, I'm not following convention here since ANTLR
|
||||
* needs the opposite. Here's what I assume for sorting:
|
||||
*
|
||||
* If there exists an edge u -> v then u depends on v and v
|
||||
* must happen before u.
|
||||
*
|
||||
* So if this gives nonreversed postorder traversal, I get the order
|
||||
* I want.
|
||||
*/
|
||||
public List<T> sort() {
|
||||
Set<Node<T>> visited = new org.antlr.misc.OrderedHashSet<Node<T>>();
|
||||
ArrayList<T> sorted = new ArrayList<T>();
|
||||
while ( visited.size() < nodes.size() ) {
|
||||
// pick any unvisited node, n
|
||||
Node<T> n = null;
|
||||
for (Node<T> tNode : nodes.values()) {
|
||||
n = tNode;
|
||||
if ( !visited.contains(n) ) break;
|
||||
}
|
||||
DFS(n, visited, sorted);
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
|
||||
public void DFS(Node n, Set<Node> visited, ArrayList<Object> sorted) {
|
||||
if ( visited.contains(n) ) return;
|
||||
visited.add(n);
|
||||
if ( n.edges!=null ) {
|
||||
for (Iterator it = n.edges.iterator(); it.hasNext();) {
|
||||
Node target = (Node) it.next();
|
||||
DFS(target, visited, sorted);
|
||||
}
|
||||
}
|
||||
sorted.add(n.payload);
|
||||
}
|
||||
public void DFS(Node<T> n, Set<Node<T>> visited, ArrayList<T> sorted) {
|
||||
if ( visited.contains(n) ) return;
|
||||
visited.add(n);
|
||||
if ( n.edges!=null ) {
|
||||
for (Node<T> target : n.edges) {
|
||||
DFS(target, visited, sorted);
|
||||
}
|
||||
}
|
||||
sorted.add(n.payload);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue