ranges_equal.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ALGORITHM_RANGES_EQUAL_H
  9. #define _LIBCPP___ALGORITHM_RANGES_EQUAL_H
  10. #include <__config>
  11. #include <__functional/identity.h>
  12. #include <__functional/invoke.h>
  13. #include <__functional/ranges_operations.h>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/distance.h>
  16. #include <__iterator/indirectly_comparable.h>
  17. #include <__ranges/access.h>
  18. #include <__ranges/concepts.h>
  19. #include <__utility/move.h>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. namespace ranges {
  26. namespace __equal {
  27. struct __fn {
  28. private:
  29. template <class _Iter1, class _Sent1,
  30. class _Iter2, class _Sent2,
  31. class _Pred,
  32. class _Proj1,
  33. class _Proj2>
  34. _LIBCPP_HIDE_FROM_ABI constexpr static
  35. bool __equal_impl(_Iter1 __first1, _Sent1 __last1,
  36. _Iter2 __first2, _Sent2 __last2,
  37. _Pred& __pred,
  38. _Proj1& __proj1,
  39. _Proj2& __proj2) {
  40. while (__first1 != __last1 && __first2 != __last2) {
  41. if (!std::invoke(__pred, std::invoke(__proj1, *__first1), std::invoke(__proj2, *__first2)))
  42. return false;
  43. ++__first1;
  44. ++__first2;
  45. }
  46. return __first1 == __last1 && __first2 == __last2;
  47. }
  48. public:
  49. template <input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  50. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  51. class _Pred = ranges::equal_to,
  52. class _Proj1 = identity,
  53. class _Proj2 = identity>
  54. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  55. _LIBCPP_HIDE_FROM_ABI constexpr
  56. bool operator()(_Iter1 __first1, _Sent1 __last1,
  57. _Iter2 __first2, _Sent2 __last2,
  58. _Pred __pred = {},
  59. _Proj1 __proj1 = {},
  60. _Proj2 __proj2 = {}) const {
  61. if constexpr (sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2>) {
  62. if (__last1 - __first1 != __last2 - __first2)
  63. return false;
  64. }
  65. return __equal_impl(std::move(__first1), std::move(__last1),
  66. std::move(__first2), std::move(__last2),
  67. __pred,
  68. __proj1,
  69. __proj2);
  70. }
  71. template <input_range _Range1,
  72. input_range _Range2,
  73. class _Pred = ranges::equal_to,
  74. class _Proj1 = identity,
  75. class _Proj2 = identity>
  76. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
  77. _LIBCPP_HIDE_FROM_ABI constexpr
  78. bool operator()(_Range1&& __range1,
  79. _Range2&& __range2,
  80. _Pred __pred = {},
  81. _Proj1 __proj1 = {},
  82. _Proj2 __proj2 = {}) const {
  83. if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
  84. if (ranges::distance(__range1) != ranges::distance(__range2))
  85. return false;
  86. }
  87. return __equal_impl(ranges::begin(__range1), ranges::end(__range1),
  88. ranges::begin(__range2), ranges::end(__range2),
  89. __pred,
  90. __proj1,
  91. __proj2);
  92. return false;
  93. }
  94. };
  95. } // namespace __equal
  96. inline namespace __cpo {
  97. inline constexpr auto equal = __equal::__fn{};
  98. } // namespace __cpo
  99. } // namespace ranges
  100. _LIBCPP_END_NAMESPACE_STD
  101. #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  102. #endif // _LIBCPP___ALGORITHM_RANGES_EQUAL_H