1fe6060f1SDimitry Andric // -*- C++ -*- 2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 3fe6060f1SDimitry Andric // 4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7fe6060f1SDimitry Andric // 8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 9fe6060f1SDimitry Andric 10fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_IS_PERMUTATION_H 11fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_IS_PERMUTATION_H 12fe6060f1SDimitry Andric 13fe6060f1SDimitry Andric #include <__algorithm/comp.h> 1461cfbce3SDimitry Andric #include <__algorithm/iterator_operations.h> 15fe6060f1SDimitry Andric #include <__config> 1661cfbce3SDimitry Andric #include <__functional/identity.h> 1761cfbce3SDimitry Andric #include <__functional/invoke.h> 1861cfbce3SDimitry Andric #include <__iterator/concepts.h> 19349cc55cSDimitry Andric #include <__iterator/distance.h> 20fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h> 21fe6060f1SDimitry Andric #include <__iterator/next.h> 2206c3fb27SDimitry Andric #include <__type_traits/is_callable.h> 2361cfbce3SDimitry Andric #include <__utility/move.h> 24fe6060f1SDimitry Andric 25fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 26fe6060f1SDimitry Andric # pragma GCC system_header 27fe6060f1SDimitry Andric #endif 28fe6060f1SDimitry Andric 2906c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS 3006c3fb27SDimitry Andric #include <__undef_macros> 3106c3fb27SDimitry Andric 32fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 33fe6060f1SDimitry Andric 3461cfbce3SDimitry Andric template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class = void> 3561cfbce3SDimitry Andric struct _ConstTimeDistance : false_type {}; 3661cfbce3SDimitry Andric 3706c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20 3861cfbce3SDimitry Andric 3961cfbce3SDimitry Andric template <class _Iter1, class _Sent1, class _Iter2, class _Sent2> 40cb14a3feSDimitry Andric struct _ConstTimeDistance<_Iter1, 41cb14a3feSDimitry Andric _Sent1, 42cb14a3feSDimitry Andric _Iter2, 43cb14a3feSDimitry Andric _Sent2, 44cb14a3feSDimitry Andric __enable_if_t< sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2> >> 45cb14a3feSDimitry Andric : true_type {}; 4661cfbce3SDimitry Andric 4761cfbce3SDimitry Andric #else 4861cfbce3SDimitry Andric 4961cfbce3SDimitry Andric template <class _Iter1, class _Iter2> 50cb14a3feSDimitry Andric struct _ConstTimeDistance< 51cb14a3feSDimitry Andric _Iter1, 52cb14a3feSDimitry Andric _Iter1, 53cb14a3feSDimitry Andric _Iter2, 54cb14a3feSDimitry Andric _Iter2, 55cb14a3feSDimitry Andric __enable_if_t< is_same<typename iterator_traits<_Iter1>::iterator_category, random_access_iterator_tag>::value && 56cb14a3feSDimitry Andric is_same<typename iterator_traits<_Iter2>::iterator_category, random_access_iterator_tag>::value > > 57cb14a3feSDimitry Andric : true_type {}; 5861cfbce3SDimitry Andric 5906c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20 6061cfbce3SDimitry Andric 6161cfbce3SDimitry Andric // Internal functions 6261cfbce3SDimitry Andric 6361cfbce3SDimitry Andric // For each element in [f1, l1) see if there are the same number of equal elements in [f2, l2) 6461cfbce3SDimitry Andric template <class _AlgPolicy, 65cb14a3feSDimitry Andric class _Iter1, 66cb14a3feSDimitry Andric class _Sent1, 67cb14a3feSDimitry Andric class _Iter2, 68cb14a3feSDimitry Andric class _Sent2, 69cb14a3feSDimitry Andric class _Proj1, 70cb14a3feSDimitry Andric class _Proj2, 71cb14a3feSDimitry Andric class _Pred> 72cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation_impl( 73cb14a3feSDimitry Andric _Iter1 __first1, 74cb14a3feSDimitry Andric _Sent1 __last1, 75cb14a3feSDimitry Andric _Iter2 __first2, 76cb14a3feSDimitry Andric _Sent2 __last2, 77cb14a3feSDimitry Andric _Pred&& __pred, 78cb14a3feSDimitry Andric _Proj1&& __proj1, 79cb14a3feSDimitry Andric _Proj2&& __proj2) { 8061cfbce3SDimitry Andric using _D1 = __iter_diff_t<_Iter1>; 8161cfbce3SDimitry Andric 8261cfbce3SDimitry Andric for (auto __i = __first1; __i != __last1; ++__i) { 8361cfbce3SDimitry Andric // Have we already counted the number of *__i in [f1, l1)? 8461cfbce3SDimitry Andric auto __match = __first1; 8561cfbce3SDimitry Andric for (; __match != __i; ++__match) { 8661cfbce3SDimitry Andric if (std::__invoke(__pred, std::__invoke(__proj1, *__match), std::__invoke(__proj1, *__i))) 8761cfbce3SDimitry Andric break; 8861cfbce3SDimitry Andric } 8961cfbce3SDimitry Andric 9061cfbce3SDimitry Andric if (__match == __i) { 9161cfbce3SDimitry Andric // Count number of *__i in [f2, l2) 9261cfbce3SDimitry Andric _D1 __c2 = 0; 9361cfbce3SDimitry Andric for (auto __j = __first2; __j != __last2; ++__j) { 9461cfbce3SDimitry Andric if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj2, *__j))) 9561cfbce3SDimitry Andric ++__c2; 9661cfbce3SDimitry Andric } 9761cfbce3SDimitry Andric if (__c2 == 0) 9861cfbce3SDimitry Andric return false; 9961cfbce3SDimitry Andric 10061cfbce3SDimitry Andric // Count number of *__i in [__i, l1) (we can start with 1) 10161cfbce3SDimitry Andric _D1 __c1 = 1; 10261cfbce3SDimitry Andric for (auto __j = _IterOps<_AlgPolicy>::next(__i); __j != __last1; ++__j) { 10361cfbce3SDimitry Andric if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, *__j))) 10461cfbce3SDimitry Andric ++__c1; 10561cfbce3SDimitry Andric } 10661cfbce3SDimitry Andric if (__c1 != __c2) 10761cfbce3SDimitry Andric return false; 10861cfbce3SDimitry Andric } 10961cfbce3SDimitry Andric } 11061cfbce3SDimitry Andric 11161cfbce3SDimitry Andric return true; 11261cfbce3SDimitry Andric } 11361cfbce3SDimitry Andric 11461cfbce3SDimitry Andric // 2+1 iterators, predicate. Not used by range algorithms. 11561cfbce3SDimitry Andric template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _BinaryPredicate> 116*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation( 117cb14a3feSDimitry Andric _ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _BinaryPredicate&& __pred) { 11861cfbce3SDimitry Andric // Shorten sequences as much as possible by lopping of any equal prefix. 11961cfbce3SDimitry Andric for (; __first1 != __last1; ++__first1, (void)++__first2) { 120fe6060f1SDimitry Andric if (!__pred(*__first1, *__first2)) 121fe6060f1SDimitry Andric break; 12261cfbce3SDimitry Andric } 12361cfbce3SDimitry Andric 124fe6060f1SDimitry Andric if (__first1 == __last1) 125fe6060f1SDimitry Andric return true; 126fe6060f1SDimitry Andric 127fe6060f1SDimitry Andric // __first1 != __last1 && *__first1 != *__first2 12861cfbce3SDimitry Andric using _D1 = __iter_diff_t<_ForwardIterator1>; 12961cfbce3SDimitry Andric _D1 __l1 = _IterOps<_AlgPolicy>::distance(__first1, __last1); 130fe6060f1SDimitry Andric if (__l1 == _D1(1)) 131fe6060f1SDimitry Andric return false; 13261cfbce3SDimitry Andric auto __last2 = _IterOps<_AlgPolicy>::next(__first2, __l1); 13361cfbce3SDimitry Andric 13461cfbce3SDimitry Andric return std::__is_permutation_impl<_AlgPolicy>( 135cb14a3feSDimitry Andric std::move(__first1), 136cb14a3feSDimitry Andric std::move(__last1), 137cb14a3feSDimitry Andric std::move(__first2), 138cb14a3feSDimitry Andric std::move(__last2), 139cb14a3feSDimitry Andric __pred, 140cb14a3feSDimitry Andric __identity(), 141cb14a3feSDimitry Andric __identity()); 142fe6060f1SDimitry Andric } 143fe6060f1SDimitry Andric 14461cfbce3SDimitry Andric // 2+2 iterators, predicate, non-constant time `distance`. 14561cfbce3SDimitry Andric template <class _AlgPolicy, 146cb14a3feSDimitry Andric class _Iter1, 147cb14a3feSDimitry Andric class _Sent1, 148cb14a3feSDimitry Andric class _Iter2, 149cb14a3feSDimitry Andric class _Sent2, 150cb14a3feSDimitry Andric class _Proj1, 151cb14a3feSDimitry Andric class _Proj2, 152cb14a3feSDimitry Andric class _Pred> 153cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation( 154cb14a3feSDimitry Andric _Iter1 __first1, 155cb14a3feSDimitry Andric _Sent1 __last1, 156cb14a3feSDimitry Andric _Iter2 __first2, 157cb14a3feSDimitry Andric _Sent2 __last2, 158cb14a3feSDimitry Andric _Pred&& __pred, 159cb14a3feSDimitry Andric _Proj1&& __proj1, 160cb14a3feSDimitry Andric _Proj2&& __proj2, 16161cfbce3SDimitry Andric /*_ConstTimeDistance=*/false_type) { 16261cfbce3SDimitry Andric // Shorten sequences as much as possible by lopping of any equal prefix. 16361cfbce3SDimitry Andric while (__first1 != __last1 && __first2 != __last2) { 16461cfbce3SDimitry Andric if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2))) 16561cfbce3SDimitry Andric break; 16661cfbce3SDimitry Andric ++__first1; 16761cfbce3SDimitry Andric ++__first2; 168fe6060f1SDimitry Andric } 169fe6060f1SDimitry Andric 170fe6060f1SDimitry Andric if (__first1 == __last1) 171fe6060f1SDimitry Andric return __first2 == __last2; 17261cfbce3SDimitry Andric if (__first2 == __last2) // Second range is shorter 173fe6060f1SDimitry Andric return false; 174fe6060f1SDimitry Andric 17561cfbce3SDimitry Andric using _D1 = __iter_diff_t<_Iter1>; 17661cfbce3SDimitry Andric _D1 __l1 = _IterOps<_AlgPolicy>::distance(__first1, __last1); 177fe6060f1SDimitry Andric 17861cfbce3SDimitry Andric using _D2 = __iter_diff_t<_Iter2>; 17961cfbce3SDimitry Andric _D2 __l2 = _IterOps<_AlgPolicy>::distance(__first2, __last2); 180fe6060f1SDimitry Andric if (__l1 != __l2) 181fe6060f1SDimitry Andric return false; 182fe6060f1SDimitry Andric 18361cfbce3SDimitry Andric return std::__is_permutation_impl<_AlgPolicy>( 184cb14a3feSDimitry Andric std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2); 185fe6060f1SDimitry Andric } 186fe6060f1SDimitry Andric 18761cfbce3SDimitry Andric // 2+2 iterators, predicate, specialization for constant-time `distance` call. 18861cfbce3SDimitry Andric template <class _AlgPolicy, 189cb14a3feSDimitry Andric class _Iter1, 190cb14a3feSDimitry Andric class _Sent1, 191cb14a3feSDimitry Andric class _Iter2, 192cb14a3feSDimitry Andric class _Sent2, 193cb14a3feSDimitry Andric class _Proj1, 194cb14a3feSDimitry Andric class _Proj2, 195cb14a3feSDimitry Andric class _Pred> 196cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation( 197cb14a3feSDimitry Andric _Iter1 __first1, 198cb14a3feSDimitry Andric _Sent1 __last1, 199cb14a3feSDimitry Andric _Iter2 __first2, 200cb14a3feSDimitry Andric _Sent2 __last2, 201cb14a3feSDimitry Andric _Pred&& __pred, 202cb14a3feSDimitry Andric _Proj1&& __proj1, 203cb14a3feSDimitry Andric _Proj2&& __proj2, 20461cfbce3SDimitry Andric /*_ConstTimeDistance=*/true_type) { 20561cfbce3SDimitry Andric if (std::distance(__first1, __last1) != std::distance(__first2, __last2)) 206fe6060f1SDimitry Andric return false; 20761cfbce3SDimitry Andric return std::__is_permutation<_AlgPolicy>( 208cb14a3feSDimitry Andric std::move(__first1), 209cb14a3feSDimitry Andric std::move(__last1), 210cb14a3feSDimitry Andric std::move(__first2), 211cb14a3feSDimitry Andric std::move(__last2), 212cb14a3feSDimitry Andric __pred, 213cb14a3feSDimitry Andric __proj1, 214cb14a3feSDimitry Andric __proj2, 21561cfbce3SDimitry Andric /*_ConstTimeDistance=*/false_type()); 216fe6060f1SDimitry Andric } 217fe6060f1SDimitry Andric 21861cfbce3SDimitry Andric // 2+2 iterators, predicate 21961cfbce3SDimitry Andric template <class _AlgPolicy, 220cb14a3feSDimitry Andric class _Iter1, 221cb14a3feSDimitry Andric class _Sent1, 222cb14a3feSDimitry Andric class _Iter2, 223cb14a3feSDimitry Andric class _Sent2, 224cb14a3feSDimitry Andric class _Proj1, 225cb14a3feSDimitry Andric class _Proj2, 226cb14a3feSDimitry Andric class _Pred> 227cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation( 228cb14a3feSDimitry Andric _Iter1 __first1, 229cb14a3feSDimitry Andric _Sent1 __last1, 230cb14a3feSDimitry Andric _Iter2 __first2, 231cb14a3feSDimitry Andric _Sent2 __last2, 232cb14a3feSDimitry Andric _Pred&& __pred, 233cb14a3feSDimitry Andric _Proj1&& __proj1, 234cb14a3feSDimitry Andric _Proj2&& __proj2) { 23561cfbce3SDimitry Andric return std::__is_permutation<_AlgPolicy>( 236cb14a3feSDimitry Andric std::move(__first1), 237cb14a3feSDimitry Andric std::move(__last1), 238cb14a3feSDimitry Andric std::move(__first2), 239cb14a3feSDimitry Andric std::move(__last2), 240cb14a3feSDimitry Andric __pred, 241cb14a3feSDimitry Andric __proj1, 242cb14a3feSDimitry Andric __proj2, 24361cfbce3SDimitry Andric _ConstTimeDistance<_Iter1, _Sent1, _Iter2, _Sent2>()); 24461cfbce3SDimitry Andric } 24561cfbce3SDimitry Andric 24661cfbce3SDimitry Andric // Public interface 24761cfbce3SDimitry Andric 24861cfbce3SDimitry Andric // 2+1 iterators, predicate 249fe6060f1SDimitry Andric template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 250*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool is_permutation( 251cb14a3feSDimitry Andric _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _BinaryPredicate __pred) { 25261cfbce3SDimitry Andric static_assert(__is_callable<_BinaryPredicate, decltype(*__first1), decltype(*__first2)>::value, 25361cfbce3SDimitry Andric "The predicate has to be callable"); 25461cfbce3SDimitry Andric 255cb14a3feSDimitry Andric return std::__is_permutation<_ClassicAlgPolicy>(std::move(__first1), std::move(__last1), std::move(__first2), __pred); 256fe6060f1SDimitry Andric } 257fe6060f1SDimitry Andric 25861cfbce3SDimitry Andric // 2+1 iterators 259fe6060f1SDimitry Andric template <class _ForwardIterator1, class _ForwardIterator2> 260*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool 26161cfbce3SDimitry Andric is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) { 262bdd1243dSDimitry Andric return std::is_permutation(__first1, __last1, __first2, __equal_to()); 26361cfbce3SDimitry Andric } 26461cfbce3SDimitry Andric 26506c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 14 26661cfbce3SDimitry Andric 26761cfbce3SDimitry Andric // 2+2 iterators 26861cfbce3SDimitry Andric template <class _ForwardIterator1, class _ForwardIterator2> 269*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool is_permutation( 270bdd1243dSDimitry Andric _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2) { 27161cfbce3SDimitry Andric return std::__is_permutation<_ClassicAlgPolicy>( 272bdd1243dSDimitry Andric std::move(__first1), 273bdd1243dSDimitry Andric std::move(__last1), 274bdd1243dSDimitry Andric std::move(__first2), 275bdd1243dSDimitry Andric std::move(__last2), 276bdd1243dSDimitry Andric __equal_to(), 277bdd1243dSDimitry Andric __identity(), 278bdd1243dSDimitry Andric __identity()); 279fe6060f1SDimitry Andric } 28061cfbce3SDimitry Andric 28161cfbce3SDimitry Andric // 2+2 iterators, predicate 28261cfbce3SDimitry Andric template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 283*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool is_permutation( 284cb14a3feSDimitry Andric _ForwardIterator1 __first1, 285cb14a3feSDimitry Andric _ForwardIterator1 __last1, 286cb14a3feSDimitry Andric _ForwardIterator2 __first2, 287cb14a3feSDimitry Andric _ForwardIterator2 __last2, 288cb14a3feSDimitry Andric _BinaryPredicate __pred) { 28961cfbce3SDimitry Andric static_assert(__is_callable<_BinaryPredicate, decltype(*__first1), decltype(*__first2)>::value, 29061cfbce3SDimitry Andric "The predicate has to be callable"); 29161cfbce3SDimitry Andric 29261cfbce3SDimitry Andric return std::__is_permutation<_ClassicAlgPolicy>( 293cb14a3feSDimitry Andric std::move(__first1), 294cb14a3feSDimitry Andric std::move(__last1), 295cb14a3feSDimitry Andric std::move(__first2), 296cb14a3feSDimitry Andric std::move(__last2), 297cb14a3feSDimitry Andric __pred, 298cb14a3feSDimitry Andric __identity(), 299cb14a3feSDimitry Andric __identity()); 30061cfbce3SDimitry Andric } 30161cfbce3SDimitry Andric 30206c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 14 303fe6060f1SDimitry Andric 304fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 305fe6060f1SDimitry Andric 30606c3fb27SDimitry Andric _LIBCPP_POP_MACROS 30706c3fb27SDimitry Andric 308fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_IS_PERMUTATION_H 309