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