| 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.Lists; |
| 21 |
|
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.Collection; |
| 24 |
import java.util.Deque; |
| 25 |
import java.util.Iterator; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public abstract class Graph<N, E> implements AdjacencyGraph<N, E> { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private static final class AnnotationState { |
| 63 |
private final Annotatable first; |
| 64 |
private final Annotation second; |
| 65 |
|
| 66 |
public AnnotationState(Annotatable annotatable, Annotation annotation) { |
| 67 |
this.first = annotatable; |
| 68 |
this.second = annotation; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private static class GraphAnnotationState extends ArrayList<AnnotationState> { |
| 77 |
private static final long serialVersionUID = 1L; |
| 78 |
|
| 79 |
public GraphAnnotationState(int size) { |
| 80 |
super(size); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private Deque<GraphAnnotationState> nodeAnnotationStack; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private Deque<GraphAnnotationState> edgeAnnotationStack; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public abstract void connect(N n1, E edge, N n2); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public abstract void disconnect(N n1, N n2); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public final void connectIfNotFound(N n1, E edge, N n2) { |
| 120 |
if (!isConnected(n1, edge, n2)) { |
| 121 |
connect(n1, edge, n2); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public abstract GraphNode<N, E> createNode(N value); |
| 134 |
|
| 135 |
|
| 136 |
@Override |
| 137 |
public abstract Collection<GraphNode<N, E>> getNodes(); |
| 138 |
|
| 139 |
|
| 140 |
public abstract List<GraphEdge<N, E>> getEdges(); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public abstract int getNodeDegree(N value); |
| 149 |
|
| 150 |
@Override |
| 151 |
public int getWeight(N value) { |
| 152 |
return getNodeDegree(value); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public abstract List<GraphNode<N, E>> getNeighborNodes(N value); |
| 162 |
|
| 163 |
public abstract Iterator<GraphNode<N, E>> getNeighborNodesIterator(N value); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public abstract List<GraphEdge<N, E>> getEdges(N n1, N n2); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public abstract GraphEdge<N, E> getFirstEdge(N n1, N n2); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public final boolean hasNode(N n) { |
| 192 |
return getNode(n) != null; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public abstract boolean isConnected(N n1, N n2); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public abstract boolean isConnected(N n1, E e, N n2); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
@SuppressWarnings("unchecked") |
| 219 |
<T extends GraphNode<N, E>> T getNodeOrFail(N val) { |
| 220 |
T node = (T) getNode(val); |
| 221 |
if (node == null) { |
| 222 |
throw new IllegalArgumentException(val + " does not exist in graph"); |
| 223 |
} |
| 224 |
return node; |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public final void clearNodeAnnotations() { |
| 229 |
for (GraphNode<N, E> n : getNodes()) { |
| 230 |
n.setAnnotation(null); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
public final void clearEdgeAnnotations() { |
| 236 |
for (GraphEdge<N, E> e : getEdges()) { |
| 237 |
e.setAnnotation(null); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public final void pushNodeAnnotations() { |
| 246 |
if (nodeAnnotationStack == null) { |
| 247 |
nodeAnnotationStack = Lists.newLinkedList(); |
| 248 |
} |
| 249 |
pushAnnotations(nodeAnnotationStack, getNodes()); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public final void popNodeAnnotations() { |
| 257 |
Preconditions.checkNotNull(nodeAnnotationStack, |
| 258 |
"Popping node annotations without pushing."); |
| 259 |
popAnnotations(nodeAnnotationStack); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public final void pushEdgeAnnotations() { |
| 267 |
if (edgeAnnotationStack == null) { |
| 268 |
edgeAnnotationStack = Lists.newLinkedList(); |
| 269 |
} |
| 270 |
pushAnnotations(edgeAnnotationStack, getEdges()); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public final void popEdgeAnnotations() { |
| 278 |
Preconditions.checkNotNull(edgeAnnotationStack, |
| 279 |
"Popping edge annotations without pushing."); |
| 280 |
popAnnotations(edgeAnnotationStack); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public interface GraphEdge<N, E> extends Annotatable { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
E getValue(); |
| 296 |
|
| 297 |
GraphNode<N, E> getNodeA(); |
| 298 |
|
| 299 |
GraphNode<N, E> getNodeB(); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
class SimpleSubGraph<N, E> implements SubGraph<N, E> { |
| 307 |
private Graph<N, E> graph; |
| 308 |
private List<GraphNode<N, E>> nodes = Lists.newArrayList(); |
| 309 |
|
| 310 |
SimpleSubGraph(Graph<N, E> graph) { |
| 311 |
this.graph = graph; |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public boolean isIndependentOf(N value) { |
| 316 |
GraphNode<N, E> node = graph.getNode(value); |
| 317 |
for (GraphNode<N, E> n : nodes) { |
| 318 |
if (graph.getNeighborNodes(n.getValue()).contains(node)) { |
| 319 |
return false; |
| 320 |
} |
| 321 |
} |
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
@Override |
| 326 |
public void addNode(N value) { |
| 327 |
nodes.add(graph.getNodeOrFail(value)); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
private static void pushAnnotations( |
| 336 |
Deque<GraphAnnotationState> stack, |
| 337 |
Collection<? extends Annotatable> haveAnnotations) { |
| 338 |
stack.push(new GraphAnnotationState(haveAnnotations.size())); |
| 339 |
for (Annotatable h : haveAnnotations) { |
| 340 |
stack.peek().add(new AnnotationState(h, h.getAnnotation())); |
| 341 |
h.setAnnotation(null); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
private static void popAnnotations(Deque<GraphAnnotationState> stack) { |
| 349 |
for (AnnotationState as : stack.pop()) { |
| 350 |
as.first.setAnnotation(as.second); |
| 351 |
} |
| 352 |
} |
| 353 |
} |