xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/is_permutation.h (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___ALGORITHM_IS_PERMUTATION_H
11 #define _LIBCPP___ALGORITHM_IS_PERMUTATION_H
12 
13 #include <__algorithm/comp.h>
14 #include <__algorithm/iterator_operations.h>
15 #include <__config>
16 #include <__functional/identity.h>
17 #include <__functional/invoke.h>
18 #include <__iterator/concepts.h>
19 #include <__iterator/distance.h>
20 #include <__iterator/iterator_traits.h>
21 #include <__iterator/next.h>
22 #include <__utility/move.h>
23 #include <type_traits>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #  pragma GCC system_header
27 #endif
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class = void>
32 struct _ConstTimeDistance : false_type {};
33 
34 #if _LIBCPP_STD_VER > 17
35 
36 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2>
37 struct _ConstTimeDistance<_Iter1, _Sent1, _Iter2, _Sent2, __enable_if_t<
38   sized_sentinel_for<_Sent1, _Iter1> &&
39   sized_sentinel_for<_Sent2, _Iter2>
40 >> : true_type {};
41 
42 #else
43 
44 template <class _Iter1, class _Iter2>
45 struct _ConstTimeDistance<_Iter1, _Iter1, _Iter2, _Iter2, __enable_if_t<
46     is_same<typename iterator_traits<_Iter1>::iterator_category, random_access_iterator_tag>::value &&
47     is_same<typename iterator_traits<_Iter2>::iterator_category, random_access_iterator_tag>::value
48 > > : true_type {};
49 
50 #endif // _LIBCPP_STD_VER > 17
51 
52 // Internal functions
53 
54 // For each element in [f1, l1) see if there are the same number of equal elements in [f2, l2)
55 template <class _AlgPolicy,
56           class _Iter1, class _Sent1, class _Iter2, class _Sent2,
57           class _Proj1, class _Proj2, class _Pred>
58 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
59 __is_permutation_impl(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
60                       _Pred&& __pred, _Proj1&& __proj1, _Proj2&& __proj2) {
61   using _D1 = __iter_diff_t<_Iter1>;
62 
63   for (auto __i = __first1; __i != __last1; ++__i) {
64     //  Have we already counted the number of *__i in [f1, l1)?
65     auto __match = __first1;
66     for (; __match != __i; ++__match) {
67       if (std::__invoke(__pred, std::__invoke(__proj1, *__match), std::__invoke(__proj1, *__i)))
68         break;
69     }
70 
71     if (__match == __i) {
72       // Count number of *__i in [f2, l2)
73       _D1 __c2 = 0;
74       for (auto __j = __first2; __j != __last2; ++__j) {
75         if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj2, *__j)))
76           ++__c2;
77       }
78       if (__c2 == 0)
79         return false;
80 
81       // Count number of *__i in [__i, l1) (we can start with 1)
82       _D1 __c1 = 1;
83       for (auto __j = _IterOps<_AlgPolicy>::next(__i); __j != __last1; ++__j) {
84         if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, *__j)))
85           ++__c1;
86       }
87       if (__c1 != __c2)
88         return false;
89     }
90   }
91 
92   return true;
93 }
94 
95 // 2+1 iterators, predicate. Not used by range algorithms.
96 template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _BinaryPredicate>
97 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
98 __is_permutation(_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2,
99                  _BinaryPredicate&& __pred) {
100   // Shorten sequences as much as possible by lopping of any equal prefix.
101   for (; __first1 != __last1; ++__first1, (void)++__first2) {
102     if (!__pred(*__first1, *__first2))
103       break;
104   }
105 
106   if (__first1 == __last1)
107     return true;
108 
109   //  __first1 != __last1 && *__first1 != *__first2
110   using _D1 = __iter_diff_t<_ForwardIterator1>;
111   _D1 __l1 = _IterOps<_AlgPolicy>::distance(__first1, __last1);
112   if (__l1 == _D1(1))
113     return false;
114   auto __last2 = _IterOps<_AlgPolicy>::next(__first2, __l1);
115 
116   return std::__is_permutation_impl<_AlgPolicy>(
117       std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2),
118       __pred, __identity(), __identity());
119 }
120 
121 // 2+2 iterators, predicate, non-constant time `distance`.
122 template <class _AlgPolicy,
123           class _Iter1, class _Sent1, class _Iter2, class _Sent2,
124           class _Proj1, class _Proj2, class _Pred>
125 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
126 __is_permutation(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
127                  _Pred&& __pred, _Proj1&& __proj1, _Proj2&& __proj2,
128                  /*_ConstTimeDistance=*/false_type) {
129   // Shorten sequences as much as possible by lopping of any equal prefix.
130   while (__first1 != __last1 && __first2 != __last2) {
131     if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
132       break;
133     ++__first1;
134     ++__first2;
135   }
136 
137   if (__first1 == __last1)
138     return __first2 == __last2;
139   if (__first2 == __last2) // Second range is shorter
140     return false;
141 
142   using _D1 = __iter_diff_t<_Iter1>;
143   _D1 __l1 = _IterOps<_AlgPolicy>::distance(__first1, __last1);
144 
145   using _D2 = __iter_diff_t<_Iter2>;
146   _D2 __l2 = _IterOps<_AlgPolicy>::distance(__first2, __last2);
147   if (__l1 != __l2)
148     return false;
149 
150   return std::__is_permutation_impl<_AlgPolicy>(
151       std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2),
152       __pred, __proj1, __proj2);
153 }
154 
155 // 2+2 iterators, predicate, specialization for constant-time `distance` call.
156 template <class _AlgPolicy,
157           class _Iter1, class _Sent1, class _Iter2, class _Sent2,
158           class _Proj1, class _Proj2, class _Pred>
159 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
160 __is_permutation(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
161                  _Pred&& __pred, _Proj1&& __proj1, _Proj2&& __proj2,
162                  /*_ConstTimeDistance=*/true_type) {
163   if (std::distance(__first1, __last1) != std::distance(__first2, __last2))
164     return false;
165   return std::__is_permutation<_AlgPolicy>(
166       std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2),
167       __pred, __proj1, __proj2,
168       /*_ConstTimeDistance=*/false_type());
169 }
170 
171 // 2+2 iterators, predicate
172 template <class _AlgPolicy,
173           class _Iter1, class _Sent1, class _Iter2, class _Sent2,
174           class _Proj1, class _Proj2, class _Pred>
175 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
176 __is_permutation(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
177                  _Pred&& __pred, _Proj1&& __proj1, _Proj2&& __proj2) {
178   return std::__is_permutation<_AlgPolicy>(
179       std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2),
180       __pred, __proj1, __proj2,
181       _ConstTimeDistance<_Iter1, _Sent1, _Iter2, _Sent2>());
182 }
183 
184 // Public interface
185 
186 // 2+1 iterators, predicate
187 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
188 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
189 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
190                _BinaryPredicate __pred) {
191   static_assert(__is_callable<_BinaryPredicate, decltype(*__first1), decltype(*__first2)>::value,
192       "The predicate has to be callable");
193 
194   return std::__is_permutation<_ClassicAlgPolicy>(
195       std::move(__first1), std::move(__last1), std::move(__first2), __pred);
196 }
197 
198 // 2+1 iterators
199 template <class _ForwardIterator1, class _ForwardIterator2>
200 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
201 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
202   using __v1 = __iter_value_type<_ForwardIterator1>;
203   using __v2 = __iter_value_type<_ForwardIterator2>;
204   return std::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
205 }
206 
207 #if _LIBCPP_STD_VER > 11
208 
209 // 2+2 iterators
210 template <class _ForwardIterator1, class _ForwardIterator2>
211 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
212 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
213                _ForwardIterator2 __last2) {
214   using __v1 = __iter_value_type<_ForwardIterator1>;
215   using __v2 = __iter_value_type<_ForwardIterator2>;
216 
217   return std::__is_permutation<_ClassicAlgPolicy>(
218       std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2),
219       __equal_to<__v1, __v2>(), __identity(), __identity());
220 }
221 
222 // 2+2 iterators, predicate
223 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
224 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
225 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
226                _ForwardIterator2 __last2, _BinaryPredicate __pred) {
227   static_assert(__is_callable<_BinaryPredicate, decltype(*__first1), decltype(*__first2)>::value,
228       "The predicate has to be callable");
229 
230   return std::__is_permutation<_ClassicAlgPolicy>(
231       std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2),
232       __pred, __identity(), __identity());
233 }
234 
235 #endif // _LIBCPP_STD_VER > 11
236 
237 _LIBCPP_END_NAMESPACE_STD
238 
239 #endif // _LIBCPP___ALGORITHM_IS_PERMUTATION_H
240