xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/ranges_equal.h (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_RANGES_EQUAL_H
10 #define _LIBCPP___ALGORITHM_RANGES_EQUAL_H
11 
12 #include <__config>
13 #include <__functional/identity.h>
14 #include <__functional/invoke.h>
15 #include <__functional/ranges_operations.h>
16 #include <__iterator/concepts.h>
17 #include <__iterator/distance.h>
18 #include <__iterator/indirectly_comparable.h>
19 #include <__ranges/access.h>
20 #include <__ranges/concepts.h>
21 #include <__utility/move.h>
22 
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 #  pragma GCC system_header
25 #endif
26 
27 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 namespace ranges {
32 namespace __equal {
33 struct __fn {
34 private:
35   template <class _Iter1, class _Sent1,
36             class _Iter2, class _Sent2,
37             class _Pred,
38             class _Proj1,
39             class _Proj2>
40   _LIBCPP_HIDE_FROM_ABI constexpr static
41   bool __equal_impl(_Iter1 __first1, _Sent1 __last1,
42                     _Iter2 __first2, _Sent2 __last2,
43                     _Pred& __pred,
44                     _Proj1& __proj1,
45                     _Proj2& __proj2) {
46     while (__first1 != __last1 && __first2 != __last2) {
47       if (!std::invoke(__pred, std::invoke(__proj1, *__first1), std::invoke(__proj2, *__first2)))
48         return false;
49       ++__first1;
50       ++__first2;
51     }
52     return __first1 == __last1 && __first2 == __last2;
53   }
54 
55 public:
56 
57   template <input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
58             input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
59             class _Pred = ranges::equal_to,
60             class _Proj1 = identity,
61             class _Proj2 = identity>
62     requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
63   _LIBCPP_HIDE_FROM_ABI constexpr
64   bool operator()(_Iter1 __first1, _Sent1 __last1,
65                   _Iter2 __first2, _Sent2 __last2,
66                   _Pred __pred = {},
67                   _Proj1 __proj1 = {},
68                   _Proj2 __proj2 = {}) const {
69     if constexpr (sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2>) {
70       if (__last1 - __first1 != __last2 - __first2)
71         return false;
72     }
73     return __equal_impl(std::move(__first1), std::move(__last1),
74                         std::move(__first2), std::move(__last2),
75                         __pred,
76                         __proj1,
77                         __proj2);
78   }
79 
80   template <input_range _Range1,
81             input_range _Range2,
82             class _Pred = ranges::equal_to,
83             class _Proj1 = identity,
84             class _Proj2 = identity>
85     requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
86   _LIBCPP_HIDE_FROM_ABI constexpr
87   bool operator()(_Range1&& __range1,
88                   _Range2&& __range2,
89                   _Pred __pred = {},
90                   _Proj1 __proj1 = {},
91                   _Proj2 __proj2 = {}) const {
92     if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
93       if (ranges::distance(__range1) != ranges::distance(__range2))
94         return false;
95     }
96     return __equal_impl(ranges::begin(__range1), ranges::end(__range1),
97                         ranges::begin(__range2), ranges::end(__range2),
98                         __pred,
99                         __proj1,
100                         __proj2);
101     return false;
102   }
103 };
104 } // namespace __equal
105 
106 inline namespace __cpo {
107   inline constexpr auto equal = __equal::__fn{};
108 } // namespace __cpo
109 } // namespace ranges
110 
111 _LIBCPP_END_NAMESPACE_STD
112 
113 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
114 
115 #endif // _LIBCPP___ALGORITHM_RANGES_EQUAL_H
116