unique.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ALGORITHM_UNIQUE_H
  9. #define _LIBCPP___ALGORITHM_UNIQUE_H
  10. #include <__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. #include <__utility/pair.h>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_PUSH_MACROS
  21. #include <__undef_macros>
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. // unique
  24. template <class _AlgPolicy, class _Iter, class _Sent, class _BinaryPredicate>
  25. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 std::pair<_Iter, _Iter>
  26. __unique(_Iter __first, _Sent __last, _BinaryPredicate&& __pred) {
  27. __first = std::__adjacent_find(__first, __last, __pred);
  28. if (__first != __last) {
  29. // ... a a ? ...
  30. // f i
  31. _Iter __i = __first;
  32. for (++__i; ++__i != __last;)
  33. if (!__pred(*__first, *__i))
  34. *++__first = _IterOps<_AlgPolicy>::__iter_move(__i);
  35. ++__first;
  36. return std::pair<_Iter, _Iter>(std::move(__first), std::move(__i));
  37. }
  38. return std::pair<_Iter, _Iter>(__first, __first);
  39. }
  40. template <class _ForwardIterator, class _BinaryPredicate>
  41. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
  42. unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) {
  43. return std::__unique<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __pred).first;
  44. }
  45. template <class _ForwardIterator>
  46. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
  47. unique(_ForwardIterator __first, _ForwardIterator __last) {
  48. return std::unique(__first, __last, __equal_to());
  49. }
  50. _LIBCPP_END_NAMESPACE_STD
  51. _LIBCPP_POP_MACROS
  52. #endif // _LIBCPP___ALGORITHM_UNIQUE_H