equal.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___ALGORITHM_EQUAL_H
  10. #define _LIBCPP___ALGORITHM_EQUAL_H
  11. #include <__algorithm/comp.h>
  12. #include <__algorithm/unwrap_iter.h>
  13. #include <__config>
  14. #include <__functional/identity.h>
  15. #include <__functional/invoke.h>
  16. #include <__iterator/distance.h>
  17. #include <__iterator/iterator_traits.h>
  18. #include <__string/constexpr_c_functions.h>
  19. #include <__type_traits/enable_if.h>
  20. #include <__type_traits/integral_constant.h>
  21. #include <__type_traits/is_constant_evaluated.h>
  22. #include <__type_traits/is_equality_comparable.h>
  23. #include <__type_traits/is_volatile.h>
  24. #include <__type_traits/operation_traits.h>
  25. #include <__utility/move.h>
  26. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  27. # pragma GCC system_header
  28. #endif
  29. _LIBCPP_PUSH_MACROS
  30. #include <__undef_macros>
  31. _LIBCPP_BEGIN_NAMESPACE_STD
  32. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  33. _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __equal_iter_impl(
  34. _InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate& __pred) {
  35. for (; __first1 != __last1; ++__first1, (void)++__first2)
  36. if (!__pred(*__first1, *__first2))
  37. return false;
  38. return true;
  39. }
  40. template <class _Tp,
  41. class _Up,
  42. class _BinaryPredicate,
  43. __enable_if_t<__desugars_to<__equal_tag, _BinaryPredicate, _Tp, _Up>::value && !is_volatile<_Tp>::value &&
  44. !is_volatile<_Up>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,
  45. int> = 0>
  46. _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  47. __equal_iter_impl(_Tp* __first1, _Tp* __last1, _Up* __first2, _BinaryPredicate&) {
  48. return std::__constexpr_memcmp_equal(__first1, __first2, __element_count(__last1 - __first1));
  49. }
  50. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  51. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  52. equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) {
  53. return std::__equal_iter_impl(
  54. std::__unwrap_iter(__first1), std::__unwrap_iter(__last1), std::__unwrap_iter(__first2), __pred);
  55. }
  56. template <class _InputIterator1, class _InputIterator2>
  57. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  58. equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {
  59. return std::equal(__first1, __last1, __first2, __equal_to());
  60. }
  61. #if _LIBCPP_STD_VER >= 14
  62. template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
  63. _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __equal_impl(
  64. _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __comp, _Proj1& __proj1, _Proj2& __proj2) {
  65. while (__first1 != __last1 && __first2 != __last2) {
  66. if (!std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
  67. return false;
  68. ++__first1;
  69. ++__first2;
  70. }
  71. return __first1 == __last1 && __first2 == __last2;
  72. }
  73. template <class _Tp,
  74. class _Up,
  75. class _Pred,
  76. class _Proj1,
  77. class _Proj2,
  78. __enable_if_t<__desugars_to<__equal_tag, _Pred, _Tp, _Up>::value && __is_identity<_Proj1>::value &&
  79. __is_identity<_Proj2>::value && !is_volatile<_Tp>::value && !is_volatile<_Up>::value &&
  80. __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,
  81. int> = 0>
  82. _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  83. __equal_impl(_Tp* __first1, _Tp* __last1, _Up* __first2, _Up*, _Pred&, _Proj1&, _Proj2&) {
  84. return std::__constexpr_memcmp_equal(__first1, __first2, __element_count(__last1 - __first1));
  85. }
  86. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  87. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  88. equal(_InputIterator1 __first1,
  89. _InputIterator1 __last1,
  90. _InputIterator2 __first2,
  91. _InputIterator2 __last2,
  92. _BinaryPredicate __pred) {
  93. if constexpr (__has_random_access_iterator_category<_InputIterator1>::value &&
  94. __has_random_access_iterator_category<_InputIterator2>::value) {
  95. if (std::distance(__first1, __last1) != std::distance(__first2, __last2))
  96. return false;
  97. }
  98. __identity __proj;
  99. return std::__equal_impl(
  100. std::__unwrap_iter(__first1),
  101. std::__unwrap_iter(__last1),
  102. std::__unwrap_iter(__first2),
  103. std::__unwrap_iter(__last2),
  104. __pred,
  105. __proj,
  106. __proj);
  107. }
  108. template <class _InputIterator1, class _InputIterator2>
  109. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  110. equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
  111. return std::equal(__first1, __last1, __first2, __last2, __equal_to());
  112. }
  113. #endif // _LIBCPP_STD_VER >= 14
  114. _LIBCPP_END_NAMESPACE_STD
  115. _LIBCPP_POP_MACROS
  116. #endif // _LIBCPP___ALGORITHM_EQUAL_H