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. */ /** Retrieve the corrected offset. */
@Override @Override
protected int correct(int currentOff) { protected int correct(int currentOff) {
if (offsets == null || currentOff < offsets[0]) { if (offsets == null) {
return currentOff; return currentOff;
} }
int hi = size - 1; int index = Arrays.binarySearch(offsets, 0, size, currentOff);
if(currentOff >= offsets[hi]) if (index < -1) {
return currentOff + diffs[hi]; index = -2 - index;
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];
} }
if (currentOff < offsets[mid]) final int diff = index < 0 ? 0 : diffs[index];
return mid == 0 ? currentOff : currentOff + diffs[mid-1]; return currentOff + diff;
else
return currentOff + diffs[mid];
} }
protected int getLastCumulativeDiff() { protected int getLastCumulativeDiff() {