// finds lowest index where predicate turns true // returns "to" when predicate is false on whole range // predicate operates on a range and returns true // any value in range is contained static int indexOfRangePredicate(int from default 0, int to, IF2 pred) { // interval empty? if (from >= to) ret to; // predicate false on whole range? if (pred.get(from, to)) ret to; do ping { // predicate turns true somewhere in range. split in middle. // + 1 ensures we don't run into an endless loop int mid = (from + to + 1) >>> 1; // check lower half if (from < mid && pred.get(from, mid)) { // predicate turns true in lower half. move there, continue. to = mid; continue; } // lower half is clean, move to higher half from = mid; } while (from < to); // we're on a single value, return index after it ret to; }