xref: /llvm-project/libcxx/include/__algorithm/is_permutation.h (revision 09e3a360581dc36d0820d3fb6da9bd7cfed87b5d)
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 <__iterator/concepts.h>
18 #include <__iterator/distance.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__iterator/next.h>
21 #include <__type_traits/enable_if.h>
22 #include <__type_traits/invoke.h>
23 #include <__type_traits/is_callable.h>
24 #include <__type_traits/is_same.h>
25 #include <__utility/move.h>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_PUSH_MACROS
32 #include <__undef_macros>
33 
34 _LIBCPP_BEGIN_NAMESPACE_STD
35 
36 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class = void>
37 struct _ConstTimeDistance : false_type {};
38 
39 #if _LIBCPP_STD_VER >= 20
40 
41 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2>
42 struct _ConstTimeDistance<_Iter1,
43                           _Sent1,
44                           _Iter2,
45                           _Sent2,
46                           __enable_if_t< sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2> >>
47     : true_type {};
48 
49 #else
50 
51 template <class _Iter1, class _Iter2>
52 struct _ConstTimeDistance<
53     _Iter1,
54     _Iter1,
55     _Iter2,
56     _Iter2,
57     __enable_if_t< is_same<typename iterator_traits<_Iter1>::iterator_category, random_access_iterator_tag>::value &&
58                    is_same<typename iterator_traits<_Iter2>::iterator_category, random_access_iterator_tag>::value > >
59     : true_type {};
60 
61 #endif // _LIBCPP_STD_VER >= 20
62 
63 // Internal functions
64 
65 // For each element in [f1, l1) see if there are the same number of equal elements in [f2, l2)
66 template <class _AlgPolicy,
67           class _Iter1,
68           class _Sent1,
69           class _Iter2,
70           class _Sent2,
71           class _Proj1,
72           class _Proj2,
73           class _Pred>
74 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation_impl(
75     _Iter1 __first1,
76     _Sent1 __last1,
77     _Iter2 __first2,
78     _Sent2 __last2,
79     _Pred&& __pred,
80     _Proj1&& __proj1,
81     _Proj2&& __proj2) {
82   using _D1 = __iter_diff_t<_Iter1>;
83 
84   for (auto __i = __first1; __i != __last1; ++__i) {
85     //  Have we already counted the number of *__i in [f1, l1)?
86     auto __match = __first1;
87     for (; __match != __i; ++__match) {
88       if (std::__invoke(__pred, std::__invoke(__proj1, *__match), std::__invoke(__proj1, *__i)))
89         break;
90     }
91 
92     if (__match == __i) {
93       // Count number of *__i in [f2, l2)
94       _D1 __c2 = 0;
95       for (auto __j = __first2; __j != __last2; ++__j) {
96         if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj2, *__j)))
97           ++__c2;
98       }
99       if (__c2 == 0)
100         return false;
101 
102       // Count number of *__i in [__i, l1) (we can start with 1)
103       _D1 __c1 = 1;
104       for (auto __j = _IterOps<_AlgPolicy>::next(__i); __j != __last1; ++__j) {
105         if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, *__j)))
106           ++__c1;
107       }
108       if (__c1 != __c2)
109         return false;
110     }
111   }
112 
113   return true;
114 }
115 
116 // 2+1 iterators, predicate. Not used by range algorithms.
117 template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _BinaryPredicate>
118 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(
119     _ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _BinaryPredicate&& __pred) {
120   // Shorten sequences as much as possible by lopping of any equal prefix.
121   for (; __first1 != __last1; ++__first1, (void)++__first2) {
122     if (!__pred(*__first1, *__first2))
123       break;
124   }
125 
126   if (__first1 == __last1)
127     return true;
128 
129   //  __first1 != __last1 && *__first1 != *__first2
130   using _D1 = __iter_diff_t<_ForwardIterator1>;
131   _D1 __l1  = _IterOps<_AlgPolicy>::distance(__first1, __last1);
132   if (__l1 == _D1(1))
133     return false;
134   auto __last2 = _IterOps<_AlgPolicy>::next(__first2, __l1);
135 
136   return std::__is_permutation_impl<_AlgPolicy>(
137       std::move(__first1),
138       std::move(__last1),
139       std::move(__first2),
140       std::move(__last2),
141       __pred,
142       __identity(),
143       __identity());
144 }
145 
146 // 2+2 iterators, predicate, non-constant time `distance`.
147 template <class _AlgPolicy,
148           class _Iter1,
149           class _Sent1,
150           class _Iter2,
151           class _Sent2,
152           class _Proj1,
153           class _Proj2,
154           class _Pred>
155 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(
156     _Iter1 __first1,
157     _Sent1 __last1,
158     _Iter2 __first2,
159     _Sent2 __last2,
160     _Pred&& __pred,
161     _Proj1&& __proj1,
162     _Proj2&& __proj2,
163     /*_ConstTimeDistance=*/false_type) {
164   // Shorten sequences as much as possible by lopping of any equal prefix.
165   while (__first1 != __last1 && __first2 != __last2) {
166     if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
167       break;
168     ++__first1;
169     ++__first2;
170   }
171 
172   if (__first1 == __last1)
173     return __first2 == __last2;
174   if (__first2 == __last2) // Second range is shorter
175     return false;
176 
177   using _D1 = __iter_diff_t<_Iter1>;
178   _D1 __l1  = _IterOps<_AlgPolicy>::distance(__first1, __last1);
179 
180   using _D2 = __iter_diff_t<_Iter2>;
181   _D2 __l2  = _IterOps<_AlgPolicy>::distance(__first2, __last2);
182   if (__l1 != __l2)
183     return false;
184 
185   return std::__is_permutation_impl<_AlgPolicy>(
186       std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2);
187 }
188 
189 // 2+2 iterators, predicate, specialization for constant-time `distance` call.
190 template <class _AlgPolicy,
191           class _Iter1,
192           class _Sent1,
193           class _Iter2,
194           class _Sent2,
195           class _Proj1,
196           class _Proj2,
197           class _Pred>
198 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(
199     _Iter1 __first1,
200     _Sent1 __last1,
201     _Iter2 __first2,
202     _Sent2 __last2,
203     _Pred&& __pred,
204     _Proj1&& __proj1,
205     _Proj2&& __proj2,
206     /*_ConstTimeDistance=*/true_type) {
207   if (std::distance(__first1, __last1) != std::distance(__first2, __last2))
208     return false;
209   return std::__is_permutation<_AlgPolicy>(
210       std::move(__first1),
211       std::move(__last1),
212       std::move(__first2),
213       std::move(__last2),
214       __pred,
215       __proj1,
216       __proj2,
217       /*_ConstTimeDistance=*/false_type());
218 }
219 
220 // 2+2 iterators, predicate
221 template <class _AlgPolicy,
222           class _Iter1,
223           class _Sent1,
224           class _Iter2,
225           class _Sent2,
226           class _Proj1,
227           class _Proj2,
228           class _Pred>
229 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(
230     _Iter1 __first1,
231     _Sent1 __last1,
232     _Iter2 __first2,
233     _Sent2 __last2,
234     _Pred&& __pred,
235     _Proj1&& __proj1,
236     _Proj2&& __proj2) {
237   return std::__is_permutation<_AlgPolicy>(
238       std::move(__first1),
239       std::move(__last1),
240       std::move(__first2),
241       std::move(__last2),
242       __pred,
243       __proj1,
244       __proj2,
245       _ConstTimeDistance<_Iter1, _Sent1, _Iter2, _Sent2>());
246 }
247 
248 // Public interface
249 
250 // 2+1 iterators, predicate
251 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
252 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool is_permutation(
253     _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _BinaryPredicate __pred) {
254   static_assert(__is_callable<_BinaryPredicate&, decltype(*__first1), decltype(*__first2)>::value,
255                 "The comparator has to be callable");
256 
257   return std::__is_permutation<_ClassicAlgPolicy>(std::move(__first1), std::move(__last1), std::move(__first2), __pred);
258 }
259 
260 // 2+1 iterators
261 template <class _ForwardIterator1, class _ForwardIterator2>
262 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
263 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
264   return std::is_permutation(__first1, __last1, __first2, __equal_to());
265 }
266 
267 #if _LIBCPP_STD_VER >= 14
268 
269 // 2+2 iterators
270 template <class _ForwardIterator1, class _ForwardIterator2>
271 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool is_permutation(
272     _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2) {
273   return std::__is_permutation<_ClassicAlgPolicy>(
274       std::move(__first1),
275       std::move(__last1),
276       std::move(__first2),
277       std::move(__last2),
278       __equal_to(),
279       __identity(),
280       __identity());
281 }
282 
283 // 2+2 iterators, predicate
284 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
285 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool is_permutation(
286     _ForwardIterator1 __first1,
287     _ForwardIterator1 __last1,
288     _ForwardIterator2 __first2,
289     _ForwardIterator2 __last2,
290     _BinaryPredicate __pred) {
291   static_assert(__is_callable<_BinaryPredicate&, decltype(*__first1), decltype(*__first2)>::value,
292                 "The comparator has to be callable");
293 
294   return std::__is_permutation<_ClassicAlgPolicy>(
295       std::move(__first1),
296       std::move(__last1),
297       std::move(__first2),
298       std::move(__last2),
299       __pred,
300       __identity(),
301       __identity());
302 }
303 
304 #endif // _LIBCPP_STD_VER >= 14
305 
306 _LIBCPP_END_NAMESPACE_STD
307 
308 _LIBCPP_POP_MACROS
309 
310 #endif // _LIBCPP___ALGORITHM_IS_PERMUTATION_H
311