equal.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_EQUAL_H
  10. #define _LIBCPP___ALGORITHM_EQUAL_H
  11. #include <__algorithm/comp.h>
  12. #include <__config>
  13. #include <__iterator/distance.h>
  14. #include <__iterator/iterator_traits.h>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  20. _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  21. equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) {
  22. for (; __first1 != __last1; ++__first1, (void)++__first2)
  23. if (!__pred(*__first1, *__first2))
  24. return false;
  25. return true;
  26. }
  27. template <class _InputIterator1, class _InputIterator2>
  28. _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  29. equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {
  30. return std::equal(__first1, __last1, __first2, __equal_to());
  31. }
  32. #if _LIBCPP_STD_VER > 11
  33. template <class _BinaryPredicate, class _InputIterator1, class _InputIterator2>
  34. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  35. __equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
  36. _BinaryPredicate __pred, input_iterator_tag, input_iterator_tag) {
  37. for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void)++__first2)
  38. if (!__pred(*__first1, *__first2))
  39. return false;
  40. return __first1 == __last1 && __first2 == __last2;
  41. }
  42. template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
  43. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  44. __equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
  45. _RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag,
  46. random_access_iterator_tag) {
  47. if (_VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2))
  48. return false;
  49. return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2,
  50. _BinaryPredicate&>(__first1, __last1, __first2, __pred);
  51. }
  52. template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
  53. _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  54. equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
  55. _BinaryPredicate __pred) {
  56. return _VSTD::__equal<_BinaryPredicate&>(
  57. __first1, __last1, __first2, __last2, __pred, typename iterator_traits<_InputIterator1>::iterator_category(),
  58. typename iterator_traits<_InputIterator2>::iterator_category());
  59. }
  60. template <class _InputIterator1, class _InputIterator2>
  61. _LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
  62. equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
  63. return std::__equal(
  64. __first1,
  65. __last1,
  66. __first2,
  67. __last2,
  68. __equal_to(),
  69. typename iterator_traits<_InputIterator1>::iterator_category(),
  70. typename iterator_traits<_InputIterator2>::iterator_category());
  71. }
  72. #endif
  73. _LIBCPP_END_NAMESPACE_STD
  74. #endif // _LIBCPP___ALGORITHM_EQUAL_H