net.walend.digraph
Class AbstractHashUEDigraph

java.lang.Object
  |
  +--net.walend.digraph.AbstractHashUEDigraph
All Implemented Interfaces:
HasState, UEDigraph
Direct Known Subclasses:
HashUEDigraph, MutableHashUEDigraph

public abstract class AbstractHashUEDigraph
extends java.lang.Object
implements UEDigraph

This abstract class implements the UEDigraph interface using three HashMaps. It's great for sparse graphs. Subclass it as is for an immutable UEDigraph. Mix in the MutableUEDigraph interface for one you can change.

Since:
20010613
Author:
David Walend

Nested Class Summary
protected  class AbstractHashUEDigraph.HashEdgeIterator
           
protected  class AbstractHashUEDigraph.IteratorWrapper
           
protected  class AbstractHashUEDigraph.NodePair
           
 
Field Summary
private  MutableMap edgesToFromNodes
           
private  MutableMap edgesToToNodes
           
private  MutableMap nodePairsToEdges
           
private  MutableSet nodes
           
 
Fields inherited from interface net.walend.digraph.UEDigraph
EMPTY
 
Constructor Summary
protected AbstractHashUEDigraph(int nodeCapacity, int edgeCapacity)
           
protected AbstractHashUEDigraph(UEDigraph digraph)
           
 
Method Summary
protected  java.lang.Object addEdge(java.lang.Object fromNode, java.lang.Object toNode, java.lang.Object edge)
           
protected  boolean addNode(java.lang.Object node)
           
protected  boolean addNodes(Set nodesToAdd)
           
protected  void clear()
           
protected  void clearEdges()
           
 boolean containsEdge(java.lang.Object edge)
          Returns true if edge exists in the digraph anywhere
 boolean containsEdge(java.lang.Object fromNode, java.lang.Object toNode)
          Returns true if the digraph contains any edge from fromNode to toNode
 boolean containsEdge(java.lang.Object fromNode, java.lang.Object toNode, java.lang.Object edge)
          Returns true if edge links fromNode to toNode
 boolean containsEdges(Set edges)
           
 boolean containsNode(java.lang.Object node)
           
 boolean containsNodes(Set nodes)
           
 boolean containsUEDigraph(UEDigraph digraph)
          Returns true if digraph is a subgraph of this UEDigraph.
 int countInboundEdges(java.lang.Object node)
          Inefficient.
 int countOutboundEdges(java.lang.Object node)
          Inefficient.
 int edgeCount()
           
 EdgeIterator edgeIterator()
          Since HashUEDigraph is immutable, edgeIterator()'s remove() method throws an UnsupportedOperationException.
 java.lang.Object getEdge(java.lang.Object fromNode, java.lang.Object toNode)
          Returns null if either node is not in the digraph or if no edge links fromNode to toNode
 Set getEdges()
           
 java.lang.Object getFromNode(java.lang.Object edge)
          Returns null if edge is not in the digraph.
 Set getFromNodes(java.lang.Object node)
          Returns the set of nodes that can reach this node by crossing one edge.
 Set getInboundEdges(java.lang.Object node)
          Returns the empty set if node is not in the digraph, or if node has no inbound edges.
 Set getNodes()
           
 Set getOutboundEdges(java.lang.Object node)
          Returns the empty set if node is not in the digraph, or if node has no outbound edges.
 java.lang.Class getPrincipleInterface()
          Returns the class's principle interface for state comparisons.
 java.lang.Object getToNode(java.lang.Object edge)
          Returns null if edge is not in the digraph
 Set getToNodes(java.lang.Object node)
          Returns the set of nodes that can be reached from this node by crossing one edge.
 UEDigraph intersectWithUEDigraph(UEDigraph digraph)
          Returns a new digraph that is the intersection of this with digraph.
 boolean isEdgeFree()
          Returns true if this UEDigraph has no edges.
 boolean isEmpty()
           
 int nodeCount()
           
 java.util.Iterator nodeIterator()
          Since HashUEDigraph is immutable, nodeIterator()'s remove() method throws an UnsupportedOperationException.
protected  boolean removeEdge(java.lang.Object edge)
           
protected  java.lang.Object removeEdge(java.lang.Object fromNode, java.lang.Object toNode)
           
