is_permutation.h 9.2 KB

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