Libraryless. Click here for Pure Java version (2719L/16K).
1 | // finds lowest index where predicate turns true. |
2 | // returns "to" 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 indexOfRangePredicate_IIntIntPred(int from default 0, int to, IIntIntPred pred) { |
6 | // interval empty? |
7 | if (from >= to) ret to; |
8 | |
9 | // predicate false on whole range? |
10 | if (!pred.get(from, to)) ret to; |
11 | |
12 | while ping (to-from > 1) { |
13 | // predicate turns true somewhere in range. split in middle. |
14 | int mid = (from + to) >>> 1; |
15 | |
16 | // check lower half |
17 | if (pred.get(from, mid)) |
18 | // predicate turns true in lower half. move there. |
19 | to = mid; |
20 | else |
21 | // lower half is clean, move to higher half |
22 | from = mid; |
23 | } |
24 | |
25 | // we've narrowed things down to at most one element, return index |
26 | ret from; |
27 | } |
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: | #1030515 |
Snippet name: | indexOfRangePredicate_IIntIntPred - faster version of indexOfRangePredicate |
Eternal ID of this version: | #1030515/1 |
Text MD5: | 6e76e52d80ea6701d1a935ea1e09b9ee |
Transpilation MD5: | 34f72a96efee9ba7201f914dd0de70a9 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-01-04 01:14:20 |
Source code size: | 881 bytes / 27 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 237 / 324 |
Referenced in: | [show references] |