mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2026-06-04 06:34:32 +08:00
Merge remote-tracking branch
'origin/GP-1888_dragonmacher_PR-4079_HakanIlbas_graph_path_javadoc' (Closes #4079)
This commit is contained in:
@@ -20,9 +20,9 @@ import java.util.*;
|
|||||||
/**
|
/**
|
||||||
* Class for storing paths with fast "contains" method.
|
* Class for storing paths with fast "contains" method.
|
||||||
*
|
*
|
||||||
* Note: a path can only contain a vertex once.
|
* <p>Note: a path can only contain a vertex once.
|
||||||
*
|
*
|
||||||
* @param <V>
|
* @param <V> the vertex type.
|
||||||
*/
|
*/
|
||||||
public class GraphPath<V> {
|
public class GraphPath<V> {
|
||||||
|
|
||||||
@@ -30,14 +30,26 @@ public class GraphPath<V> {
|
|||||||
private Set<V> pathSet = new HashSet<>();
|
private Set<V> pathSet = new HashSet<>();
|
||||||
private List<V> pathList = new ArrayList<>();
|
private List<V> pathList = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor.
|
||||||
|
*/
|
||||||
public GraphPath() {
|
public GraphPath() {
|
||||||
// default constructor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with a vertex.
|
||||||
|
*
|
||||||
|
* @param v the first vertex of the newly initialized GraphPath object
|
||||||
|
*/
|
||||||
public GraphPath(V v) {
|
public GraphPath(V v) {
|
||||||
add(v);
|
add(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new GraphPath object by performing a shallow copy on another GraphPath object.
|
||||||
|
*
|
||||||
|
* @return the new shallow copy of the original GraphPath object
|
||||||
|
*/
|
||||||
public GraphPath<V> copy() {
|
public GraphPath<V> copy() {
|
||||||
GraphPath<V> newPath = new GraphPath<>();
|
GraphPath<V> newPath = new GraphPath<>();
|
||||||
newPath.pathList.addAll(pathList);
|
newPath.pathList.addAll(pathList);
|
||||||
@@ -45,6 +57,12 @@ public class GraphPath<V> {
|
|||||||
return newPath;
|
return newPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a GraphPath starts with another GraphPath.
|
||||||
|
*
|
||||||
|
* @param otherPath the other GraphPath we are checking
|
||||||
|
* @return true if the current GraphPath starts with otherPath, false otherwise
|
||||||
|
*/
|
||||||
public boolean startsWith(GraphPath<V> otherPath) {
|
public boolean startsWith(GraphPath<V> otherPath) {
|
||||||
if (size() < otherPath.size()) {
|
if (size() < otherPath.size()) {
|
||||||
return false;
|
return false;
|
||||||
@@ -58,6 +76,14 @@ public class GraphPath<V> {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all vertices that two GraphPaths have in common. For example if you have
|
||||||
|
* a-b-c-d-e-f and a-b-c-d-k-l-z, the common start path will be a-b-c-d. If there is no common
|
||||||
|
* start path, an empty GraphPath object is returned.
|
||||||
|
*
|
||||||
|
* @param other the other GraphPath to get the common start path of
|
||||||
|
* @return a new GraphPath object containing the common start path vertices
|
||||||
|
*/
|
||||||
public GraphPath<V> getCommonStartPath(GraphPath<V> other) {
|
public GraphPath<V> getCommonStartPath(GraphPath<V> other) {
|
||||||
int n = Math.min(size(), other.size());
|
int n = Math.min(size(), other.size());
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
@@ -68,31 +94,69 @@ public class GraphPath<V> {
|
|||||||
return subPath(0, n);
|
return subPath(0, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the size of the GraphPath.
|
||||||
|
*
|
||||||
|
* @return size of the GraphPath
|
||||||
|
*/
|
||||||
public int size() {
|
public int size() {
|
||||||
return pathList.size();
|
return pathList.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if vertex v is in the GraphPath.
|
||||||
|
*
|
||||||
|
* @param v the vertex
|
||||||
|
* @return true if vertex v is in this GraphPath
|
||||||
|
*/
|
||||||
public boolean contains(V v) {
|
public boolean contains(V v) {
|
||||||
return pathSet.contains(v);
|
return pathSet.contains(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a vertex to the GraphPath.
|
||||||
|
*
|
||||||
|
* @param v the new vertex
|
||||||
|
*/
|
||||||
public void add(V v) {
|
public void add(V v) {
|
||||||
pathSet.add(v);
|
pathSet.add(v);
|
||||||
pathList.add(v);
|
pathList.add(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get last vertex of GraphPath.
|
||||||
|
*
|
||||||
|
* @return last vertex of GraphPath
|
||||||
|
*/
|
||||||
public V getLast() {
|
public V getLast() {
|
||||||
return pathList.get(pathList.size() - 1);
|
return pathList.get(pathList.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the depth of the vertex that is specified by the parameter.
|
||||||
|
*
|
||||||
|
* @param v the vertex for which we get the depth
|
||||||
|
* @return the depth of the vertex
|
||||||
|
*/
|
||||||
public int depth(V v) {
|
public int depth(V v) {
|
||||||
return pathList.indexOf(v);
|
return pathList.indexOf(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get vertex that is specified by the parameter.
|
||||||
|
*
|
||||||
|
* @param depth of the vertex to retrieve
|
||||||
|
* @return the vertex
|
||||||
|
*/
|
||||||
public V get(int depth) {
|
public V get(int depth) {
|
||||||
return pathList.get(depth);
|
return pathList.get(depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the last vertex of the GraphPath.
|
||||||
|
*
|
||||||
|
* @return the removed vertex
|
||||||
|
*/
|
||||||
public V removeLast() {
|
public V removeLast() {
|
||||||
V v = pathList.remove(pathList.size() - 1);
|
V v = pathList.remove(pathList.size() - 1);
|
||||||
pathSet.remove(v);
|
pathSet.remove(v);
|
||||||
@@ -100,11 +164,10 @@ public class GraphPath<V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all entries that are before the given vertex in this path. The results will
|
* Return a set with all of the predecessors of the vertex in the GraphPath.
|
||||||
* include the vertex.
|
|
||||||
*
|
*
|
||||||
* @param v the vertex
|
* @param v the vertex we want to get the predecessors of
|
||||||
* @return the predecessors
|
* @return the predecessors of the vertex as a set, return empty set if there are none
|
||||||
*/
|
*/
|
||||||
public Set<V> getPredecessors(V v) {
|
public Set<V> getPredecessors(V v) {
|
||||||
Set<V> set = new HashSet<>();
|
Set<V> set = new HashSet<>();
|
||||||
@@ -118,11 +181,10 @@ public class GraphPath<V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all entries that are later in this path than the given vertex. The results will
|
* Return a set with all of the successors of the vertex in the GraphPath.
|
||||||
* include the vertex.
|
|
||||||
*
|
*
|
||||||
* @param v the vertex
|
* @param v the vertex we want to get the successors of
|
||||||
* @return the successors
|
* @return the successors of the vertex as a set, return empty set if there are none
|
||||||
*/
|
*/
|
||||||
public Set<V> getSuccessors(V v) {
|
public Set<V> getSuccessors(V v) {
|
||||||
Set<V> set = new HashSet<>();
|
Set<V> set = new HashSet<>();
|
||||||
@@ -141,6 +203,13 @@ public class GraphPath<V> {
|
|||||||
return pathList.toString();
|
return pathList.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a part of the whole GraphPath, similar to substring with strings.
|
||||||
|
*
|
||||||
|
* @param start the start of the sub-path of the GraphPath
|
||||||
|
* @param end the end of the sub-path of the GraphPath
|
||||||
|
* @return a new GraphPath which is a sub-path of the original GraphPath from start to end
|
||||||
|
*/
|
||||||
public GraphPath<V> subPath(int start, int end) {
|
public GraphPath<V> subPath(int start, int end) {
|
||||||
GraphPath<V> subPath = new GraphPath<>();
|
GraphPath<V> subPath = new GraphPath<>();
|
||||||
subPath.pathList = new ArrayList<>(pathList.subList(start, end));
|
subPath.pathList = new ArrayList<>(pathList.subList(start, end));
|
||||||
|
|||||||
Reference in New Issue
Block a user