Path: blob/main/contrib/llvm-project/libcxx/include/__algorithm/equal.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_EQUAL_H10#define _LIBCPP___ALGORITHM_EQUAL_H1112#include <__algorithm/comp.h>13#include <__algorithm/unwrap_iter.h>14#include <__config>15#include <__functional/identity.h>16#include <__functional/invoke.h>17#include <__iterator/distance.h>18#include <__iterator/iterator_traits.h>19#include <__string/constexpr_c_functions.h>20#include <__type_traits/desugars_to.h>21#include <__type_traits/enable_if.h>22#include <__type_traits/is_constant_evaluated.h>23#include <__type_traits/is_equality_comparable.h>24#include <__type_traits/is_volatile.h>25#include <__utility/move.h>2627#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)28# pragma GCC system_header29#endif3031_LIBCPP_PUSH_MACROS32#include <__undef_macros>3334_LIBCPP_BEGIN_NAMESPACE_STD3536template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>37_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __equal_iter_impl(38_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate& __pred) {39for (; __first1 != __last1; ++__first1, (void)++__first2)40if (!__pred(*__first1, *__first2))41return false;42return true;43}4445template <class _Tp,46class _Up,47class _BinaryPredicate,48__enable_if_t<__desugars_to_v<__equal_tag, _BinaryPredicate, _Tp, _Up> && !is_volatile<_Tp>::value &&49!is_volatile<_Up>::value && __libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,50int> = 0>51_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool52__equal_iter_impl(_Tp* __first1, _Tp* __last1, _Up* __first2, _BinaryPredicate&) {53return std::__constexpr_memcmp_equal(__first1, __first2, __element_count(__last1 - __first1));54}5556template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>57_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool58equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) {59return std::__equal_iter_impl(60std::__unwrap_iter(__first1), std::__unwrap_iter(__last1), std::__unwrap_iter(__first2), __pred);61}6263template <class _InputIterator1, class _InputIterator2>64_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool65equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {66return std::equal(__first1, __last1, __first2, __equal_to());67}6869#if _LIBCPP_STD_VER >= 147071template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>72_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __equal_impl(73_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __comp, _Proj1& __proj1, _Proj2& __proj2) {74while (__first1 != __last1 && __first2 != __last2) {75if (!std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))76return false;77++__first1;78++__first2;79}80return __first1 == __last1 && __first2 == __last2;81}8283template <class _Tp,84class _Up,85class _Pred,86class _Proj1,87class _Proj2,88__enable_if_t<__desugars_to_v<__equal_tag, _Pred, _Tp, _Up> && __is_identity<_Proj1>::value &&89__is_identity<_Proj2>::value && !is_volatile<_Tp>::value && !is_volatile<_Up>::value &&90__libcpp_is_trivially_equality_comparable<_Tp, _Up>::value,91int> = 0>92_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool93__equal_impl(_Tp* __first1, _Tp* __last1, _Up* __first2, _Up*, _Pred&, _Proj1&, _Proj2&) {94return std::__constexpr_memcmp_equal(__first1, __first2, __element_count(__last1 - __first1));95}9697template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>98_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool99equal(_InputIterator1 __first1,100_InputIterator1 __last1,101_InputIterator2 __first2,102_InputIterator2 __last2,103_BinaryPredicate __pred) {104if constexpr (__has_random_access_iterator_category<_InputIterator1>::value &&105__has_random_access_iterator_category<_InputIterator2>::value) {106if (std::distance(__first1, __last1) != std::distance(__first2, __last2))107return false;108}109__identity __proj;110return std::__equal_impl(111std::__unwrap_iter(__first1),112std::__unwrap_iter(__last1),113std::__unwrap_iter(__first2),114std::__unwrap_iter(__last2),115__pred,116__proj,117__proj);118}119120template <class _InputIterator1, class _InputIterator2>121_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool122equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {123return std::equal(__first1, __last1, __first2, __last2, __equal_to());124}125126#endif // _LIBCPP_STD_VER >= 14127128_LIBCPP_END_NAMESPACE_STD129130_LIBCPP_POP_MACROS131132#endif // _LIBCPP___ALGORITHM_EQUAL_H133134135