Ticket #5631: speed-sort.patch

File speed-sort.patch, 2.9 KB (added by bilbo, 15 years ago)

Patch to faster or no sorting when object count exceeds given values

  • src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

     
    675675         * Sorts the current elements in the selection
    676676         */
    677677        public void sort() {
    678             Collections.sort(this.selection, new OsmPrimitiveComparator());
     678            if (this.selection.size()>Main.pref.getInteger("selection.no_sort_above",100000)) return;
     679            if (this.selection.size()>Main.pref.getInteger("selection.fast_sort_above",10000)) {
     680                Collections.sort(this.selection, new OsmPrimitiveQuickComparator());
     681            } else {
     682                Collections.sort(this.selection, new OsmPrimitiveComparator());
     683            }
    679684        }
    680685
    681686        /* ------------------------------------------------------------------------ */
     
    910915        }
    911916    }
    912917
     918    /** Comparator, comparing by type and objects display names */
    913919    static private class OsmPrimitiveComparator implements Comparator<OsmPrimitive> {
    914920        final private HashMap<OsmPrimitive, String> cache= new HashMap<OsmPrimitive, String>();
    915921        final private DefaultNameFormatter df  = DefaultNameFormatter.getInstance();
     
    940946        private int compareType(OsmPrimitive a, OsmPrimitive b) {
    941947            // show ways before relations, then nodes
    942948            //
    943             if (a.getType().equals(b.getType())) return 0;
    944949            if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
    945950            if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
    946951            // a is a relation
     
    954959            return compareType(a, b);
    955960        }
    956961    }
     962
     963    /** Quicker comparator, comparing just by type and ID's */
     964    static private class OsmPrimitiveQuickComparator implements Comparator<OsmPrimitive> {
     965
     966        private int compareId(OsmPrimitive a, OsmPrimitive b) {
     967            long id_a=a.getUniqueId();
     968            long id_b=b.getUniqueId();
     969            if (id_a<id_b) return -1;
     970            if (id_a>id_b) return 1;
     971            return 0;
     972        }
     973
     974        private int compareType(OsmPrimitive a, OsmPrimitive b) {
     975            // show ways before relations, then nodes
     976            //
     977            if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
     978            if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
     979            // a is a relation
     980            if (b.getType().equals(OsmPrimitiveType.WAY)) return 1;
     981            // b is a node
     982            return -1;
     983        }
     984        public int compare(OsmPrimitive a, OsmPrimitive b) {
     985            if (a.getType().equals(b.getType()))
     986                return compareId(a, b);
     987            return compareType(a, b);
     988        }
     989    }
     990
    957991}