Debian Codegraph @

src/com/google/javascript/jscomp/graph/LinkedDirectedGraph.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.base.Predicate;
21 import com.google.common.base.Predicates;
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Maps;
24
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30
31 /**
32 * A directed graph using linked list within nodes to store edge information.
33 * <p>
34 * This implementation favors directed graph operations inherited from <code>
35 * DirectedGraph</code>.
36 * Operations from <code>Graph</code> would tends to be slower.
37 *
38 *
39 * @param <N> Value type that the graph node stores.
40 * @param <E> Value type that the graph edge stores.
41 */
42 public class LinkedDirectedGraph<N, E>
43 extends DiGraph<N, E> implements GraphvizGraph {
44 protected final Map<N, LinkedDirectedGraphNode<N, E>> nodes =
45 Maps.newHashMap();
46
47 @Override
48 public SubGraph<N, E> newSubGraph() {
49 return new SimpleSubGraph<N, E>(this);
50 }
51
52 public static <N, E> LinkedDirectedGraph<N, E> createWithoutAnnotations() {
53 return new LinkedDirectedGraph<N, E>(false, false);
54 }
55
56 public static <N, E> LinkedDirectedGraph<N, E> createWithNodeAnnotations() {
57 return new LinkedDirectedGraph<N, E>(true, false);
58 }
59
60 public static <N, E> LinkedDirectedGraph<N, E> createWithEdgeAnnotations() {
61 return new LinkedDirectedGraph<N, E>(false, true);
62 }
63
64 public static <N, E> LinkedDirectedGraph<N, E> create() {
65 return new LinkedDirectedGraph<N, E>(true, true);
66 }
67
68 private final boolean useNodeAnnotations;
69 private final boolean useEdgeAnnotations;
70
71 protected LinkedDirectedGraph(
72 boolean useNodeAnnotations, boolean useEdgeAnnotations) {
73 this.useNodeAnnotations = useNodeAnnotations;
74 this.useEdgeAnnotations = useEdgeAnnotations;
75 }
76
77 @Override
78 public void connect(N srcValue, E edgeValue, N destValue) {
79 LinkedDirectedGraphNode<N, E> src = getNodeOrFail(srcValue);
80 LinkedDirectedGraphNode<N, E> dest = getNodeOrFail(destValue);
81 LinkedDirectedGraphEdge<N, E> edge =
82 useEdgeAnnotations ?
83 new AnnotatedLinkedDirectedGraphEdge<N, E>(src, edgeValue, dest) :
84 new LinkedDirectedGraphEdge<N, E>(src, edgeValue, dest);
85 src.getOutEdges().add(edge);
86 dest.getInEdges().add(edge);
87 }
88
89 @Override
90 public void disconnect(N n1, N n2) {
91 disconnectInDirection(n1, n2);
92 disconnectInDirection(n2, n1);
93 }
94
95 @Override
96 public void disconnectInDirection(N srcValue, N destValue) {
97 LinkedDirectedGraphNode<N, E> src = getNodeOrFail(srcValue);
98 LinkedDirectedGraphNode<N, E> dest = getNodeOrFail(destValue);
99 for (DiGraphEdge<?, E> edge : getDirectedGraphEdges(srcValue, destValue)) {
100 src.getOutEdges().remove(edge);
101 dest.getInEdges().remove(edge);
102 }
103 }
104
105 @Override
106 public Iterable<DiGraphNode<N, E>> getDirectedGraphNodes() {
107 return Collections.<DiGraphNode<N, E>>unmodifiableCollection(
108 nodes.values());
109 }
110
111 @Override
112 public DiGraphNode<N, E> getDirectedGraphNode(N nodeValue) {
113 return nodes.get(nodeValue);
114 }
115
116 @Override
117 public GraphNode<N, E> getNode(N nodeValue) {
118 return getDirectedGraphNode(nodeValue);
119 }
120
121 @Override
122 public List<DiGraphEdge<N, E>> getInEdges(N nodeValue) {
123 LinkedDirectedGraphNode<N, E> node = getNodeOrFail(nodeValue);
124 return Collections.<DiGraphEdge<N, E>>unmodifiableList(node.getInEdges());
125 }
126
127 @Override
128 public List<DiGraphEdge<N, E>> getOutEdges(N nodeValue) {
129 LinkedDirectedGraphNode<N, E> node = getNodeOrFail(nodeValue);
130 return Collections.<DiGraphEdge<N, E>>unmodifiableList(node.getOutEdges());
131 }
132
133 @Override
134 public DiGraphNode<N, E> createDirectedGraphNode(N nodeValue) {
135 LinkedDirectedGraphNode<N, E> node = nodes.get(nodeValue);
136 if (node == null) {
137 node = useNodeAnnotations ?
138 new AnnotatedLinkedDirectedGraphNode<N, E>(nodeValue) :
139 new LinkedDirectedGraphNode<N, E>(nodeValue);
140 nodes.put(nodeValue, node);
141 }
142 return node;
143 }
144
145 @Override
146 public List<GraphEdge<N, E>> getEdges(N n1, N n2) {
147 // Since this is a method from a generic graph, edges from both
148 // directions must be added to the returning list.
149 List<DiGraphEdge<N, E>> forwardEdges = getDirectedGraphEdges(n1, n2);
150 List<DiGraphEdge<N, E>> backwardEdges = getDirectedGraphEdges(n2, n1);
151 int totalSize = forwardEdges.size() + backwardEdges.size();
152 List<GraphEdge<N, E>> edges = Lists.newArrayListWithCapacity(totalSize);
153 edges.addAll(forwardEdges);
154 edges.addAll(backwardEdges);
155 return edges;
156 }
157
158 @Override
159 public GraphEdge<N, E> getFirstEdge(N n1, N n2) {
160 DiGraphNode<N, E> dNode1 = getNodeOrFail(n1);
161 DiGraphNode<N, E> dNode2 = getNodeOrFail(n2);
162 for (DiGraphEdge<N, E> outEdge : dNode1.getOutEdges()) {
163 if (outEdge.getDestination() == dNode2) {
164 return outEdge;
165 }
166 }
167 for (DiGraphEdge<N, E> outEdge : dNode2.getOutEdges()) {
168 if (outEdge.getDestination() == dNode1) {
169 return outEdge;
170 }
171 }
172 return null;
173 }
174
175 @Override
176 public GraphNode<N, E> createNode(N value) {
177 return createDirectedGraphNode(value);
178 }
179
180 @Override
181 public List<DiGraphEdge<N, E>> getDirectedGraphEdges(N n1, N n2) {
182 DiGraphNode<N, E> dNode1 = getNodeOrFail(n1);
183 DiGraphNode<N, E> dNode2 = getNodeOrFail(n2);
184 List<DiGraphEdge<N, E>> edges = Lists.newArrayList();
185 for (DiGraphEdge<N, E> outEdge : dNode1.getOutEdges()) {
186 if (outEdge.getDestination() == dNode2) {
187 edges.add(outEdge);
188 }
189 }
190 return edges;
191 }
192
193 @Override
194 public boolean isConnectedInDirection(N n1, N n2) {
195 return isConnectedInDirection(n1, Predicates.<E>alwaysTrue(), n2);
196 }
197
198 @Override
199 public boolean isConnectedInDirection(N n1, E edgeValue, N n2) {
200 return isConnectedInDirection(n1, Predicates.equalTo(edgeValue), n2);
201 }
202
203 private boolean isConnectedInDirection(N n1, Predicate<E> edgeMatcher, N n2) {
204 // Verify the nodes.
205 DiGraphNode<N, E> dNode1 = getNodeOrFail(n1);
206 DiGraphNode<N, E> dNode2 = getNodeOrFail(n2);
207 for (DiGraphEdge<N, E> outEdge : dNode1.getOutEdges()) {
208 if (outEdge.getDestination() == dNode2 &&
209 edgeMatcher.apply(outEdge.getValue())) {
210 return true;
211 }
212 }
213
214 return false;
215 }
216
217 @Override
218 public List<DiGraphNode<N, E>> getDirectedPredNodes(N nodeValue) {
219 return getDirectedPredNodes(nodes.get(nodeValue));
220 }
221
222 @Override
223 public List<DiGraphNode<N, E>> getDirectedSuccNodes(N nodeValue) {
224 return getDirectedSuccNodes(nodes.get(nodeValue));
225 }
226
227 @Override
228 public List<DiGraphNode<N, E>> getDirectedPredNodes(
229 DiGraphNode<N, E> dNode) {
230 if (dNode == null) {
231 throw new IllegalArgumentException(dNode + " is null");
232 }
233 List<DiGraphNode<N, E>> nodeList = Lists.newArrayList();
234 for (DiGraphEdge<N, E> edge : dNode.getInEdges()) {
235 nodeList.add(edge.getSource());
236 }
237 return nodeList;
238 }
239
240 @Override
241 public List<DiGraphNode<N, E>> getDirectedSuccNodes(
242 DiGraphNode<N, E> dNode) {
243 if (dNode == null) {
244 throw new IllegalArgumentException(dNode + " is null");
245 }
246 List<DiGraphNode<N, E>> nodeList = Lists.newArrayList();
247 for (DiGraphEdge<N, E> edge : dNode.getOutEdges()) {
248 nodeList.add(edge.getDestination());
249 }
250 return nodeList;
251 }
252
253 @Override
254 public List<GraphvizEdge> getGraphvizEdges() {
255 List<GraphvizEdge> edgeList = Lists.newArrayList();
256 for (LinkedDirectedGraphNode<N, E> node : nodes.values()) {
257 for (DiGraphEdge<N, E> edge : node.getOutEdges()) {
258 edgeList.add((LinkedDirectedGraphEdge<N, E>) edge);
259 }
260 }
261 return edgeList;
262 }
263
264 @Override
265 public List<GraphvizNode> getGraphvizNodes() {
266 List<GraphvizNode> nodeList =
267 Lists.newArrayListWithCapacity(nodes.size());
268 for (LinkedDirectedGraphNode<N, E> node : nodes.values()) {
269 nodeList.add(node);
270 }
271 return nodeList;
272 }
273
274 @Override
275 public String getName() {
276 return "LinkedGraph";
277 }
278
279 @Override
280 public boolean isDirected() {
281 return true;
282 }
283
284 @Override
285 public Collection<GraphNode<N, E>> getNodes() {
286 return Collections.<GraphNode<N, E>>unmodifiableCollection(nodes.values());
287 }
288
289 @Override
290 public List<GraphNode<N, E>> getNeighborNodes(N value) {
291 DiGraphNode<N, E> node = getDirectedGraphNode(value);
292 return getNeighborNodes(node);
293 }
294
295 public List<GraphNode<N, E>> getNeighborNodes(DiGraphNode<N, E> node) {
296 List<GraphNode<N, E>> result = Lists.newArrayList();
297 for (Iterator<GraphNode<N, E>> i =
298 ((LinkedDirectedGraphNode<N, E>) node).neighborIterator();i.hasNext();) {
299 result.add(i.next());
300 }
301 return result;
302 }
303
304 @Override
305 public Iterator<GraphNode<N, E>> getNeighborNodesIterator(N value) {
306 LinkedDirectedGraphNode<N, E> node = nodes.get(value);
307 Preconditions.checkNotNull(node);
308 return node.neighborIterator();
309 }
310
311 @Override
312 public List<GraphEdge<N, E>> getEdges() {
313 List<GraphEdge<N, E>> result = Lists.newArrayList();
314 for (DiGraphNode<N, E> node : nodes.values()) {
315 for (DiGraphEdge<N, E> edge : node.getOutEdges()) {
316 result.add(edge);
317 }
318 }
319 return Collections.unmodifiableList(result);
320 }
321
322 @Override
323 public int getNodeDegree(N value) {
324 DiGraphNode<N, E> node = getNodeOrFail(value);
325 return node.getInEdges().size() + node.getOutEdges().size();
326 }
327
328 /**
329 * A directed graph node that stores outgoing edges and incoming edges as an
330 * list within the node itself.
331 */
332 static class LinkedDirectedGraphNode<N, E> implements DiGraphNode<N, E>,
333 GraphvizNode {
334
335 List<DiGraphEdge<N, E>> inEdgeList = Lists.newArrayList();
336 List<DiGraphEdge<N, E>> outEdgeList =
337 Lists.newArrayList();
338
339 protected final N value;
340
341 /**
342 * Constructor
343 *
344 * @param nodeValue Node's value.
345 */
346 LinkedDirectedGraphNode(N nodeValue) {
347 this.value = nodeValue;
348 }
349
350 @Override
351 public N getValue() {
352 return value;
353 }
354
355 @Override
356 public <A extends Annotation> A getAnnotation() {
357 throw new UnsupportedOperationException(
358 "Graph initialized with node annotations turned off");
359 }
360
361 @Override
362 public void setAnnotation(Annotation data) {
363 throw new UnsupportedOperationException(
364 "Graph initialized with node annotations turned off");
365 }
366
367 @Override
368 public String getColor() {
369 return "white";
370 }
371
372 @Override
373 public String getId() {
374 return "LDN" + hashCode();
375 }
376
377 @Override
378 public String getLabel() {
379 return value != null ? value.toString() : "null";
380 }
381
382 @Override
383 public String toString() {
384 return getLabel();
385 }
386
387 @Override
388 public List<DiGraphEdge<N, E>> getInEdges() {
389 return inEdgeList;
390 }
391
392 @Override
393 public List<DiGraphEdge<N, E>> getOutEdges() {
394 return outEdgeList;
395 }
396
397 private Iterator<GraphNode<N, E>> neighborIterator() {
398 return new NeighborIterator();
399 }
400
401 private class NeighborIterator implements Iterator<GraphNode<N, E>> {
402
403 private final Iterator<DiGraphEdge<N, E>> in = inEdgeList.iterator();
404 private final Iterator<DiGraphEdge<N, E>> out = outEdgeList.iterator();
405
406 @Override
407 public boolean hasNext() {
408 return in.hasNext() || out.hasNext();
409 }
410
411 @Override
412 public GraphNode<N, E> next() {
413 boolean isOut = !in.hasNext();
414 Iterator<DiGraphEdge<N, E>> curIterator = isOut ? out : in;
415 DiGraphEdge<N, E> s = curIterator.next();
416 return isOut ? s.getDestination() : s.getSource();
417 }
418
419 @Override
420 public void remove() {
421 throw new UnsupportedOperationException("Remove not supported.");
422 }
423 }
424 }
425
426 /**
427 * A directed graph node with annotations.
428 */
429 static class AnnotatedLinkedDirectedGraphNode<N, E>
430 extends LinkedDirectedGraphNode<N, E> {
431
432 protected Annotation annotation;
433
434 /**
435 * @param nodeValue Node's value.
436 */
437 AnnotatedLinkedDirectedGraphNode(N nodeValue) {
438 super(nodeValue);
439 }
440
441 @SuppressWarnings("unchecked")
442 @Override
443 public <A extends Annotation> A getAnnotation() {
444 return (A) annotation;
445 }
446
447 @Override
448 public void setAnnotation(Annotation data) {
449 annotation = data;
450 }
451 }
452
453 /**
454 * A directed graph edge that stores the source and destination nodes at each
455 * edge.
456 */
457 static class LinkedDirectedGraphEdge<N, E> implements DiGraphEdge<N, E>,
458 GraphvizEdge {
459
460 private DiGraphNode<N, E> sourceNode;
461
462 private DiGraphNode<N, E> destNode;
463
464 protected final E value;
465
466 /**
467 * Constructor.
468 *
469 * @param edgeValue Edge Value.
470 */
471 LinkedDirectedGraphEdge(DiGraphNode<N, E> sourceNode,
472 E edgeValue, DiGraphNode<N, E> destNode) {
473 this.value = edgeValue;
474 this.sourceNode = sourceNode;
475 this.destNode = destNode;
476 }
477
478 @Override
479 public DiGraphNode<N, E> getSource() {
480 return sourceNode;
481 }
482
483 @Override
484 public DiGraphNode<N, E> getDestination() {
485 return destNode;
486 }
487
488 @Override
489 public void setDestination(DiGraphNode<N, E> node) {
490 destNode = node;
491 }
492
493 @Override
494 public void setSource(DiGraphNode<N, E> node) {
495 sourceNode = node;
496 }
497
498 @Override
499 public E getValue() {
500 return value;
501 }
502
503 @Override
504 public <A extends Annotation> A getAnnotation() {
505 throw new UnsupportedOperationException(
506 "Graph initialized with edge annotations turned off");
507 }
508
509 @Override
510 public void setAnnotation(Annotation data) {
511 throw new UnsupportedOperationException(
512 "Graph initialized with edge annotations turned off");
513 }
514
515 @Override
516 public String getColor() {
517 return "black";
518 }
519
520 @Override
521 public String getLabel() {
522 return value != null ? value.toString() : "null";
523 }
524
525 @Override
526 public String getNode1Id() {
527 return ((LinkedDirectedGraphNode<N, E>) sourceNode).getId();
528 }
529
530 @Override
531 public String getNode2Id() {
532 return ((LinkedDirectedGraphNode<N, E>) destNode).getId();
533 }
534
535 @Override
536 public String toString() {
537 return sourceNode.toString() + " -> " + destNode.toString();
538 }
539
540 @Override
541 public GraphNode<N, E> getNodeA() {
542 return sourceNode;
543 }
544
545 @Override
546 public GraphNode<N, E> getNodeB() {
547 return destNode;
548 }
549 }
550
551 /**
552 * A directed graph edge that stores the source and destination nodes at each
553 * edge.
554 */
555 static class AnnotatedLinkedDirectedGraphEdge<N, E>
556 extends LinkedDirectedGraphEdge<N, E> {
557
558 protected Annotation annotation;
559
560 /**
561 * Constructor.
562 *
563 * @param edgeValue Edge Value.
564 */
565 AnnotatedLinkedDirectedGraphEdge(DiGraphNode<N, E> sourceNode,
566 E edgeValue, DiGraphNode<N, E> destNode) {
567 super(sourceNode, edgeValue, destNode);
568 }
569
570 @SuppressWarnings("unchecked")
571 @Override
572 public <A extends Annotation> A getAnnotation() {
573 return (A) annotation;
574 }
575
576 @Override
577 public void setAnnotation(Annotation data) {
578 annotation = data;
579 }
580 }
581 }