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