ranges_equal.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <__algorithm/equal.h>
  11. #include <__algorithm/unwrap_range.h>
  12. #include <__config>
  13. #include <__functional/identity.h>
  14. #include <__functional/invoke.h>
  15. #include <__functional/ranges_operations.h>
  16. #include <__iterator/concepts.h>
  17. #include <__iterator/distance.h>
  18. #include <__iterator/indirectly_comparable.h>
  19. #include <__ranges/access.h>
  20. #include <__ranges/concepts.h>
  21. #include <__utility/move.h>
  22. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  23. # pragma GCC system_header
  24. #endif
  25. _LIBCPP_PUSH_MACROS
  26. #include <__undef_macros>
  27. #if _LIBCPP_STD_VER >= 20
  28. _LIBCPP_BEGIN_NAMESPACE_STD
  29. namespace ranges {
  30. namespace __equal {
  31. struct __fn {
  32. template <input_iterator _Iter1,
  33. sentinel_for<_Iter1> _Sent1,
  34. input_iterator _Iter2,
  35. sentinel_for<_Iter2> _Sent2,
  36. class _Pred = ranges::equal_to,
  37. class _Proj1 = identity,
  38. class _Proj2 = identity>
  39. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  40. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
  41. _Iter1 __first1,
  42. _Sent1 __last1,
  43. _Iter2 __first2,
  44. _Sent2 __last2,
  45. _Pred __pred = {},
  46. _Proj1 __proj1 = {},
  47. _Proj2 __proj2 = {}) const {
  48. if constexpr (sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2>) {
  49. if (__last1 - __first1 != __last2 - __first2)
  50. return false;
  51. }
  52. auto __unwrapped1 = std::__unwrap_range(std::move(__first1), std::move(__last1));
  53. auto __unwrapped2 = std::__unwrap_range(std::move(__first2), std::move(__last2));
  54. return std::__equal_impl(
  55. std::move(__unwrapped1.first),
  56. std::move(__unwrapped1.second),
  57. std::move(__unwrapped2.first),
  58. std::move(__unwrapped2.second),
  59. __pred,
  60. __proj1,
  61. __proj2);
  62. }
  63. template <input_range _Range1,
  64. input_range _Range2,
  65. class _Pred = ranges::equal_to,
  66. class _Proj1 = identity,
  67. class _Proj2 = identity>
  68. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
  69. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
  70. _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
  71. if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
  72. if (ranges::distance(__range1) != ranges::distance(__range2))
  73. return false;
  74. }
  75. auto __unwrapped1 = std::__unwrap_range(ranges::begin(__range1), ranges::end(__range1));
  76. auto __unwrapped2 = std::__unwrap_range(ranges::begin(__range2), ranges::end(__range2));
  77. return std::__equal_impl(
  78. std::move(__unwrapped1.first),
  79. std::move(__unwrapped1.second),
  80. std::move(__unwrapped2.first),
  81. std::move(__unwrapped2.second),
  82. __pred,
  83. __proj1,
  84. __proj2);
  85. return false;
  86. }
  87. };
  88. } // namespace __equal
  89. inline namespace __cpo {
  90. inline constexpr auto equal = __equal::__fn{};
  91. } // namespace __cpo
  92. } // namespace ranges
  93. _LIBCPP_END_NAMESPACE_STD
  94. #endif // _LIBCPP_STD_VER >= 20
  95. _LIBCPP_POP_MACROS
  96. #endif // _LIBCPP___ALGORITHM_RANGES_EQUAL_H