| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <iterator> |
| 8 |
#include <version> |
| 9 |
|
| 10 |
namespace Common |
| 11 |
{ |
| 12 |
#if defined(__cpp_lib_ranges_contains) && __cpp_lib_ranges_contains >= 202202L |
| 13 |
|
| 14 |
|
| 15 |
inline constexpr auto& Contains = std::ranges::contains; |
| 16 |
inline constexpr auto& ContainsSubrange = std::ranges::contains_subrange; |
| 17 |
|
| 18 |
#else |
| 19 |
|
| 20 |
|
| 21 |
struct ContainsFn |
| 22 |
{ |
| 23 |
template <std::input_iterator I, std::sentinel_for<I> S, class T, class Proj = std::identity> |
| 24 |
requires std::indirect_binary_predicate<std::ranges::equal_to, std::projected<I, Proj>, const T*> |
| 25 |
constexpr bool operator()(I first, S last, const T& value, Proj proj = {}) const |
| 26 |
{ |
| 27 |
return std::ranges::find(std::move(first), last, value, std::move(proj)) != last; |
| 28 |
} |
| 29 |
|
| 30 |
template <std::ranges::input_range R, class T, class Proj = std::identity> |
| 31 |
requires std::indirect_binary_predicate< |
| 32 |
std::ranges::equal_to, std::projected<std::ranges::iterator_t<R>, Proj>, const T*> |
| 33 |
constexpr bool operator()(R&& r, const T& value, Proj proj = {}) const |
| 34 |
{ |
| 35 |
return (*this)(std::ranges::begin(r), std::ranges::end(r), value, std::move(proj)); |
| 36 |
} |
| 37 |
}; |
| 38 |
|
| 39 |
struct ContainsSubrangeFn |
| 40 |
{ |
| 41 |
template <std::forward_iterator I1, std::sentinel_for<I1> S1, std::forward_iterator I2, |
| 42 |
std::sentinel_for<I2> S2, class Pred = std::ranges::equal_to, |
| 43 |
class Proj1 = std::identity, class Proj2 = std::identity> |
| 44 |
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2> |
| 45 |
constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, |
| 46 |
Proj1 proj1 = {}, Proj2 proj2 = {}) const |
| 47 |
{ |
| 48 |
return !std::ranges::search(std::move(first1), std::move(last1), std::move(first2), |
| 49 |
std::move(last2), std::move(pred), std::move(proj1), |
| 50 |
std::move(proj2)) |
| 51 |
.empty(); |
| 52 |
} |
| 53 |
|
| 54 |
template <std::ranges::forward_range R1, std::ranges::forward_range R2, |
| 55 |
class Pred = std::ranges::equal_to, class Proj1 = std::identity, |
| 56 |
class Proj2 = std::identity> |
| 57 |
requires std::indirectly_comparable<std::ranges::iterator_t<R1>, std::ranges::iterator_t<R2>, |
| 58 |
Pred, Proj1, Proj2> |
| 59 |
constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, |
| 60 |
Proj2 proj2 = {}) const |
| 61 |
{ |
| 62 |
return (*this)(std::ranges::begin(r1), std::ranges::end(r1), std::ranges::begin(r2), |
| 63 |
std::ranges::end(r2), std::move(pred), std::move(proj1), std::move(proj2)); |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
inline constexpr ContainsFn Contains{}; |
| 68 |
inline constexpr ContainsSubrangeFn ContainsSubrange{}; |
| 69 |
|
| 70 |
#endif |
| 71 |
} |