1 19 20 22 package edu.umd.cs.findbugs.graph; 23 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import java.util.TreeSet ; 27 28 32 public class MergeVertices 33 < 34 GraphType extends Graph<EdgeType, VertexType>, 35 EdgeType extends GraphEdge<EdgeType, VertexType>, 36 VertexType extends GraphVertex<VertexType> 37 > { 38 39 42 public MergeVertices() { 43 } 44 45 53 public void mergeVertices(Set <VertexType> vertexSet, GraphType g, VertexCombinator<VertexType> combinator, 54 GraphToolkit<GraphType, EdgeType, VertexType> toolkit) { 55 56 if (vertexSet.size() <= 1) 59 return; 60 61 TreeSet <EdgeType> edgeSet = new TreeSet <EdgeType>(); 65 for (Iterator <EdgeType> i = g.edgeIterator(); i.hasNext();) { 66 EdgeType e = i.next(); 67 if (vertexSet.contains(e.getSource()) || vertexSet.contains(e.getTarget())) 68 edgeSet.add(e); 69 } 70 71 VertexType compositeVertex = combinator.combineVertices(vertexSet); 73 74 for (EdgeType e : edgeSet) { 78 VertexType source = vertexSet.contains(e.getSource()) ? compositeVertex : e.getSource(); 79 VertexType target = vertexSet.contains(e.getTarget()) ? compositeVertex : e.getTarget(); 80 81 84 if (source == compositeVertex && target == compositeVertex && 88 e.getSource() != e.getTarget()) 89 continue; 90 91 if (g.lookupEdge(source, target) != null) 93 continue; 94 95 EdgeType compositeEdge = g.createEdge(source, target); 96 toolkit.copyEdge(e, compositeEdge); 98 } 99 100 for (VertexType aVertexSet : vertexSet) { 104 g.removeVertex(aVertexSet); 105 } 106 107 } 108 109 } 110 111 | Popular Tags |