LUCENE-4851: Use Java 7's {Integer,Long,Float,Double}.compare instead of branches.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1457868 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrien Grand 2013-03-18 17:10:29 +00:00
parent 12c2d46446
commit 05509413d4
12 changed files with 37 additions and 210 deletions

View File

@ -134,14 +134,7 @@ class BufferedDeletesStream {
private static final Comparator<SegmentInfoPerCommit> sortSegInfoByDelGen = new Comparator<SegmentInfoPerCommit>() { private static final Comparator<SegmentInfoPerCommit> sortSegInfoByDelGen = new Comparator<SegmentInfoPerCommit>() {
@Override @Override
public int compare(SegmentInfoPerCommit si1, SegmentInfoPerCommit si2) { public int compare(SegmentInfoPerCommit si1, SegmentInfoPerCommit si2) {
final long cmp = si1.getBufferedDeletesGen() - si2.getBufferedDeletesGen(); return Long.compare(si1.getBufferedDeletesGen(), si2.getBufferedDeletesGen());
if (cmp > 0) {
return 1;
} else if (cmp < 0) {
return -1;
} else {
return 0;
}
} }
}; };

View File

@ -119,12 +119,6 @@ public abstract class IndexCommit implements Comparable<IndexCommit> {
long gen = getGeneration(); long gen = getGeneration();
long comgen = commit.getGeneration(); long comgen = commit.getGeneration();
if (gen < comgen) { return Long.compare(gen, comgen);
return -1;
} else if (gen > comgen) {
return 1;
} else {
return 0;
}
} }
} }

View File

@ -545,13 +545,7 @@ public abstract class LogMergePolicy extends MergePolicy {
// Sorts largest to smallest // Sorts largest to smallest
@Override @Override
public int compareTo(SegmentInfoAndLevel other) { public int compareTo(SegmentInfoAndLevel other) {
if (level < other.level) { return Float.compare(other.level, level);
return 1;
} else if (level > other.level) {
return -1;
} else {
return 0;
}
} }
} }

View File

