/** 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 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;
}
if (n!=null) { // if at least one unvisited
DFS(n, visited, sorted);
}
}
return sorted;
}
java类org.antlr.v4.runtime.misc.OrderedHashSet的实例源码
Graph.java 文件源码
项目:codebuff
阅读 25
收藏 0
点赞 0
评论 0
LeftRecursionDetector.java 文件源码
项目:codebuff
阅读 23
收藏 0
点赞 0
评论 0
/** enclosingRule calls targetRule. Find the cycle containing
* the target and add the caller. Find the cycle containing the caller
* and add the target. If no cycles contain either, then create a new
* cycle.
*/
protected void addRulesToCycle(Rule enclosingRule, Rule targetRule) {
//System.err.println("left-recursion to "+targetRule.name+" from "+enclosingRule.name);
boolean foundCycle = false;
for (Set<Rule> rulesInCycle : listOfRecursiveCycles) {
// ensure both rules are in same cycle
if (rulesInCycle.contains(targetRule)) {
rulesInCycle.add(enclosingRule);
foundCycle = true;
}
if (rulesInCycle.contains(enclosingRule)) {
rulesInCycle.add(targetRule);
foundCycle = true;
}
}
if ( !foundCycle ) {
Set<Rule> cycle = new OrderedHashSet<Rule>();
cycle.add(targetRule);
cycle.add(enclosingRule);
listOfRecursiveCycles.add(cycle);
}
}
FormEventManager.java 文件源码
项目:poly-ql
阅读 22
收藏 0
点赞 0
评论 0
/**
* Subscribes given component to given identifier. If there are no subscriptions
* yet for this identifier, an entry in the subscriptions map is created.
*
* @param formComponent the component to subscribe.
* @param identifier the identifier the given component should be
* subscribed to.
*/
private void subscribeComponentToIdentifier(AbstractFormComponent formComponent,
String identifier) {
if (hasSubscriptionsForIdentifier(identifier)) {
subscriptions.get(identifier).add(formComponent);
} else {
HashSet<AbstractFormComponent> subscribedComponents =
new OrderedHashSet<AbstractFormComponent>();
subscribedComponents.add(formComponent);
subscriptions.put(identifier, subscribedComponents);
}
}
FormFrameBuilder.java 文件源码
项目:poly-ql
阅读 21
收藏 0
点赞 0
评论 0
private FormFrameBuilder(Form form, GUIController controller) {
formComponents = new OrderedHashSet<AbstractFormComponent>();
EvaluationVisitor evaluator = new EvaluationVisitor();
FormEventManager eventManager = new FormEventManager(evaluator);
formFrame = new FormFrame(form.getName(), evaluator, eventManager);
AbstractFormComponent topComponent = form.getBody().accept(this);
initValues();
formFrame.initUI(topComponent, controller);
}
RuleFunction.java 文件源码
项目:codebuff
阅读 32
收藏 0
点赞 0
评论 0
/** Add local var decl */
public void addLocalDecl(Decl d) {
if ( locals ==null ) locals = new OrderedHashSet<Decl>();
locals.add(d);
d.isLocal = true;
}
CodeBlock.java 文件源码
项目:codebuff
阅读 24
收藏 0
点赞 0
评论 0
/** Add local var decl */
public void addLocalDecl(Decl d) {
if ( locals==null ) locals = new OrderedHashSet<Decl>();
locals.add(d);
d.isLocal = true;
}