| e8555c89 Andy McCall |
1 |
#ifndef SUBRANGE_BETWEEN_HH |
| 2 |
#define SUBRANGE_BETWEEN_HH |
| 3 |
|
| 4 |
#include <algorithm> |
| 5 |
#include <functional> |
| 6 |
#include <ranges> |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
template<std::ranges::forward_range R, |
| 26 |
typename T, |
| 27 |
typename Comp = std::ranges::less, |
| 28 |
typename Proj = std::identity> |
| 29 |
auto subrange_between(R&& r, const T& l, const T& h, Comp comp = {}, Proj proj = {}) |
| 30 |
{ |
| 31 |
|
| 32 |
auto map_range = [l,h,&comp,&proj](const auto& x) -> int { |
| 33 |
auto val = std::invoke(proj, x); |
| 34 |
if (std::invoke(comp, val, l)) return -1; |
| 35 |
if (!std::invoke(comp, val, h)) return 1; |
| 36 |
return 0; |
| 37 |
}; |
| 38 |
|
| 39 |
auto [first, last] = std::ranges::equal_range(r, 0, std::ranges::less{}, map_range); |
| 40 |
return std::ranges::subrange(first, last); |
| 41 |
} |
| 42 |
|
| 43 |
#endif |