xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/equal.h (revision 577b62c2bacc7dfa228591ca3da361e1bc398301)
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_AFTER_CXX17 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_AFTER_CXX17 bool
35 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {
36   typedef typename iterator_traits<_InputIterator1>::value_type __v1;
37   typedef typename iterator_traits<_InputIterator2>::value_type __v2;
38   return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
39 }
40 
41 #if _LIBCPP_STD_VER > 11
42 template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
43 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
44 __equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
45         _BinaryPredicate __pred, input_iterator_tag, input_iterator_tag) {
46   for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void)++__first2)
47     if (!__pred(*__first1, *__first2))
48       return false;
49   return __first1 == __last1 && __first2 == __last2;
50 }
51 
52 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
53 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
54 __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
55         _RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag,
56         random_access_iterator_tag) {
57   if (_VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
58     return false;
59   return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
60                       _BinaryPredicate&>(__first1, __last1, __first2, __pred);
61 }
62 
63 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
64 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
65 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
66       _BinaryPredicate __pred) {
67   return _VSTD::__equal<_BinaryPredicate&>(
68       __first1, __last1, __first2, __last2, __pred, typename iterator_traits<_InputIterator1>::iterator_category(),
69       typename iterator_traits<_InputIterator2>::iterator_category());
70 }
71 
72 template <class _InputIterator1, class _InputIterator2>
73 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
74 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
75   typedef typename iterator_traits<_InputIterator1>::value_type __v1;
76   typedef typename iterator_traits<_InputIterator2>::value_type __v2;
77   return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
78                         typename iterator_traits<_InputIterator1>::iterator_category(),
79                         typename iterator_traits<_InputIterator2>::iterator_category());
80 }
81 #endif
82 
83 _LIBCPP_END_NAMESPACE_STD
84 
85 #endif // _LIBCPP___ALGORITHM_EQUAL_H
86