Path: blob/main/contrib/llvm-project/libcxx/include/__algorithm/is_permutation.h
35232 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___ALGORITHM_IS_PERMUTATION_H10#define _LIBCPP___ALGORITHM_IS_PERMUTATION_H1112#include <__algorithm/comp.h>13#include <__algorithm/iterator_operations.h>14#include <__config>15#include <__functional/identity.h>16#include <__functional/invoke.h>17#include <__iterator/concepts.h>18#include <__iterator/distance.h>19#include <__iterator/iterator_traits.h>20#include <__iterator/next.h>21#include <__type_traits/is_callable.h>22#include <__utility/move.h>2324#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)25# pragma GCC system_header26#endif2728_LIBCPP_PUSH_MACROS29#include <__undef_macros>3031_LIBCPP_BEGIN_NAMESPACE_STD3233template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class = void>34struct _ConstTimeDistance : false_type {};3536#if _LIBCPP_STD_VER >= 203738template <class _Iter1, class _Sent1, class _Iter2, class _Sent2>39struct _ConstTimeDistance<_Iter1,40_Sent1,41_Iter2,42_Sent2,43__enable_if_t< sized_sentinel_for<_Sent1, _Iter1> && sized_sentinel_for<_Sent2, _Iter2> >>44: true_type {};4546#else4748template <class _Iter1, class _Iter2>49struct _ConstTimeDistance<50_Iter1,51_Iter1,52_Iter2,53_Iter2,54__enable_if_t< is_same<typename iterator_traits<_Iter1>::iterator_category, random_access_iterator_tag>::value &&55is_same<typename iterator_traits<_Iter2>::iterator_category, random_access_iterator_tag>::value > >56: true_type {};5758#endif // _LIBCPP_STD_VER >= 205960// Internal functions6162// For each element in [f1, l1) see if there are the same number of equal elements in [f2, l2)63template <class _AlgPolicy,64class _Iter1,65class _Sent1,66class _Iter2,67class _Sent2,68class _Proj1,69class _Proj2,70class _Pred>71_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation_impl(72_Iter1 __first1,73_Sent1 __last1,74_Iter2 __first2,75_Sent2 __last2,76_Pred&& __pred,77_Proj1&& __proj1,78_Proj2&& __proj2) {79using _D1 = __iter_diff_t<_Iter1>;8081for (auto __i = __first1; __i != __last1; ++__i) {82// Have we already counted the number of *__i in [f1, l1)?83auto __match = __first1;84for (; __match != __i; ++__match) {85if (std::__invoke(__pred, std::__invoke(__proj1, *__match), std::__invoke(__proj1, *__i)))86break;87}8889if (__match == __i) {90// Count number of *__i in [f2, l2)91_D1 __c2 = 0;92for (auto __j = __first2; __j != __last2; ++__j) {93if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj2, *__j)))94++__c2;95}96if (__c2 == 0)97return false;9899// Count number of *__i in [__i, l1) (we can start with 1)100_D1 __c1 = 1;101for (auto __j = _IterOps<_AlgPolicy>::next(__i); __j != __last1; ++__j) {102if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, *__j)))103++__c1;104}105if (__c1 != __c2)106return false;107}108}109110return true;111}112113// 2+1 iterators, predicate. Not used by range algorithms.114template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _BinaryPredicate>115_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(116_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _BinaryPredicate&& __pred) {117// Shorten sequences as much as possible by lopping of any equal prefix.118for (; __first1 != __last1; ++__first1, (void)++__first2) {119if (!__pred(*__first1, *__first2))120break;121}122123if (__first1 == __last1)124return true;125126// __first1 != __last1 && *__first1 != *__first2127using _D1 = __iter_diff_t<_ForwardIterator1>;128_D1 __l1 = _IterOps<_AlgPolicy>::distance(__first1, __last1);129if (__l1 == _D1(1))130return false;131auto __last2 = _IterOps<_AlgPolicy>::next(__first2, __l1);132133return std::__is_permutation_impl<_AlgPolicy>(134std::move(__first1),135std::move(__last1),136std::move(__first2),137std::move(__last2),138__pred,139__identity(),140__identity());141}142143// 2+2 iterators, predicate, non-constant time `distance`.144template <class _AlgPolicy,145class _Iter1,146class _Sent1,147class _Iter2,148class _Sent2,149class _Proj1,150class _Proj2,151class _Pred>152_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(153_Iter1 __first1,154_Sent1 __last1,155_Iter2 __first2,156_Sent2 __last2,157_Pred&& __pred,158_Proj1&& __proj1,159_Proj2&& __proj2,160/*_ConstTimeDistance=*/false_type) {161// Shorten sequences as much as possible by lopping of any equal prefix.162while (__first1 != __last1 && __first2 != __last2) {163if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))164break;165++__first1;166++__first2;167}168169if (__first1 == __last1)170return __first2 == __last2;171if (__first2 == __last2) // Second range is shorter172return false;173174using _D1 = __iter_diff_t<_Iter1>;175_D1 __l1 = _IterOps<_AlgPolicy>::distance(__first1, __last1);176177using _D2 = __iter_diff_t<_Iter2>;178_D2 __l2 = _IterOps<_AlgPolicy>::distance(__first2, __last2);179if (__l1 != __l2)180return false;181182return std::__is_permutation_impl<_AlgPolicy>(183std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2);184}185186// 2+2 iterators, predicate, specialization for constant-time `distance` call.187template <class _AlgPolicy,188class _Iter1,189class _Sent1,190class _Iter2,191class _Sent2,192class _Proj1,193class _Proj2,194class _Pred>195_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(196_Iter1 __first1,197_Sent1 __last1,198_Iter2 __first2,199_Sent2 __last2,200_Pred&& __pred,201_Proj1&& __proj1,202_Proj2&& __proj2,203/*_ConstTimeDistance=*/true_type) {204if (std::distance(__first1, __last1) != std::distance(__first2, __last2))205return false;206return std::__is_permutation<_AlgPolicy>(207std::move(__first1),208std::move(__last1),209std::move(__first2),210std::move(__last2),211__pred,212__proj1,213__proj2,214/*_ConstTimeDistance=*/false_type());215}216217// 2+2 iterators, predicate218template <class _AlgPolicy,219class _Iter1,220class _Sent1,221class _Iter2,222class _Sent2,223class _Proj1,224class _Proj2,225class _Pred>226_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(227_Iter1 __first1,228_Sent1 __last1,229_Iter2 __first2,230_Sent2 __last2,231_Pred&& __pred,232_Proj1&& __proj1,233_Proj2&& __proj2) {234return std::__is_permutation<_AlgPolicy>(235std::move(__first1),236std::move(__last1),237std::move(__first2),238std::move(__last2),239__pred,240__proj1,241__proj2,242_ConstTimeDistance<_Iter1, _Sent1, _Iter2, _Sent2>());243}244245// Public interface246247// 2+1 iterators, predicate248template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>249_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool is_permutation(250_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _BinaryPredicate __pred) {251static_assert(__is_callable<_BinaryPredicate, decltype(*__first1), decltype(*__first2)>::value,252"The predicate has to be callable");253254return std::__is_permutation<_ClassicAlgPolicy>(std::move(__first1), std::move(__last1), std::move(__first2), __pred);255}256257// 2+1 iterators258template <class _ForwardIterator1, class _ForwardIterator2>259_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool260is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2) {261return std::is_permutation(__first1, __last1, __first2, __equal_to());262}263264#if _LIBCPP_STD_VER >= 14265266// 2+2 iterators267template <class _ForwardIterator1, class _ForwardIterator2>268_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool is_permutation(269_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2) {270return std::__is_permutation<_ClassicAlgPolicy>(271std::move(__first1),272std::move(__last1),273std::move(__first2),274std::move(__last2),275__equal_to(),276__identity(),277__identity());278}279280// 2+2 iterators, predicate281template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>282_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool is_permutation(283_ForwardIterator1 __first1,284_ForwardIterator1 __last1,285_ForwardIterator2 __first2,286_ForwardIterator2 __last2,287_BinaryPredicate __pred) {288static_assert(__is_callable<_BinaryPredicate, decltype(*__first1), decltype(*__first2)>::value,289"The predicate has to be callable");290291return std::__is_permutation<_ClassicAlgPolicy>(292std::move(__first1),293std::move(__last1),294std::move(__first2),295std::move(__last2),296__pred,297__identity(),298__identity());299}300301#endif // _LIBCPP_STD_VER >= 14302303_LIBCPP_END_NAMESPACE_STD304305_LIBCPP_POP_MACROS306307#endif // _LIBCPP___ALGORITHM_IS_PERMUTATION_H308309310