xref: /llvm-project/libcxx/include/__algorithm/ranges_is_permutation.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_IS_PERMUTATION_H
10 #define _LIBCPP___ALGORITHM_RANGES_IS_PERMUTATION_H
11 
12 #include <__algorithm/is_permutation.h>
13 #include <__algorithm/iterator_operations.h>
14 #include <__config>
15 #include <__functional/identity.h>
16 #include <__functional/ranges_operations.h>
17 #include <__iterator/concepts.h>
18 #include <__iterator/distance.h>
19 #include <__iterator/projected.h>
20 #include <__ranges/access.h>
21 #include <__ranges/concepts.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 __is_permutation {
37   template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Proj1, class _Proj2, class _Pred>
38   _LIBCPP_HIDE_FROM_ABI constexpr static bool __is_permutation_func_impl(
39       _Iter1 __first1,
40       _Sent1 __last1,
41       _Iter2 __first2,
42       _Sent2 __last2,
43       _Pred& __pred,
44       _Proj1& __proj1,
45       _Proj2& __proj2) {
46     return std::__is_permutation<_RangeAlgPolicy>(
47         std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2);
48   }
49 
50   template <
51       forward_iterator _Iter1,
52       sentinel_for<_Iter1> _Sent1,
53       forward_iterator _Iter2,
54       sentinel_for<_Iter2> _Sent2,
55       class _Proj1                                                                              = identity,
56       class _Proj2                                                                              = identity,
57       indirect_equivalence_relation<projected<_Iter1, _Proj1>, projected<_Iter2, _Proj2>> _Pred = ranges::equal_to>
58   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
59       _Iter1 __first1,
60       _Sent1 __last1,
61       _Iter2 __first2,
62       _Sent2 __last2,
63       _Pred __pred   = {},
64       _Proj1 __proj1 = {},
65       _Proj2 __proj2 = {}) const {
66     return __is_permutation_func_impl(
67         std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2);
68   }
69 
70   template <forward_range _Range1,
71             forward_range _Range2,
72             class _Proj1                                                                = identity,
73             class _Proj2                                                                = identity,
74             indirect_equivalence_relation<projected<iterator_t<_Range1>, _Proj1>,
75                                           projected<iterator_t<_Range2>, _Proj2>> _Pred = ranges::equal_to>
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 
83     return __is_permutation_func_impl(
84         ranges::begin(__range1),
85         ranges::end(__range1),
86         ranges::begin(__range2),
87         ranges::end(__range2),
88         __pred,
89         __proj1,
90         __proj2);
91   }
92 };
93 
94 inline namespace __cpo {
95 inline constexpr auto is_permutation = __is_permutation{};
96 } // namespace __cpo
97 } // namespace ranges
98 
99 _LIBCPP_END_NAMESPACE_STD
100 
101 #endif // _LIBCPP_STD_VER >= 20
102 
103 _LIBCPP_POP_MACROS
104 
105 #endif // _LIBCPP___ALGORITHM_RANGES_IS_PERMUTATION_H
106