public bool TryGetEdges( TVertex source, TVertex target, out IEnumerable <TEdge> edges) { GraphContracts.AssumeInVertexSet(this, source, "source"); GraphContracts.AssumeInVertexSet(this, target, "target"); EdgeList edgeList; if (this.vertexOutEdges.TryGetValue(source, out edgeList)) { List <TEdge> list = new List <TEdge>(edgeList.Count); foreach (TEdge edge in edgeList) { if (edge.Target.Equals(target)) { list.Add(edge); } } edges = list; return(true); } else { edges = null; return(false); } }
public void ClearEdges(TVertex v) { GraphContracts.AssumeInVertexSet(this, v, "v"); ClearOutEdges(v); ClearInEdges(v); }
public bool TryGetEdge( TVertex source, TVertex target, out TEdge edge) { GraphContracts.AssumeInVertexSet(this, source, "source"); GraphContracts.AssumeInVertexSet(this, target, "target"); EdgeList edgeList; if (this.vertexOutEdges.TryGetValue(source, out edgeList) && edgeList.Count > 0) { foreach (TEdge e in edgeList) { if (e.Target.Equals(target)) { edge = e; return(true); } } } edge = default(TEdge); return(false); }
public int RemoveOutEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate) { GraphContracts.AssumeInVertexSet(this, v, "v"); GraphContracts.AssumeNotNull(predicate, "predicate"); EdgeList edges = this.vertexEdges[v]; EdgeList edgeToRemove = new EdgeList(edges.Count); foreach (TEdge edge in edges) { if (predicate(edge)) { edgeToRemove.Add(edge); } } foreach (TEdge edge in edgeToRemove) { edges.Remove(edge); this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge)); } this.edgeCount -= edgeToRemove.Count; GraphContracts.Assert(this.edgeCount >= 0); return(edgeToRemove.Count); }
public void MergeVertex(TVertex v, IEdgeFactory <TVertex, TEdge> edgeFactory) { GraphContracts.AssumeInVertexSet(this, v, "v"); GraphContracts.AssumeNotNull(edgeFactory, "edgeFactory"); // storing edges in local array EdgeList inedges = this.vertexInEdges[v]; EdgeList outedges = this.vertexOutEdges[v]; // remove vertex this.RemoveVertex(v); // add edges from each source to each target foreach (TEdge source in inedges) { //is it a self edge if (source.Source.Equals(v)) { continue; } foreach (TEdge target in outedges) { if (v.Equals(target.Target)) { continue; } // we add an new edge this.AddEdge(edgeFactory.CreateEdge(source.Source, target.Target)); } } }
public EdgeEdgeEventArgs(TEdge edge, TEdge targetEdge) { GraphContracts.AssumeNotNull(edge, "edge"); GraphContracts.AssumeNotNull(targetEdge, "targetEdge"); this.edge = edge; this.targetEdge = targetEdge; }
public virtual void AddVertexRange(IEnumerable <TVertex> vertices) { GraphContracts.AssumeNotNull(vertices, "vertices"); foreach (TVertex v in vertices) { this.AddVertex(v); } }
public Edge(TVertex source, TVertex target) { GraphContracts.AssumeNotNull(source, "source"); GraphContracts.AssumeNotNull(target, "target"); this.source = source; this.target = target; }
public virtual bool RemoveVertex(TVertex v) { GraphContracts.AssumeNotNull(v, "v"); if (!this.ContainsVertex(v)) { return(false); } // remove outedges { EdgeList edges = this.vertexEdges[v]; if (this.EdgeRemoved != null) // lazily notify { foreach (TEdge edge in edges) { this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge)); } } this.edgeCount -= edges.Count; edges.Clear(); } // iterage over edges and remove each edge touching the vertex EdgeList edgeToRemove = new EdgeList(); foreach (KeyValuePair <TVertex, EdgeList> kv in this.vertexEdges) { if (kv.Key.Equals(v)) { continue; // we've already } // collect edge to remove foreach (TEdge edge in kv.Value) { if (edge.Target.Equals(v)) { edgeToRemove.Add(edge); } } // remove edges foreach (TEdge edge in edgeToRemove) { kv.Value.Remove(edge); this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge)); } // update count this.edgeCount -= edgeToRemove.Count; edgeToRemove.Clear(); } System.Diagnostics.Debug.Assert(this.edgeCount >= 0); this.vertexEdges.Remove(v); this.OnVertexRemoved(new VertexEventArgs <TVertex>(v)); return(true); }
public bool ContainsEdge(TEdge edge) { GraphContracts.AssumeInVertexSet(this, edge, "edge"); foreach (TEdge e in this.Edges) { if (e.Equals(edge)) { return(true); } } return(false); }
public bool ContainsEdge(TVertex source, TVertex target) { GraphContracts.AssumeInVertexSet(this, source, "source"); GraphContracts.AssumeInVertexSet(this, target, "target"); foreach (TEdge outEdge in this.OutEdges(source)) { if (outEdge.Target.Equals(target)) { return(true); } } return(false); }
public virtual void AddVertex(TVertex v) { GraphContracts.AssumeNotInVertexSet(this, v, "v"); if (this.EdgeCapacity > 0) { this.vertexEdges.Add(v, new EdgeList(this.EdgeCapacity)); } else { this.vertexEdges.Add(v, new EdgeList()); } this.OnVertexAdded(new VertexEventArgs <TVertex>(v)); }
public virtual bool AddVerticesAndEdge(TEdge e) { GraphContracts.AssumeNotNull(e, "e"); if (!this.ContainsVertex(e.Source)) { this.AddVertex(e.Source); } if (!this.ContainsVertex(e.Target)) { this.AddVertex(e.Target); } return(this.AddEdge(e)); }
public int RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate) { GraphContracts.AssumeNotNull(predicate, "predicate"); List <TEdge> edges = new List <TEdge>(); foreach (TEdge edge in this.Edges) { if (predicate(edge)) { edges.Add(edge); } } return(this.RemoveEdges(edges)); }
public virtual bool RemoveEdge(TEdge e) { GraphContracts.AssumeInVertexSet(this, e, "e"); if (this.vertexEdges[e.Source].Remove(e)) { this.edgeCount--; System.Diagnostics.Debug.Assert(this.edgeCount >= 0); this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(e)); return(true); } else { return(false); } }
public int RemoveEdges(IEnumerable <TEdge> edges) { GraphContracts.AssumeNotNull(edges, "edges"); int count = 0; foreach (TEdge edge in edges) { if (RemoveEdge(edge)) { count++; } } return(count); }
public void ClearInEdges(TVertex v) { GraphContracts.AssumeInVertexSet(this, v, "v"); EdgeList inEdges = this.vertexInEdges[v]; foreach (TEdge edge in inEdges) { this.vertexOutEdges[edge.Source].Remove(edge); this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge)); } this.edgeCount -= inEdges.Count; inEdges.Clear(); GraphContracts.Assert(this.edgeCount >= 0); }
public virtual bool AddEdge(TEdge e) { GraphContracts.AssumeInVertexSet <TVertex, TEdge>(this, e, "e"); if (!this.AllowParallelEdges) { if (this.ContainsEdge(e.Source, e.Target)) { return(false); } } this.vertexEdges[e.Source].Add(e); this.edgeCount++; this.OnEdgeAdded(new EdgeEventArgs <TVertex, TEdge>(e)); return(true); }
public void ClearOutEdges(TVertex v) { GraphContracts.AssumeInVertexSet(this, v, "v"); EdgeList edges = this.vertexEdges[v]; int count = edges.Count; if (this.EdgeRemoved != null) // call only if someone is listening { foreach (TEdge edge in edges) { this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge)); } } edges.Clear(); this.edgeCount -= count; GraphContracts.Assert(this.edgeCount >= 0); }
public void ClearAdjacentEdges(TVertex v) { GraphContracts.AssumeInVertexSet(this, v, "v"); IList <TEdge> edges = this.adjacentEdges[v]; this.edgeCount -= edges.Count; foreach (TEdge edge in edges) { if (edge.Source.Equals(v)) { this.adjacentEdges[edge.Target].Remove(edge); } else { this.adjacentEdges[edge.Source].Remove(edge); } } System.Diagnostics.Debug.Assert(this.edgeCount >= 0); }
public int RemoveAdjacentEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate) { GraphContracts.AssumeInVertexSet(this, v, "v"); GraphContracts.AssumeNotNull(predicate, "predicate"); IList <TEdge> outEdges = this.adjacentEdges[v]; List <TEdge> edges = new List <TEdge>(outEdges.Count); foreach (TEdge edge in outEdges) { if (predicate(edge)) { edges.Add(edge); } } this.RemoveEdges(edges); return(edges.Count); }
public int RemoveVertexIf(VertexPredicate <TVertex> pred) { GraphContracts.AssumeNotNull(pred, "pred"); List <TVertex> vertices = new List <TVertex>(); foreach (TVertex v in this.Vertices) { if (pred(v)) { vertices.Add(v); } } foreach (TVertex v in vertices) { RemoveVertex(v); } return(vertices.Count); }
public int RemoveInEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate) { GraphContracts.AssumeInVertexSet(this, v, "v"); GraphContracts.AssumeNotNull(predicate, "predicate"); EdgeList edges = new EdgeList(); foreach (TEdge edge in this.InEdges(v)) { if (predicate(edge)) { edges.Add(edge); } } foreach (TEdge edge in edges) { this.RemoveEdge(edge); } return(edges.Count); }
public bool AddEdge(TEdge edge) { GraphContracts.AssumeInVertexSet(this, edge, "edge"); if (!this.AllowParallelEdges) { if (this.adjacentEdges[edge.Source].Contains(edge)) { return(false); } } this.adjacentEdges[edge.Source].Add(edge); this.adjacentEdges[edge.Target].Add(edge); this.edgeCount++; this.OnEdgeAdded(new EdgeEventArgs <TVertex, TEdge>(edge)); return(true); }
public virtual bool RemoveVertex(TVertex v) { GraphContracts.AssumeNotNull(v, "v"); if (!this.ContainsVertex(v)) { return(false); } // collect edges to remove EdgeList edgesToRemove = new EdgeList(); foreach (TEdge outEdge in this.OutEdges(v)) { this.vertexInEdges[outEdge.Target].Remove(outEdge); edgesToRemove.Add(outEdge); } foreach (TEdge inEdge in this.InEdges(v)) { // might already have been removed if (this.vertexOutEdges[inEdge.Source].Remove(inEdge)) { edgesToRemove.Add(inEdge); } } // notify users if (this.EdgeRemoved != null) { foreach (TEdge edge in edgesToRemove) { this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge)); } } this.vertexOutEdges.Remove(v); this.vertexInEdges.Remove(v); this.edgeCount -= edgesToRemove.Count; this.OnVertexRemoved(new VertexEventArgs <TVertex>(v)); GraphContracts.Assert(this.edgeCount >= 0); return(true); }
public int RemoveVertexIf(VertexPredicate <TVertex> predicate) { GraphContracts.AssumeNotNull(predicate, "predicate"); VertexList vertices = new VertexList(); foreach (TVertex v in this.Vertices) { if (predicate(v)) { vertices.Add(v); } } foreach (TVertex v in vertices) { this.RemoveVertex(v); } return(vertices.Count); }
public void MergeVertexIf(VertexPredicate <TVertex> vertexPredicate, IEdgeFactory <TVertex, TEdge> edgeFactory) { GraphContracts.AssumeNotNull(vertexPredicate, "vertexPredicate"); GraphContracts.AssumeNotNull(edgeFactory, "edgeFactory"); // storing vertices to merge VertexList mergeVertices = new VertexList(this.VertexCount / 4); foreach (TVertex v in this.Vertices) { if (vertexPredicate(v)) { mergeVertices.Add(v); } } // applying merge recursively foreach (TVertex v in mergeVertices) { MergeVertex(v, edgeFactory); } }
public int RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate) { GraphContracts.AssumeNotNull(predicate, "predicate"); EdgeList edges = new EdgeList(); foreach (TEdge edge in this.Edges) { if (predicate(edge)) { edges.Add(edge); } } foreach (TEdge edge in edges) { this.RemoveEdge(edge); } GraphContracts.Assert(this.edgeCount >= 0); return(edges.Count); }
public NamedEdge(TVertex source, TVertex target, string name) : base(source, target) { GraphContracts.AssumeNotNull(name, "name"); this.name = name; }
public ReversedEdge(TEdge originalEdge) { GraphContracts.AssumeNotNull(originalEdge, "originalEdge"); this.originalEdge = originalEdge; }