public override string ToString() { StringBuilder s = new StringBuilder(); foreach (object label in nodes.Labels) { s.Append(label).AppendLine(":"); foreach (NodeType node in nodes.WithLabel(label)) { foreach (NodeType target in node.Targets) { s.AppendLine(String.Format(" {0} -> {1}", node, target)); } HasSources <NodeType> intoNode = node as HasSources <NodeType>; if (intoNode != null) { foreach (NodeType source in intoNode.Sources) { s.AppendLine(String.Format(" {0} <- {1}", node, source)); } } } } return(s.ToString()); }
public virtual void ClearEdgesInto(NodeType target) { HasSources <NodeType> inNode = target as HasSources <NodeType>; if (inNode != null) { // doubly-linked case foreach (NodeType source in inNode.Sources) { source.Targets.Remove(target); } inNode.Sources.Clear(); } else { // singly-linked case // must search all nodes in the graph foreach (NodeType source in nodes) { while (source.Targets.Contains(target)) { source.Targets.Remove(target); } } } }
/// <summary> /// Remove a directed edge from source to target. /// </summary> /// <param name="source">The source node.</param> /// <param name="target">The target node.</param> /// <remarks>If there are multiple edges from source to target, only one is removed.</remarks> public virtual bool RemoveEdge(NodeType source, NodeType target) { HasSources <NodeType> intoNode = target as HasSources <NodeType>; if (intoNode != null) { intoNode.Sources.Remove(source); } return(source.Targets.Remove(target)); }
/// <summary> /// Add a directed edge from node to target. /// </summary> /// <param name="source">The source node.</param> /// <param name="target">The target node.</param> /// <remarks>The two nodes need not be in the graph, and will not be added to the graph.</remarks> public void AddEdge(NodeType source, NodeType target) { source.Targets.Add(target); HasSources <NodeType> intoNode = target as HasSources <NodeType>; if (intoNode != null) { intoNode.Sources.Add(source); } }
/// <summary> /// Remove all edges in the graph. /// </summary> /// <remarks>In the singly-linked case, there may still be links from outside the graph. /// </remarks> public virtual void ClearEdges() { foreach (NodeType node in nodes) { node.Targets.Clear(); HasSources <NodeType> inNode = node as HasSources <NodeType>; if (inNode != null) { inNode.Sources.Clear(); } } }
public virtual void ClearEdgesOutOf(NodeType source) { foreach (NodeType target in source.Targets) { HasSources <NodeType> intoNode = target as HasSources <NodeType>; if (intoNode != null) { intoNode.Sources.Remove(source); } } source.Targets.Clear(); }
public IEnumerable <NodeType> SourcesOf(NodeType target) { HasSources <NodeType> inNode = target as HasSources <NodeType>; if (inNode != null) { return(inNode.Sources); } else { // singly-linked case // must search all nodes in the graph return(new SourceEnumerator(this, target)); } }
public virtual int SourceCount(NodeType target) { HasSources <NodeType> inNode = target as HasSources <NodeType>; if (inNode != null) { return(inNode.Sources.Count); } else { // singly-linked case // must search all nodes in the graph int count = 0; foreach (NodeType source in nodes) { if (source.Targets.Contains(target)) { count++; } } return(count); } }