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