| 2606 | |
| 2607 | template <typename Vec, typename Pred> |
| 2608 | std::pair<typename Vec::const_iterator, |
| 2609 | typename Vec::const_iterator> |
| 2610 | longest_wrapping_true_run(const Vec& v, Pred pred) { |
| 2611 | using It = typename Vec::const_iterator; |
| 2612 | |
| 2613 | const auto n = v.size(); |
| 2614 | if (n == 0) { |
| 2615 | return {v.end(), v.end()}; |
| 2616 | } |
| 2617 | |
| 2618 | // Find best non-wrapping run |
| 2619 | std::size_t best_len = 0; |
| 2620 | std::size_t best_start = 0; |
| 2621 | |
| 2622 | std::size_t curr_len = 0; |
| 2623 | std::size_t curr_start = 0; |
| 2624 | |
| 2625 | for (std::size_t i = 0; i < n; ++i) { |
| 2626 | if (pred(v[i])) { |
| 2627 | if (curr_len == 0) { |
| 2628 | curr_start = i; |
| 2629 | } |
| 2630 | ++curr_len; |
| 2631 | if (curr_len > best_len) { |
| 2632 | best_len = curr_len; |
| 2633 | best_start = curr_start; |
| 2634 | } |
| 2635 | } else { |
| 2636 | curr_len = 0; |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | // Count leading true |
| 2641 | std::size_t leading = 0; |
| 2642 | while (leading < n && pred(v[leading])) { |
| 2643 | ++leading; |
| 2644 | } |
| 2645 | |
| 2646 | // All true |
| 2647 | if (leading == n) { |
| 2648 | return {v.begin(), v.end()}; |
| 2649 | } |
| 2650 | |
| 2651 | // Count trailing true |
| 2652 | std::size_t trailing = 0; |
| 2653 | while (trailing < n && pred(v[n - 1 - trailing])) { |
| 2654 | ++trailing; |
| 2655 | } |
| 2656 | |
| 2657 | // Wrapped run = [n - trailing, n) + [0, leading) |
| 2658 | const std::size_t wrapped_len = leading + trailing; |
| 2659 | |
| 2660 | if (wrapped_len > best_len) { |
| 2661 | It first = v.begin() + static_cast<std::ptrdiff_t>(n - trailing); |
| 2662 | It last = v.begin() + static_cast<std::ptrdiff_t>(leading); |
| 2663 | return {first, last}; |
| 2664 | } |
| 2665 |
no test coverage detected