adjacent_find.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_ADJACENT_FIND_H
  10. #define _LIBCPP___ALGORITHM_ADJACENT_FIND_H
  11. #include <__algorithm/comp.h>
  12. #include <__algorithm/iterator_operations.h>
  13. #include <__config>
  14. #include <__iterator/iterator_traits.h>
  15. #include <__utility/move.h>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_PUSH_MACROS
  20. #include <__undef_macros>
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. template <class _Iter, class _Sent, class _BinaryPredicate>
  23. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Iter
  24. __adjacent_find(_Iter __first, _Sent __last, _BinaryPredicate&& __pred) {
  25. if (__first == __last)
  26. return __first;
  27. _Iter __i = __first;
  28. while (++__i != __last) {
  29. if (__pred(*__first, *__i))
  30. return __first;
  31. __first = __i;
  32. }
  33. return __i;
  34. }
  35. template <class _ForwardIterator, class _BinaryPredicate>
  36. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
  37. adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) {
  38. return std::__adjacent_find(std::move(__first), std::move(__last), __pred);
  39. }
  40. template <class _ForwardIterator>
  41. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
  42. adjacent_find(_ForwardIterator __first, _ForwardIterator __last) {
  43. return std::adjacent_find(std::move(__first), std::move(__last), __equal_to());
  44. }
  45. _LIBCPP_END_NAMESPACE_STD
  46. _LIBCPP_POP_MACROS
  47. #endif // _LIBCPP___ALGORITHM_ADJACENT_FIND_H