xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/is_permutation.h (revision 994297b01b98816bea1abf45ae4bac1bc69ee7a0)
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 <__config>
15 #include <__iterator/iterator_traits.h>
16 #include <__iterator/next.h>
17 #include <iterator> // FIXME: replace with <__iterator/distance.h> when it lands
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #pragma GCC system_header
21 #endif
22 
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
29 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
30 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
31                _BinaryPredicate __pred) {
32   //  shorten sequences as much as possible by lopping of any equal prefix
33   for (; __first1 != __last1; ++__first1, (void)++__first2)
34     if (!__pred(*__first1, *__first2))
35       break;
36   if (__first1 == __last1)
37     return true;
38 
39   //  __first1 != __last1 && *__first1 != *__first2
40   typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
41   _D1 __l1 = _VSTD::distance(__first1, __last1);
42   if (__l1 == _D1(1))
43     return false;
44   _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1);
45   // For each element in [f1, l1) see if there are the same number of
46   //    equal elements in [f2, l2)
47   for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i) {
48     //  Have we already counted the number of *__i in [f1, l1)?
49     _ForwardIterator1 __match = __first1;
50     for (; __match != __i; ++__match)
51       if (__pred(*__match, *__i))
52         break;
53     if (__match == __i) {
54       // Count number of *__i in [f2, l2)
55       _D1 __c2 = 0;
56       for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
57         if (__pred(*__i, *__j))
58           ++__c2;
59       if (__c2 == 0)
60         return false;
61       // Count number of *__i in [__i, l1) (we can start with 1)
62       _D1 __c1 = 1;
63       for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
64         if (__pred(*__i, *__j))
65           ++__c1;
66       if (__c1 != __c2)
67         return false;
68     }
69   }
70   return true;
71 }
72 
73 template <class _ForwardIterator1, class _ForwardIterator2>
74 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
75 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {
76   typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
77   typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
78   return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>());
79 }
80 
81 #if _LIBCPP_STD_VER > 11
82 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
83 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
84 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
85                  _ForwardIterator2 __last2, _BinaryPredicate __pred, forward_iterator_tag, forward_iterator_tag) {
86   //  shorten sequences as much as possible by lopping of any equal prefix
87   for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void)++__first2)
88     if (!__pred(*__first1, *__first2))
89       break;
90   if (__first1 == __last1)
91     return __first2 == __last2;
92   else if (__first2 == __last2)
93     return false;
94 
95   typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1;
96   _D1 __l1 = _VSTD::distance(__first1, __last1);
97 
98   typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2;
99   _D2 __l2 = _VSTD::distance(__first2, __last2);
100   if (__l1 != __l2)
101     return false;
102 
103   // For each element in [f1, l1) see if there are the same number of
104   //    equal elements in [f2, l2)
105   for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i) {
106     //  Have we already counted the number of *__i in [f1, l1)?
107     _ForwardIterator1 __match = __first1;
108     for (; __match != __i; ++__match)
109       if (__pred(*__match, *__i))
110         break;
111     if (__match == __i) {
112       // Count number of *__i in [f2, l2)
113       _D1 __c2 = 0;
114       for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
115         if (__pred(*__i, *__j))
116           ++__c2;
117       if (__c2 == 0)
118         return false;
119       // Count number of *__i in [__i, l1) (we can start with 1)
120       _D1 __c1 = 1;
121       for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j)
122         if (__pred(*__i, *__j))
123           ++__c1;
124       if (__c1 != __c2)
125         return false;
126     }
127   }
128   return true;
129 }
130 
131 template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
132 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool __is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1,
133                                                     _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2,
134                                                     _BinaryPredicate __pred, random_access_iterator_tag,
135                                                     random_access_iterator_tag) {
136   if (_VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
137     return false;
138   return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2,
139                                typename add_lvalue_reference<_BinaryPredicate>::type>(__first1, __last1, __first2,
140                                                                                       __pred);
141 }
142 
143 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
144 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
145 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
146                _ForwardIterator2 __last2, _BinaryPredicate __pred) {
147   return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type>(
148       __first1, __last1, __first2, __last2, __pred, typename iterator_traits<_ForwardIterator1>::iterator_category(),
149       typename iterator_traits<_ForwardIterator2>::iterator_category());
150 }
151 
152 template <class _ForwardIterator1, class _ForwardIterator2>
153 _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
154 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
155                _ForwardIterator2 __last2) {
156   typedef typename iterator_traits<_ForwardIterator1>::value_type __v1;
157   typedef typename iterator_traits<_ForwardIterator2>::value_type __v2;
158   return _VSTD::__is_permutation(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(),
159                                  typename iterator_traits<_ForwardIterator1>::iterator_category(),
160                                  typename iterator_traits<_ForwardIterator2>::iterator_category());
161 }
162 #endif
163 
164 _LIBCPP_END_NAMESPACE_STD
165 
166 _LIBCPP_POP_MACROS
167 
168 #endif // _LIBCPP___ALGORITHM_IS_PERMUTATION_H
169