LUCENE-3054: fix remaining quickSort

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1099041 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2011-05-03 13:07:24 +00:00
parent 422f8dea4a
commit 11e962d16b
7 changed files with 19 additions and 19 deletions

View File

@ -231,7 +231,7 @@ public class TokenSources {
if (unsortedTokens != null) { if (unsortedTokens != null) {
tokensInOriginalOrder = unsortedTokens.toArray(new Token[unsortedTokens tokensInOriginalOrder = unsortedTokens.toArray(new Token[unsortedTokens
.size()]); .size()]);
ArrayUtil.quickSort(tokensInOriginalOrder, new Comparator<Token>() { ArrayUtil.mergeSort(tokensInOriginalOrder, new Comparator<Token>() {
public int compare(Token t1, Token t2) { public int compare(Token t1, Token t2) {
if (t1.startOffset() == t2.startOffset()) if (t1.startOffset() == t2.startOffset())
return t1.endOffset() - t2.endOffset(); return t1.endOffset() - t2.endOffset();

View File

@ -1420,7 +1420,7 @@ public abstract class IndexReader implements Cloneable,Closeable {
cfr = new CompoundFileReader(dir, filename); cfr = new CompoundFileReader(dir, filename);
String [] files = cfr.listAll(); String [] files = cfr.listAll();
ArrayUtil.quickSort(files); // sort the array of filename so that the output is more readable ArrayUtil.mergeSort(files); // sort the array of filename so that the output is more readable
for (int i = 0; i < files.length; ++i) { for (int i = 0; i < files.length; ++i) {
long len = cfr.fileLength(files[i]); long len = cfr.fileLength(files[i]);

View File

@ -219,7 +219,7 @@ public class MultiPhraseQuery extends Query {
// sort by increasing docFreq order // sort by increasing docFreq order
if (slop == 0) { if (slop == 0) {
ArrayUtil.quickSort(postingsFreqs); ArrayUtil.mergeSort(postingsFreqs);
} }
if (slop == 0) { if (slop == 0) {

View File

@ -234,7 +234,7 @@ public class PhraseQuery extends Query {
// sort by increasing docFreq order // sort by increasing docFreq order
if (slop == 0) { if (slop == 0) {
ArrayUtil.quickSort(postingsFreqs); ArrayUtil.mergeSort(postingsFreqs);
} }
if (slop == 0) { // optimize exact case if (slop == 0) { // optimize exact case

View File

@ -134,7 +134,7 @@ public abstract class TopTermsRewrite<Q extends Query> extends TermCollectingRew
final Term placeholderTerm = new Term(query.field); final Term placeholderTerm = new Term(query.field);
final Q q = getTopLevelQuery(); final Q q = getTopLevelQuery();
final ScoreTerm[] scoreTerms = stQueue.toArray(new ScoreTerm[stQueue.size()]); final ScoreTerm[] scoreTerms = stQueue.toArray(new ScoreTerm[stQueue.size()]);
ArrayUtil.quickSort(scoreTerms, scoreTermSortByTermComp); ArrayUtil.mergeSort(scoreTerms, scoreTermSortByTermComp);
for (final ScoreTerm st : scoreTerms) { for (final ScoreTerm st : scoreTerms) {
final Term term = placeholderTerm.createTerm(st.bytes); final Term term = placeholderTerm.createTerm(st.bytes);
assert reader.docFreq(term) == st.termState.docFreq() : "reader DF is " + reader.docFreq(term) + " vs " + st.termState.docFreq(); assert reader.docFreq(term) == st.termState.docFreq() : "reader DF is " + reader.docFreq(term) + " vs " + st.termState.docFreq();

View File

@ -190,7 +190,7 @@ public class NearSpansOrdered extends Spans {
/** Advance the subSpans to the same document */ /** Advance the subSpans to the same document */
private boolean toSameDoc() throws IOException { private boolean toSameDoc() throws IOException {
ArrayUtil.quickSort(subSpansByDoc, spanDocComparator); ArrayUtil.mergeSort(subSpansByDoc, spanDocComparator);
int firstIndex = 0; int firstIndex = 0;
int maxDoc = subSpansByDoc[subSpansByDoc.length - 1].doc(); int maxDoc = subSpansByDoc[subSpansByDoc.length - 1].doc();
while (subSpansByDoc[firstIndex].doc() != maxDoc) { while (subSpansByDoc[firstIndex].doc() != maxDoc) {

View File

@ -30,7 +30,6 @@ package org.apache.lucene.util;
public abstract class SorterTemplate { public abstract class SorterTemplate {
private static final int MERGESORT_THRESHOLD = 12; private static final int MERGESORT_THRESHOLD = 12;
private static final int MERGE_TO_QUICKSORT_THRESHOLD = 40;
private static final int QUICKSORT_THRESHOLD = 7; private static final int QUICKSORT_THRESHOLD = 7;
/** Implement this method, that swaps slots {@code i} and {@code j} in your data */ /** Implement this method, that swaps slots {@code i} and {@code j} in your data */
@ -63,17 +62,26 @@ public abstract class SorterTemplate {
/** Sorts via in-place, but unstable, QuickSort algorithm. /** Sorts via in-place, but unstable, QuickSort algorithm.
* For small collections falls back to {@link #insertionSort(int,int)}. */ * For small collections falls back to {@link #insertionSort(int,int)}. */
public final void quickSort(int lo, int hi) { public final void quickSort(final int lo, final int hi) {
quickSort(lo, hi, MERGE_TO_QUICKSORT_THRESHOLD); if (hi <= lo) return;
// from Integer's Javadocs: ceil(log2(x)) = 32 - numberOfLeadingZeros(x - 1)
quickSort(lo, hi, (Integer.SIZE - Integer.numberOfLeadingZeros(hi - lo)) << 1);
} }
private void quickSort(int lo, int hi, int maxDepth) { private void quickSort(int lo, int hi, int maxDepth) {
// fall back to insertion when array has short length
final int diff = hi - lo; final int diff = hi - lo;
if (diff <= QUICKSORT_THRESHOLD) { if (diff <= QUICKSORT_THRESHOLD) {
insertionSort(lo, hi); insertionSort(lo, hi);
return; return;
} }
// fall back to merge sort when recursion depth gets too big
if (--maxDepth == 0) {
mergeSort(lo, hi);
return;
}
final int mid = lo + (diff >>> 1); final int mid = lo + (diff >>> 1);
if (compare(lo, mid) > 0) { if (compare(lo, mid) > 0) {
@ -106,16 +114,8 @@ public abstract class SorterTemplate {
} }
} }
// fall back to merge sort when recursion depth gets too big quickSort(lo, left, maxDepth);
if (maxDepth == 0) { quickSort(left + 1, hi, maxDepth);
// for testing: new Exception("Hit recursion depth limit").printStackTrace();
mergeSort(lo, left);
mergeSort(left + 1, hi);
} else {
--maxDepth;
quickSort(lo, left, maxDepth);
quickSort(left + 1, hi, maxDepth);
}
} }
/** Sorts via stable in-place MergeSort algorithm /** Sorts via stable in-place MergeSort algorithm