xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/equal.h (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
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 
10 #ifndef _LIBCPP___ALGORITHM_EQUAL_H
11 #define _LIBCPP___ALGORITHM_EQUAL_H
12 
13 #include <__algorithm/comp.h>
14 #include <__algorithm/unwrap_iter.h>
15 #include <__config>
16 #include <__functional/identity.h>
17 #include <__functional/invoke.h>
18 #include <__iterator/distance.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__string/constexpr_c_functions.h>
21 #include <__type_traits/enable_if.h>
22 #include <__type_traits/integral_constant.h>
23 #include <__type_traits/is_constant_evaluated.h>
24 #include <__type_traits/is_equality_comparable.h>
25 #include <__type_traits/is_volatile.h>
26 #include <__type_traits/operation_traits.h>
27 #include <__utility/move.h>
28 
29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30 #  pragma GCC system_header
31 #endif
32 
33 _LIBCPP_BEGIN_NAMESPACE_STD
34 
35 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
36 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __equal_iter_impl(
37     _InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate& __pred) {
38   for (; __first1 != __last1; ++__first1, (void)++__first2)
39     if (!__pred(*__first1, *__first2))
40       return false;
41   return true;
42 }
43 
44 template <class _Tp,
45           class _Up,
46           class _BinaryPredicate,
47           __enable_if_t<__desugars_to<__equal_tag, _BinaryPredicate, _Tp, _Up>::value && !is_volatile<_Tp>::value &&
48                             !is_volatile<_Up>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,
49                         int> = 0>
50 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
51 __equal_iter_impl(_Tp* __first1, _Tp* __last1, _Up* __first2, _BinaryPredicate&) {
52   return std::__constexpr_memcmp_equal(__first1, __first2, __element_count(__last1 - __first1));
53 }
54 
55 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
56 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
57 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) {
58   return std::__equal_iter_impl(
59       std::__unwrap_iter(__first1), std::__unwrap_iter(__last1), std::__unwrap_iter(__first2), __pred);
60 }
61 
62 template <class _InputIterator1, class _InputIterator2>
63 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
64 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {
65   return std::equal(__first1, __last1, __first2, __equal_to());
66 }
67 
68 #if _LIBCPP_STD_VER >= 14
69 template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
70 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
71 __equal(_InputIterator1 __first1,
72         _InputIterator1 __last1,
73         _InputIterator2 __first2,
74         _InputIterator2 __last2,
75         _BinaryPredicate __pred,
76         input_iterator_tag,
77         input_iterator_tag) {
78   for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void)++__first2)
79     if (!__pred(*__first1, *__first2))
80       return false;
81   return __first1 == __last1 && __first2 == __last2;
82 }
83 
84 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
85 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __equal_impl(
86     _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __comp, _Proj1& __proj1, _Proj2& __proj2) {
87   while (__first1 != __last1 && __first2 != __last2) {
88     if (!std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
89       return false;
90     ++__first1;
91     ++__first2;
92   }
93   return __first1 == __last1 && __first2 == __last2;
94 }
95 
96 template <class _Tp,
97           class _Up,
98           class _Pred,
99           class _Proj1,
100           class _Proj2,
101           __enable_if_t<__desugars_to<__equal_tag, _Pred, _Tp, _Up>::value && __is_identity<_Proj1>::value &&
102                             __is_identity<_Proj2>::value && !is_volatile<_Tp>::value && !is_volatile<_Up>::value &&
103                             __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,
104                         int> = 0>
105 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
106 __equal_impl(_Tp* __first1, _Tp* __last1, _Up* __first2, _Up*, _Pred&, _Proj1&, _Proj2&) {
107   return std::__constexpr_memcmp_equal(__first1, __first2, __element_count(__last1 - __first1));
108 }
109 
110 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
111 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
112 __equal(_RandomAccessIterator1 __first1,
113         _RandomAccessIterator1 __last1,
114         _RandomAccessIterator2 __first2,
115         _RandomAccessIterator2 __last2,
116         _BinaryPredicate __pred,
117         random_access_iterator_tag,
118         random_access_iterator_tag) {
119   if (std::distance(__first1, __last1) != std::distance(__first2, __last2))
120     return false;
121   __identity __proj;
122   return std::__equal_impl(
123       std::__unwrap_iter(__first1),
124       std::__unwrap_iter(__last1),
125       std::__unwrap_iter(__first2),
126       std::__unwrap_iter(__last2),
127       __pred,
128       __proj,
129       __proj);
130 }
131 
132 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
133 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
134 equal(_InputIterator1 __first1,
135       _InputIterator1 __last1,
136       _InputIterator2 __first2,
137       _InputIterator2 __last2,
138       _BinaryPredicate __pred) {
139   return std::__equal<_BinaryPredicate&>(
140       __first1,
141       __last1,
142       __first2,
143       __last2,
144       __pred,
145       typename iterator_traits<_InputIterator1>::iterator_category(),
146       typename iterator_traits<_InputIterator2>::iterator_category());
147 }
148 
149 template <class _InputIterator1, class _InputIterator2>
150 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
151 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
152   return std::__equal(
153       __first1,
154       __last1,
155       __first2,
156       __last2,
157       __equal_to(),
158       typename iterator_traits<_InputIterator1>::iterator_category(),
159       typename iterator_traits<_InputIterator2>::iterator_category());
160 }
161 #endif
162 
163 _LIBCPP_END_NAMESPACE_STD
164 
165 #endif // _LIBCPP___ALGORITHM_EQUAL_H
166