Make sorting faster

This commit is contained in:
Sergey Solovyev 2017-05-25 16:01:42 +02:00
parent 6297ec5b90
commit ef4ca97e2e
2 changed files with 33 additions and 10 deletions

View File

@ -22,8 +22,15 @@
package org.solovyev.common.collections; 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 javax.annotation.Nonnull;
import java.util.*;
public class SortedList<T> implements List<T> { public class SortedList<T> implements List<T> {
@ -105,14 +112,14 @@ public class SortedList<T> implements List<T> {
@Override @Override
public boolean add(T t) { public boolean add(T t) {
boolean result = list.add(t); boolean result = list.add(t);
sort(); insertionSort();
return result; return result;
} }
@Override @Override
public boolean remove(Object o) { public boolean remove(Object o) {
boolean result = list.remove(o); boolean result = list.remove(o);
sort(); insertionSort();
return result; return result;
} }
@ -169,13 +176,13 @@ public class SortedList<T> implements List<T> {
@Override @Override
public void add(int index, T element) { public void add(int index, T element) {
this.list.add(index, element); this.list.add(index, element);
sort(); insertionSort();
} }
@Override @Override
public T remove(int index) { public T remove(int index) {
T result = this.list.remove(index); T result = this.list.remove(index);
sort(); insertionSort();
return result; return result;
} }
@ -254,6 +261,19 @@ public class SortedList<T> implements List<T> {
Collections.sort(list, comparator); 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 @Nonnull
@Override @Override
public List<T> subList(int fromIndex, int toIndex) { public List<T> subList(int fromIndex, int toIndex) {

View File

@ -24,13 +24,14 @@ package org.solovyev.common.math;
import org.solovyev.common.collections.SortedList; 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.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
/** /**
* User: serso * User: serso
* Date: 9/29/11 * Date: 9/29/11
@ -204,9 +205,11 @@ public abstract class AbstractMathRegistry<T extends MathEntity> implements Math
} }
public int compare(T l, T r) { 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) { if (result == 0) {
result = l.getName().compareTo(r.getName()); result = lName.compareTo(rName);
} }
return result; return result;
} }