is_permutation.h 9.6 KB

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