Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2011 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.Predicate;
20 import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge;
21 import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode;
22
23 /**
24 * Prunes a graph, creating a new graph with nodes removed.
25 *
26 * If a node is removed from the graph, any paths through that node
27 * will be replaced with edges. In other words, if A and B are nodes
28 * in the original graph and the pruned graph, then there exists a path
29 * from A -> B in the original graph iff there's a path from A -> B
30 * in the pruned graph.
31 *
32 * We do not make any guarantees about what edges are in the pruned graph.
33 *
34 * @author [email protected] (Nick Santos)
35 */
36 public class GraphPruner<N, E> {
37 private final DiGraph<N, E> graph;
38
39 public GraphPruner(DiGraph<N, E> graph) {
40 this.graph = graph;
41 }
42
43 public LinkedDirectedGraph<N, E> prune(Predicate<N> keep) {
44 LinkedDirectedGraph<N, E> workGraph = cloneGraph(graph);
45
46 // Create a work graph where all nodes with a path between them have
47 // an edge.
48 for (DiGraphNode<N, E> node : workGraph.getDirectedGraphNodes()) {
49 for (DiGraphEdge<N, E> inEdge : node.getInEdges()) {
50 for (DiGraphEdge<N, E> outEdge : node.getOutEdges()) {
51 N source = inEdge.getSource().getValue();
52 N dest = outEdge.getDestination().getValue();
53 if (!workGraph.isConnectedInDirection(source, dest)) {
54 workGraph.connect(source, outEdge.getValue(), dest);
55 }
56 }
57 }
58 }
59
60
61 // Build a complete subgraph of workGraph.
62 LinkedDirectedGraph<N, E> resultGraph =
63 LinkedDirectedGraph.create();
64 for (DiGraphNode<N, E> node : workGraph.getDirectedGraphNodes()) {
65 if (keep.apply(node.getValue())) {
66 resultGraph.createNode(node.getValue());
67
68 for (DiGraphEdge<N, E> outEdge : node.getOutEdges()) {
69 N source = node.getValue();
70 N dest = outEdge.getDestination().getValue();
71 if (keep.apply(dest)) {
72 resultGraph.createNode(dest);
73 if (source != dest &&
74 !resultGraph.isConnectedInDirection(source, dest)) {
75 resultGraph.connect(source, outEdge.getValue(), dest);
76 }
77 }
78 }
79 }
80 }
81
82 return resultGraph;
83 }
84
85 private static <N, E> LinkedDirectedGraph<N, E> cloneGraph(
86 DiGraph<N, E> graph) {
87 LinkedDirectedGraph<N, E> newGraph = LinkedDirectedGraph.create();
88 for (DiGraphNode<N, E> node : graph.getDirectedGraphNodes()) {
89 newGraph.createNode(node.getValue());
90
91 for (DiGraphEdge<N, E> outEdge : node.getOutEdges()) {
92 N dest = outEdge.getDestination().getValue();
93 newGraph.createNode(dest);
94 newGraph.connect(node.getValue(), outEdge.getValue(), dest);
95 }
96 }
97
98 return newGraph;
99 }
100 }