Class SingleArrayCsrGraphChunk

java.lang.Object
org.jhotdraw8.graph.SingleArrayCsrGraphChunk
All Implemented Interfaces:
GraphChunk

public class SingleArrayCsrGraphChunk extends Object implements GraphChunk
Stores a chunk of a graph using a "Compressed Sparse Row" representation.

Uses one shared gap between the list of sibling vertices/arrows. Moves the gap at each insertion/deletion operation. This may make it slow for updates.

The vertex data, the siblings list and arrows list are stored in a single array.

  • Constructor Details

    • SingleArrayCsrGraphChunk

      public SingleArrayCsrGraphChunk(int vertexCount, int initialArrowCapacity)
  • Method Details

    • indexOf

      public int indexOf(int v, int u)
      Finds the index of vertex u in the sibling list of vertex v.
      Specified by:
      indexOf in interface GraphChunk
      Parameters:
      v - index of vertex v
      u - index of vertex u
      Returns:
      the index of u or (-index -1) if u is not in the index list.
    • getSiblingsArray

      public int[] getSiblingsArray()
      Description copied from interface: GraphChunk
      Gets the siblings array. This is a single array for all siblings.

      Use GraphChunk.getSiblingsFromOffset(int) and GraphChunk.getSiblingCount(int) to access the siblings of a specific vertex.

      The content of this array and the offsets changes when arrows are added or removed!

      Specified by:
      getSiblingsArray in interface GraphChunk
      Returns:
      the siblings array
    • getSiblingCount

      public int getSiblingCount(int v)
      Description copied from interface: GraphChunk
      Gets the number of siblings of vertex v.
      Specified by:
      getSiblingCount in interface GraphChunk
      Parameters:
      v - the index of vertex v
      Returns:
      the number of siblings
    • getVertexData

      public int getVertexData(int v)
      Specified by:
      getVertexData in interface GraphChunk
    • setVertexData

      public void setVertexData(int v, int data)
      Specified by:
      setVertexData in interface GraphChunk
    • getArrow

      public int getArrow(int v, int k)
      Specified by:
      getArrow in interface GraphChunk
    • getSiblingsFromOffset

      public int getSiblingsFromOffset(int v)
      Description copied from interface: GraphChunk
      Gets the from-offset at which the siblings array contains indices of sibling vertices for the vertex v.
      Specified by:
      getSiblingsFromOffset in interface GraphChunk
      Parameters:
      v - the index of vertex v
      Returns:
      the from-offset in the siblings array
      See Also:
    • getSibling

      public int getSibling(int v, int k)
      Specified by:
      getSibling in interface GraphChunk
    • tryAddArrow

      public boolean tryAddArrow(int v, int u, int data, boolean updateIfPresent)
      Adds an arrow from vertex v to vertex u with the provided arrow data. Optionally updates the arrow data if the arrow is present.
      Specified by:
      tryAddArrow in interface GraphChunk
      Parameters:
      v - index of vertex v
      u - index of vertex u
      data - the arrow data
      updateIfPresent - sets the data if the arrow is present
      Returns:
      true if a new arrow was added
    • tryRemoveArrow

      public boolean tryRemoveArrow(int v, int u)
      Removes an arrow from vertex v to vertex u, if it is present.
      Specified by:
      tryRemoveArrow in interface GraphChunk
      Parameters:
      v - index of vertex v
      u - index of vertex u
      Returns:
      true on success
    • removeAllArrows

      public void removeAllArrows(int v)
      Removes all arrows starting at vertex v.
      Specified by:
      removeAllArrows in interface GraphChunk
      Parameters:
      v - index of vertex v
    • removeArrowAt

      public int removeArrowAt(int v, int removalIndex)
      Removes an arrow from vertex v to the a vertex u at the specified index.
      Specified by:
      removeArrowAt in interface GraphChunk
      Parameters:
      v - index of vertex v
      removalIndex - index of vertex u
      Returns:
      returns the removed arrow u