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

27
LINES

< > BotCompany Repo | #1030507 // lastIndexOfRangePredicate

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

Transpiled version (2723L) is out of date.

// finds highest index where predicate is true.
// returns from-1 when predicate is false on whole range.
// Predicate operates on a range and returns true
// iff any value in the range is contained in the set.
static <A> int lastIndexOfRangePredicate(int from default 0, int to, IF2<Int, Int, Bool> pred) {
  // interval empty?
  if (from >= to) ret from-1;
  
  // predicate false on whole range?
  if (!pred.get(from, to)) ret from-1;
  
  while ping (to-from > 1) {
    // predicate is true somewhere in range. split in middle.
    int mid = (from + to) >>> 1;
    
    // check higher half
    if (pred.get(mid, to))
      // predicate is true in higher half. move there.
      from = mid;
    else
      // higher half is clean, move to lower half
      to = mid;
  }

  // we've narrowed things down to at most one element, return index
  ret to-1;
}

Author comment

Began life as a copy of #1030504

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030507
Snippet name: lastIndexOfRangePredicate
Eternal ID of this version: #1030507/9
Text MD5: 01c819d9cdfa1164630eb29cba5dd577
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-03 22:34:23
Source code size: 883 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 198 / 298
Version history: 8 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1030516 - lastIndexOfRangePredicate_IIntIntPred - faster version of lastIndexOfRangePredicate