ranges_merge.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #if _LIBCPP_STD_VER >= 20
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. namespace ranges {
  29. template <class _InIter1, class _InIter2, class _OutIter>
  30. using merge_result = in_in_out_result<_InIter1, _InIter2, _OutIter>;
  31. namespace __merge {
  32. template < class _InIter1,
  33. class _Sent1,
  34. class _InIter2,
  35. class _Sent2,
  36. class _OutIter,
  37. class _Comp,
  38. class _Proj1,
  39. class _Proj2>
  40. _LIBCPP_HIDE_FROM_ABI constexpr merge_result<__remove_cvref_t<_InIter1>,
  41. __remove_cvref_t<_InIter2>,
  42. __remove_cvref_t<_OutIter>>
  43. __merge_impl(_InIter1&& __first1,
  44. _Sent1&& __last1,
  45. _InIter2&& __first2,
  46. _Sent2&& __last2,
  47. _OutIter&& __result,
  48. _Comp&& __comp,
  49. _Proj1&& __proj1,
  50. _Proj2&& __proj2) {
  51. for (; __first1 != __last1 && __first2 != __last2; ++__result) {
  52. if (std::invoke(__comp, std::invoke(__proj2, *__first2), std::invoke(__proj1, *__first1))) {
  53. *__result = *__first2;
  54. ++__first2;
  55. } else {
  56. *__result = *__first1;
  57. ++__first1;
  58. }
  59. }
  60. auto __ret1 = ranges::copy(std::move(__first1), std::move(__last1), std::move(__result));
  61. auto __ret2 = ranges::copy(std::move(__first2), std::move(__last2), std::move(__ret1.out));
  62. return {std::move(__ret1.in), std::move(__ret2.in), std::move(__ret2.out)};
  63. }
  64. struct __fn {
  65. template <input_iterator _InIter1,
  66. sentinel_for<_InIter1> _Sent1,
  67. input_iterator _InIter2,
  68. sentinel_for<_InIter2> _Sent2,
  69. weakly_incrementable _OutIter,
  70. class _Comp = less,
  71. class _Proj1 = identity,
  72. class _Proj2 = identity>
  73. requires mergeable<_InIter1, _InIter2, _OutIter, _Comp, _Proj1, _Proj2>
  74. _LIBCPP_HIDE_FROM_ABI constexpr merge_result<_InIter1, _InIter2, _OutIter> operator()(
  75. _InIter1 __first1,
  76. _Sent1 __last1,
  77. _InIter2 __first2,
  78. _Sent2 __last2,
  79. _OutIter __result,
  80. _Comp __comp = {},
  81. _Proj1 __proj1 = {},
  82. _Proj2 __proj2 = {}) const {
  83. return __merge::__merge_impl(__first1, __last1, __first2, __last2, __result, __comp, __proj1, __proj2);
  84. }
  85. template <input_range _Range1,
  86. input_range _Range2,
  87. weakly_incrementable _OutIter,
  88. class _Comp = less,
  89. class _Proj1 = identity,
  90. class _Proj2 = identity>
  91. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _OutIter, _Comp, _Proj1, _Proj2>
  92. _LIBCPP_HIDE_FROM_ABI constexpr merge_result<borrowed_iterator_t<_Range1>, borrowed_iterator_t<_Range2>, _OutIter>
  93. operator()(_Range1&& __range1,
  94. _Range2&& __range2,
  95. _OutIter __result,
  96. _Comp __comp = {},
  97. _Proj1 __proj1 = {},
  98. _Proj2 __proj2 = {}) const {
  99. return __merge::__merge_impl(
  100. ranges::begin(__range1),
  101. ranges::end(__range1),
  102. ranges::begin(__range2),
  103. ranges::end(__range2),
  104. __result,
  105. __comp,
  106. __proj1,
  107. __proj2);
  108. }
  109. };
  110. } // namespace __merge
  111. inline namespace __cpo {
  112. inline constexpr auto merge = __merge::__fn{};
  113. } // namespace __cpo
  114. } // namespace ranges
  115. _LIBCPP_END_NAMESPACE_STD
  116. #endif // _LIBCPP_STD_VER >= 20
  117. #endif // _LIBCPP___ALGORITHM_RANGES_MERGE_H