Class Graph<N,E>
- java.lang.Object
-
- com.google.javascript.jscomp.graph.Graph<N,E>
-
- Type Parameters:
N- Value type that the graph node stores.E- Value type that the graph edge stores.
- All Implemented Interfaces:
AdjacencyGraph<N,E>
public abstract class Graph<N,E> extends java.lang.Object implements AdjacencyGraph<N,E>
The base generic class for graph-like data structure and algorithms in the compiler.Nodes and edges in the graph can store a piece of data that this graph is used to represent. For example, a variable interference graph might store a variable in the node. This piece of data can be accessed with
GraphNode.getValue()andGraph.GraphEdge.getValue().Algorithms and analysis can annotate information on the nodes and edges using
GraphNode.getValue()andGraph.GraphEdge.getValue(). For example, a graph coloring algorithm can store the color as an annotation. If multiple analyses are required, it is up to the user of the analysis to save the annotated solution between passes.We implemented our own graph data structure (as opposed to using
com.google.common.graph) for two reasons. First, aside from the node's label value, we would like to annotate information on the nodes and edges. Using a map to annotate would introduce too much overhead during fix point analysis. Also,com.google.common.graphdoes not support labeling of edges. Secondly, not using another external package would limit our dependencies.TODO(user): All functionality for removing nodes and edges.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceGraph.GraphEdge<N,E>A generic edge.
-
Constructor Summary
Constructors Constructor Description Graph()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclearEdgeAnnotations()Makes each edge's annotation null.voidclearNodeAnnotations()Makes each node's annotation null.abstract voidconnect(N n1, E edge, N n2)Connects two nodes in the graph with an edge.voidconnectIfNotFound(N n1, E edge, N n2)Connects two nodes in the graph with an edge if such edge does not already exists between the nodes.abstract GraphNode<N,E>createNode(N value)Gets a node from the graph given a value.abstract voiddisconnect(N n1, N n2)Disconnects two nodes in the graph by removing all edges between them.abstract java.util.List<? extends Graph.GraphEdge<N,E>>getEdges()Gets an immutable list of all edges.abstract java.util.List<? extends Graph.GraphEdge<N,E>>getEdges(N n1, N n2)Retrieves an edge from the graph.abstract Graph.GraphEdge<N,E>getFirstEdge(N n1, N n2)Retrieves any edge from the graph.abstract java.util.List<GraphNode<N,E>>getNeighborNodes(N value)Gets the neighboring nodes.abstract intgetNodeCount()abstract intgetNodeDegree(N value)Gets the degree of a node.abstract java.util.Collection<? extends GraphNode<N,E>>getNodes()Gets an immutable list of all nodes.intgetWeight(N value)Returns a weight for the given value to be used in ordering nodes, e.g.booleanhasNode(N n)Checks whether the node exists in the graph (createNode(Object)has been called with that value).abstract booleanisConnected(N n1, E e, N n2)Checks whether two nodes in the graph are connected by the given edge type.abstract booleanisConnected(N n1, N n2)Checks whether two nodes in the graph are connected.voidpopEdgeAnnotations()Restores edges' annotation values to state before lastpushEdgeAnnotations().voidpopNodeAnnotations()Restores nodes' annotation values to state before lastpushNodeAnnotations().voidpushEdgeAnnotations()Pushes edges' annotation values.voidpushNodeAnnotations()Pushes nodes' annotation values.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.google.javascript.jscomp.graph.AdjacencyGraph
getNode, newSubGraph
-
-
-
-
Method Detail
-
connect
public abstract void connect(N n1, E edge, N n2)
Connects two nodes in the graph with an edge.- Parameters:
n1- First node.edge- The edge.n2- Second node.
-
disconnect
public abstract void disconnect(N n1, N n2)
Disconnects two nodes in the graph by removing all edges between them.- Parameters:
n1- First node.n2- Second node.
-
connectIfNotFound
public final void connectIfNotFound(N n1, E edge, N n2)
Connects two nodes in the graph with an edge if such edge does not already exists between the nodes.- Parameters:
n1- First node.edge- The edge.n2- Second node.
-
createNode
public abstract GraphNode<N,E> createNode(N value)
Gets a node from the graph given a value. New nodes are created if that value has not been assigned a graph node. Values equality are compared usingObject.equals.- Parameters:
value- The node's value.- Returns:
- The corresponding node in the graph.
-
getNodes
public abstract java.util.Collection<? extends GraphNode<N,E>> getNodes()
Gets an immutable list of all nodes.- Specified by:
getNodesin interfaceAdjacencyGraph<N,E>
-
getNodeCount
public abstract int getNodeCount()
- Specified by:
getNodeCountin interfaceAdjacencyGraph<N,E>
-
getEdges
public abstract java.util.List<? extends Graph.GraphEdge<N,E>> getEdges()
Gets an immutable list of all edges.
-
getEdges
public abstract java.util.List<? extends Graph.GraphEdge<N,E>> getEdges(N n1, N n2)
Retrieves an edge from the graph.- Parameters:
n1- Node one.n2- Node two.- Returns:
- The list of edges between those two values in the graph.
-
getNodeDegree
public abstract int getNodeDegree(N value)
Gets the degree of a node.- Parameters:
value- The node's value.- Returns:
- The degree of the node.
-
getWeight
public int getWeight(N value)
Description copied from interface:AdjacencyGraphReturns a weight for the given value to be used in ordering nodes, e.g. inGraphColoring.- Specified by:
getWeightin interfaceAdjacencyGraph<N,E>
-
getNeighborNodes
public abstract java.util.List<GraphNode<N,E>> getNeighborNodes(N value)
Gets the neighboring nodes.- Parameters:
value- The node's value.- Returns:
- A list of neighboring nodes.
-
getFirstEdge
public abstract Graph.GraphEdge<N,E> getFirstEdge(N n1, N n2)
Retrieves any edge from the graph.- Parameters:
n1- Node one.n2- Node two.- Returns:
- The first edges between those two values in the graph. null if there are none.
-
hasNode
public final boolean hasNode(N n)
Checks whether the node exists in the graph (createNode(Object)has been called with that value).- Parameters:
n- Node.- Returns:
trueif it exist.
-
isConnected
public abstract boolean isConnected(N n1, N n2)
Checks whether two nodes in the graph are connected.- Parameters:
n1- Node 1.n2- Node 2.- Returns:
trueif the two nodes are connected.
-
isConnected
public abstract boolean isConnected(N n1, E e, N n2)
Checks whether two nodes in the graph are connected by the given edge type.- Parameters:
n1- Node 1.e- The edge type.n2- Node 2.
-
clearNodeAnnotations
public final void clearNodeAnnotations()
Description copied from interface:AdjacencyGraphMakes each node's annotation null.- Specified by:
clearNodeAnnotationsin interfaceAdjacencyGraph<N,E>
-
clearEdgeAnnotations
public final void clearEdgeAnnotations()
Makes each edge's annotation null.
-
pushNodeAnnotations
public final void pushNodeAnnotations()
Pushes nodes' annotation values. Restored withpopNodeAnnotations(). Nodes' annotation values are cleared.
-
popNodeAnnotations
public final void popNodeAnnotations()
Restores nodes' annotation values to state before lastpushNodeAnnotations().
-
pushEdgeAnnotations
public final void pushEdgeAnnotations()
Pushes edges' annotation values. Restored withpopEdgeAnnotations(). Edges' annotation values are cleared.
-
popEdgeAnnotations
public final void popEdgeAnnotations()
Restores edges' annotation values to state before lastpushEdgeAnnotations().
-
-