1 | // finds highest index where predicate is true. |
2 | // returns from-1 when predicate is false on whole range. |
3 | // Predicate operates on a range and returns true |
4 | // iff any value in the range is contained in the set. |
5 | static <A> int lastIndexOfRangePredicate_IIntIntPred(int from default 0, int to, IIntIntPred pred) { |
6 | // interval empty? |
7 | if (from >= to) ret from-1; |
8 | |
9 | // predicate false on whole range? |
10 | if (!pred.get(from, to)) ret from-1; |
11 | |
12 | while ping (to-from > 1) { |
13 | // predicate is true somewhere in range. split in middle. |
14 | int mid = (from + to) >>> 1; |
15 | |
16 | // check higher half |
17 | if (pred.get(mid, to)) |
18 | // predicate is true in higher half. move there. |
19 | from = mid; |
20 | else |
21 | // higher half is clean, move to lower half |
22 | to = mid; |
23 | } |
24 | |
25 | // we've narrowed things down to one element, return index |
26 | ret from; |
27 | } |
Began life as a copy of #1030507
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1030516 |
Snippet name: | lastIndexOfRangePredicate_IIntIntPred - faster version of lastIndexOfRangePredicate |
Eternal ID of this version: | #1030516/3 |
Text MD5: | 3aea6ec3a2c85226d1e53f7273b8d14c |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-01-05 22:20:14 |
Source code size: | 879 bytes / 27 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 213 / 241 |
Version history: | 2 change(s) |
Referenced in: | [show references] |