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 #if _LIBCPP_STD_VER >= 20
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 namespace ranges {
34 namespace __equal {
35 struct __fn {
36   template <input_iterator _Iter1,
37             sentinel_for<_Iter1> _Sent1,
38             input_iterator _Iter2,
39             sentinel_for<_Iter2> _Sent2,
40             class _Pred  = ranges::equal_to,
41             class _Proj1 = identity,
42             class _Proj2 = identity>
43     requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
44   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
45       _Iter1 __first1,
46       _Sent1 __last1,
47       _Iter2 __first2,
48       _Sent2 __last2,
49       _Pred __pred   = {},
50       _Proj1 __proj1 = {},
51       _Proj2 __proj2 = {}) const {
52     if constexpr (sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2>) {
53       if (__last1 - __first1 != __last2 - __first2)
54         return false;
55     }
56     auto __unwrapped1 = std::__unwrap_range(std::move(__first1), std::move(__last1));
57     auto __unwrapped2 = std::__unwrap_range(std::move(__first2), std::move(__last2));
58     return std::__equal_impl(
59         std::move(__unwrapped1.first),
60         std::move(__unwrapped1.second),
61         std::move(__unwrapped2.first),
62         std::move(__unwrapped2.second),
63         __pred,
64         __proj1,
65         __proj2);
66   }
67 
68   template <input_range _Range1,
69             input_range _Range2,
70             class _Pred  = ranges::equal_to,
71             class _Proj1 = identity,
72             class _Proj2 = identity>
73     requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
74   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
75       _Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
76     if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
77       if (ranges::distance(__range1) != ranges::distance(__range2))
78         return false;
79     }
80     auto __unwrapped1 = std::__unwrap_range(ranges::begin(__range1), ranges::end(__range1));
81     auto __unwrapped2 = std::__unwrap_range(ranges::begin(__range2), ranges::end(__range2));
82     return std::__equal_impl(
83         std::move(__unwrapped1.first),
84         std::move(__unwrapped1.second),
85         std::move(__unwrapped2.first),
86         std::move(__unwrapped2.second),
87         __pred,
88         __proj1,
89         __proj2);
90     return false;
91   }
92 };
93 } // namespace __equal
94 
95 inline namespace __cpo {
96 inline constexpr auto equal = __equal::__fn{};
97 } // namespace __cpo
98 } // namespace ranges
99 
100 _LIBCPP_END_NAMESPACE_STD
101 
102 #endif // _LIBCPP_STD_VER >= 20
103 
104 #endif // _LIBCPP___ALGORITHM_RANGES_EQUAL_H
105