@ -47,7 +47,7 @@ class ConjunctionScorer extends Scorer {
ArrayUtil.mergeSort(docsAndFreqs, new Comparator<DocsAndFreqs>() { ArrayUtil.mergeSort(docsAndFreqs, new Comparator<DocsAndFreqs>() {
@Override @Override
public int compare(DocsAndFreqs o1, DocsAndFreqs o2) { public int compare(DocsAndFreqs o1, DocsAndFreqs o2) {
return Long.signum(o1.cost - o2.cost); return Long.compare(o1.cost, o2.cost);
} }
}); });

View File

@ -19,7 +19,6 @@ package org.apache.lucene.search;
import java.io.IOException; import java.io.IOException;
import org.apache.lucene.index.AtomicReader; // javadocs
import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.BinaryDocValues; import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.SortedDocValues; import org.apache.lucene.index.SortedDocValues;
@ -235,7 +234,7 @@ public abstract class FieldComparator<T> {
@Override @Override
public int compare(int slot1, int slot2) { public int compare(int slot1, int slot2) {
return values[slot1] - values[slot2]; return Byte.compare(values[slot1], values[slot2]);
} }
@Override @Override
@ -247,7 +246,7 @@ public abstract class FieldComparator<T> {
v2 = missingValue; v2 = missingValue;
} }
return bottom - v2; return Byte.compare(bottom, v2);
} }
@Override @Override
@ -287,7 +286,7 @@ public abstract class FieldComparator<T> {
if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) {
docValue = missingValue; docValue = missingValue;
} }
return docValue - value.byteValue(); return Byte.compare(docValue, value.byteValue());
} }
} }
@ -307,15 +306,7 @@ public abstract class FieldComparator<T> {
@Override @Override
public int compare(int slot1, int slot2) { public int compare(int slot1, int slot2) {
final double v1 = values[slot1]; return Double.compare(values[slot1], values[slot2]);
final double v2 = values[slot2];
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
@ -327,13 +318,7 @@ public abstract class FieldComparator<T> {
v2 = missingValue; v2 = missingValue;
} }
if (bottom > v2) { return Double.compare(bottom, v2);
return 1;
} else if (bottom < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
@ -375,13 +360,7 @@ public abstract class FieldComparator<T> {
if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) {
docValue = missingValue; docValue = missingValue;
} }
if (docValue < value) { return Double.compare(docValue, value);
return -1;
} else if (docValue > value) {
return 1;
} else {
return 0;
}
} }
} }
@ -401,17 +380,7 @@ public abstract class FieldComparator<T> {
@Override @Override
public int compare(int slot1, int slot2) { public int compare(int slot1, int slot2) {
// TODO: are there sneaky non-branch ways to compute return Float.compare(values[slot1], values[slot2]);
// sign of float?
final float v1 = values[slot1];
final float v2 = values[slot2];
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
@ -424,13 +393,7 @@ public abstract class FieldComparator<T> {
v2 = missingValue; v2 = missingValue;
} }
if (bottom > v2) { return Float.compare(bottom, v2);
return 1;
} else if (bottom < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
@ -472,13 +435,7 @@ public abstract class FieldComparator<T> {
if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) {
docValue = missingValue; docValue = missingValue;
} }
if (docValue < value) { return Float.compare(docValue, value);
return -1;
} else if (docValue > value) {
return 1;
} else {
return 0;
}
} }
} }
@ -498,7 +455,7 @@ public abstract class FieldComparator<T> {
@Override @Override
public int compare(int slot1, int slot2) { public int compare(int slot1, int slot2) {
return values[slot1] - values[slot2]; return Short.compare(values[slot1], values[slot2]);
} }
@Override @Override
@ -510,7 +467,7 @@ public abstract class FieldComparator<T> {
v2 = missingValue; v2 = missingValue;
} }
return bottom - v2; return Short.compare(bottom, v2);
} }
@Override @Override
@ -552,7 +509,7 @@ public abstract class FieldComparator<T> {
if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) {
docValue = missingValue; docValue = missingValue;
} }
return docValue - value; return Short.compare(docValue, value);
} }
} }
@ -572,27 +529,11 @@ public abstract class FieldComparator<T> {
@Override @Override
public int compare(int slot1, int slot2) { public int compare(int slot1, int slot2) {
// TODO: there are sneaky non-branch ways to compute return Integer.compare(values[slot1], values[slot2]);
// -1/+1/0 sign
// Cannot return values[slot1] - values[slot2] because that
// may overflow
final int v1 = values[slot1];
final int v2 = values[slot2];
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
public int compareBottom(int doc) { public int compareBottom(int doc) {
// TODO: there are sneaky non-branch ways to compute
// -1/+1/0 sign
// Cannot return bottom - values[slot2] because that
// may overflow
int v2 = currentReaderValues.get(doc); int v2 = currentReaderValues.get(doc);
// Test for v2 == 0 to save Bits.get method call for // Test for v2 == 0 to save Bits.get method call for
// the common case (doc has value and value is non-zero): // the common case (doc has value and value is non-zero):
@ -600,13 +541,7 @@ public abstract class FieldComparator<T> {
v2 = missingValue; v2 = missingValue;
} }
if (bottom > v2) { return Integer.compare(bottom, v2);
return 1;
} else if (bottom < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
@ -648,13 +583,7 @@ public abstract class FieldComparator<T> {
if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) {
docValue = missingValue; docValue = missingValue;
} }
if (docValue < value) { return Integer.compare(docValue, value);
return -1;
} else if (docValue > value) {
return 1;
} else {
return 0;
}
} }
} }
@ -674,17 +603,7 @@ public abstract class FieldComparator<T> {
@Override @Override
public int compare(int slot1, int slot2) { public int compare(int slot1, int slot2) {
// TODO: there are sneaky non-branch ways to compute return Long.compare(values[slot1], values[slot2]);
// -1/+1/0 sign
final long v1 = values[slot1];
final long v2 = values[slot2];
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
@ -698,13 +617,7 @@ public abstract class FieldComparator<T> {
v2 = missingValue; v2 = missingValue;
} }
if (bottom > v2) { return Long.compare(bottom, v2);
return 1;
} else if (bottom < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
@ -746,13 +659,7 @@ public abstract class FieldComparator<T> {
if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) { if (docsWithField != null && docValue == 0 && !docsWithField.get(doc)) {
docValue = missingValue; docValue = missingValue;
} }
if (docValue < value) { return Long.compare(docValue, value);
return -1;
} else if (docValue > value) {
return 1;
} else {
return 0;
}
} }
} }
@ -773,16 +680,14 @@ public abstract class FieldComparator<T> {
@Override @Override
public int compare(int slot1, int slot2) { public int compare(int slot1, int slot2) {
final float score1 = scores[slot1]; return Float.compare(scores[slot2], scores[slot1]);
final float score2 = scores[slot2];
return score1 > score2 ? -1 : (score1 < score2 ? 1 : 0);
} }
@Override @Override
public int compareBottom(int doc) throws IOException { public int compareBottom(int doc) throws IOException {
float score = scorer.score(); float score = scorer.score();
assert !Float.isNaN(score); assert !Float.isNaN(score);
return bottom > score ? -1 : (bottom < score ? 1 : 0); return Float.compare(score, bottom);
} }
@Override @Override
@ -831,15 +736,7 @@ public abstract class FieldComparator<T> {
final float value = valueObj.floatValue(); final float value = valueObj.floatValue();
float docValue = scorer.score(); float docValue = scorer.score();
assert !Float.isNaN(docValue); assert !Float.isNaN(docValue);
if (docValue < value) { return Float.compare(value, docValue);
// reverse of FloatComparator
return 1;
} else if (docValue > value) {
// reverse of FloatComparator
return -1;
} else {
return 0;
}
} }
} }
@ -893,13 +790,7 @@ public abstract class FieldComparator<T> {
public int compareDocToValue(int doc, Integer valueObj) { public int compareDocToValue(int doc, Integer valueObj) {
final int value = valueObj.intValue(); final int value = valueObj.intValue();
int docValue = docBase + doc; int docValue = docBase + doc;
if (docValue < value) { return Integer.compare(docValue, value);
return -1;
} else if (docValue > value) {
return 1;
} else {
return 0;
}
} }
} }

