| 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.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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class LinkedUndirectedGraph<N, E> |
| 40 |
extends UndiGraph<N, E> implements GraphvizGraph { |
| 41 |
protected final Map<N, LinkedUndirectedGraphNode<N, E>> nodes = |
| 42 |
Maps.newHashMap(); |
| 43 |
|
| 44 |
@Override |
| 45 |
public SubGraph<N, E> newSubGraph() { |
| 46 |
return new SimpleSubGraph<N, E>(this); |
| 47 |
} |
| 48 |
|
| 49 |
public static <N, E> LinkedUndirectedGraph<N, E> createWithoutAnnotations() { |
| 50 |
return new LinkedUndirectedGraph<N, E>(false, false); |
| 51 |
} |
| 52 |
|
| 53 |
public static <N, E> LinkedUndirectedGraph<N, E> createWithNodeAnnotations() { |
| 54 |
return new LinkedUndirectedGraph<N, E>(true, false); |
| 55 |
} |
| 56 |
|
| 57 |
public static <N, E> LinkedUndirectedGraph<N, E> createWithEdgeAnnotations() { |
| 58 |
return new LinkedUndirectedGraph<N, E>(false, true); |
| 59 |
} |
| 60 |
|
| 61 |
public static <N, E> LinkedUndirectedGraph<N, E> create() { |
| 62 |
return new LinkedUndirectedGraph<N, E>(true, true); |
| 63 |
} |
| 64 |
|
| 65 |
private final boolean useNodeAnnotations; |
| 66 |
private final boolean useEdgeAnnotations; |
| 67 |
|
| 68 |
protected LinkedUndirectedGraph( |
| 69 |
boolean useNodeAnnotations, boolean useEdgeAnnotations) { |
| 70 |
this.useNodeAnnotations = useNodeAnnotations; |
| 71 |
this.useEdgeAnnotations = useEdgeAnnotations; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void connect(N srcValue, E edgeValue, N destValue) { |
| 76 |
LinkedUndirectedGraphNode<N, E> src = getNodeOrFail(srcValue); |
| 77 |
LinkedUndirectedGraphNode<N, E> dest = getNodeOrFail(destValue); |
| 78 |
LinkedUndirectedGraphEdge<N, E> edge = |
| 79 |
useEdgeAnnotations ? |
| 80 |
new AnnotatedLinkedUndirectedGraphEdge<N, E>(src, edgeValue, dest) : |
| 81 |
new LinkedUndirectedGraphEdge<N, E>(src, edgeValue, dest); |
| 82 |
src.getNeighborEdges().add(edge); |
| 83 |
dest.getNeighborEdges().add(edge); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void disconnect(N srcValue, N destValue) { |
| 88 |
LinkedUndirectedGraphNode<N, E> src = getNodeOrFail(srcValue); |
| 89 |
LinkedUndirectedGraphNode<N, E> dest = getNodeOrFail(destValue); |
| 90 |
for (UndiGraphEdge<N, E> edge : |
| 91 |
getUndirectedGraphEdges(srcValue, destValue)) { |
| 92 |
src.getNeighborEdges().remove(edge); |
| 93 |
dest.getNeighborEdges().remove(edge); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public UndiGraphNode<N, E> createUndirectedGraphNode( |
| 99 |
N nodeValue) { |
| 100 |
LinkedUndirectedGraphNode<N, E> node = nodes.get(nodeValue); |
| 101 |
if (node == null) { |
| 102 |
node = useNodeAnnotations ? |
| 103 |
new AnnotatedLinkedUndirectedGraphNode<N, E>(nodeValue) : |
| 104 |
new LinkedUndirectedGraphNode<N, E>(nodeValue); |
| 105 |
nodes.put(nodeValue, node); |
| 106 |
} |
| 107 |
return node; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public List<GraphNode<N, E>> getNeighborNodes(N value) { |
| 112 |
List<GraphNode<N, E>> nodeList = Lists.newArrayList(); |
| 113 |
for (Iterator<GraphNode<N, E>> i = getNeighborNodesIterator(value); |
| 114 |
i.hasNext();) { |
| 115 |
nodeList.add(i.next()); |
| 116 |
} |
| 117 |
return nodeList; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public Iterator<GraphNode<N, E>> getNeighborNodesIterator(N value) { |
| 122 |
UndiGraphNode<N, E> uNode = getUndirectedGraphNode(value); |
| 123 |
Preconditions.checkNotNull(uNode, "%s should be in the graph.", value); |
| 124 |
return ((LinkedUndirectedGraphNode<N, E>) uNode).neighborIterator(); |
| 125 |
} |
| 126 |
|
| 127 |
@SuppressWarnings("unchecked") |
| 128 |
@Override |
| 129 |
public List<UndiGraphEdge<N, E>> getUndirectedGraphEdges(N n1, N n2) { |
| 130 |
UndiGraphNode<N, E> dNode1 = nodes.get(n1); |
| 131 |
if (dNode1 == null) { |
| 132 |
return null; |
| 133 |
} |
| 134 |
UndiGraphNode<N, E> dNode2 = nodes.get(n2); |
| 135 |
if (dNode2 == null) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
List<UndiGraphEdge<N, E>> edges = Lists.newArrayList(); |
| 139 |
for (UndiGraphEdge<N, E> outEdge : dNode1.getNeighborEdges()) { |
| 140 |
if (outEdge.getNodeA() == dNode2 || outEdge.getNodeB() == dNode2) { |
| 141 |
edges.add(outEdge); |
| 142 |
} |
| 143 |
} |
| 144 |
return edges; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public UndiGraphNode<N, E> getUndirectedGraphNode(N nodeValue) { |
| 149 |
return nodes.get(nodeValue); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public Collection<UndiGraphNode<N, E>> getUndirectedGraphNodes() { |
| 154 |
return Collections.<UndiGraphNode<N, E>>unmodifiableCollection( |
| 155 |
nodes.values()); |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public GraphNode<N, E> createNode(N value) { |
| 160 |
return createUndirectedGraphNode(value); |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public List<GraphEdge<N, E>> getEdges(N n1, N n2) { |
| 165 |
return Collections.<GraphEdge<N, E>>unmodifiableList( |
| 166 |
getUndirectedGraphEdges(n1, n2)); |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public GraphEdge<N, E> getFirstEdge(N n1, N n2) { |
| 171 |
UndiGraphNode<N, E> dNode1 = getNodeOrFail(n1); |
| 172 |
UndiGraphNode<N, E> dNode2 = getNodeOrFail(n2); |
| 173 |
for (UndiGraphEdge<N, E> outEdge : dNode1.getNeighborEdges()) { |
| 174 |
if (outEdge.getNodeA() == dNode2 || outEdge.getNodeB() == dNode2) { |
| 175 |
return outEdge; |
| 176 |
} |
| 177 |
} |
| 178 |
return null; |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public GraphNode<N, E> getNode(N value) { |
| 183 |
return getUndirectedGraphNode(value); |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public boolean isConnected(N n1, N n2) { |
| 188 |
return isConnected(n1, Predicates.<E>alwaysTrue(), n2); |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public boolean isConnected(N n1, E e, N n2) { |
| 193 |
return isConnected(n1, Predicates.<E>equalTo(e), n2); |
| 194 |
} |
| 195 |
|
| 196 |
private boolean isConnected(N n1, Predicate<E> edgePredicate, N n2) { |
| 197 |
UndiGraphNode<N, E> dNode1 = nodes.get(n1); |
| 198 |
if (dNode1 == null) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
UndiGraphNode<N, E> dNode2 = nodes.get(n2); |
| 202 |
if (dNode2 == null) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
for (UndiGraphEdge<N, E> outEdge : dNode1.getNeighborEdges()) { |
| 206 |
if ((outEdge.getNodeA() == dNode1 && outEdge.getNodeB() == dNode2) || |
| 207 |
(outEdge.getNodeA() == dNode2 && outEdge.getNodeB() == dNode1)) { |
| 208 |
if (edgePredicate.apply(outEdge.getValue())) { |
| 209 |
return true; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public List<GraphvizEdge> getGraphvizEdges() { |
| 218 |
List<GraphvizEdge> edgeList = Lists.newArrayList(); |
| 219 |
for (LinkedUndirectedGraphNode<N, E> node : nodes.values()) { |
| 220 |
for (UndiGraphEdge<N, E> edge : node.getNeighborEdges()) { |
| 221 |
if (edge.getNodeA() == node) { |
| 222 |
edgeList.add((GraphvizEdge) edge); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
return edgeList; |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public String getName() { |
| 231 |
return "LinkedUndirectedGraph"; |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public List<GraphvizNode> getGraphvizNodes() { |
| 236 |
List<GraphvizNode> nodeList = |
| 237 |
Lists.newArrayListWithCapacity(nodes.size()); |
| 238 |
for (LinkedUndirectedGraphNode<N, E> node : nodes.values()) { |
| 239 |
nodeList.add(node); |
| 240 |
} |
| 241 |
return nodeList; |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public boolean isDirected() { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public Collection<GraphNode<N, E>> getNodes() { |
| 251 |
return Collections.<GraphNode<N, E>> unmodifiableCollection(nodes.values()); |
| 252 |
} |
| 253 |
|
| 254 |
@SuppressWarnings("unchecked") |
| 255 |
@Override |
| 256 |
public List<GraphEdge<N, E>> getEdges() { |
| 257 |
List<GraphEdge<N, E>> result = Lists.newArrayList(); |
| 258 |
for (LinkedUndirectedGraphNode<N, E> node : nodes.values()) { |
| 259 |
for (UndiGraphEdge<N, E> edge : node.getNeighborEdges()) { |
| 260 |
if (edge.getNodeA() == node) { |
| 261 |
result.add(edge); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
return result; |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public int getNodeDegree(N value) { |
| 270 |
UndiGraphNode<N, E> uNode = getUndirectedGraphNode(value); |
| 271 |
if (uNode == null) { |
| 272 |
throw new IllegalArgumentException(value + " not found in graph"); |
| 273 |
} |
| 274 |
return uNode.getNeighborEdges().size(); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
static class LinkedUndirectedGraphNode<N, E> implements UndiGraphNode<N, E>, |
| 282 |
GraphvizNode { |
| 283 |
|
| 284 |
private List<UndiGraphEdge<N, E>> neighborList = |
| 285 |
Lists.newArrayList(); |
| 286 |
private final N value; |
| 287 |
|
| 288 |
LinkedUndirectedGraphNode(N nodeValue) { |
| 289 |
this.value = nodeValue; |
| 290 |
} |
| 291 |
|
| 292 |
@Override |
| 293 |
public List<UndiGraphEdge<N, E>> getNeighborEdges() { |
| 294 |
return neighborList; |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public Iterator<UndiGraphEdge<N, E>> getNeighborEdgesIterator() { |
| 299 |
return neighborList.iterator(); |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public <A extends Annotation> A getAnnotation() { |
| 304 |
throw new UnsupportedOperationException( |
| 305 |
"Graph initialized with node annotations turned off"); |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
public void setAnnotation(Annotation data) { |
| 310 |
throw new UnsupportedOperationException( |
| 311 |
"Graph initialized with node annotations turned off"); |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public N getValue() { |
| 316 |
return value; |
| 317 |
} |
| 318 |
|
| 319 |
@Override |
| 320 |
public String getColor() { |
| 321 |
return "white"; |
| 322 |
} |
| 323 |
|
| 324 |
@Override |
| 325 |
public String getId() { |
| 326 |
return "LDN" + hashCode(); |
| 327 |
} |
| 328 |
|
| 329 |
@Override |
| 330 |
public String getLabel() { |
| 331 |
return value != null ? value.toString() : "null"; |
| 332 |
} |
| 333 |
|
| 334 |
public Iterator<GraphNode<N, E>> neighborIterator() { |
| 335 |
return new NeighborIterator(); |
| 336 |
} |
| 337 |
|
| 338 |
private class NeighborIterator implements Iterator<GraphNode<N, E>> { |
| 339 |
|
| 340 |
private final Iterator<UndiGraphEdge<N, E>> edgeIterator = |
| 341 |
neighborList.iterator(); |
| 342 |
|
| 343 |
@Override |
| 344 |
public boolean hasNext() { |
| 345 |
return edgeIterator.hasNext(); |
| 346 |
} |
| 347 |
|
| 348 |
@Override |
| 349 |
public GraphNode<N, E> next() { |
| 350 |
UndiGraphEdge<N, E> edge = edgeIterator.next(); |
| 351 |
if (edge.getNodeA() == LinkedUndirectedGraphNode.this) { |
| 352 |
return edge.getNodeB(); |
| 353 |
} else { |
| 354 |
return edge.getNodeA(); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
public void remove() { |
| 360 |
throw new UnsupportedOperationException("Remove not supported."); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
static class AnnotatedLinkedUndirectedGraphNode<N, E> |
| 369 |
extends LinkedUndirectedGraphNode<N, E> { |
| 370 |
|
| 371 |
protected Annotation annotation; |
| 372 |
|
| 373 |
AnnotatedLinkedUndirectedGraphNode(N nodeValue) { |
| 374 |
super(nodeValue); |
| 375 |
} |
| 376 |
|
| 377 |
@SuppressWarnings("unchecked") |
| 378 |
@Override |
| 379 |
public <A extends Annotation> A getAnnotation() { |
| 380 |
return (A) annotation; |
| 381 |
} |
| 382 |
|
| 383 |
@Override |
| 384 |
public void setAnnotation(Annotation data) { |
| 385 |
annotation = data; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
static class LinkedUndirectedGraphEdge<N, E> implements UndiGraphEdge<N, E>, |
| 393 |
GraphvizEdge { |
| 394 |
|
| 395 |
private UndiGraphNode<N, E> nodeA; |
| 396 |
private UndiGraphNode<N, E> nodeB; |
| 397 |
protected final E value; |
| 398 |
|
| 399 |
LinkedUndirectedGraphEdge(UndiGraphNode<N, E> nodeA, E edgeValue, |
| 400 |
UndiGraphNode<N, E> nodeB) { |
| 401 |
this.value = edgeValue; |
| 402 |
this.nodeA = nodeA; |
| 403 |
this.nodeB = nodeB; |
| 404 |
} |
| 405 |
|
| 406 |
@Override |
| 407 |
public E getValue() { |
| 408 |
return value; |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public GraphNode<N, E> getNodeA() { |
| 413 |
return nodeA; |
| 414 |
} |
| 415 |
|
| 416 |
@Override |
| 417 |
public GraphNode<N, E> getNodeB() { |
| 418 |
return nodeB; |
| 419 |
} |
| 420 |
|
| 421 |
@Override |
| 422 |
public <A extends Annotation> A getAnnotation() { |
| 423 |
throw new UnsupportedOperationException( |
| 424 |
"Graph initialized with edge annotations turned off"); |
| 425 |
} |
| 426 |
|
| 427 |
@Override |
| 428 |
public void setAnnotation(Annotation data) { |
| 429 |
throw new UnsupportedOperationException( |
| 430 |
"Graph initialized with edge annotations turned off"); |
| 431 |
} |
| 432 |
|
| 433 |
@Override |
| 434 |
public String getColor() { |
| 435 |
return "black"; |
| 436 |
} |
| 437 |
|
| 438 |
@Override |
| 439 |
public String getLabel() { |
| 440 |
return value != null ? value.toString() : "null"; |
| 441 |
} |
| 442 |
|
| 443 |
@SuppressWarnings("unchecked") |
| 444 |
@Override |
| 445 |
public String getNode1Id() { |
| 446 |
return ((LinkedUndirectedGraphNode<N, E>) nodeA).getId(); |
| 447 |
} |
| 448 |
|
| 449 |
@SuppressWarnings("unchecked") |
| 450 |
@Override |
| 451 |
public String getNode2Id() { |
| 452 |
return ((LinkedUndirectedGraphNode<N, E>) nodeB).getId(); |
| 453 |
} |
| 454 |
|
| 455 |
@Override |
| 456 |
public String toString() { |
| 457 |
return nodeA.toString() + " -- " + nodeB.toString(); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
static class AnnotatedLinkedUndirectedGraphEdge<N, E> |
| 465 |
extends LinkedUndirectedGraphEdge<N, E> { |
| 466 |
|
| 467 |
protected Annotation annotation; |
| 468 |
|
| 469 |
AnnotatedLinkedUndirectedGraphEdge( |
| 470 |
UndiGraphNode<N, E> nodeA, E edgeValue, |
| 471 |
UndiGraphNode<N, E> nodeB) { |
| 472 |
super(nodeA, edgeValue, nodeB); |
| 473 |
} |
| 474 |
|
| 475 |
@SuppressWarnings("unchecked") |
| 476 |
@Override |
| 477 |
public <A extends Annotation> A getAnnotation() { |
| 478 |
return (A) annotation; |
| 479 |
} |
| 480 |
|
| 481 |
@Override |
| 482 |
public void setAnnotation(Annotation data) { |
| 483 |
annotation = data; |
| 484 |
} |
| 485 |
} |
| 486 |
} |