remove dup'd code

This commit is contained in:
Mike McCandless 2016-03-01 14:32:08 -05:00
parent 3c27980c4a
commit 6261767b33
6 changed files with 34 additions and 48 deletions

View File

@ -122,32 +122,17 @@ public final class NumericUtils {
}
}
/** Returns positive int if a > b, negative int if a < b and 0 if a == b */
public static int compare(int bytesPerDim, byte[] a, int aIndex, byte[] b, int bIndex) {
assert aIndex >= 0;
assert bIndex >= 0;
int aOffset = aIndex*bytesPerDim;
int bOffset = bIndex*bytesPerDim;
for(int i=0;i<bytesPerDim;i++) {
int cmp = (a[aOffset+i]&0xff) - (b[bOffset+i]&0xff);
if (cmp != 0) {
return cmp;
}
}
return 0;
}
/** Returns true if N-dim rect A contains N-dim rect B */
public static boolean contains(int bytesPerDim,
byte[] minPackedA, byte[] maxPackedA,
byte[] minPackedB, byte[] maxPackedB) {
int dims = minPackedA.length / bytesPerDim;
for(int dim=0;dim<dims;dim++) {
if (compare(bytesPerDim, minPackedA, dim, minPackedB, dim) > 0) {
int offset = dim * bytesPerDim;
if (StringHelper.compare(bytesPerDim, minPackedA, offset, minPackedB, offset) > 0) {
return false;
}
if (compare(bytesPerDim, maxPackedA, dim, maxPackedB, dim) < 0) {
if (StringHelper.compare(bytesPerDim, maxPackedA, offset, maxPackedB, offset) < 0) {
return false;
}
}

View File

@ -25,7 +25,6 @@ import org.apache.lucene.index.PointValues.Relation;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.StringHelper;
/** Handles intersection of an multi-dimensional shape in byte[] space with a block KD-tree previously written with {@link BKDWriter}.
@ -175,7 +174,7 @@ public class BKDReader implements Accountable {
// With only 1D, all values should always be in sorted order
if (lastPackedValue == null) {
lastPackedValue = Arrays.copyOf(packedValue, packedValue.length);
} else if (NumericUtils.compare(bytesPerDim, lastPackedValue, 0, packedValue, 0) > 0) {
} else if (StringHelper.compare(bytesPerDim, lastPackedValue, 0, packedValue, 0) > 0) {
throw new RuntimeException("value=" + new BytesRef(packedValue) + " for docID=" + docID + " dim=0" + " sorts before last value=" + new BytesRef(lastPackedValue));
} else {
System.arraycopy(packedValue, 0, lastPackedValue, 0, bytesPerDim);

View File

@ -574,7 +574,7 @@ public class BKDWriter implements Closeable {
int block = j / writer.valuesPerBlock;
int index = j % writer.valuesPerBlock;
assert index >= 0: "index=" + index + " j=" + j;
int cmp = NumericUtils.compare(bytesPerDim, pivotPackedValue, 0, writer.blocks.get(block), index*numDims+dim);
int cmp = StringHelper.compare(bytesPerDim, pivotPackedValue, 0, writer.blocks.get(block), bytesPerDim*(index*numDims+dim));
if (cmp != 0) {
return cmp;
}
@ -618,7 +618,7 @@ public class BKDWriter implements Closeable {
int dimI = i % writer.valuesPerBlock;
int blockJ = j / writer.valuesPerBlock;
int dimJ = j % writer.valuesPerBlock;
int cmp = NumericUtils.compare(bytesPerDim, writer.blocks.get(blockI), dimI*numDims+dim, writer.blocks.get(blockJ), dimJ*numDims+dim);
int cmp = StringHelper.compare(bytesPerDim, writer.blocks.get(blockI), bytesPerDim*(dimI*numDims+dim), writer.blocks.get(blockJ), bytesPerDim*(dimJ*numDims+dim));
if (cmp != 0) {
return cmp;
}
@ -681,7 +681,7 @@ public class BKDWriter implements Closeable {
final int docIDB = reader.readVInt();
final long ordB = reader.readVLong();
int cmp = NumericUtils.compare(bytesPerDim, scratch1, dim, scratch2, dim);
int cmp = StringHelper.compare(bytesPerDim, scratch1, bytesPerDim*dim, scratch2, bytesPerDim*dim);
if (cmp != 0) {
return cmp;
@ -967,10 +967,11 @@ public class BKDWriter implements Closeable {
/** Called only in assert */
private boolean valueInBounds(byte[] packedValue, byte[] minPackedValue, byte[] maxPackedValue) {
for(int dim=0;dim<numDims;dim++) {
if (NumericUtils.compare(bytesPerDim, packedValue, dim, minPackedValue, dim) < 0) {
int offset = bytesPerDim*dim;
if (StringHelper.compare(bytesPerDim, packedValue, offset, minPackedValue, offset) < 0) {
return false;
}
if (NumericUtils.compare(bytesPerDim, packedValue, dim, maxPackedValue, dim) > 0) {
if (StringHelper.compare(bytesPerDim, packedValue, offset, maxPackedValue, offset) > 0) {
return false;
}
}
@ -984,7 +985,7 @@ public class BKDWriter implements Closeable {
int splitDim = -1;
for(int dim=0;dim<numDims;dim++) {
NumericUtils.subtract(bytesPerDim, dim, maxPackedValue, minPackedValue, scratchDiff);
if (splitDim == -1 || NumericUtils.compare(bytesPerDim, scratchDiff, 0, scratch1, 0) > 0) {
if (splitDim == -1 || StringHelper.compare(bytesPerDim, scratchDiff, 0, scratch1, 0) > 0) {
System.arraycopy(scratchDiff, 0, scratch1, 0, bytesPerDim);
splitDim = dim;
}
@ -1194,7 +1195,7 @@ public class BKDWriter implements Closeable {
// only called from assert
private boolean valueInOrder(long ord, byte[] lastPackedValue, byte[] packedValue) {
if (ord > 0 && NumericUtils.compare(bytesPerDim, lastPackedValue, 0, packedValue, 0) > 0) {
if (ord > 0 && StringHelper.compare(bytesPerDim, lastPackedValue, 0, packedValue, 0) > 0) {
throw new AssertionError("values out of order: last value=" + new BytesRef(lastPackedValue) + " current value=" + new BytesRef(packedValue) + " ord=" + ord);
}
System.arraycopy(packedValue, 0, lastPackedValue, 0, bytesPerDim);

View File

@ -678,7 +678,7 @@ public class TestPointQueries extends LuceneTestCase {
// open-ended on the upper bound
}
if (lower[dim] != null && upper[dim] != null && NumericUtils.compare(bytesPerDim, lower[dim], 0, upper[dim], 0) > 0) {
if (lower[dim] != null && upper[dim] != null && StringHelper.compare(bytesPerDim, lower[dim], 0, upper[dim], 0) > 0) {
byte[] x = lower[dim];
lower[dim] = upper[dim];
upper[dim] = x;
@ -797,7 +797,7 @@ public class TestPointQueries extends LuceneTestCase {
if (lower[dim] == null) {
cmp = 1;
} else {
cmp = NumericUtils.compare(bytesPerDim, value[dim], 0, lower[dim], 0);
cmp = StringHelper.compare(bytesPerDim, value[dim], 0, lower[dim], 0);
}
if (cmp < 0 || (cmp == 0 && includeLower[dim] == false)) {
@ -808,7 +808,7 @@ public class TestPointQueries extends LuceneTestCase {
if (upper[dim] == null) {
cmp = -1;
} else {
cmp = NumericUtils.compare(bytesPerDim, value[dim], 0, upper[dim], 0);
cmp = StringHelper.compare(bytesPerDim, value[dim], 0, upper[dim], 0);
}
if (cmp > 0 || (cmp == 0 && includeUpper[dim] == false)) {

View File

@ -35,6 +35,7 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.TestUtil;
public class TestBKD extends LuceneTestCase {
@ -734,7 +735,7 @@ public class TestBKD extends LuceneTestCase {
random().nextBytes(queryMin[dim]);
queryMax[dim] = new byte[numBytesPerDim];
random().nextBytes(queryMax[dim]);
if (NumericUtils.compare(numBytesPerDim, queryMin[dim], 0, queryMax[dim], 0) > 0) {
if (StringHelper.compare(numBytesPerDim, queryMin[dim], 0, queryMax[dim], 0) > 0) {
byte[] x = queryMin[dim];
queryMin[dim] = queryMax[dim];
queryMax[dim] = x;
@ -753,8 +754,8 @@ public class TestBKD extends LuceneTestCase {
public void visit(int docID, byte[] packedValue) {
//System.out.println("visit check docID=" + docID);
for(int dim=0;dim<numDims;dim++) {
if (NumericUtils.compare(numBytesPerDim, packedValue, dim, queryMin[dim], 0) < 0 ||
NumericUtils.compare(numBytesPerDim, packedValue, dim, queryMax[dim], 0) > 0) {
if (StringHelper.compare(numBytesPerDim, packedValue, dim*numBytesPerDim, queryMin[dim], 0) < 0 ||
StringHelper.compare(numBytesPerDim, packedValue, dim*numBytesPerDim, queryMax[dim], 0) > 0) {
//System.out.println(" no");
return;
}
@ -768,11 +769,11 @@ public class TestBKD extends LuceneTestCase {
public Relation compare(byte[] minPacked, byte[] maxPacked) {
boolean crosses = false;
for(int dim=0;dim<numDims;dim++) {
if (NumericUtils.compare(numBytesPerDim, maxPacked, dim, queryMin[dim], 0) < 0 ||
NumericUtils.compare(numBytesPerDim, minPacked, dim, queryMax[dim], 0) > 0) {
if (StringHelper.compare(numBytesPerDim, maxPacked, dim*numBytesPerDim, queryMin[dim], 0) < 0 ||
StringHelper.compare(numBytesPerDim, minPacked, dim*numBytesPerDim, queryMax[dim], 0) > 0) {
return Relation.CELL_OUTSIDE_QUERY;
} else if (NumericUtils.compare(numBytesPerDim, minPacked, dim, queryMin[dim], 0) < 0 ||
NumericUtils.compare(numBytesPerDim, maxPacked, dim, queryMax[dim], 0) > 0) {
} else if (StringHelper.compare(numBytesPerDim, minPacked, dim*numBytesPerDim, queryMin[dim], 0) < 0 ||
StringHelper.compare(numBytesPerDim, maxPacked, dim*numBytesPerDim, queryMax[dim], 0) > 0) {
crosses = true;
}
}
@ -790,8 +791,8 @@ public class TestBKD extends LuceneTestCase {
boolean matches = true;
for(int dim=0;dim<numDims;dim++) {
byte[] x = docValues[ord][dim];
if (NumericUtils.compare(numBytesPerDim, x, 0, queryMin[dim], 0) < 0 ||
NumericUtils.compare(numBytesPerDim, x, 0, queryMax[dim], 0) > 0) {
if (StringHelper.compare(numBytesPerDim, x, 0, queryMin[dim], 0) < 0 ||
StringHelper.compare(numBytesPerDim, x, 0, queryMax[dim], 0) > 0) {
matches = false;
break;
}

View File

@ -701,7 +701,7 @@ public abstract class BasePointFormatTestCase extends BaseIndexFileFormatTestCas
random().nextBytes(queryMin[dim]);
queryMax[dim] = new byte[numBytesPerDim];
random().nextBytes(queryMax[dim]);
if (NumericUtils.compare(numBytesPerDim, queryMin[dim], 0, queryMax[dim], 0) > 0) {
if (StringHelper.compare(numBytesPerDim, queryMin[dim], 0, queryMax[dim], 0) > 0) {
byte[] x = queryMin[dim];
queryMin[dim] = queryMax[dim];
queryMax[dim] = x;
@ -733,8 +733,8 @@ public abstract class BasePointFormatTestCase extends BaseIndexFileFormatTestCas
//System.out.println("visit check docID=" + docID + " id=" + idValues.get(docID));
for(int dim=0;dim<numDims;dim++) {
//System.out.println(" dim=" + dim + " value=" + new BytesRef(packedValue, dim*numBytesPerDim, numBytesPerDim));
if (NumericUtils.compare(numBytesPerDim, packedValue, dim, queryMin[dim], 0) < 0 ||
NumericUtils.compare(numBytesPerDim, packedValue, dim, queryMax[dim], 0) > 0) {
if (StringHelper.compare(numBytesPerDim, packedValue, dim*numBytesPerDim, queryMin[dim], 0) < 0 ||
StringHelper.compare(numBytesPerDim, packedValue, dim*numBytesPerDim, queryMax[dim], 0) > 0) {
//System.out.println(" no");
return;
}
@ -749,12 +749,12 @@ public abstract class BasePointFormatTestCase extends BaseIndexFileFormatTestCas
boolean crosses = false;
//System.out.println("compare");
for(int dim=0;dim<numDims;dim++) {
if (NumericUtils.compare(numBytesPerDim, maxPacked, dim, queryMin[dim], 0) < 0 ||
NumericUtils.compare(numBytesPerDim, minPacked, dim, queryMax[dim], 0) > 0) {
if (StringHelper.compare(numBytesPerDim, maxPacked, dim*numBytesPerDim, queryMin[dim], 0) < 0 ||
StringHelper.compare(numBytesPerDim, minPacked, dim*numBytesPerDim, queryMax[dim], 0) > 0) {
//System.out.println(" query_outside_cell");
return Relation.CELL_OUTSIDE_QUERY;
} else if (NumericUtils.compare(numBytesPerDim, minPacked, dim, queryMin[dim], 0) < 0 ||
NumericUtils.compare(numBytesPerDim, maxPacked, dim, queryMax[dim], 0) > 0) {
} else if (StringHelper.compare(numBytesPerDim, minPacked, dim*numBytesPerDim, queryMin[dim], 0) < 0 ||
StringHelper.compare(numBytesPerDim, maxPacked, dim*numBytesPerDim, queryMax[dim], 0) > 0) {
crosses = true;
}
}
@ -774,8 +774,8 @@ public abstract class BasePointFormatTestCase extends BaseIndexFileFormatTestCas
boolean matches = true;
for(int dim=0;dim<numDims;dim++) {
byte[] x = docValues[ord][dim];
if (NumericUtils.compare(numBytesPerDim, x, 0, queryMin[dim], 0) < 0 ||
NumericUtils.compare(numBytesPerDim, x, 0, queryMax[dim], 0) > 0) {
if (StringHelper.compare(numBytesPerDim, x, 0, queryMin[dim], 0) < 0 ||
StringHelper.compare(numBytesPerDim, x, 0, queryMax[dim], 0) > 0) {
matches = false;
break;
}