Use Arrays.binarySearch()
This commit is contained in:
parent
612d842931
commit
1804df31a7
|
@ -9,6 +9,7 @@ import java.io.BufferedInputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import dummydomain.yetanothercallblocker.sia.utils.LittleEndianDataInputStream;
|
import dummydomain.yetanothercallblocker.sia.utils.LittleEndianDataInputStream;
|
||||||
|
|
||||||
|
@ -38,21 +39,7 @@ public abstract class AbstractDatabaseDataSlice<T> {
|
||||||
|
|
||||||
protected int indexOf(long number) {
|
protected int indexOf(long number) {
|
||||||
if (numberOfItems > 0) {
|
if (numberOfItems > 0) {
|
||||||
int low = 0;
|
return Arrays.binarySearch(numbers, number);
|
||||||
int high = numberOfItems - 1;
|
|
||||||
|
|
||||||
while (low <= high) {
|
|
||||||
int mid = (high + low) / 2;
|
|
||||||
long num = numbers[mid];
|
|
||||||
|
|
||||||
if (num < number) {
|
|
||||||
low = mid + 1;
|
|
||||||
} else if (num > number) {
|
|
||||||
high = mid - 1;
|
|
||||||
} else {
|
|
||||||
return mid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -154,12 +154,12 @@ public class CommunityDatabaseDataSlice extends AbstractDatabaseDataSlice<Commun
|
||||||
if (newSlice != null) {
|
if (newSlice != null) {
|
||||||
if (this.numberOfItems > 0) {
|
if (this.numberOfItems > 0) {
|
||||||
for (long n : newSlice.numbers) {
|
for (long n : newSlice.numbers) {
|
||||||
if (indexOf(n) == -1) {
|
if (indexOf(n) < 0) {
|
||||||
newNumberOfItems++;
|
newNumberOfItems++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (long n : newSlice.numbersToDelete) {
|
for (long n : newSlice.numbersToDelete) {
|
||||||
if (indexOf(n) == -1) {
|
if (indexOf(n) < 0) {
|
||||||
newNumberOfItems++;
|
newNumberOfItems++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue