Apache Lucene open-source search software
LUCENE-2990: ArrayUtil/CollectionUtil.*Sort() methods now exit early on empty or one-element lists/arrays
Uwe Schindler4b2a94e0c75a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 7edbc323343..4784ca1a609 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -209,11 +209,8 @@ API Changeswhich takes Analyzer as a parameter, for easier customization by subclasses.(Robert Muir)-* LUCENE-2953: PriorityQueue's internal heap was made private, as subclassing- with generics can lead to ClassCastException. For advanced use (e.g. in Solr)- a method getHeapArray() was added to retrieve the internal heap array as a- non-generic Object[]. Also the initialize(int) function was moved into the- ctor. (Uwe Schindler, Yonik Seeley)+* LUCENE-2953: In addition to changes in 3.x, PriorityQueue#initialize(int)+ function was moved into the ctor. (Uwe Schindler, Yonik Seeley)New features@@ -371,7 +368,17 @@ Bug fixes======================= Lucene 3.x (not yet released) =======================-(No changes)+Changes in backwards compatibility policy++* LUCENE-2953: PriorityQueue's internal heap was made private, as subclassing+ with generics can lead to ClassCastException. For advanced use (e.g. in Solr)+ a method getHeapArray() was added to retrieve the internal heap array as a+ non-generic Object[]. (Uwe Schindler, Yonik Seeley)++Optimizations++* LUCENE-2990: ArrayUtil/CollectionUtil.*Sort() methods now exit early+ on empty or one-element lists/arrays. (Uwe Schindler)======================= Lucene 3.1 (not yet released) =======================diff --git a/lucene/src/java/org/apache/lucene/util/ArrayUtil.java b/lucene/src/java/org/apache/lucene/util/ArrayUtil.javaindex c9c0f149b21..0bff229c340 100644--- a/lucene/src/java/org/apache/lucene/util/ArrayUtil.java+++ b/lucene/src/java/org/apache/lucene/util/ArrayUtil.java@@ -563,6 +563,7 @@ public final class ArrayUtil {* @param toIndex end index (exclusive)*/public static <T> void quickSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp) {+ if (toIndex-fromIndex <= 1) return;getSorter(a, comp).quickSort(fromIndex, toIndex-1);}@@ -581,6 +582,7 @@ public final class ArrayUtil {* @param toIndex end index (exclusive)*/public static <T extends Comparable<? super T>> void quickSort(T[] a, int fromIndex, int toIndex) {+ if (toIndex-fromIndex <= 1) return;getSorter(a).quickSort(fromIndex, toIndex-1);}@@ -601,6 +603,7 @@ public final class ArrayUtil {* @param toIndex end index (exclusive)*/public static <T> void mergeSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp) {+ if (toIndex-fromIndex <= 1) return;getSorter(a, comp).mergeSort(fromIndex, toIndex-1);}@@ -619,6 +622,7 @@ public final class ArrayUtil {* @param toIndex end index (exclusive)*/public static <T extends Comparable<? super T>> void mergeSort(T[] a, int fromIndex, int toIndex) {+ if (toIndex-fromIndex <= 1) return;getSorter(a).mergeSort(fromIndex, toIndex-1);}@@ -639,6 +643,7 @@ public final class ArrayUtil {* @param toIndex end index (exclusive)*/public static <T> void insertionSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp) {+ if (toIndex-fromIndex <= 1) return;getSorter(a, comp).insertionSort(fromIndex, toIndex-1);}@@ -657,6 +662,7 @@ public final class ArrayUtil {* @param toIndex end index (exclusive)*/public static <T extends Comparable<? super T>> void insertionSort(T[] a, int fromIndex, int toIndex) {+ if (toIndex-fromIndex <= 1) return;getSorter(a).insertionSort(fromIndex, toIndex-1);}diff --git a/lucene/src/java/org/apache/lucene/util/CollectionUtil.java b/lucene/src/java/org/apache/lucene/util/CollectionUtil.javaindex ff2a76a6b87..d2384cba09a 100644--- a/lucene/src/java/org/apache/lucene/util/CollectionUtil.java+++ b/lucene/src/java/org/apache/lucene/util/CollectionUtil.java@@ -100,7 +100,9 @@ public final class CollectionUtil {* @throws IllegalArgumentException if list is e.g. a linked list without random access.*/public static <T> void quickSort(List<T> list, Comparator<? super T> comp) {- getSorter(list, comp).quickSort(0, list.size()-1);+ final int size = list.size();+ if (size <= 1) return;+ getSorter(list, comp).quickSort(0, size-1);}/**@@ -110,7 +112,9 @@ public final class CollectionUtil {* @throws IllegalArgumentException if list is e.g. a linked list without random access.*/public static <T extends Comparable<? super T>> void quickSort(List<T> list) {- getSorter(list).quickSort(0, list.size()-1);+ final int size = list.size();+ if (size <= 1) return;+ getSorter(list).quickSort(0, size-1);}// mergeSorts:@@ -122,7 +126,9 @@ public final class CollectionUtil {* @throws IllegalArgumentException if list is e.g. a linked list without random access.*/public static <T> void mergeSort(List<T> list, Comparator<? super T> comp) {- getSorter(list, comp).mergeSort(0, list.size()-1);+ final int size = list.size();+ if (size <= 1) return;+ getSorter(list, comp).mergeSort(0, size-1);}/**@@ -132,7 +138,9 @@ public final class CollectionUtil {* @throws IllegalArgumentException if list is e.g. a linked list without random access.*/public static <T extends Comparable<? super T>> void mergeSort(List<T> list) {- getSorter(list).mergeSort(0, list.size()-1);+ final int size = list.size();+ if (size <= 1) return;+ getSorter(list).mergeSort(0, size-1);}// insertionSorts:@@ -144,7 +152,9 @@ public final class CollectionUtil {* @throws IllegalArgumentException if list is e.g. a linked list without random access.*/public static <T> void insertionSort(List<T> list, Comparator<? super T> comp) {- getSorter(list, comp).insertionSort(0, list.size()-1);+ final int size = list.size();+ if (size <= 1) return;+ getSorter(list, comp).insertionSort(0, size-1);}/**@@ -154,7 +164,9 @@ public final class CollectionUtil {* @throws IllegalArgumentException if list is e.g. a linked list without random access.*/public static <T extends Comparable<? super T>> void insertionSort(List<T> list) {- getSorter(list).insertionSort(0, list.size()-1);+ final int size = list.size();+ if (size <= 1) return;+ getSorter(list).insertionSort(0, size-1);}}\ No newline at end of filediff --git a/lucene/src/test/org/apache/lucene/util/TestCollectionUtil.java b/lucene/src/test/org/apache/lucene/util/TestCollectionUtil.javaindex 1c59c887db9..23d1ac9b140 100644--- a/lucene/src/test/org/apache/lucene/util/TestCollectionUtil.java+++ b/lucene/src/test/org/apache/lucene/util/TestCollectionUtil.java@@ -20,6 +20,7 @@ package org.apache.lucene.util;import java.util.Arrays;import java.util.ArrayList;import java.util.Collections;+import java.util.LinkedList;import java.util.List;public class TestCollectionUtil extends LuceneTestCase {@@ -89,8 +90,8 @@ public class TestCollectionUtil extends LuceneTestCase {}}- // should produce no exceptions- public void testEmptyArraySort() {+ public void testEmptyListSort() {+ // should produce no exceptionsList<Integer> list = Arrays.asList(new Integer[0]);CollectionUtil.quickSort(list);CollectionUtil.mergeSort(list);@@ -98,6 +99,27 @@ public class TestCollectionUtil extends LuceneTestCase {CollectionUtil.quickSort(list, Collections.reverseOrder());CollectionUtil.mergeSort(list, Collections.reverseOrder());CollectionUtil.insertionSort(list, Collections.reverseOrder());++ // check that empty non-random access lists pass sorting without ex (as sorting is not needed)+ list = new LinkedList<Integer>();+ CollectionUtil.quickSort(list);+ CollectionUtil.mergeSort(list);+ CollectionUtil.insertionSort(list);+ CollectionUtil.quickSort(list, Collections.reverseOrder());+ CollectionUtil.mergeSort(list, Collections.reverseOrder());+ CollectionUtil.insertionSort(list, Collections.reverseOrder());+ }++ public void testOneElementListSort() {+ // check that one-element non-random access lists pass sorting without ex (as sorting is not needed)+ List<Integer> list = new LinkedList<Integer>();+ list.add(1);+ CollectionUtil.quickSort(list);+ CollectionUtil.mergeSort(list);+ CollectionUtil.insertionSort(list);+ CollectionUtil.quickSort(list, Collections.reverseOrder());+ CollectionUtil.mergeSort(list, Collections.reverseOrder());+ CollectionUtil.insertionSort(list, Collections.reverseOrder());}}