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

27
LINES

< > BotCompany Repo | #1029422 // intArrayBinarySearchWithGeneralizedComparator

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

Transpiled version (52L) is out of date.

1  
static int intArrayBinarySearchWithGeneralizedComparator(int[] a, IF1_IntToInt comparator) {
2  
  ret intArrayBinarySearchWithGeneralizedComparator(a, 0, a.length, comparator);
3  
}
4  
5  
static int intArrayBinarySearchWithGeneralizedComparator(int[] a, int fromIndex, int toIndex, IF1_IntToInt comparator) {
6  
  int low = fromIndex;
7  
  int high = toIndex - 1;
8  
9  
  while (low <= high) {
10  
    int mid = (low + high) >>> 1;
11  
    int midVal = a[mid];
12  
13  
    int cmp = comparator.get(midVal);
14  
    
15  
    ifdef intArrayBinarySearchWithGeneralizedComparator_debug
16  
      printVars_str(+low, +high, +mid, +midVal, +cmp);
17  
    endifdef
18  
    
19  
    if (cmp < 0)
20  
        low = mid + 1;
21  
    else if (cmp > 0)
22  
        high = mid - 1;
23  
    else
24  
        ret mid; // key found
25  
  }
26  
  ret -(low + 1);  // key not found.
27  
}

Author comment

Began life as a copy of #1029419

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: #1029422
Snippet name: intArrayBinarySearchWithGeneralizedComparator
Eternal ID of this version: #1029422/4
Text MD5: 5616be11bc3884d64bc23e1d7b9c1d67
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-05 22:17:13
Source code size: 805 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 202 / 304
Version history: 3 change(s)
Referenced in: [show references]