@@ -102,6 +102,17 @@ private int lookup(int targetBCI) {
102
102
throw EspressoError .shouldNotReachHere ();
103
103
}
104
104
105
+ /**
106
+ * Lookup the index of the largest element which is smaller or equal to {@code targetBCI}.
107
+ */
108
+ public int lookupBucket (int targetBCI ) {
109
+ int res = slowLookup (targetBCI , 0 , length );
110
+ if (res >= 0 ) {
111
+ return res ;
112
+ }
113
+ return -res - 1 ;
114
+ }
115
+
105
116
public int lookup (int curIndex , int curBCI , int targetBCI ) {
106
117
int res ;
107
118
int start ;
@@ -117,7 +128,7 @@ public int lookup(int curIndex, int curBCI, int targetBCI) {
117
128
if (res >= 0 ) {
118
129
return res ;
119
130
}
120
- return -res - 2 ;
131
+ return -res - 1 ;
121
132
}
122
133
123
134
public int checkNext (int curIndex , int targetBCI ) {
@@ -128,20 +139,20 @@ public int checkNext(int curIndex, int targetBCI) {
128
139
}
129
140
130
141
/**
131
- * inlined binary search. No bounds checks.
142
+ * Inlined binary search. No bounds checks.
132
143
*
133
144
* @see Arrays#binarySearch(int[], int, int, int)
145
+ * @return either the index of the element that is equal to {@code targetBCI} or a negative
146
+ * number {@code -(i + 1)} where i is the index of the largest element which is smaller
147
+ * than {@code targetBCI}.
134
148
*/
135
149
@ ExplodeLoop (kind = ExplodeLoop .LoopExplosionKind .FULL_EXPLODE )
136
150
private int slowLookup (int targetBCI , int start , int end ) {
137
- // Our usage should not see an out of bounds
138
151
int low = start ;
139
152
int high = end - 1 ;
140
-
141
153
while (low <= high ) {
142
154
int mid = (low + high ) >>> 1 ;
143
155
int midVal = bcis [mid ];
144
-
145
156
if (midVal < targetBCI ) {
146
157
low = mid + 1 ;
147
158
} else if (midVal > targetBCI ) {
@@ -150,7 +161,7 @@ private int slowLookup(int targetBCI, int start, int end) {
150
161
return mid ;
151
162
}
152
163
}
153
- return -( low + 1 ); // key not found.
164
+ return -low ;
154
165
}
155
166
156
167
}
0 commit comments