protected  boolean removeEdges(Set edges)
           
protected  Set removeNode(java.lang.Object node)
           
protected  Set removeNodes(Set nodesToRemove)
           
protected  Set removeUEDigraph(UEDigraph digraph)
           
protected  boolean retainEdges(Set retainedEdges)
           
protected  Set retainNodes(Set retainedNodes)
           
 boolean sameStateAs(HasState victem)
          If two HasStates have the same internal state, return true.
 boolean sameUEDigraphAs(UEDigraph digraph)
          Returns true if digraph is the same as this, and all their contents have the same state.
 java.lang.String toString()
           
 UEDigraph unionUEDigraph(UEDigraph digraph)
          Returns a new digraph that is the union of this with digraph.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

nodes

private MutableSet nodes

nodePairsToEdges

private MutableMap nodePairsToEdges

edgesToToNodes

private MutableMap edgesToToNodes

edgesToFromNodes

private MutableMap edgesToFromNodes
Constructor Detail

AbstractHashUEDigraph

protected AbstractHashUEDigraph(int nodeCapacity,
                                int edgeCapacity)

AbstractHashUEDigraph

protected AbstractHashUEDigraph(UEDigraph digraph)
Method Detail

nodeCount

public int nodeCount()
Specified by:
nodeCount in interface UEDigraph

edgeCount

public int edgeCount()
Specified by:
edgeCount in interface UEDigraph

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface UEDigraph

containsNode

public boolean containsNode(java.lang.Object node)
Specified by:
containsNode in interface UEDigraph

containsEdge

public boolean containsEdge(java.lang.Object fromNode,
                            java.lang.Object toNode)
                     throws NodeMissingException
Description copied from interface: UEDigraph
Returns true if the digraph contains any edge from fromNode to toNode

Specified by:
containsEdge in interface UEDigraph

containsEdge

public boolean containsEdge(java.lang.Object fromNode,
                            java.lang.Object toNode,
                            java.lang.Object edge)
                     throws NodeMissingException
Returns true if edge links fromNode to toNode

Specified by:
containsEdge in interface UEDigraph

containsEdge

public boolean containsEdge(java.lang.Object edge)
Returns true if edge exists in the digraph anywhere

Specified by:
containsEdge in interface UEDigraph

countInboundEdges

public int countInboundEdges(java.lang.Object node)
                      throws NodeMissingException
Inefficient. Iterates through all the edges' toNodes. This method could iterate over just the nodes, but would involve creating lots of NodePairs as trial keys. Since the digraph should be sparse anyway, iterating through the edges should be faster.

Specified by:
countInboundEdges in interface UEDigraph

countOutboundEdges

public int countOutboundEdges(java.lang.Object node)
                       throws NodeMissingException
Inefficient. Iterates through all the edges' fromNodes.

Specified by:
countOutboundEdges in interface UEDigraph

getInboundEdges

public Set getInboundEdges(java.lang.Object node)
                    throws NodeMissingException
Returns the empty set if node is not in the digraph, or if node has no inbound edges.

Inefficient. Iterates through all the edges.

Specified by:
getInboundEdges in interface UEDigraph

getOutboundEdges

public Set getOutboundEdges(java.lang.Object node)
                     throws NodeMissingException
Returns the empty set if node is not in the digraph, or if node has no outbound edges.

Inefficient. Iterates through all the edges.

Specified by:
getOutboundEdges in interface UEDigraph

getFromNode

public java.lang.Object getFromNode(java.lang.Object edge)
                             throws EdgeMissingException
Returns null if edge is not in the digraph.

Specified by:
getFromNode in interface UEDigraph

getToNode

public java.lang.Object getToNode(java.lang.Object edge)
                           throws EdgeMissingException
Returns null if edge is not in the digraph

Specified by:
getToNode in interface UEDigraph

getEdge

public java.lang.Object getEdge(java.lang.Object fromNode,
                                java.lang.Object toNode)
                         throws NodeMissingException
Returns null if either node is not in the digraph or if no edge links fromNode to toNode

Specified by:
getEdge in interface UEDigraph

getFromNodes

public Set getFromNodes(java.lang.Object node)
                 throws NodeMissingException
