ranges_merge.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_RANGES_MERGE_H
  9. #define _LIBCPP___ALGORITHM_RANGES_MERGE_H
  10. #include <__algorithm/in_in_out_result.h>
  11. #include <__algorithm/ranges_copy.h>
  12. #include <__config>
  13. #include <__functional/identity.h>
  14. #include <__functional/invoke.h>
  15. #include <__functional/ranges_operations.h>
  16. #include <__iterator/concepts.h>
  17. #include <__iterator/mergeable.h>
  18. #include <__ranges/access.h>
  19. #include <__ranges/concepts.h>
  20. #include <__ranges/dangling.h>
  21. #include <__type_traits/remove_cvref.h>
  22. #include <__utility/move.h>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. _LIBCPP_PUSH_MACROS
  27. #include <__undef_macros>
  28. #if _LIBCPP_STD_VER >= 20
  29. _LIBCPP_BEGIN_NAMESPACE_STD
  30. namespace ranges {
  31. template <class _InIter1, class _InIter2, class _OutIter>
  32. using merge_result = in_in_out_result<_InIter1, _InIter2, _OutIter>;
  33. namespace __merge {
  34. template < class _InIter1,
  35. class _Sent1,
  36. class _InIter2,
  37. class _Sent2,
  38. class _OutIter,
  39. class _Comp,
  40. class _Proj1,
  41. class _Proj2>
  42. _LIBCPP_HIDE_FROM_ABI constexpr merge_result<__remove_cvref_t<_InIter1>,
  43. __remove_cvref_t<_InIter2>,
  44. __remove_cvref_t<_OutIter>>
  45. __merge_impl(_InIter1&& __first1,
  46. _Sent1&& __last1,
  47. _InIter2&& __first2,
  48. _Sent2&& __last2,
  49. _OutIter&& __result,
  50. _Comp&& __comp,
  51. _Proj1&& __proj1,
  52. _Proj2&& __proj2) {
  53. for (; __first1 != __last1 && __first2 != __last2; ++__result) {
  54. if (std::invoke(__comp, std::invoke(__proj2, *__first2), std::invoke(__proj1, *__first1))) {
  55. *__result = *__first2;
  56. ++__first2;
  57. } else {
  58. *__result = *__first1;
  59. ++__first1;
  60. }
  61. }
  62. auto __ret1 = ranges::copy(std::move(__first1), std::move(__last1), std::move(__result));
  63. auto __ret2 = ranges::copy(std::move(__first2), std::move(__last2), std::move(__ret1.out));
  64. return {std::move(__ret1.in), std::move(__ret2.in), std::move(__ret2.out)};
  65. }
  66. struct __fn {
  67. template <input_iterator _InIter1,
  68. sentinel_for<_InIter1> _Sent1,
  69. input_iterator _InIter2,
  70. sentinel_for<_InIter2> _Sent2,
  71. weakly_incrementable _OutIter,
  72. class _Comp = less,
  73. class _Proj1 = identity,
  74. class _Proj2 = identity>
  75. requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2>
  76. _LIBCPP_HIDE_FROM_ABI constexpr merge_result<_InIter1, _InIter2, _OutIter> operator()(
  77. _InIter1 __first1,
  78. _Sent1 __last1,
  79. _InIter2 __first2,
  80. _Sent2 __last2,
  81. _OutIter __result,
  82. _Comp __comp = {},
  83. _Proj1 __proj1 = {},
  84. _Proj2 __proj2 = {}) const {
  85. return __merge::__merge_impl(__first1, __last1, __first2, __last2, __result, __comp, __proj1, __proj2);
  86. }
  87. template <input_range _Range1,
  88. input_range _Range2,
  89. weakly_incrementable _OutIter,
  90. class _Comp = less,
  91. class _Proj1 = identity,
  92. class _Proj2 = identity>
  93. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _OutIter, _Comp, _Proj1, _Proj2>
  94. _LIBCPP_HIDE_FROM_ABI constexpr merge_result<borrowed_iterator_t<_Range1>, borrowed_iterator_t<_Range2>, _OutIter>
  95. operator()(_Range1&& __range1,
  96. _Range2&& __range2,
  97. _OutIter __result,
  98. _Comp __comp = {},
  99. _Proj1 __proj1 = {},
  100. _Proj2 __proj2 = {}) const {
  101. return __merge::__merge_impl(
  102. ranges::begin(__range1),
  103. ranges::end(__range1),
  104. ranges::begin(__range2),
  105. ranges::end(__range2),
  106. __result,
  107. __comp,
  108. __proj1,
  109. __proj2);
  110. }
  111. };
  112. } // namespace __merge
  113. inline namespace __cpo {
  114. inline constexpr auto merge = __merge::__fn{};
  115. } // namespace __cpo
  116. } // namespace ranges
  117. _LIBCPP_END_NAMESPACE_STD
  118. #endif // _LIBCPP_STD_VER >= 20
  119. _LIBCPP_POP_MACROS
  120. #endif // _LIBCPP___ALGORITHM_RANGES_MERGE_H