| 77 | } |
| 78 | |
| 79 | public int binarySearch(double value, int scanDir) { |
| 80 | |
| 81 | // this is the same algorithm as implemented in C (util.h) |
| 82 | // template<class T, class V> |
| 83 | // inline int64_t binary_search(T *data, V value, int64_t low, int64_t high, int32_t scan_dir) |
| 84 | // please ensure these implementations are in sync |
| 85 | |
| 86 | int low = 0; |
| 87 | int high = pos - 1; |
| 88 | while (high - low > 65) { |
| 89 | final int mid = (low + high) >>> 1; |
| 90 | final double midVal = data[mid]; |
| 91 | int cmp = Numbers.compare(midVal, value); |
| 92 | |
| 93 | if (cmp < 0) { |
| 94 | low = mid; |
| 95 | } else if (cmp > 0) { |
| 96 | high = mid - 1; |
| 97 | } else { |
| 98 | // In case of multiple equal values, find the first |
| 99 | return scanDir == Vect.BIN_SEARCH_SCAN_UP ? |
| 100 | scrollUp(mid, midVal) : |
| 101 | scrollDown(mid, high, midVal); |
| 102 | } |
| 103 | } |
| 104 | return scanDir == Vect.BIN_SEARCH_SCAN_UP ? |
| 105 | scanUp(value, low, high + 1) : |
| 106 | scanDown(value, low, high + 1); |
| 107 | } |
| 108 | |
| 109 | public void checkCapacity(int capacity) { |
| 110 | if (capacity < 0) { |