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