File indexing completed on 2026-05-03 08:13:17
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___ALGORITHM_EQUAL_H
0011 #define _LIBCPP___CXX03___ALGORITHM_EQUAL_H
0012
0013 #include <__cxx03/__algorithm/comp.h>
0014 #include <__cxx03/__algorithm/unwrap_iter.h>
0015 #include <__cxx03/__config>
0016 #include <__cxx03/__functional/identity.h>
0017 #include <__cxx03/__functional/invoke.h>
0018 #include <__cxx03/__iterator/distance.h>
0019 #include <__cxx03/__iterator/iterator_traits.h>
0020 #include <__cxx03/__string/constexpr_c_functions.h>
0021 #include <__cxx03/__type_traits/desugars_to.h>
0022 #include <__cxx03/__type_traits/enable_if.h>
0023 #include <__cxx03/__type_traits/is_constant_evaluated.h>
0024 #include <__cxx03/__type_traits/is_equality_comparable.h>
0025 #include <__cxx03/__type_traits/is_volatile.h>
0026 #include <__cxx03/__utility/move.h>
0027
0028 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0029 # pragma GCC system_header
0030 #endif
0031
0032 _LIBCPP_PUSH_MACROS
0033 #include <__cxx03/__undef_macros>
0034
0035 _LIBCPP_BEGIN_NAMESPACE_STD
0036
0037 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
0038 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __equal_iter_impl(
0039 _InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate& __pred) {
0040 for (; __first1 != __last1; ++__first1, (void)++__first2)
0041 if (!__pred(*__first1, *__first2))
0042 return false;
0043 return true;
0044 }
0045
0046 template <class _Tp,
0047 class _Up,
0048 class _BinaryPredicate,
0049 __enable_if_t<__desugars_to_v<__equal_tag, _BinaryPredicate, _Tp, _Up> && !is_volatile<_Tp>::value &&
0050 !is_volatile<_Up>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,
0051 int> = 0>
0052 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
0053 __equal_iter_impl(_Tp* __first1, _Tp* __last1, _Up* __first2, _BinaryPredicate&) {
0054 return std::__constexpr_memcmp_equal(__first1, __first2, __element_count(__last1 - __first1));
0055 }
0056
0057 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
0058 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
0059 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) {
0060 return std::__equal_iter_impl(
0061 std::__unwrap_iter(__first1), std::__unwrap_iter(__last1), std::__unwrap_iter(__first2), __pred);
0062 }
0063
0064 template <class _InputIterator1, class _InputIterator2>
0065 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
0066 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {
0067 return std::equal(__first1, __last1, __first2, __equal_to());
0068 }
0069
0070 #if _LIBCPP_STD_VER >= 14
0071
0072 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
0073 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __equal_impl(
0074 _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __comp, _Proj1& __proj1, _Proj2& __proj2) {
0075 while (__first1 != __last1 && __first2 != __last2) {
0076 if (!std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
0077 return false;
0078 ++__first1;
0079 ++__first2;
0080 }
0081 return __first1 == __last1 && __first2 == __last2;
0082 }
0083
0084 template <class _Tp,
0085 class _Up,
0086 class _Pred,
0087 class _Proj1,
0088 class _Proj2,
0089 __enable_if_t<__desugars_to_v<__equal_tag, _Pred, _Tp, _Up> && __is_identity<_Proj1>::value &&
0090 __is_identity<_Proj2>::value && !is_volatile<_Tp>::value && !is_volatile<_Up>::value &&
0091 __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,
0092 int> = 0>
0093 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
0094 __equal_impl(_Tp* __first1, _Tp* __last1, _Up* __first2, _Up*, _Pred&, _Proj1&, _Proj2&) {
0095 return std::__constexpr_memcmp_equal(__first1, __first2, __element_count(__last1 - __first1));
0096 }
0097
0098 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
0099 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
0100 equal(_InputIterator1 __first1,
0101 _InputIterator1 __last1,
0102 _InputIterator2 __first2,
0103 _InputIterator2 __last2,
0104 _BinaryPredicate __pred) {
0105 if constexpr (__has_random_access_iterator_category<_InputIterator1>::value &&
0106 __has_random_access_iterator_category<_InputIterator2>::value) {
0107 if (std::distance(__first1, __last1) != std::distance(__first2, __last2))
0108 return false;
0109 }
0110 __identity __proj;
0111 return std::__equal_impl(
0112 std::__unwrap_iter(__first1),
0113 std::__unwrap_iter(__last1),
0114 std::__unwrap_iter(__first2),
0115 std::__unwrap_iter(__last2),
0116 __pred,
0117 __proj,
0118 __proj);
0119 }
0120
0121 template <class _InputIterator1, class _InputIterator2>
0122 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
0123 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
0124 return std::equal(__first1, __last1, __first2, __last2, __equal_to());
0125 }
0126
0127 #endif
0128
0129 _LIBCPP_END_NAMESPACE_STD
0130
0131 _LIBCPP_POP_MACROS
0132
0133 #endif