xref: /llvm-project/libcxx/include/__algorithm/ranges_equal.h (revision d10dc5a06fac4dcabf2264c64c8672c6f6ae36fb)
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 <__algorithm/equal.h>
13 #include <__algorithm/unwrap_range.h>
14 #include <__config>
15 #include <__functional/identity.h>
16 #include <__functional/invoke.h>
17 #include <__functional/ranges_operations.h>
18 #include <__iterator/concepts.h>
19 #include <__iterator/distance.h>
20 #include <__iterator/indirectly_comparable.h>
21 #include <__ranges/access.h>
22 #include <__ranges/concepts.h>
23 #include <__utility/move.h>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_PUSH_MACROS
30 #include <__undef_macros>
31 
32 #if _LIBCPP_STD_VER >= 20
33 
34 _LIBCPP_BEGIN_NAMESPACE_STD
35 
36 namespace ranges {
37 struct __equal {
38   template <input_iterator _Iter1,
39             sentinel_for<_Iter1> _Sent1,
40             input_iterator _Iter2,
41             sentinel_for<_Iter2> _Sent2,
42             class _Pred  = ranges::equal_to,
43             class _Proj1 = identity,
44             class _Proj2 = identity>
45     requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
46   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
47       _Iter1 __first1,
48       _Sent1 __last1,
49       _Iter2 __first2,
50       _Sent2 __last2,
51       _Pred __pred   = {},
52       _Proj1 __proj1 = {},
53       _Proj2 __proj2 = {}) const {
54     if constexpr (sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2>) {
55       if (__last1 - __first1 != __last2 - __first2)
56         return false;
57     }
58     auto __unwrapped1 = std::__unwrap_range(std::move(__first1), std::move(__last1));
59     auto __unwrapped2 = std::__unwrap_range(std::move(__first2), std::move(__last2));
60     return std::__equal_impl(
61         std::move(__unwrapped1.first),
62         std::move(__unwrapped1.second),
63         std::move(__unwrapped2.first),
64         std::move(__unwrapped2.second),
65         __pred,
66         __proj1,
67         __proj2);
68   }
69 
70   template <input_range _Range1,
71             input_range _Range2,
72             class _Pred  = ranges::equal_to,
73             class _Proj1 = identity,
74             class _Proj2 = identity>
75     requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
76   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
77       _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
78     if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
79       if (ranges::distance(__range1) != ranges::distance(__range2))
80         return false;
81     }
82     auto __unwrapped1 = std::__unwrap_range(ranges::begin(__range1), ranges::end(__range1));
83     auto __unwrapped2 = std::__unwrap_range(ranges::begin(__range2), ranges::end(__range2));
84     return std::__equal_impl(
85         std::move(__unwrapped1.first),
86         std::move(__unwrapped1.second),
87         std::move(__unwrapped2.first),
88         std::move(__unwrapped2.second),
89         __pred,
90         __proj1,
91         __proj2);
92     return false;
93   }
94 };
95 
96 inline namespace __cpo {
97 inline constexpr auto equal = __equal{};
98 } // namespace __cpo
99 } // namespace ranges
100 
101 _LIBCPP_END_NAMESPACE_STD
102 
103 #endif // _LIBCPP_STD_VER >= 20
104 
105 _LIBCPP_POP_MACROS
106 
107 #endif // _LIBCPP___ALGORITHM_RANGES_EQUAL_H
108