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 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
29 
30 _LIBCPP_BEGIN_NAMESPACE_STD
31 
32 namespace ranges {
33 namespace __is_permutation {
34 struct __fn {
35 
36   template <class _Iter1, class _Sent1, class _Iter2, class _Sent2,
37             class _Proj1, class _Proj2, class _Pred>
38   _LIBCPP_HIDE_FROM_ABI constexpr static
39   bool __is_permutation_func_impl(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
40                                   _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
41     return std::__is_permutation<_RangeAlgPolicy>(
42         std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2),
43         __pred, __proj1, __proj2);
44   }
45 
46   template <forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
47             forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
48             class _Proj1 = identity,
49             class _Proj2 = identity,
50             indirect_equivalence_relation<projected<_Iter1, _Proj1>,
51                                           projected<_Iter2, _Proj2>> _Pred = ranges::equal_to>
52   _LIBCPP_HIDE_FROM_ABI constexpr
53   bool operator()(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
54                   _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
55     return __is_permutation_func_impl(
56         std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2),
57         __pred, __proj1, __proj2);
58   }
59 
60   template <forward_range _Range1,
61             forward_range _Range2,
62             class _Proj1 = identity,
63             class _Proj2 = identity,
64             indirect_equivalence_relation<projected<iterator_t<_Range1>, _Proj1>, projected<iterator_t<_Range2>, _Proj2>> _Pred = ranges::equal_to>
65   _LIBCPP_HIDE_FROM_ABI constexpr
66   bool operator()(_Range1&& __range1, _Range2&& __range2,
67                   _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
68     if constexpr (sized_range<_Range1> && sized_range<_Range2>) {
69       if (ranges::distance(__range1) != ranges::distance(__range2))
70         return false;
71     }
72 
73     return __is_permutation_func_impl(
74         ranges::begin(__range1), ranges::end(__range1), ranges::begin(__range2), ranges::end(__range2),
75         __pred, __proj1, __proj2);
76   }
77 };
78 } // namespace __is_permutation
79 
80 inline namespace __cpo {
81   inline constexpr auto is_permutation = __is_permutation::__fn{};
82 } // namespace __cpo
83 } // namespace ranges
84 
85 _LIBCPP_END_NAMESPACE_STD
86 
87 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
88 
89 #endif // _LIBCPP___ALGORITHM_RANGES_IS_PERMUTATION_H
90