private void BuildNewGraph() { var newPairState = GetNoneMarkPairs(); if (newPairState.Count == 0) { return; } var newStates = States.ToList(); var deletedStates = new List <Vertex>(); foreach (var oldState in States) { foreach (var pair in newPairState) { if (oldState.Compare(pair.Value1) || oldState.Compare(pair.Value2)) { newStates.Remove(oldState); deletedStates.Add(oldState); } } } var createdCombineVertex = new List <CombineVertex>(); foreach (var pair in newPairState) { var cv = new CombineVertex(new HashSet <Vertex>() { pair.Value1, pair.Value2 }); createdCombineVertex.Add(cv); newStates.Add(cv); } var deletedEdges = new List <BaseEdge <Vertex> >(); var newEdges = new HashSet <BaseEdge <Vertex> >(Edges.ToList()); foreach (var oldEdge in Edges) { if (deletedStates.Any( vertex => oldEdge.Source.Compare(vertex) || oldEdge.Target.Compare(vertex))) { newEdges.Remove(oldEdge); deletedEdges.Add(oldEdge); } } foreach (var deletedEdge in deletedEdges) { if (deletedStates.Any(vertex => deletedEdge.Target.Compare(vertex) && deletedEdge.Source.Compare(vertex))) { var sourceTargetVertex = createdCombineVertex.Find( vertex => vertex.Vertices.Contains(deletedEdge.Target) && vertex.Vertices.Contains(deletedEdge.Source)); var newEdge = new BaseEdge <Vertex>(sourceTargetVertex, sourceTargetVertex, deletedEdge.Tag); if (!newEdges.Any(edge => edge.Compare(newEdge))) { newEdges.Add(newEdge); } } else if (!newPairState.Any(pair => pair.SamePair(new Pair <Vertex>(deletedEdge.Source, deletedEdge.Target)))) { if (deletedStates.Any(vertex => deletedEdge.Target.Compare(vertex))) { var targetVertex = createdCombineVertex.Find( vertex => vertex.Vertices.Contains(deletedEdge.Target)); var newEdge = new BaseEdge <Vertex>(deletedEdge.Source, targetVertex, deletedEdge.Tag); if (!newEdges.Any(edge => edge.Compare(newEdge))) { newEdges.Add(newEdge); } } else if (deletedStates.Any(vertex => deletedEdge.Source.Compare(vertex))) { var sourceVertex = createdCombineVertex.Find( vertex => vertex.Vertices.Contains(deletedEdge.Source)); var newEdge = new BaseEdge <Vertex>(sourceVertex, deletedEdge.Target, deletedEdge.Tag); if (!newEdges.Any(edge => edge.Compare(newEdge))) { newEdges.Add(newEdge); } } } } States = newStates.Where(state => state.GetHashCode() != DeadState.GetHashCode()).ToList(); Edges = newEdges.Where(edge => edge.Source.GetHashCode() != DeadState.GetHashCode() && edge.Target.GetHashCode() != DeadState.GetHashCode()).ToList(); }
public bool Compare(BaseEdge <TVertex> edge) { return(Source.Equals(edge.Source) && Target.Equals(edge.Target) && Tag.Equals(edge.Tag)); }