java.lang.Object
org.jhotdraw8.graph.AbstractDirectedGraphBuilder
- All Implemented Interfaces:
IndexedDirectedGraph
- Direct Known Subclasses:
SimpleMutableDirectedGraph
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. arrowHeadsis 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
lastArrowscontains two fields:- A pointer to an entry in
arrowHeads. - The arrow count for this vertex.
- A pointer to an entry in
- Each entry in
arrowHeadscontains two fields:- A vertex.
A tombstone=-2 marks a deleted arrow head. - A pointer to the next entry in
arrowHeads.
A sentinel=-1 marks the end of a linked list.
- A vertex.
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 Summary
FieldsModifier and TypeFieldDescriptionprotected intThe number of used arrowHeads.protected static final intprotected static final intprotected static final intprotected static final intprotected static final intprotected static final intprotected static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected intbuildAddArrow(int a, int b) Builder-method: adds a directed arrow from 'a' to 'b'.protected voidBuilder-method: adds a vertex.protected voidbuildInsertVertexAt(int vidx) protected intbuildRemoveArrowAt(int a, int i) Removes the i-th arrow of vertex vi.protected intbuildRemoveArrowAt(int vidx, int i, int @NonNull [] lastArrow, int @NonNull [] arrowHeads, int arrowCount) Removes the i-th arrow of vertex v.protected voidbuildRemoveVertex(int vidx) protected voidvoidclear()protected intdoAddArrow(int a, int b, int @NonNull [] arrowHeads, int @NonNull [] lastArrow) Builder-method: adds a directed arrow from 'a' to 'b'.intReturns the number of arrows.protected intgetArrowIndex(int vi, int i, int @NonNull [] lastArrow, int @NonNull [] arrowHeads) protected intgetNextArrowIndex(int vi, int i) intgetNextAsInt(int v, int i) Returns thei-th next vertex ofv.intgetNextCount(int v) Returns the number of next vertices of v.getNextVerticesOrdered(int vidx) getNextVerticesUnordered(int vidx) intReturns the number of verticesV.booleannextVerticesEnumerator(int v) Returns the direct successor vertices of the specified vertex.voidsetOrdered(boolean ordered) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.jhotdraw8.graph.IndexedDirectedGraph
findIndexOfNextAsInt, getNextArrowAsInt, isNextAsInt
-
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 arrowCountIncludingDeletedArrowsThe 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 ab- vertex b- Returns:
- index of the arrow
-
doAddArrow
Builder-method: adds a directed arrow from 'a' to 'b'.- Parameters:
a- vertex ab- vertex barrowHeads- the array of arrow headslastArrow- the array of last arrows
-
buildAddVertex
protected void buildAddVertex()Builder-method: adds a vertex. -
getArrowCount
public int getArrowCount()Description copied from interface:IndexedDirectedGraphReturns the number of arrows.- Specified by:
getArrowCountin interfaceIndexedDirectedGraph- Returns:
- arrow count
-
buildRemoveArrowAt
protected int buildRemoveArrowAt(int a, int i) Removes the i-th arrow of vertex vi.- Parameters:
a- a vertexi- 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 vi- the i-th arrow of vertex vlastArrow- the array of last arrowsarrowHeads- the array of arrow headsarrowCount- 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
-
getNextAsInt
public int getNextAsInt(int v, int i) Description copied from interface:IndexedDirectedGraphReturns thei-th next vertex ofv.- Specified by:
getNextAsIntin interfaceIndexedDirectedGraph- Parameters:
v- a vertex indexi- 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:IndexedDirectedGraphReturns the number of next vertices of v.- Specified by:
getNextCountin interfaceIndexedDirectedGraph- Parameters:
v- a vertex- Returns:
- the number of next vertices of v.
-
getVertexCount
public int getVertexCount()Description copied from interface:IndexedDirectedGraphReturns the number of verticesV.- Specified by:
getVertexCountin interfaceIndexedDirectedGraph- Returns:
- vertex count
-
clear
public void clear() -
isOrdered
public boolean isOrdered() -
setOrdered
public void setOrdered(boolean ordered) -
nextVerticesEnumerator
Description copied from interface:IndexedDirectedGraphReturns the direct successor vertices of the specified vertex.- Specified by:
nextVerticesEnumeratorin interfaceIndexedDirectedGraph- Parameters:
v- a vertex index- Returns:
- a collection view on the direct successor vertices of vertex
-
getNextVerticesUnordered
-
getNextVerticesOrdered
-