xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/merge.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric 
9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_MERGE_H
10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_MERGE_H
11fe6060f1SDimitry Andric 
12fe6060f1SDimitry Andric #include <__algorithm/comp.h>
13fe6060f1SDimitry Andric #include <__algorithm/comp_ref_type.h>
14fe6060f1SDimitry Andric #include <__algorithm/copy.h>
1504eeddc0SDimitry Andric #include <__config>
16fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
17fe6060f1SDimitry Andric 
18fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19fe6060f1SDimitry Andric #  pragma GCC system_header
20fe6060f1SDimitry Andric #endif
21fe6060f1SDimitry Andric 
22fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
23fe6060f1SDimitry Andric 
24fe6060f1SDimitry Andric template <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
__merge(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_InputIterator2 __last2,_OutputIterator __result,_Compare __comp)25*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator __merge(
26*cb14a3feSDimitry Andric     _InputIterator1 __first1,
27*cb14a3feSDimitry Andric     _InputIterator1 __last1,
28*cb14a3feSDimitry Andric     _InputIterator2 __first2,
29*cb14a3feSDimitry Andric     _InputIterator2 __last2,
30*cb14a3feSDimitry Andric     _OutputIterator __result,
31*cb14a3feSDimitry Andric     _Compare __comp) {
32*cb14a3feSDimitry Andric   for (; __first1 != __last1; ++__result) {
33fe6060f1SDimitry Andric     if (__first2 == __last2)
345f757f3fSDimitry Andric       return std::copy(__first1, __last1, __result);
35*cb14a3feSDimitry Andric     if (__comp(*__first2, *__first1)) {
36fe6060f1SDimitry Andric       *__result = *__first2;
37fe6060f1SDimitry Andric       ++__first2;
38*cb14a3feSDimitry Andric     } else {
39fe6060f1SDimitry Andric       *__result = *__first1;
40fe6060f1SDimitry Andric       ++__first1;
41fe6060f1SDimitry Andric     }
42fe6060f1SDimitry Andric   }
435f757f3fSDimitry Andric   return std::copy(__first2, __last2, __result);
44fe6060f1SDimitry Andric }
45fe6060f1SDimitry Andric 
46fe6060f1SDimitry Andric template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>
47*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
merge(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_InputIterator2 __last2,_OutputIterator __result,_Compare __comp)48*cb14a3feSDimitry Andric merge(_InputIterator1 __first1,
49*cb14a3feSDimitry Andric       _InputIterator1 __last1,
50*cb14a3feSDimitry Andric       _InputIterator2 __first2,
51*cb14a3feSDimitry Andric       _InputIterator2 __last2,
52*cb14a3feSDimitry Andric       _OutputIterator __result,
53*cb14a3feSDimitry Andric       _Compare __comp) {
545f757f3fSDimitry Andric   return std::__merge<__comp_ref_type<_Compare> >(__first1, __last1, __first2, __last2, __result, __comp);
55fe6060f1SDimitry Andric }
56fe6060f1SDimitry Andric 
57fe6060f1SDimitry Andric template <class _InputIterator1, class _InputIterator2, class _OutputIterator>
58*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
merge(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_InputIterator2 __last2,_OutputIterator __result)59*cb14a3feSDimitry Andric merge(_InputIterator1 __first1,
60*cb14a3feSDimitry Andric       _InputIterator1 __last1,
61*cb14a3feSDimitry Andric       _InputIterator2 __first2,
62*cb14a3feSDimitry Andric       _InputIterator2 __last2,
63*cb14a3feSDimitry Andric       _OutputIterator __result) {
645f757f3fSDimitry Andric   return std::merge(__first1, __last1, __first2, __last2, __result, __less<>());
65fe6060f1SDimitry Andric }
66fe6060f1SDimitry Andric 
67fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
68fe6060f1SDimitry Andric 
69fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_MERGE_H
70