Libraryless. Click here for Pure Java version (2539L/16K).
1 | static <A, B> int generalizedBinarySearch(L<A> l, B key, IF2<A, B, Int> cmp) {
|
2 | ret generalizedBinarySearch(l, 0, l(l), key, cmp); |
3 | } |
4 | |
5 | static <A, B> int generalizedBinarySearch(L<A> l, int fromIndex, int toIndex, B key, IF2<A, B, Int> cmp) {
|
6 | int low = fromIndex; |
7 | int high = toIndex - 1; |
8 | |
9 | while (low <= high) {
|
10 | int mid = (low + high) >>> 1; |
11 | A midVal = l.get(mid); |
12 | |
13 | int c = cmp.get(midVal, key); |
14 | if (c < 0) |
15 | low = mid + 1; |
16 | else if (c > 0) |
17 | high = mid - 1; |
18 | else |
19 | ret mid; // key found |
20 | } |
21 | ret -(low + 1); // key not found. |
22 | } |
Began life as a copy of #1029041
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
| Snippet ID: | #1029231 |
| Snippet name: | generalizedBinarySearch - comparator is IF2<A, B, Int> and there is a key |
| Eternal ID of this version: | #1029231/4 |
| Text MD5: | 52f3fae05c2c1f7adba1af6252f82f4b |
| Transpilation MD5: | 194a8aed4381fb32743aa3424c63c9bc |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2020-11-09 11:33:39 |
| Source code size: | 592 bytes / 22 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 432 / 587 |
| Version history: | 3 change(s) |
| Referenced in: | [show references] |