Class AbstractDirectedGraphBuilder

java.lang.Object
org.jhotdraw8.graph.AbstractDirectedGraphBuilder
All Implemented Interfaces:
IndexedDirectedGraph
Direct Known Subclasses:
SimpleMutableDirectedGraph

public abstract class AbstractDirectedGraphBuilder extends Object implements IndexedDirectedGraph
AbstractDirectedGraphBuilder.

Implementation:

Example graph:

     0 ──→ 1 ──→ 2
     │     │
     ↓     ↓
     3 ←── 4
 
If the graph is inserted in the following sequence into the builder:
     buildAddVertex();
     buildAddVertex();
     buildAddVertex();
     buildAddVertex();
     buildAddVertex();
     buildAddVertex();
     build.addArrow(0, 1);
     build.addArrow(0, 3);
     build.addArrow(1, 2);
     build.addArrow(1, 4);
     build.addArrow(4, 3);
 
Then the internal representation is as follows:
  • For each vertex, there is an entry in table lastArrows.
  • For each arrow, there is an entry in table arrowHeads.
  • arrowHeads is a linked list. The linked list is ordered from the last arrow to the first. So we have to read it backwards!
  • Each entry in lastArrows contains two fields:
    1. A pointer to an entry in arrowHeads.
    2. The arrow count for this vertex.
  • Each entry in arrowHeads contains two fields:
    1. A vertex.
      A tombstone=-2 marks a deleted arrow head.
    2. A pointer to the next entry in arrowHeads.
      A sentinel=-1 marks the end of a linked list.
 vertexCount: 5
 arrowCountInclusiveDeleted: 5
 deletedArrowCount: 0
 lastDeletedArrow: SENT

  vertex#    lastArrow             arrow#    arrowHeads
           pointer,count                    vertex, next
    0     [  1  ][  2  ] ─────┐       0    [  1  ][SENT ] ←┐
    1     [  2  ][  2  ] ───┐ └─────→ 1    [  3  ][  0  ] ─┘
    2     [  0  ][  0  ] X  │         2    [  2  ][SENT ] ←┐
    3     [  0  ][  0  ] X  └───────→ 3    [  4  ][  2  ] ─┘
    4     [  4  ][  1  ] ───────────→ 4    [  3  ][SENT ] X
 
If the arrow 1 → 3 is deleted, it is removed from the linked list of vertex 1. The arrow head is marked with a tombstone.
 vertexCount: 5
 arrowCountInclusiveDeleted: 5
 deletedArrowCount: 1
 lastDeletedArrow: 1

  vertex#    lastArrow             arrow#    arrowHeads
           pointer,count                    vertex, next
    0     [  1  ][  2  ] ───────────→ 0    [  1  ][SENT ]
    1     [  2  ][  2  ] ───┐         1    [TOMB ][SENT ]
    2     [  0  ][  0  ] X  │         2    [  2  ][SENT ] ←┐
    3     [  0  ][  0  ] X  └───────→ 3    [  4  ][  2 ] ─┘
    4     [  4  ][  1  ] ───────────→ 4    [  3  ][SENT ] X
 
