Binary search for keys in indexes. @param arr array of byte arrays to search for @param key the key you want to find @param offset the offset in the key you want to find @param length the length of the key @param comparator a comparator to compare. @retu
(byte[][] arr, byte[] key, int offset,
int length, RawComparator<byte[]> comparator)
| 1340 | * @return index of key |
| 1341 | */ |
| 1342 | public static int binarySearch(byte[][] arr, byte[] key, int offset, |
| 1343 | int length, RawComparator<byte[]> comparator) { |
| 1344 | int low = 0; |
| 1345 | int high = arr.length - 1; |
| 1346 | |
| 1347 | while (low <= high) { |
| 1348 | int mid = (low + high) >>> 1; |
| 1349 | // we have to compare in this order, because the comparator order |
| 1350 | // has special logic when the 'left side' is a special key. |
| 1351 | int cmp = comparator.compare(key, offset, length, arr[mid], 0, |
| 1352 | arr[mid].length); |
| 1353 | // key lives above the midpoint |
| 1354 | if (cmp > 0) |
| 1355 | low = mid + 1; |
| 1356 | // key lives below the midpoint |
| 1357 | else if (cmp < 0) |
| 1358 | high = mid - 1; |
| 1359 | // BAM. how often does this really happen? |
| 1360 | else |
| 1361 | return mid; |
| 1362 | } |
| 1363 | return -(low + 1); |
| 1364 | } |
| 1365 | |
| 1366 | /** |
| 1367 | * Bytewise binary increment/deincrement of long contained in byte array on |