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.

static int intArrayBinarySearchWithGeneralizedComparator(int[] a, IF1_IntToInt comparator) {
  ret intArrayBinarySearchWithGeneralizedComparator(a, 0, a.length, comparator);
}

static int intArrayBinarySearchWithGeneralizedComparator(int[] a, int fromIndex, int toIndex, IF1_IntToInt comparator) {
  int low = fromIndex;
  int high = toIndex - 1;

  while (low <= high) {
    int mid = (low + high) >>> 1;
    int midVal = a[mid];

    int cmp = comparator.get(midVal);
    
    ifdef intArrayBinarySearchWithGeneralizedComparator_debug
      printVars_str(+low, +high, +mid, +midVal, +cmp);
    endifdef
    
    if (cmp < 0)
        low = mid + 1;
    else if (cmp > 0)
        high = mid - 1;
    else
        ret mid; // key found
  }
  ret -(low + 1);  // key not found.
}

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: 201 / 303
Version history: 3 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)