| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.graph; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.Sets; |
| 21 |
import com.google.javascript.jscomp.graph.DiGraph; |
| 22 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge; |
| 23 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode; |
| 24 |
|
| 25 |
import java.util.LinkedHashSet; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Set; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public final class FixedPointGraphTraversal<N, E> { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private final EdgeCallback<N, E> callback; |
| 44 |
|
| 45 |
public static final String NON_HALTING_ERROR_MSG = |
| 46 |
"Fixed point computation not halting"; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public FixedPointGraphTraversal(EdgeCallback<N, E> callback) { |
| 54 |
this.callback = callback; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static <NODE, EDGE> FixedPointGraphTraversal<NODE, EDGE> newTraversal( |
| 61 |
EdgeCallback<NODE, EDGE> callback) { |
| 62 |
return new FixedPointGraphTraversal<NODE, EDGE>(callback); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public void computeFixedPoint(DiGraph<N, E> graph) { |
| 70 |
Set<N> nodes = Sets.newHashSet(); |
| 71 |
for (DiGraphNode<N, E> node : graph.getDirectedGraphNodes()) { |
| 72 |
nodes.add(node.getValue()); |
| 73 |
} |
| 74 |
computeFixedPoint(graph, nodes); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public void computeFixedPoint(DiGraph<N, E> graph, N entry) { |
| 83 |
Set<N> entrySet = Sets.newHashSet(); |
| 84 |
entrySet.add(entry); |
| 85 |
computeFixedPoint(graph, entrySet); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public void computeFixedPoint(DiGraph<N, E> graph, Set<N> entrySet) { |
| 94 |
int cycleCount = 0; |
| 95 |
long nodeCount = graph.getNodes().size(); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
long maxIterations = Math.max(nodeCount * nodeCount * nodeCount, 100); |
| 100 |
|
| 101 |
|
| 102 |
LinkedHashSet<DiGraphNode<N, E>> workSet = |
| 103 |
Sets.newLinkedHashSet(); |
| 104 |
for (N n : entrySet) { |
| 105 |
workSet.add(graph.getDirectedGraphNode(n)); |
| 106 |
} |
| 107 |
for (; !workSet.isEmpty() && cycleCount < maxIterations; cycleCount++) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
DiGraphNode<N, E> source = workSet.iterator().next(); |
| 113 |
N sourceValue = source.getValue(); |
| 114 |
|
| 115 |
workSet.remove(source); |
| 116 |
|
| 117 |
List<DiGraphEdge<N, E>> outEdges = source.getOutEdges(); |
| 118 |
for (DiGraphEdge<N, E> edge : outEdges) { |
| 119 |
N destNode = edge.getDestination().getValue(); |
| 120 |
if (callback.traverseEdge(sourceValue, edge.getValue(), destNode)) { |
| 121 |
workSet.add(edge.getDestination()); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
Preconditions.checkState(cycleCount != maxIterations, |
| 127 |
NON_HALTING_ERROR_MSG); |
| 128 |
} |
| 129 |
|
| 130 |
public static interface EdgeCallback<Node, Edge> { |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
boolean traverseEdge(Node source, Edge e, Node destination); |
| 143 |
} |
| 144 |
} |