public int RemoveOutEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate) { GraphContracts.AssumeInVertexSet(this, v, "v"); GraphContracts.AssumeNotNull(predicate, "predicate"); var edges = this.vertexEdges[v]; var edgeToRemove = new EdgeList(edges.Count); foreach (var edge in edges) { if (predicate(edge)) { edgeToRemove.Add(edge); } } foreach (var 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 (var source in inedges) { //is it a self edge if (source.Source.Equals(v)) { continue; } foreach (var target in outedges) { if (v.Equals(target.Target)) { continue; } // we add an new edge this.AddEdge(edgeFactory.CreateEdge(source.Source, target.Target)); } } }
public virtual void AddVertexRange(IEnumerable <TVertex> vertices) { GraphContracts.AssumeNotNull(vertices, "vertices"); foreach (var v in vertices) { this.AddVertex(v); } }
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 (var 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 (var kv in this.vertexEdges) { if (kv.Key.Equals(v)) { continue; // we've already } // collect edge to remove foreach (var edge in kv.Value) { if (edge.Target.Equals(v)) { edgeToRemove.Add(edge); } } // remove edges foreach (var 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 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 RemoveInEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate) { GraphContracts.AssumeInVertexSet(this, v, "v"); GraphContracts.AssumeNotNull(predicate, "predicate"); EdgeList edges = new EdgeList(); foreach (var edge in this.InEdges(v)) { if (predicate(edge)) { edges.Add(edge); } } foreach (var edge in edges) { this.RemoveEdge(edge); } return(edges.Count); }
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 (var outEdge in this.OutEdges(v)) { this.vertexInEdges[outEdge.Target].Remove(outEdge); edgesToRemove.Add(outEdge); } foreach (var 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 (var v in this.Vertices) { if (predicate(v)) { vertices.Add(v); } } foreach (var 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 (var v in this.Vertices) { if (vertexPredicate(v)) { mergeVertices.Add(v); } } // applying merge recursively foreach (var v in mergeVertices) { MergeVertex(v, edgeFactory); } }
public int RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate) { GraphContracts.AssumeNotNull(predicate, "predicate"); var edges = new EdgeList(); foreach (var edge in this.Edges) { if (predicate(edge)) { edges.Add(edge); } } foreach (var edge in edges) { this.RemoveEdge(edge); } GraphContracts.Assert(this.edgeCount >= 0); return(edges.Count); }
public bool ContainsVertex(TVertex v) { GraphContracts.AssumeNotNull(v, "v"); return(this.vertexOutEdges.ContainsKey(v)); }