xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/equal.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 <__config>
15 #include <__iterator/distance.h>
16 #include <__iterator/iterator_traits.h>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
25 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
26 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) {
27   for (; __first1 != __last1; ++__first1, (void)++__first2)
28     if (!__pred(*__first1, *__first2))
29       return false;
30   return true;
31 }
32 
33 template <class _InputIterator1, class _InputIterator2>
34 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
35 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {
36   return std::equal(__first1, __last1, __first2, __equal_to());
37 }
38 
39 #if _LIBCPP_STD_VER > 11
40 template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
41 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
42 __equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
43         _BinaryPredicate __pred, input_iterator_tag, input_iterator_tag) {
44   for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void)++__first2)
45     if (!__pred(*__first1, *__first2))
46       return false;
47   return __first1 == __last1 && __first2 == __last2;
48 }
49 
50 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
51 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
52 __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
53         _RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag,
54         random_access_iterator_tag) {
55   if (_VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
56     return false;
57   return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
58                       _BinaryPredicate&>(__first1, __last1, __first2, __pred);
59 }
60 
61 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
62 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
63 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
64       _BinaryPredicate __pred) {
65   return _VSTD::__equal<_BinaryPredicate&>(
66       __first1, __last1, __first2, __last2, __pred, typename iterator_traits<_InputIterator1>::iterator_category(),
67       typename iterator_traits<_InputIterator2>::iterator_category());
68 }
69 
70 template <class _InputIterator1, class _InputIterator2>
71 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
72 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
73   return std::__equal(
74       __first1,
75       __last1,
76       __first2,
77       __last2,
78       __equal_to(),
79       typename iterator_traits<_InputIterator1>::iterator_category(),
80       typename iterator_traits<_InputIterator2>::iterator_category());
81 }
82 #endif
83 
84 _LIBCPP_END_NAMESPACE_STD
85 
86 #endif // _LIBCPP___ALGORITHM_EQUAL_H
87