unique_copy.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_COPY_H
  9. #define _LIBCPP___ALGORITHM_UNIQUE_COPY_H
  10. #include <__algorithm/comp.h>
  11. #include <__algorithm/iterator_operations.h>
  12. #include <__config>
  13. #include <__iterator/iterator_traits.h>
  14. #include <__type_traits/conditional.h>
  15. #include <__type_traits/is_base_of.h>
  16. #include <__type_traits/is_same.h>
  17. #include <__utility/move.h>
  18. #include <__utility/pair.h>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. namespace __unique_copy_tags {
  24. struct __reread_from_input_tag {};
  25. struct __reread_from_output_tag {};
  26. struct __read_from_tmp_value_tag {};
  27. } // namespace __unique_copy_tags
  28. template <class _AlgPolicy, class _BinaryPredicate, class _InputIterator, class _Sent, class _OutputIterator>
  29. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _OutputIterator>
  30. __unique_copy(_InputIterator __first,
  31. _Sent __last,
  32. _OutputIterator __result,
  33. _BinaryPredicate&& __pred,
  34. __unique_copy_tags::__read_from_tmp_value_tag) {
  35. if (__first != __last) {
  36. typename _IterOps<_AlgPolicy>::template __value_type<_InputIterator> __t(*__first);
  37. *__result = __t;
  38. ++__result;
  39. while (++__first != __last) {
  40. if (!__pred(__t, *__first)) {
  41. __t = *__first;
  42. *__result = __t;
  43. ++__result;
  44. }
  45. }
  46. }
  47. return pair<_InputIterator, _OutputIterator>(std::move(__first), std::move(__result));
  48. }
  49. template <class _AlgPolicy, class _BinaryPredicate, class _ForwardIterator, class _Sent, class _OutputIterator>
  50. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _OutputIterator>
  51. __unique_copy(_ForwardIterator __first,
  52. _Sent __last,
  53. _OutputIterator __result,
  54. _BinaryPredicate&& __pred,
  55. __unique_copy_tags::__reread_from_input_tag) {
  56. if (__first != __last) {
  57. _ForwardIterator __i = __first;
  58. *__result = *__i;
  59. ++__result;
  60. while (++__first != __last) {
  61. if (!__pred(*__i, *__first)) {
  62. *__result = *__first;
  63. ++__result;
  64. __i = __first;
  65. }
  66. }
  67. }
  68. return pair<_ForwardIterator, _OutputIterator>(std::move(__first), std::move(__result));
  69. }
  70. template <class _AlgPolicy, class _BinaryPredicate, class _InputIterator, class _Sent, class _InputAndOutputIterator>
  71. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _InputAndOutputIterator>
  72. __unique_copy(_InputIterator __first,
  73. _Sent __last,
  74. _InputAndOutputIterator __result,
  75. _BinaryPredicate&& __pred,
  76. __unique_copy_tags::__reread_from_output_tag) {
  77. if (__first != __last) {
  78. *__result = *__first;
  79. while (++__first != __last)
  80. if (!__pred(*__result, *__first))
  81. *++__result = *__first;
  82. ++__result;
  83. }
  84. return pair<_InputIterator, _InputAndOutputIterator>(std::move(__first), std::move(__result));
  85. }
  86. template <class _InputIterator, class _OutputIterator, class _BinaryPredicate>
  87. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
  88. unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred) {
  89. using __algo_tag = __conditional_t<
  90. is_base_of<forward_iterator_tag, typename iterator_traits<_InputIterator>::iterator_category>::value,
  91. __unique_copy_tags::__reread_from_input_tag,
  92. __conditional_t<
  93. is_base_of<forward_iterator_tag, typename iterator_traits<_OutputIterator>::iterator_category>::value &&
  94. is_same< typename iterator_traits<_InputIterator>::value_type,
  95. typename iterator_traits<_OutputIterator>::value_type>::value,
  96. __unique_copy_tags::__reread_from_output_tag,
  97. __unique_copy_tags::__read_from_tmp_value_tag> >;
  98. return std::__unique_copy<_ClassicAlgPolicy>(
  99. std::move(__first), std::move(__last), std::move(__result), __pred, __algo_tag())
  100. .second;
  101. }
  102. template <class _InputIterator, class _OutputIterator>
  103. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
  104. unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) {
  105. return std::unique_copy(std::move(__first), std::move(__last), std::move(__result), __equal_to());
  106. }
  107. _LIBCPP_END_NAMESPACE_STD
  108. #endif // _LIBCPP___ALGORITHM_UNIQUE_COPY_H