Returns the set of nodes that can reach this node by crossing one edge. Returns the empty set if node is not in the digraph.

Inefficient. Iterates through all the edges.

Specified by:
getFromNodes in interface UEDigraph

getToNodes

public Set getToNodes(java.lang.Object node)
               throws NodeMissingException
Returns the set of nodes that can be reached from this node by crossing one edge. Returns the empty set if node is not in the digraph.

Inefficient. Iterates through all the edges.

Specified by:
getToNodes in interface UEDigraph

nodeIterator

public java.util.Iterator nodeIterator()
Since HashUEDigraph is immutable, nodeIterator()'s remove() method throws an UnsupportedOperationException.

Specified by:
nodeIterator in interface UEDigraph

edgeIterator

public EdgeIterator edgeIterator()
Since HashUEDigraph is immutable, edgeIterator()'s remove() method throws an UnsupportedOperationException.

Specified by:
edgeIterator in interface UEDigraph

getNodes

public Set getNodes()
Specified by:
getNodes in interface UEDigraph

getEdges

public Set getEdges()
Specified by:
getEdges in interface UEDigraph

isEdgeFree

public boolean isEdgeFree()
Returns true if this UEDigraph has no edges.

Specified by:
isEdgeFree in interface UEDigraph

containsNodes

public boolean containsNodes(Set nodes)
Specified by:
containsNodes in interface UEDigraph

containsEdges

public boolean containsEdges(Set edges)
Specified by:
containsEdges in interface UEDigraph

containsUEDigraph

public boolean containsUEDigraph(UEDigraph digraph)
Returns true if digraph is a subgraph of this UEDigraph.

Specified by:
containsUEDigraph in interface UEDigraph

sameUEDigraphAs

public boolean sameUEDigraphAs(UEDigraph digraph)
Returns true if digraph is the same as this, and all their contents have the same state.

Specified by:
sameUEDigraphAs in interface UEDigraph

intersectWithUEDigraph

public UEDigraph intersectWithUEDigraph(UEDigraph digraph)
Returns a new digraph that is the intersection of this with digraph. Implementations should generally return the same implementation of UEDigraph as they have themselves.

Specified by:
intersectWithUEDigraph in interface UEDigraph

unionUEDigraph

public UEDigraph unionUEDigraph(UEDigraph digraph)
Returns a new digraph that is the union of this with digraph. Implementations should generally return the same implementation of UEDigraph as they are themselves. If the digraphs contain conflicting edges, then (unless you have a better rule) let digraph's edge win out.

Specified by:
unionUEDigraph in interface UEDigraph

getPrincipleInterface

public java.lang.Class getPrincipleInterface()
Description copied from interface: HasState
Returns the class's principle interface for state comparisons. If two objects have different principle interfaces, they never have the same state.

Specified by:
getPrincipleInterface in interface HasState

sameStateAs

public boolean sameStateAs(HasState victem)
Description copied from interface: HasState
If two HasStates have the same internal state, return true.

For objects with subobjects, Generally this method should only return true if the internal objects are equal. Implement a contentsHaveSameState() method to determine if the contents have the same state.

Specified by:
sameStateAs in interface HasState

addNode

protected boolean addNode(java.lang.Object node)

addEdge

protected java.lang.Object addEdge(java.lang.Object fromNode,
                                   java.lang.Object toNode,
                                   java.lang.Object edge)
                            throws NodeMissingException,
                                   EdgeNotUniqueException

removeNode

protected Set removeNode(java.lang.Object node)
                  throws NodeMissingException

removeEdge

protected boolean removeEdge(java.lang.Object edge)

removeEdge

protected java.lang.Object removeEdge(java.lang.Object fromNode,
                                      java.lang.Object toNode)
                               throws NodeMissingException

addNodes

protected boolean addNodes(Set nodesToAdd)

removeNodes

protected Set removeNodes(Set nodesToRemove)

removeEdges

protected boolean removeEdges(Set edges)

removeUEDigraph

protected Set removeUEDigraph(UEDigraph digraph)

retainNodes

protected Set retainNodes(Set retainedNodes)

retainEdges

protected boolean retainEdges(Set retainedEdges)

clear

protected void clear()

clearEdges

protected void clearEdges()

toString

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


Copyright (c) 2000, 2001, David Walend