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).

static <A> int generalizedBinarySearch2(L<A> l, IF1<A, Int> cmp) {
  ret generalizedBinarySearch2(l, 0, l(l), cmp);
}

static <A> int generalizedBinarySearch2(L<A> l, int fromIndex, int toIndex, IF1<A, Int> cmp) {
  int low = fromIndex;
  int high = toIndex - 1;

  while (low <= high) {
    int mid = (low + high) >>> 1;
    A midVal = l.get(mid);

    int c = cmp.get(midVal);
    if (c < 0)
      low = mid + 1;
    else if (c > 0)
      high = mid - 1;
    else
      ret mid; // key found
  }
  ret -(low + 1);  // key not found.
}

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: 232 / 345
Version history: 3 change(s)
Referenced in: [show references]