From ef4ca97e2e79b0a1bfa93c8d2e4915fe0ec8e837 Mon Sep 17 00:00:00 2001 From: Sergey Solovyev Date: Thu, 25 May 2017 16:01:42 +0200 Subject: [PATCH] Make sorting faster --- .../common/collections/SortedList.java | 30 +++++++++++++++---- .../common/math/AbstractMathRegistry.java | 13 ++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/jscl/src/main/java/org/solovyev/common/collections/SortedList.java b/jscl/src/main/java/org/solovyev/common/collections/SortedList.java index c22b800e..8dcad340 100644 --- a/jscl/src/main/java/org/solovyev/common/collections/SortedList.java +++ b/jscl/src/main/java/org/solovyev/common/collections/SortedList.java @@ -22,8 +22,15 @@ package org.solovyev.common.collections; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + import javax.annotation.Nonnull; -import java.util.*; public class SortedList implements List { @@ -105,14 +112,14 @@ public class SortedList implements List { @Override public boolean add(T t) { boolean result = list.add(t); - sort(); + insertionSort(); return result; } @Override public boolean remove(Object o) { boolean result = list.remove(o); - sort(); + insertionSort(); return result; } @@ -169,13 +176,13 @@ public class SortedList implements List { @Override public void add(int index, T element) { this.list.add(index, element); - sort(); + insertionSort(); } @Override public T remove(int index) { T result = this.list.remove(index); - sort(); + insertionSort(); return result; } @@ -254,6 +261,19 @@ public class SortedList implements List { Collections.sort(list, comparator); } + private void insertionSort() { + for (int i = 1; i < list.size(); i++) { + final T t = list.get(i); + + int j = i - 1; + while (j >= 0 && comparator.compare(list.get(j), t) > 0) { + list.set(j + 1, list.get(j)); + j--; + } + list.set(j + 1, t); + } + } + @Nonnull @Override public List subList(int fromIndex, int toIndex) { diff --git a/jscl/src/main/java/org/solovyev/common/math/AbstractMathRegistry.java b/jscl/src/main/java/org/solovyev/common/math/AbstractMathRegistry.java index 9861256f..15484de4 100644 --- a/jscl/src/main/java/org/solovyev/common/math/AbstractMathRegistry.java +++ b/jscl/src/main/java/org/solovyev/common/math/AbstractMathRegistry.java @@ -24,13 +24,14 @@ package org.solovyev.common.math; import org.solovyev.common.collections.SortedList; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.annotation.concurrent.GuardedBy; import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.concurrent.GuardedBy; + /** * User: serso * Date: 9/29/11 @@ -204,9 +205,11 @@ public abstract class AbstractMathRegistry implements Math } public int compare(T l, T r) { - int result = r.getName().length() - l.getName().length(); + final String rName = r.getName(); + final String lName = l.getName(); + int result = rName.length() - lName.length(); if (result == 0) { - result = l.getName().compareTo(r.getName()); + result = lName.compareTo(rName); } return result; }