Package EDU.purdue.cs.bloat.util
Class Graph
- java.lang.Object
-
- EDU.purdue.cs.bloat.util.Graph
-
- Direct Known Subclasses:
FlowGraph
public class Graph extends java.lang.ObjectGraph represents a graph of nodes with directed edges between them. GraphNodes are created and are added to the Graph before the edges can be constructed. Each GraphNode as a unique key associated with it. For instance, if a Graph represents a control flow graph, each GraphNode would be associated with a basic block.
-
-
Field Summary
Fields Modifier and Type Field Description protected intedgeModCountprotected intnodeModCountprotected intremovingEdgeprotected intremovingNodeprotected intrevRootEdgeModCountprotected introotEdgeModCount
-
Constructor Summary
Constructors Constructor Description Graph()Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEdge(GraphNode v, GraphNode w)Adds a directed edge from node v to node w.voidaddNode(java.lang.Object key, GraphNode node)Insertes a node (and its associated key) into this Graph.GraphNodegetNode(java.lang.Object key)Returns the node in this Graph with a given key.booleanhasEdge(GraphNode v, GraphNode w)Searches this Graph for an (directed) edge between two GraphNodes.booleanhasNode(GraphNode v)Searchs this Graph for a given GraphNode.booleanisAncestorToDescendent(GraphNode v, GraphNode w)Determines whether or not a node v is an ancestor (has a lower pre-order index and a higher post-order index) of a node w.java.util.SetkeySet()Returns a Set of the keys used to uniquely identify the nodes in this Graph.java.util.Collectionnodes()Returns all the nodes in this Graph.java.util.ListpostOrder()Return the nodes in this Graph ordered by their post-order index.intpostOrderIndex(GraphNode node)Returns the index of a given node in a post-order ordering of this Graph.java.util.Collectionpreds(GraphNode v)Returns the predacessors of a given node.java.util.ListpreOrder()Returns the nodes in this Graph ordered by their pre-order index.intpreOrderIndex(GraphNode node)Returns the index of a given node in a pre-order ordering of this Graph.voidremoveEdge(GraphNode v, GraphNode w)voidremoveNode(java.lang.Object key)Removes a node with a given key from the Graph.voidremoveUnreachable()Removes all nodes from this Graph that cannot be reached in a pre-order traversal of the Graph.java.util.CollectionreverseRoots()java.util.Collectionroots()intsize()Returns the number of nodes in this Graph.java.util.Collectionsuccs(GraphNode v)Return the successors of a given node.java.lang.StringtoString()
-
-
-
Method Detail
-
roots
public java.util.Collection roots()
- Returns:
- The roots of this Graph. That is, the nodes with no predacessors.
-
reverseRoots
public java.util.Collection reverseRoots()
- Returns:
- The reverse roots of this Graph. That is, the nodes with no successors.
-
succs
public java.util.Collection succs(GraphNode v)
Return the successors of a given node.
-
preds
public java.util.Collection preds(GraphNode v)
Returns the predacessors of a given node.
-
isAncestorToDescendent
public boolean isAncestorToDescendent(GraphNode v, GraphNode w)
Determines whether or not a node v is an ancestor (has a lower pre-order index and a higher post-order index) of a node w.- Parameters:
v- Candidate ancestor node.w- Candidate descendent node.- Returns:
- True, if v is an ancestor of w.
-
preOrderIndex
public int preOrderIndex(GraphNode node)
Returns the index of a given node in a pre-order ordering of this Graph.
-
postOrderIndex
public int postOrderIndex(GraphNode node)
Returns the index of a given node in a post-order ordering of this Graph.
-
preOrder
public java.util.List preOrder()
Returns the nodes in this Graph ordered by their pre-order index.
-
postOrder
public java.util.List postOrder()
Return the nodes in this Graph ordered by their post-order index.
-
removeUnreachable
public void removeUnreachable()
Removes all nodes from this Graph that cannot be reached in a pre-order traversal of the Graph. These nodes have a pre-order index of -1.
-
addNode
public void addNode(java.lang.Object key, GraphNode node)Insertes a node (and its associated key) into this Graph.- Parameters:
key- A unique value associated with this node. For instance, if this Graph represented a control flow graph, the key would be a basic block.node- The node to be added.
-
getNode
public GraphNode getNode(java.lang.Object key)
Returns the node in this Graph with a given key.
-
keySet
public java.util.Set keySet()
Returns a Set of the keys used to uniquely identify the nodes in this Graph.
-
removeNode
public void removeNode(java.lang.Object key)
Removes a node with a given key from the Graph.- Parameters:
key- The key associated with the node to remove.
-
addEdge
public void addEdge(GraphNode v, GraphNode w)
Adds a directed edge from node v to node w.- Parameters:
v- Source node.w- Destination node.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
hasNode
public boolean hasNode(GraphNode v)
Searchs this Graph for a given GraphNode.- Parameters:
v- GraphNode to search for.- Returns:
- True, if this Graphs contains v.
-
hasEdge
public boolean hasEdge(GraphNode v, GraphNode w)
Searches this Graph for an (directed) edge between two GraphNodes.- Parameters:
v- Source node of desired edge.w- Destination node of desired edge.- Returns:
- True, if an edge exists between nodes v and w.
-
nodes
public java.util.Collection nodes()
Returns all the nodes in this Graph.
-
size
public int size()
Returns the number of nodes in this Graph.
-
-