Class NodeList<G extends CodeGraph<G,N,E>,N extends CodeNode<G,N,E>,E extends CodeEdge<G,N,E>>
- java.lang.Object
-
- it.unive.lisa.util.datastructures.graph.code.NodeList<G,N,E>
-
- Type Parameters:
G
- the type of theCodeGraph
s this list can be used inN
- the type of theCodeNode
s in this listE
- the type of theCodeEdge
s in this list
- All Implemented Interfaces:
java.lang.Iterable<N>
public class NodeList<G extends CodeGraph<G,N,E>,N extends CodeNode<G,N,E>,E extends CodeEdge<G,N,E>> extends java.lang.Object implements java.lang.Iterable<N>
A list of nodes of aCodeGraph
, together with the edges connecting them.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEdge(E e)
Adds an edge to this list.void
addNode(N node)
Adds the given node to the list of nodes.boolean
containsEdge(E edge)
Yieldstrue
if the given edge is contained in this list.boolean
containsNode(N node)
Yieldstrue
if the given node is contained in this list.int
distance(N from, N to)
Yields the minimum distance, in terms of number of edges to traverse, between the given nodes.boolean
equals(java.lang.Object obj)
java.util.Collection<N>
followersOf(N node)
Yields the collection of the nodes that are followers of the given one, that is, all nodes such that there exist an edge in this list going from the given node to such node.E
getEdgeConnecting(N source, N destination)
Yields the edge connecting the two given nodes, if any.java.util.Collection<E>
getEdges()
Yields the set of edges of this list.java.util.Collection<E>
getEdgesConnecting(N source, N destination)
Yields all edges connecting the two given nodes, if any.java.util.Collection<N>
getEntries()
Yields the entry nodes of this list, that is, the nodes that have no predecessors.java.util.Collection<N>
getExits()
Yields the exit nodes of this list, that is, the nodes that have no followers.java.util.Collection<E>
getIngoingEdges(N node)
Yields the ingoing edges to the given node.java.util.Collection<N>
getNodes()
Yields the collection of nodes of this list.java.util.Collection<E>
getOutgoingEdges(N node)
Yields the outgoing edges from the given node.int
hashCode()
java.util.Iterator<N>
iterator()
void
mergeWith(NodeList<G,N,E> other)
Merges this list with the given one, by adding all nodes and all edges contained inother
.java.util.Collection<N>
predecessorsOf(N node)
Yields the collection of the nodes that are predecessors of the given vertex, that is, all nodes such that there exist an edge in this list going from such node to the given one.void
removeEdge(E e)
Removes the given edge from the list.void
removeFrom(N root)
Removes every node and edge that is reachable from the givenroot
.void
removeNode(N node)
Removes the given node from the list, together with all its connected edges.void
simplify(java.lang.Iterable<N> targets, java.util.Collection<N> entrypoints, java.util.Collection<E> removedEdges, java.util.Map<org.apache.commons.lang3.tuple.Pair<E,E>,E> replacedEdges)
Simplifies this list, removing all the given nodes and rewriting the edge set accordingly.java.lang.String
toString()
void
validate(java.util.Collection<N> entrypoints)
Validates this list, ensuring that the it is well formed.
-
-
-
Constructor Detail
-
NodeList
public NodeList(E sequentialSingleton)
Builds a new list. Offsets of nodes added to this list will be set automatically.- Parameters:
sequentialSingleton
- an instance of an edge of this list that can be used to invokeCodeEdge.newInstance(CodeNode, CodeNode)
to obtain instances of sequential edges
-
NodeList
public NodeList(E sequentialSingleton, boolean computeOffsets)
Builds a new list.- Parameters:
sequentialSingleton
- an instance of an edge of this list that can be used to invokeCodeEdge.newInstance(CodeNode, CodeNode)
to obtain instances of sequential edgescomputeOffsets
- whether or not offsets should be set to nodes when added to this list
-
-
Method Detail
-
addNode
public void addNode(N node)
Adds the given node to the list of nodes. A cutoff point is introduced between this node and its predecessor in the list, meaning thatnode
will not be a follower of its predecessor in the list. Note that, if the given node is already present in the list, all existing edges are kept.
Also note that adding a node to a node list sets its offset.- Parameters:
node
- the node to add
-
removeNode
public void removeNode(N node)
Removes the given node from the list, together with all its connected edges. Note that to prevent its predecessor and its follower in the list to become sequentially connected, a cutoff will be introduced if not already present.- Parameters:
node
- the node to remove
-
getNodes
public final java.util.Collection<N> getNodes()
Yields the collection of nodes of this list.- Returns:
- the collection of nodes
-
addEdge
public void addEdge(E e)
Adds an edge to this list.- Parameters:
e
- the edge to add- Throws:
java.lang.UnsupportedOperationException
- if the source or the destination of the given edge are not part of this list
-
removeEdge
public void removeEdge(E e)
Removes the given edge from the list.- Parameters:
e
- the edge to remove
-
getEdgeConnecting
public final E getEdgeConnecting(N source, N destination)
Yields the edge connecting the two given nodes, if any. Yieldsnull
if such edge does not exist, or if one of the two node is not inside this list.- Parameters:
source
- the source nodedestination
- the destination node- Returns:
- the edge connecting
source
todestination
, ornull
-
getEdgesConnecting
public java.util.Collection<E> getEdgesConnecting(N source, N destination)
Yields all edges connecting the two given nodes, if any. Yields an empty collection if no edge exists, or if one of the two nodes is not inside this list.- Parameters:
source
- the source nodedestination
- the destination node- Returns:
- the edges connecting
source
todestination
-
getIngoingEdges
public final java.util.Collection<E> getIngoingEdges(N node)
Yields the ingoing edges to the given node.- Parameters:
node
- the node- Returns:
- the collection of ingoing edges
-
getOutgoingEdges
public final java.util.Collection<E> getOutgoingEdges(N node)
Yields the outgoing edges from the given node.- Parameters:
node
- the node- Returns:
- the collection of outgoing edges
-
getEdges
public final java.util.Collection<E> getEdges()
Yields the set of edges of this list.- Returns:
- the collection of edges
-
followersOf
public final java.util.Collection<N> followersOf(N node)
Yields the collection of the nodes that are followers of the given one, that is, all nodes such that there exist an edge in this list going from the given node to such node.- Parameters:
node
- the node- Returns:
- the collection of followers
- Throws:
java.lang.IllegalArgumentException
- if the node is not in the graph
-
predecessorsOf
public final java.util.Collection<N> predecessorsOf(N node)
Yields the collection of the nodes that are predecessors of the given vertex, that is, all nodes such that there exist an edge in this list going from such node to the given one.- Parameters:
node
- the node- Returns:
- the collection of predecessors
- Throws:
java.lang.IllegalArgumentException
- if the node is not in the graph
-
simplify
public void simplify(java.lang.Iterable<N> targets, java.util.Collection<N> entrypoints, java.util.Collection<E> removedEdges, java.util.Map<org.apache.commons.lang3.tuple.Pair<E,E>,E> replacedEdges)
Simplifies this list, removing all the given nodes and rewriting the edge set accordingly. This method will throw anUnsupportedOperationException
if one of the nodes being simplified has an outgoing edge that is not unconditional, according toCodeEdge.isUnconditional()
.- Parameters:
targets
- the set of theNode
s that needs to be simplifiedentrypoints
- the collection ofNode
s that are considered as entrypoints of the graph built over this adjacency listremovedEdges
- the collections of edges that got removed during the simplification, filled by this method (the collection will be cleared before simplifying)replacedEdges
- the map of edges that got replaced during the simplification, filled by this method (the map will be cleared before simplifying); each entry refers to a single simplified edge, and is in the form<<ingoing removed, outgoing removed>, added>
- Throws:
java.lang.UnsupportedOperationException
- if there exists at least one node being simplified with an outgoing non-simplifiable edge
-
containsNode
public boolean containsNode(N node)
Yieldstrue
if the given node is contained in this list.- Parameters:
node
- the node to check- Returns:
true
if the node is in this list
-
containsEdge
public boolean containsEdge(E edge)
Yieldstrue
if the given edge is contained in this list.- Parameters:
edge
- the edge to check- Returns:
true
if the edge is in this list
-
iterator
public java.util.Iterator<N> iterator()
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
removeFrom
public void removeFrom(N root)
Removes every node and edge that is reachable from the givenroot
.- Parameters:
root
- the node to use as root for the removal
-
getEntries
public java.util.Collection<N> getEntries()
Yields the entry nodes of this list, that is, the nodes that have no predecessors.- Returns:
- the entries nodes
-
getExits
public java.util.Collection<N> getExits()
Yields the exit nodes of this list, that is, the nodes that have no followers.- Returns:
- the exit nodes
-
distance
public int distance(N from, N to)
Yields the minimum distance, in terms of number of edges to traverse, between the given nodes. If one of the nodes is not inside this list, or if no path can be found, this method returns-1
. If the distance is greater thanInteger.MAX_VALUE
,Integer.MAX_VALUE
is returned.- Parameters:
from
- the starting nodeto
- the destination node- Returns:
- the minimum distance, in terms of number of edges to traverse, between the given nodes
-
mergeWith
public void mergeWith(NodeList<G,N,E> other)
Merges this list with the given one, by adding all nodes and all edges contained inother
.- Parameters:
other
- the list to merge into this one
-
validate
public void validate(java.util.Collection<N> entrypoints) throws ProgramValidationException
Validates this list, ensuring that the it is well formed. This method checks that:- Parameters:
entrypoints
- the collection ofNode
s that are considered as entrypoints of the graph built over this adjacency list- Throws:
ProgramValidationException
- if one of the aforementioned checks fail
-
-