Package ghidra.util.graph
Class AbstractDependencyGraph<T>
java.lang.Object
ghidra.util.graph.AbstractDependencyGraph<T>
- Type Parameters:
T
- the type of value. Some concrete classes might have restrictions on T.
- Direct Known Subclasses:
DependencyGraph
,DeterministicDependencyGraph
Class for managing the visiting (processing) of a set of values where some values depend
on other values being process before them. In other words, an acyclic directed graph will
be formed where the vertexes are the values and the edges represent dependencies. Values can
only be removed if they have no dependencies. Since the graph is acyclic, as values are removed
that have no dependencies, other nodes that depend on those nodes will become eligible for
processing and removal. If cycles are introduced, they will eventually cause an IllegalState
exception to occur when removing and processing values. There is also a hasCycles() method
that can be called before processing to find cycle problems up front without wasting time
processing values.
- See Also:
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addDependency
(T value1, T value2) Add a dependency such that value1 depends on value2.void
Adds the value to this graph.boolean
Returns true if this graph has the given key.abstract AbstractDependencyGraph
<T> copy()
Returns a copy of this graph.protected abstract Set
<AbstractDependencyGraph<T>.DependencyNode> Creates the Set ofAbstractDependencyGraph<T>.DependencyNode
s appropriate for the implementer.protected abstract Map
<T, AbstractDependencyGraph<T>.DependencyNode> Creates the Map of Nodes toAbstractDependencyGraph<T>.DependencyNode
s appropriate for the implementer.Creates the Set of Nodes appropriate for the implementer.Returns the set of all values that have no dependencies regardless of whether or not they have been "visited" (by the getUnvisitedIndependentValues() method.getDependentValues
(T value) Returns a set of values that depend on the given value.Returns the set of values in this graph.Returns a set of all values that have no dependencies.Returns the set of values in this graph.boolean
Checks if this graph has cycles.boolean
Returns true if there are unvisited values ready (no dependencies) for processing.boolean
isEmpty()
Returns true if the graph has no values;pop()
Removes and returns a value that has no dependencies from the graph.void
Removes the value from the graph.int
size()
Returns the number of values in this graph.
-
Field Details
-
nodeMap
-
unvisitedIndependentSet
-
-
Constructor Details
-
AbstractDependencyGraph
public AbstractDependencyGraph()
-
-
Method Details
-
getNodeMap
-
createNodeMap
Creates the Map of Nodes toAbstractDependencyGraph<T>.DependencyNode
s appropriate for the implementer.- Returns:
- a new Map of Nodes to
AbstractDependencyGraph<T>.DependencyNode
s.
-
createNodeSet
Creates the Set of Nodes appropriate for the implementer.- Returns:
- a new Set of Nodes.
-
createDependencyNodeSet
Creates the Set ofAbstractDependencyGraph<T>.DependencyNode
s appropriate for the implementer.- Returns:
- a new Set of
AbstractDependencyGraph<T>.DependencyNode
s.
-
copy
Returns a copy of this graph.- Returns:
- a copy of this graph.
-
addValue
Adds the value to this graph.- Parameters:
value
- the value to add
-
size
public int size()Returns the number of values in this graph.- Returns:
- the number of values in this graph.
-
isEmpty
public boolean isEmpty()Returns true if the graph has no values;- Returns:
- true if the graph has no values;
-
contains
Returns true if this graph has the given key.- Parameters:
value
- the value to check if its in this graph- Returns:
- true if this graph has the given key.
-
getValues
Returns the set of values in this graph.- Returns:
- the set of values in this graph.
-
getNodeMapValues
Returns the set of values in this graph.- Returns:
- the set of values in this graph.
-
addDependency
Add a dependency such that value1 depends on value2. Both value1 and value2 will be added to the graph if they are not already in the graph.- Parameters:
value1
- the value that depends on value2value2
- the value that value1 is depending on
-
hasUnVisitedIndependentValues
public boolean hasUnVisitedIndependentValues()Returns true if there are unvisited values ready (no dependencies) for processing.- Returns:
- true if there are unvisited values ready for processing.
- Throws:
IllegalStateException
- is thrown if the graph is not empty and there are no nodes without dependency which indicates there is a cycle in the graph.
-
pop
Removes and returns a value that has no dependencies from the graph. If the graph is empty or all the nodes without dependencies are currently visited, then null will be returned. NOTE: If the getUnvisitedIndependentValues() method has been called(), this method may return null until all those "visited" nodes are removed from the graph.- Returns:
- return an arbitrary value that has no dependencies and hasn't been visited or null.
-
hasCycles
public boolean hasCycles()Checks if this graph has cycles. Normal processing of this graph will eventually reveal a cycle and throw an exception at the time it is detected. This method allows for a "fail fast" way to detect cycles.- Returns:
- true if cycles exist in the graph.
-
getUnvisitedIndependentValues
Returns a set of all values that have no dependencies. As values are removed from the graph, dependencies will be removed and additional values will be eligible to be returned by this method. Once a value has been retrieved using this method, it will be considered "visited" and future calls to this method will not include those values. To continue processing the values in the graph, all values return from this method should eventually be deleted from the graph to "free up" other values. NOTE: values retrieved by this method will no longer be eligible for return by the pop() method.- Returns:
- the set of values without dependencies that have never been returned by this method before.
-
getAllIndependentValues
Returns the set of all values that have no dependencies regardless of whether or not they have been "visited" (by the getUnvisitedIndependentValues() method.- Returns:
- return the set of all values that have no dependencies.
-
remove
Removes the value from the graph. Any dependency from this node to another will be removed, possible allowing nodes that depend on this node to be eligible for processing.- Parameters:
value
- the value to remove from the graph.
-
getDependentValues
Returns a set of values that depend on the given value.- Parameters:
value
- the value that other values may depend on.- Returns:
- a set of values that depend on the given value.
-