unique_copy.h 4.7 KB

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