View File

@ -119,16 +119,7 @@ public class SearcherLifetimeManager implements Closeable {
// Newer searchers are sort before older ones: // Newer searchers are sort before older ones:
@Override @Override
public int compareTo(SearcherTracker other) { public int compareTo(SearcherTracker other) {
// Be defensive: cannot subtract since it could return Double.compare(other.recordTimeSec, recordTimeSec);
// technically overflow long, though, we'd never hit
// that in practice:
if (recordTimeSec < other.recordTimeSec) {
return 1;
} else if (other.recordTimeSec < recordTimeSec) {
return -1;
} else {
return 0;
}
} }
@Override @Override

View File

@ -44,7 +44,7 @@ public class TestSorterTemplate extends LuceneTestCase {
// only compare the last 32 bits // only compare the last 32 bits
final long a = i & 0xFFFFFFFFL; final long a = i & 0xFFFFFFFFL;
final long b = j & 0xFFFFFFFFL; final long b = j & 0xFFFFFFFFL;
return a < b ? -1 : a == b ? 0 : 1; return Long.compare(a, b);
} }
@Override @Override

View File

@ -76,8 +76,7 @@ public final class Passage {
@Override @Override
protected int compare(int i, int j) { protected int compare(int i, int j) {
// TODO: java7 use Integer.compare(starts[i], starts[j]) return Integer.compare(starts[i], starts[j]);
return Long.signum(((long)starts[i]) - starts[j]);
} }
@Override @Override
@ -87,8 +86,7 @@ public final class Passage {
@Override @Override
protected int comparePivot(int j) { protected int comparePivot(int j) {
// TODO: java7 use Integer.compare(pivot, starts[j]) return Integer.compare(pivot, starts[j]);
return Long.signum(((long)pivot) - starts[j]);
} }
int pivot; int pivot;

View File

@ -464,7 +464,7 @@ public final class PostingsHighlighter {
if (off == otherOff) { if (off == otherOff) {
return id - other.id; return id - other.id;
} else { } else {
return Long.signum(((long)off) - otherOff); return Integer.compare(off, otherOff);
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@ -211,13 +211,7 @@ final class TotalTermFreqComparatorSortDescending implements Comparator<TermStat
@Override @Override
public int compare(TermStats a, TermStats b) { public int compare(TermStats a, TermStats b) {
if (a.totalTermFreq < b.totalTermFreq) { return Long.compare(b.totalTermFreq, a.totalTermFreq);
return 1;
} else if (a.totalTermFreq > b.totalTermFreq) {
return -1;
} else {
return 0;
}
} }
} }

View File

@ -139,28 +139,12 @@ public abstract class ValueSource {
@Override @Override
public int compare(int slot1, int slot2) { public int compare(int slot1, int slot2) {
final double v1 = values[slot1]; return Double.compare(values[slot1], values[slot2]);
final double v2 = values[slot2];
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
public int compareBottom(int doc) { public int compareBottom(int doc) {
final double v2 = docVals.doubleVal(doc); return Double.compare(bottom, docVals.doubleVal(doc));
if (bottom > v2) {
return 1;
} else if (bottom < v2) {
return -1;
} else {
return 0;
}
} }
@Override @Override
@ -188,13 +172,7 @@ public abstract class ValueSource {
public int compareDocToValue(int doc, Double valueObj) { public int compareDocToValue(int doc, Double valueObj) {
final double value = valueObj; final double value = valueObj;
final double docValue = docVals.doubleVal(doc); final double docValue = docVals.doubleVal(doc);
if (docValue < value) { return Double.compare(docValue, value);
return -1;
} else if (docValue > value) {
return 1;
} else {
return 0;
}
} }
} }
} }

View File

@ -120,13 +120,7 @@ public class SortedTermFreqIteratorWrapper implements TermFreqIterator {
if (cmp != 0) { if (cmp != 0) {
return cmp; return cmp;
} }
if (leftCost < rightCost) { return Long.compare(leftCost, rightCost);
return -1;
} else if (rightCost < leftCost) {
return 1;
} else {
return 0;
}
} }
}; };