LUCENE-7645: Use JDK's Arrays.binarySearch in BaseCharFilter.

This commit is contained in:
Adrien Grand 2017-01-19 11:27:24 +01:00
parent 85a05b546b
commit a14d79366f
1 changed files with 6 additions and 20 deletions

View File

@ -41,31 +41,17 @@ public abstract class BaseCharFilter extends CharFilter {
/** Retrieve the corrected offset. */
@Override
protected int correct(int currentOff) {
if (offsets == null || currentOff < offsets[0]) {
if (offsets == null) {
return currentOff;
}
int hi = size - 1;
if(currentOff >= offsets[hi])
return currentOff + diffs[hi];
int lo = 0;
int mid = -1;
while (hi >= lo) {
mid = (lo + hi) >>> 1;
if (currentOff < offsets[mid])
hi = mid - 1;
else if (currentOff > offsets[mid])
lo = mid + 1;
else
return currentOff + diffs[mid];
int index = Arrays.binarySearch(offsets, 0, size, currentOff);
if (index < -1) {
index = -2 - index;
}
if (currentOff < offsets[mid])
return mid == 0 ? currentOff : currentOff + diffs[mid-1];
else
return currentOff + diffs[mid];
final int diff = index < 0 ? 0 : diffs[index];
return currentOff + diff;
}
protected int getLastCumulativeDiff() {