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

22
LINES

< > BotCompany Repo | #1029231 // generalizedBinarySearch - comparator is IF2<A, B, Int> and there is a key

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

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

static <A, B> int generalizedBinarySearch(L<A> l, B key, IF2<A, B, Int> cmp) {
  ret generalizedBinarySearch(l, 0, l(l), key, cmp);
}

static <A, B> int generalizedBinarySearch(L<A> l, int fromIndex, int toIndex, B key, IF2<A, B, 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, key);
    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 #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: 146 / 225
Version history: 3 change(s)
Referenced in: [show references]