Ticket #18368: 18368.patch
File 18368.patch, 2.3 KB (added by , 4 years ago) |
---|
-
src/org/openstreetmap/josm/data/osm/NodeGraph.java
4 4 import java.util.ArrayList; 5 5 import java.util.Collection; 6 6 import java.util.Collections; 7 import java.util.HashMap; 7 8 import java.util.LinkedHashMap; 8 9 import java.util.LinkedHashSet; 9 10 import java.util.LinkedList; 10 11 import java.util.List; 11 12 import java.util.Map; 13 import java.util.Map.Entry; 12 14 import java.util.Optional; 13 15 import java.util.Set; 14 16 import java.util.Stack; 17 import java.util.TreeMap; 15 18 16 19 import org.openstreetmap.josm.tools.Pair; 17 20 … … 239 242 return nodes; 240 243 } 241 244 242 protected boolean isSpanningWay( Stack<NodePair> way) {245 protected boolean isSpanningWay(Collection<NodePair> way) { 243 246 return numUndirectedEges == way.size(); 244 247 } 245 248 … … 296 299 // In the worst case this loops over all nodes which is very slow for large ways. 297 300 // 298 301 Set<Node> nodes = getTerminalNodes(); 302 nodes = nodes.isEmpty() ? getMostFrequentVisitedNodes() : nodes; 299 303 nodes = nodes.isEmpty() ? getNodes() : nodes; 300 for (Node n: 304 for (Node n:nodes) { 301 305 List<Node> path = buildSpanningPath(n); 302 306 if (!path.isEmpty()) 303 307 return path; … … 304 308 } 305 309 return null; 306 310 } 311 312 /** 313 * @return a set of those nodes which appear most often in the edges. 314 */ 315 private Set<Node> getMostFrequentVisitedNodes() { 316 if (edges.isEmpty()) 317 return Collections.emptyNavigableSet(); 318 Map<Node, Integer> counters = new HashMap<>(); 319 for (NodePair pair : edges) { 320 Integer c = counters.get(pair.getA()); 321 counters.put(pair.getA(), c == null ? 1 : c + 1); 322 c = counters.get(pair.getB()); 323 counters.put(pair.getB(), c == null ? 1 : c + 1); 324 } 325 TreeMap<Integer, Set<Node>> sorted = new TreeMap<>(); 326 for (Entry<Node, Integer> e : counters.entrySet()) { 327 sorted.computeIfAbsent(e.getValue(), LinkedHashSet::new).add(e.getKey()); 328 } 329 return sorted.lastEntry().getValue(); 330 } 331 307 332 }