ranges_equal.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #if _LIBCPP_STD_VER >= 20
  26. _LIBCPP_BEGIN_NAMESPACE_STD
  27. namespace ranges {
  28. namespace __equal {
  29. struct __fn {
  30. template <input_iterator _Iter1,
  31. sentinel_for<_Iter1> _Sent1,
  32. input_iterator _Iter2,
  33. sentinel_for<_Iter2> _Sent2,
  34. class _Pred = ranges::equal_to,
  35. class _Proj1 = identity,
  36. class _Proj2 = identity>
  37. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  38. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
  39. _Iter1 __first1,
  40. _Sent1 __last1,
  41. _Iter2 __first2,
  42. _Sent2 __last2,
  43. _Pred __pred = {},
  44. _Proj1 __proj1 = {},
  45. _Proj2 __proj2 = {}) const {
  46. if constexpr (sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2>) {
  47. if (__last1 - __first1 != __last2 - __first2)
  48. return false;
  49. }
  50. auto __unwrapped1 = std::__unwrap_range(std::move(__first1), std::move(__last1));
  51. auto __unwrapped2 = std::__unwrap_range(std::move(__first2), std::move(__last2));
  52. return std::__equal_impl(
  53. std::move(__unwrapped1.first),
  54. std::move(__unwrapped1.second),
  55. std::move(__unwrapped2.first),
  56. std::move(__unwrapped2.second),
  57. __pred,
  58. __proj1,
  59. __proj2);
  60. }
  61. template <input_range _Range1,
  62. input_range _Range2,
  63. class _Pred = ranges::equal_to,
  64. class _Proj1 = identity,
  65. class _Proj2 = identity>
  66. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
  67. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
  68. _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
  69. if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
  70. if (ranges::distance(__range1) != ranges::distance(__range2))
  71. return false;
  72. }
  73. auto __unwrapped1 = std::__unwrap_range(ranges::begin(__range1), ranges::end(__range1));
  74. auto __unwrapped2 = std::__unwrap_range(ranges::begin(__range2), ranges::end(__range2));
  75. return std::__equal_impl(
  76. std::move(__unwrapped1.first),
  77. std::move(__unwrapped1.second),
  78. std::move(__unwrapped2.first),
  79. std::move(__unwrapped2.second),
  80. __pred,
  81. __proj1,
  82. __proj2);
  83. return false;
  84. }
  85. };
  86. } // namespace __equal
  87. inline namespace __cpo {
  88. inline constexpr auto equal = __equal::__fn{};
  89. } // namespace __cpo
  90. } // namespace ranges
  91. _LIBCPP_END_NAMESPACE_STD
  92. #endif // _LIBCPP_STD_VER >= 20
  93. #endif // _LIBCPP___ALGORITHM_RANGES_EQUAL_H