From dafcffcc50a91273f228e4d5c8f5784fef551ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Avard=20Ottestad?= Date: Wed, 11 Dec 2024 10:52:51 +0100 Subject: [PATCH] code cleanup --- .../core/compact/bitmap/Bitmap375Big.java | 72 +++++++++---------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/qendpoint-core/src/main/java/com/the_qa_company/qendpoint/core/compact/bitmap/Bitmap375Big.java b/qendpoint-core/src/main/java/com/the_qa_company/qendpoint/core/compact/bitmap/Bitmap375Big.java index 48800499..01cb9c21 100644 --- a/qendpoint-core/src/main/java/com/the_qa_company/qendpoint/core/compact/bitmap/Bitmap375Big.java +++ b/qendpoint-core/src/main/java/com/the_qa_company/qendpoint/core/compact/bitmap/Bitmap375Big.java @@ -23,6 +23,8 @@ import com.the_qa_company.qendpoint.core.util.io.CloseSuppressPath; import com.the_qa_company.qendpoint.core.util.io.Closer; import com.the_qa_company.qendpoint.core.util.io.IOUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.Closeable; import java.io.IOException; @@ -39,29 +41,31 @@ * @author mario.arias */ public class Bitmap375Big extends Bitmap64Big { - private final boolean oldBinarySearch; - /** - * create disk version bitmap with in memory super index - * - * @param location location - * @param nbits number of bits - * @return bitmap - */ + private static final Logger logger = LoggerFactory.getLogger(Bitmap375Big.class); + + private static final boolean oldBinarySearch; - { + static { // check if the system property "useOldBinarySeearch" is set to true String useOldBinarySearch = System.getProperty("useOldBinarySearch"); if (useOldBinarySearch != null && useOldBinarySearch.equalsIgnoreCase("true")) { - this.oldBinarySearch = true; - System.out.println("Using old binary search"); + oldBinarySearch = true; + logger.debug("Using old binary search"); } else { - System.out.println("Using new binary search"); - this.oldBinarySearch = false; + logger.debug("Using new binary search"); + oldBinarySearch = false; } } + /** + * create disk version bitmap with in memory super index + * + * @param location location + * @param nbits number of bits + * @return bitmap + */ public static Bitmap375Big disk(Path location, long nbits) { return disk(location, nbits, false); } @@ -483,39 +487,33 @@ public static long binarySearchNew(LongArray arr, long val) { long max = arr.getEstimatedLocationUpperBound(val); long mid = arr.getEstimatedLocation(val, min, max); -// System.out.println("Searching for: " + val); - int step = 0; + int i = 0; while (min + 1 < max) { - step++; - // print min, max, mid -// System.out.println("min: " + min + " max: " + max + " mid: " + mid); - - long l = arr.get(mid); - - if (l >= val) { - max = mid; - - if (min + 1 < max && step > 1 && step < 4) { - long l1 = arr.get(min + 1); - if (l1 >= val) { - max = min + 1; - } else { - // is this actually correct? - min = min + 1; - } + // After the first iteration, the value that we are looking for is + // typically very close to the min value. Using linear search for + // the next two iterations improves the chances that we find the + // value faster than with binary search. + if (i == 1 || i == 2) { + long v = arr.get(min + 1); + if (v >= val) { + max = min + 1; + } else { + min = min + 1; } - } else { - min = mid; + long v = arr.get(mid); + if (v >= val) { + max = mid; + } else { + min = mid; + } } mid = (min + max) / 2; + i++; } -// System.out.println("Found after: " + step + " steps"); arr.updateEstimatedValueLocation(val, min); - // prevFound[index] = min; - return min; }