1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_RANGES_EQUAL_H
10 #define _LIBCPP___ALGORITHM_RANGES_EQUAL_H
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>
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 # pragma GCC system_header
27 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
29 _LIBCPP_BEGIN_NAMESPACE_STD
35 template <class _Iter1, class _Sent1,
36 class _Iter2, class _Sent2,
40 _LIBCPP_HIDE_FROM_ABI constexpr static
41 bool __equal_impl(_Iter1 __first1, _Sent1 __last1,
42 _Iter2 __first2, _Sent2 __last2,
46 while (__first1 != __last1 && __first2 != __last2) {
47 if (!std::invoke(__pred, std::invoke(__proj1, *__first1), std::invoke(__proj2, *__first2)))
52 return __first1 == __last1 && __first2 == __last2;
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,
68 _Proj2 __proj2 = {}) const {
69 if constexpr (sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2>) {
70 if (__last1 - __first1 != __last2 - __first2)
73 return __equal_impl(std::move(__first1), std::move(__last1),
74 std::move(__first2), std::move(__last2),
80 template <input_range _Range1,
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,
91 _Proj2 __proj2 = {}) const {
92 if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
93 if (ranges::distance(__range1) != ranges::distance(__range2))
96 return __equal_impl(ranges::begin(__range1), ranges::end(__range1),
97 ranges::begin(__range2), ranges::end(__range2),
104 } // namespace __equal
106 inline namespace __cpo {
107 inline constexpr auto equal = __equal::__fn{};
109 } // namespace ranges
111 _LIBCPP_END_NAMESPACE_STD
113 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
115 #endif // _LIBCPP___ALGORITHM_RANGES_EQUAL_H