Author:
Werner Randelshofer
  • Field Details

    • ARROWS_NEXT_FIELD

      protected static final int ARROWS_NEXT_FIELD
      See Also:
    • ARROWS_NUM_FIELDS

      protected static final int ARROWS_NUM_FIELDS
      See Also:
    • ARROWS_VERTEX_FIELD

      protected static final int ARROWS_VERTEX_FIELD
      See Also:
    • LASTARROW_COUNT_FIELD

      protected static final int LASTARROW_COUNT_FIELD
      See Also:
    • LASTARROW_NUM_FIELDS

      protected static final int LASTARROW_NUM_FIELDS
      See Also:
    • LASTARROW_POINTER_FIELD

      protected static final int LASTARROW_POINTER_FIELD
      See Also:
    • SENTINEL

      protected static final int SENTINEL
      See Also:
    • arrowCountIncludingDeletedArrows

      protected int arrowCountIncludingDeletedArrows
      The number of used arrowHeads.
  • Constructor Details

    • AbstractDirectedGraphBuilder

      public AbstractDirectedGraphBuilder()
    • AbstractDirectedGraphBuilder

      public AbstractDirectedGraphBuilder(int vertexCapacity, int arrowCapacity)
  • Method Details

    • buildAddArrow

      protected int buildAddArrow(int a, int b)
      Builder-method: adds a directed arrow from 'a' to 'b'.
      Parameters:
      a - vertex a
      b - vertex b
      Returns:
      index of the arrow
    • doAddArrow

      protected int doAddArrow(int a, int b, int @NonNull [] arrowHeads, int @NonNull [] lastArrow)
      Builder-method: adds a directed arrow from 'a' to 'b'.
      Parameters:
      a - vertex a
      b - vertex b
      arrowHeads - the array of arrow heads
      lastArrow - the array of last arrows
    • buildAddVertex

      protected void buildAddVertex()
      Builder-method: adds a vertex.
    • getArrowCount

      public int getArrowCount()
      Description copied from interface: IndexedDirectedGraph
      Returns the number of arrows.
      Specified by:
      getArrowCount in interface IndexedDirectedGraph
      Returns:
      arrow count
    • buildRemoveArrowAt

      protected int buildRemoveArrowAt(int a, int i)
      Removes the i-th arrow of vertex vi.
      Parameters:
      a - a vertex
      i - the i-th arrow of vertex vi
    • buildRemoveArrowAt

      protected int buildRemoveArrowAt(int vidx, int i, int @NonNull [] lastArrow, int @NonNull [] arrowHeads, int arrowCount)
      Removes the i-th arrow of vertex v.
      Parameters:
      vidx - the index of the vertex v
      i - the i-th arrow of vertex v
      lastArrow - the array of last arrows
      arrowHeads - the array of arrow heads
      arrowCount - the number of arrows
    • buildRemoveVertex

      protected void buildRemoveVertex(int vidx)
    • buildRemoveVertexAfterArrowsHaveBeenRemoved

      protected void buildRemoveVertexAfterArrowsHaveBeenRemoved(int vidx)
    • buildInsertVertexAt

      protected void buildInsertVertexAt(int vidx)
    • getNextArrowIndex

      protected int getNextArrowIndex(int vi, int i)
    • getArrowIndex

      protected int getArrowIndex(int vi, int i, int @NonNull [] lastArrow, int @NonNull [] arrowHeads)
    • getNextAsInt

      public int getNextAsInt(int v, int i)
      Description copied from interface: IndexedDirectedGraph
      Returns the i-th next vertex of v.
      Specified by:
      getNextAsInt in interface IndexedDirectedGraph
      Parameters:
      v - a vertex index
      i - the index of the desired next vertex, i ∈ {0, ..., getNextCount(v) -1 }.
      Returns:
      the vertex index of the i-th next vertex of v.
    • getNextCount

      public int getNextCount(int v)
      Description copied from interface: IndexedDirectedGraph
      Returns the number of next vertices of v.
      Specified by:
      getNextCount in interface IndexedDirectedGraph
      Parameters:
      v - a vertex
      Returns:
      the number of next vertices of v.
    • getVertexCount

      public int getVertexCount()
      Description copied from interface: IndexedDirectedGraph
      Returns the number of vertices V.
      Specified by:
      getVertexCount in interface IndexedDirectedGraph
      Returns:
      vertex count
    • clear

      public void clear()
    • isOrdered

      public boolean isOrdered()
    • setOrdered

      public void setOrdered(boolean ordered)
    • nextVerticesEnumerator

      public @NonNull Enumerator.OfInt nextVerticesEnumerator(int v)
      Description copied from interface: IndexedDirectedGraph
      Returns the direct successor vertices of the specified vertex.
      Specified by:
      nextVerticesEnumerator in interface IndexedDirectedGraph
      Parameters:
      v - a vertex index
      Returns:
      a collection view on the direct successor vertices of vertex
    • getNextVerticesUnordered

      public @NonNull Enumerator.OfInt getNextVerticesUnordered(int vidx)
    • getNextVerticesOrdered

      public @NonNull Enumerator.OfInt getNextVerticesOrdered(int vidx)