|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common.util; |
| 21 | + |
| 22 | +/** |
| 23 | + * Performs binary search on an arbitrary data structure. |
| 24 | + * |
| 25 | + * To do a search, create a subclass and implement custom {@link #compare(int)} and {@link #distance(int)} methods. |
| 26 | + * |
| 27 | + * {@link BinarySearcher} knows nothing about the value being searched for or the underlying data structure. |
| 28 | + * These things should be determined by the subclass in its overridden methods. |
| 29 | + * |
| 30 | + * Refer to {@link BigArrays.DoubleBinarySearcher} for an example. |
| 31 | + * |
| 32 | + * NOTE: this class is not thread safe |
| 33 | + */ |
| 34 | +public abstract class BinarySearcher{ |
| 35 | + |
| 36 | + /** |
| 37 | + * @return a negative integer, zero, or a positive integer if the array's value at <code>index</code> is less than, |
| 38 | + * equal to, or greater than the value being searched for. |
| 39 | + */ |
| 40 | + protected abstract int compare(int index); |
| 41 | + |
| 42 | + /** |
| 43 | + * @return the magnitude of the distance between the element at <code>index</code> and the value being searched for. |
| 44 | + * It will usually be <code>Math.abs(array[index] - searchValue)</code>. |
| 45 | + */ |
| 46 | + protected abstract double distance(int index); |
| 47 | + |
| 48 | + /** |
| 49 | + * @return the index who's underlying value is closest to the value being searched for. |
| 50 | + */ |
| 51 | + private int getClosestIndex(int index1, int index2){ |
| 52 | + if(distance(index1) < distance(index2)){ |
| 53 | + return index1; |
| 54 | + } else { |
| 55 | + return index2; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Uses a binary search to determine the index of the element within the index range {from, ... , to} that is |
| 61 | + * closest to the search value. |
| 62 | + * |
| 63 | + * Unlike most binary search implementations, the value being searched for is not an argument to search method. |
| 64 | + * Rather, this value should be stored by the subclass along with the underlying array. |
| 65 | + * |
| 66 | + * @return the index of the closest element. |
| 67 | + * |
| 68 | + * Requires: The underlying array should be sorted. |
| 69 | + **/ |
| 70 | + public int search(int from, int to){ |
| 71 | + while(from < to){ |
| 72 | + int mid = (from + to) >>> 1; |
| 73 | + int compareResult = compare(mid); |
| 74 | + |
| 75 | + if(compareResult == 0){ |
| 76 | + // arr[mid] == value |
| 77 | + return mid; |
| 78 | + } else if(compareResult < 0){ |
| 79 | + // arr[mid] < val |
| 80 | + |
| 81 | + if(mid < to) { |
| 82 | + // Check if val is between (mid, mid + 1) before setting left = mid + 1 |
| 83 | + // (mid < to) ensures that mid + 1 is not out of bounds |
| 84 | + int compareValAfterMid = compare(mid + 1); |
| 85 | + if (compareValAfterMid > 0) { |
| 86 | + return getClosestIndex(mid, mid + 1); |
| 87 | + } |
| 88 | + } else if(mid == to){ |
| 89 | + // val > arr[mid] and there are no more elements above mid, so mid is the closest |
| 90 | + return mid; |
| 91 | + } |
| 92 | + |
| 93 | + from = mid + 1; |
| 94 | + } else{ |
| 95 | + // arr[mid] > val |
| 96 | + |
| 97 | + if(mid > from) { |
| 98 | + // Check if val is between (mid - 1, mid) |
| 99 | + // (mid > from) ensures that mid - 1 is not out of bounds |
| 100 | + int compareValBeforeMid = compare(mid - 1); |
| 101 | + if (compareValBeforeMid < 0) { |
| 102 | + // val is between indices (mid - 1), mid |
| 103 | + return getClosestIndex(mid, mid - 1); |
| 104 | + } |
| 105 | + } else if(mid == 0){ |
| 106 | + // val < arr[mid] and there are no more candidates below mid, so mid is the closest |
| 107 | + return mid; |
| 108 | + } |
| 109 | + |
| 110 | + to = mid - 1; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return from; |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments