Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

22
LINES

< > BotCompany Repo | #1029232 // generalizedBinarySearch2 - comparator is IF1<A, Int>

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2539L/16K).

1  
static <A> int generalizedBinarySearch2(L<A> l, IF1<A, Int> cmp) {
2  
  ret generalizedBinarySearch2(l, 0, l(l), cmp);
3  
}
4  
5  
static <A> int generalizedBinarySearch2(L<A> l, int fromIndex, int toIndex, IF1<A, 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);
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  
}

Author comment

Began life as a copy of #1029231

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: #1029232
Snippet name: generalizedBinarySearch2 - comparator is IF1<A, Int>
Eternal ID of this version: #1029232/4
Text MD5: 18a2b103c9be8da4f5e5c7f84a843162
Transpilation MD5: e2405813ed837c24c51af7232922db84
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:17
Source code size: 559 bytes / 22 lines
Pitched / IR pitched: No / No
Views / Downloads: 234 / 348
Version history: 3 change(s)
Referenced in: [show references]