Interface IndexRemoveListener


  • public interface IndexRemoveListener
    A listener that will be notified when an IndexGraph remove a vertex or an edge.

    The int identifiers an IndexGraph uses for its vertices are always 0,1,2,...,verticesNum-1 (and similarly for its edges). To maintain this invariant, when a vertex (or an edge) is removed from the graph, the graph implementation way need to swap the identifiers of vertices (edges). If the index of the vertex (edge) is the last index (verticesNum-1), the vertex can simply be removed. Otherwise, the vertex will be swapped with the last vertex and then removed. It's possible to register to these swaps and removes using IndexGraph.addVertexRemoveListener(IndexRemoveListener) and IndexGraph.addEdgeRemoveListener(IndexRemoveListener). The listener will be called on all vertex (edge) removal functions except IndexGraph.clear() and IndexGraph.clearEdges().

    The same swap listener interface is used for both vertices and edges (a specific instance is only used to one of them, which can be determined by the context), and we use a unified term element in the documentation to refer to either of them.

    Author:
    Barak Ugav
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void removeLast​(int removedIdx)
      A callback that is called when the last element is removed.
      void swapAndRemove​(int removedIdx, int swappedIdx)
      A callback that is called when removedIdx is swapped with swappedIdx and then removed.
    • Method Detail

      • removeLast

        void removeLast​(int removedIdx)
        A callback that is called when the last element is removed.

        When the last element (vertex or edge) is removed, no swap is needed, and the element is simply removed. The index of the last vertex can be accessed using g.vertices().size()-1 (and similarly for edges) but is passed as an argument for convenience.

        Parameters:
        removedIdx - the index of the removed element, which is the highest index in the graph
      • swapAndRemove

        void swapAndRemove​(int removedIdx,
                           int swappedIdx)
        A callback that is called when removedIdx is swapped with swappedIdx and then removed.

        When an element is removed from an index graph, the graph implementation may need to swap the element with the last element and then remove it. This is done to maintain the invariant that the element identifiers are always 0,1,2,...,verticesNum-1 (and similarly for edges).

        Parameters:
        removedIdx - the index of the removed element, before the swap
        swappedIdx - the index of the element that was swapped with removedIdx, which is the highest index in the graph