Debian Codegraph @

src/com/google/javascript/jscomp/graph/FixedPointGraphTraversal.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * A utility class for doing fixed-point computations. We traverse
31 * the edges over the given directed graph until the graph reaches
32 * a steady state.
33 *
34 * @author [email protected] (Nick Santos)
35 *
36 * @param <N> Value type that the graph node stores.
37 * @param <E> Value type that the graph edge stores.
38 */
39 public final class FixedPointGraphTraversal<N, E> {
40 // TODO(nicksantos): Generalize the algorithm for undirected graphs, if we
41 // need it.
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 * Create a new traversal.
50 * @param callback A callback for updating the state of the graph each
51 * time an edge is traversed.
52 */
53 public FixedPointGraphTraversal(EdgeCallback<N, E> callback) {
54 this.callback = callback;
55 }
56
57 /**
58 * Helper method for creating new traversals.
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 * Compute a fixed point for the given graph.
67 * @param graph The graph to traverse.
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 * Compute a fixed point for the given graph, entering from the given node.
79 * @param graph The graph to traverse.
80 * @param entry The node to begin traversing from.
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 * Compute a fixed point for the given graph, entering from the given nodes.
90 * @param graph The graph to traverse.
91 * @param entrySet The nodes to begin traversing from.
92 */
93 public void computeFixedPoint(DiGraph<N, E> graph, Set<N> entrySet) {
94 int cycleCount = 0;
95 long nodeCount = graph.getNodes().size();
96
97 // Choose a bail-out heuristically in case the computation
98 // doesn't converge.
99 long maxIterations = Math.max(nodeCount * nodeCount * nodeCount, 100);
100
101 // Use a LinkedHashSet, so that the traversal is deterministic.
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 // For every out edge in the workSet, traverse that edge. If that
109 // edge updates the state of the graph, then add the destination
110 // node to the resultSet, so that we can update all of its out edges
111 // on the next iteration.
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 * Update the state of the destination node when the given edge
133 * is traversed. For the fixed-point computation to work, only the
134 * destination node may be modified. The source node and the edge must
135 * not be modified.
136 *
137 * @param source The start node.
138 * @param e The edge.
139 * @param destination The end node.
140 * @return Whether the state of the destination node changed.
141 */
142 boolean traverseEdge(Node source, Edge e, Node destination);
143 }
144 }