Contains.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2025 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <algorithm>
  5. #include <iterator>
  6. namespace Common
  7. {
  8. struct ContainsFn
  9. {
  10. template <std::input_iterator I, std::sentinel_for<I> S, class T, class Proj = std::identity>
  11. requires std::indirect_binary_predicate < std::ranges::equal_to, std::projected<I, Proj>,
  12. const T* > constexpr bool operator()(I first, S last, const T& value, Proj proj = {}) const
  13. {
  14. return std::ranges::find(std::move(first), last, value, std::move(proj)) != last;
  15. }
  16. template <std::ranges::input_range R, class T, class Proj = std::identity>
  17. requires std::indirect_binary_predicate < std::ranges::equal_to,
  18. std::projected<std::ranges::iterator_t<R>, Proj>,
  19. const T* > constexpr bool operator()(R&& r, const T& value, Proj proj = {}) const
  20. {
  21. return (*this)(std::ranges::begin(r), std::ranges::end(r), value, std::move(proj));
  22. }
  23. };
  24. struct ContainsSubrangeFn
  25. {
  26. template <std::forward_iterator I1, std::sentinel_for<I1> S1, std::forward_iterator I2,
  27. std::sentinel_for<I2> S2, class Pred = std::ranges::equal_to,
  28. class Proj1 = std::identity, class Proj2 = std::identity>
  29. requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
  30. constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
  31. Proj1 proj1 = {}, Proj2 proj2 = {}) const
  32. {
  33. return !std::ranges::search(std::move(first1), std::move(last1), std::move(first2),
  34. std::move(last2), std::move(pred), std::move(proj1),
  35. std::move(proj2))
  36. .empty();
  37. }
  38. template <std::ranges::forward_range R1, std::ranges::forward_range R2,
  39. class Pred = std::ranges::equal_to, class Proj1 = std::identity,
  40. class Proj2 = std::identity>
  41. requires std::indirectly_comparable<std::ranges::iterator_t<R1>, std::ranges::iterator_t<R2>,
  42. Pred, Proj1, Proj2>
  43. constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {},
  44. Proj2 proj2 = {}) const
  45. {
  46. return (*this)(std::ranges::begin(r1), std::ranges::end(r1), std::ranges::begin(r2),
  47. std::ranges::end(r2), std::move(pred), std::move(proj1), std::move(proj2));
  48. }
  49. };
  50. // TODO C++23: Replace with std::ranges::contains.
  51. inline constexpr ContainsFn Contains{};
  52. // TODO C++23: Replace with std::ranges::contains_subrange.
  53. inline constexpr ContainsSubrangeFn ContainsSubrange{};
  54. } // namespace Common