Class NodeList<G extends CodeGraph<G,​N,​E>,​N extends CodeNode<G,​N,​E>,​E extends CodeEdge<G,​N,​E>>

  • Type Parameters:
    G - the type of the CodeGraphs this list can be used in
    N - the type of the CodeNodes in this list
    E - the type of the CodeEdges 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 a CodeGraph, together with the edges connecting them.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  NodeList.NodeEdges<G extends CodeGraph<G,​N,​E>,​N extends CodeNode<G,​N,​E>,​E extends CodeEdge<G,​N,​E>>
      Utility class for representing the edges tied to a node, split into two sets: ingoing and outgoing.
    • Constructor Summary

      Constructors 
      Constructor Description
      NodeList​(E sequentialSingleton)
      Builds a new list.
      NodeList​(E sequentialSingleton, boolean computeOffsets)
      Builds a new list.
      NodeList​(NodeList<G,​N,​E> other)
      Copies the given list.
    • 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)
      Yields true if the given edge is contained in this list.
      boolean containsNode​(N node)
      Yields true 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 in other.
      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 given root.
      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.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • 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 invoke CodeEdge.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 invoke CodeEdge.newInstance(CodeNode, CodeNode) to obtain instances of sequential edges
        computeOffsets - whether or not offsets should be set to nodes when added to this list
      • NodeList

        public NodeList​(NodeList<G,​N,​E> other)
        Copies the given list.
        Parameters:
        other - the list to copy
    • 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 that node 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. Yields null if such edge does not exist, or if one of the two node is not inside this list.
        Parameters:
        source - the source node
        destination - the destination node
        Returns:
        the edge connecting source to destination, or null
      • 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 node
        destination - the destination node
        Returns:
        the edges connecting source to destination
      • 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 an UnsupportedOperationException if one of the nodes being simplified has an outgoing edge that is not unconditional, according to CodeEdge.isUnconditional().
        Parameters:
        targets - the set of the Nodes that needs to be simplified
        entrypoints - the collection of Nodes that are considered as entrypoints of the graph built over this adjacency list
        removedEdges - 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)
        Yields true 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)
        Yields true 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()
        Specified by:
        iterator in interface java.lang.Iterable<G extends CodeGraph<G,​N,​E>>
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • removeFrom

        public void removeFrom​(N root)
        Removes every node and edge that is reachable from the given root.
        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 than Integer.MAX_VALUE, Integer.MAX_VALUE is returned.
        Parameters:
        from - the starting node
        to - 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 in other.
        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:
        • each Edge is connected to Nodes contained in the list
        • all Nodes with no ingoing edges are marked as entrypoints of the cfg (i.e. no deadcode)
        Parameters:
        entrypoints - the collection of Nodes that are considered as entrypoints of the graph built over this adjacency list
        Throws:
        ProgramValidationException - if one of the aforementioned checks fail