| 79 | |
| 80 | template <class Iterator, class Predicate, class Compare> |
| 81 | Iterator min_element_if(Iterator first, Iterator last, Predicate pred, Compare comp) |
| 82 | { |
| 83 | auto it = std::min_element(first, last, [&](const auto& a, const auto& b) { |
| 84 | // Check if elements are valid |
| 85 | bool a_valid = pred(a); |
| 86 | bool b_valid = pred(b); |
| 87 | |
| 88 | // If neither is valid, prefer a (doesn't matter) |
| 89 | if(not a_valid and not b_valid) |
| 90 | return false; |
| 91 | |
| 92 | // If only b is valid, it should be selected |
| 93 | if(not a_valid) |
| 94 | return false; |
| 95 | |
| 96 | // If only a is valid, it should be selected |
| 97 | if(not b_valid) |
| 98 | return true; |
| 99 | |
| 100 | // Both are valid, select the smaller one using comparator |
| 101 | return comp(a, b); |
| 102 | }); |
| 103 | if(it != last and pred(*it)) |
| 104 | return it; |
| 105 | return last; |
| 106 | } |
| 107 | |
| 108 | template <class Iterator, class Output, class Predicate> |
| 109 | void group_by(Iterator start, Iterator last, Output out, Predicate pred) |
no test coverage detected