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>() {
@Override
public int compare(SegmentInfoPerCommit si1, SegmentInfoPerCommit si2) {
final long cmp = si1.getBufferedDeletesGen() - si2.getBufferedDeletesGen();
if (cmp > 0) {
return 1;
} else if (cmp < 0) {
return -1;
} else {
return 0;
}
return Long.compare(si1.getBufferedDeletesGen(), si2.getBufferedDeletesGen());
}
};

View File

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

View File

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

View File

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

View File

@ -119,16 +119,7 @@ public class SearcherLifetimeManager implements Closeable {
// Newer searchers are sort before older ones:
@Override
public int compareTo(SearcherTracker other) {
// Be defensive: cannot subtract since it could
// 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;
}
return Double.compare(other.recordTimeSec, recordTimeSec);
}
@Override

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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