Skip to content

Commit e88b40a

Browse files
committed
faster binary search using Arrays.binarySearch()
1 parent 0a132d4 commit e88b40a

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/main/java/de/tilman_neumann/jml/BinarySearch.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - [email protected]
3+
* Copyright (C) 2018-2025 Tilman Neumann - [email protected]
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -13,6 +13,8 @@
1313
*/
1414
package de.tilman_neumann.jml;
1515

16+
import java.util.Arrays;
17+
1618
import de.tilman_neumann.util.Ensure;
1719

1820
/**
@@ -34,7 +36,7 @@ public class BinarySearch {
3436
* @param x
3537
* @return the insert position
3638
*/
37-
public int getInsertPosition(int[] array, int maxIndex, int x) {
39+
public int getInsertPosition_v1(int[] array, int maxIndex, int x) {
3840
if (maxIndex<=0 || array[maxIndex-1] <= x) return maxIndex;
3941
int left = 0;
4042
int right = maxIndex-1;
@@ -56,6 +58,26 @@ public int getInsertPosition(int[] array, int maxIndex, int x) {
5658
return left;
5759
}
5860

61+
/**
62+
* Find the insert position for x into array given that array is sorted bottom-up.
63+
*
64+
* More precisely:
65+
* If array[maxIndex-1] > x, return the index of the first entry of array[0].. array[maxIndex-1] greater than x.
66+
* If array[maxIndex-1] <= x, return maxIndex.
67+
*
68+
* Faster version using Arrays.binarySearch().
69+
*
70+
* @param array
71+
* @param maxIndex the maximum index to consider, exclusive (may be smaller than the array size)
72+
* @param x
73+
* @return the insert position
74+
*/
75+
public int getInsertPosition/*_v2*/(int[] array, int maxIndex, int x) {
76+
// see @returns in Arrays.binarySearch() javadoc
77+
int i = Arrays.binarySearch(array, 0, maxIndex, x);
78+
return i >= 0 ? i + 1 : -i - 1;
79+
}
80+
5981
/**
6082
* Find the insert position for x into array given that array is sorted bottom-up.
6183
*
@@ -68,7 +90,7 @@ public int getInsertPosition(int[] array, int maxIndex, int x) {
6890
* @param x
6991
* @return the insert position
7092
*/
71-
public int getInsertPosition(byte[] array, int maxIndex, int x) {
93+
public int getInsertPosition_v1(byte[] array, int maxIndex, byte x) {
7294
if (maxIndex<=0 || array[maxIndex-1] <= x) return maxIndex;
7395
int left = 0;
7496
int right = maxIndex-1;
@@ -89,4 +111,24 @@ public int getInsertPosition(byte[] array, int maxIndex, int x) {
89111
}
90112
return left;
91113
}
114+
115+
/**
116+
* Find the insert position for x into array given that array is sorted bottom-up.
117+
*
118+
* More precisely:
119+
* If array[maxIndex-1] > x, return the index of the first entry of array[0].. array[maxIndex-1] greater than x.
120+
* If array[maxIndex-1] <= x, return maxIndex.
121+
*
122+
* Faster version using Arrays.binarySearch().
123+
*
124+
* @param array
125+
* @param maxIndex the maximum index to consider, exclusive (may be smaller than the array size)
126+
* @param x
127+
* @return the insert position
128+
*/
129+
public int getInsertPosition/*_v2*/(byte[] array, int maxIndex, byte x) {
130+
// see @returns in Arrays.binarySearch() javadoc
131+
int i = Arrays.binarySearch(array, 0, maxIndex, x);
132+
return i >= 0 ? i + 1 : -i - 1;
133+
}
92134
}

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve03h.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void initializeForAParameter(int d, BigInteger daParam, SolutionArrays so
201201
int logP = logPMax;
202202
int lastBound = filteredBaseSize;
203203
for (int i=logPBoundCount-1; i>0; i--) {
204-
lastBound = logPBounds[i] = binarySearch.getInsertPosition(logPArray, lastBound, --logP);
204+
lastBound = logPBounds[i] = binarySearch.getInsertPosition(logPArray, lastBound, (byte) --logP);
205205
if (DEBUG) LOG.debug("logPBound[" + i + "] = " + logPBounds[i] + ", logP[" + logPBounds[i] + "] = " + logPArray[logPBounds[i]] + ", logP[" + (logPBounds[i]-1) + "] = " + logPArray[logPBounds[i]-1]);
206206
}
207207
logPBounds[0] = p1Index;

src/main/java/de/tilman_neumann/jml/factor/siqs/sieve/Sieve03hU.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void initializeForAParameter(int d, BigInteger daParam, SolutionArrays so
202202
int logP = logPMax;
203203
int lastBound = filteredBaseSize;
204204
for (int i=logPBoundCount-1; i>0; i--) {
205-
lastBound = logPBounds[i] = binarySearch.getInsertPosition(logPArray, lastBound, --logP);
205+
lastBound = logPBounds[i] = binarySearch.getInsertPosition(logPArray, lastBound, (byte) --logP);
206206
if (DEBUG) LOG.debug("logPBound[" + i + "] = " + logPBounds[i] + ", logP[" + logPBounds[i] + "] = " + logPArray[logPBounds[i]] + ", logP[" + (logPBounds[i]-1) + "] = " + logPArray[logPBounds[i]-1]);
207207
}
208208
logPBounds[0] = p1Index;

0 commit comments

Comments
 (0)