LUCENE-4946: Sorter.rotate is a no-op when one of the two adjacent slices to rotate is empty.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1482111 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrien Grand 2013-05-13 21:26:08 +00:00
parent 67970fcaab
commit c1ec7aa8df
2 changed files with 13 additions and 5 deletions

View File

@ -72,7 +72,7 @@ public abstract class Sorter {
first_cut = upper(from, mid, second_cut); first_cut = upper(from, mid, second_cut);
len11 = first_cut - from; len11 = first_cut - from;
} }
rotate( first_cut, mid, second_cut); rotate(first_cut, mid, second_cut);
final int new_mid = first_cut + len22; final int new_mid = first_cut + len22;
mergeInPlace(from, first_cut, new_mid); mergeInPlace(from, first_cut, new_mid);
mergeInPlace(new_mid, second_cut, to); mergeInPlace(new_mid, second_cut, to);
@ -142,7 +142,15 @@ public abstract class Sorter {
} }
} }
void rotate(int lo, int mid, int hi) { final void rotate(int lo, int mid, int hi) {
assert lo <= mid && mid <= hi;
if (lo == mid || mid == hi) {
return;
}
doRotate(lo, mid, hi);
}
void doRotate(int lo, int mid, int hi) {
if (mid - lo == hi - mid) { if (mid - lo == hi - mid) {
// happens rarely but saves n/2 swaps // happens rarely but saves n/2 swaps
while (mid < hi) { while (mid < hi) {

View File

@ -205,9 +205,9 @@ public abstract class TimSorter extends Sorter {
} }
@Override @Override
void rotate(int lo, int mid, int hi) { void doRotate(int lo, int mid, int hi) {
int len1 = mid - lo; final int len1 = mid - lo;
int len2 = hi - mid; final int len2 = hi - mid;
if (len1 == len2) { if (len1 == len2) {
while (mid < hi) { while (mid < hi) {
swap(lo++, mid++); swap(lo++, mid++);