Horizon
Loading...
Searching...
No Matches
merge.hpp
Go to the documentation of this file.
1
2// Range v3 library
3//
4// Copyright Eric Niebler 2014-present
5//
6// Use, modification and distribution is subject to the
7// Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at
9// http://www.boost.org/LICENSE_1_0.txt)
10//
11// Project home: https://github.com/ericniebler/range-v3
12//
13// Copyright (c) 2009 Alexander Stepanov and Paul McJones
14//
15// Permission to use, copy, modify, distribute and sell this software
16// and its documentation for any purpose is hereby granted without
17// fee, provided that the above copyright notice appear in all copies
18// and that both that copyright notice and this permission notice
19// appear in supporting documentation. The authors make no
20// representations about the suitability of this software for any
21// purpose. It is provided "as is" without express or implied
22// warranty.
23//
24// Algorithms from
25// Elements of Programming
26// by Alexander Stepanov and Paul McJones
27// Addison-Wesley Professional, 2009
28
29#ifndef RANGES_V3_ALGORITHM_MERGE_HPP
30#define RANGES_V3_ALGORITHM_MERGE_HPP
31
32#include <tuple>
33
35
37#include <range/v3/algorithm/result_types.hpp>
46#include <range/v3/utility/static_const.hpp>
47
48#include <range/v3/detail/prologue.hpp>
49
50namespace ranges
51{
54 template<typename I0, typename I1, typename O>
55 using merge_result = detail::in1_in2_out_result<I0, I1, O>;
56
57 RANGES_FUNC_BEGIN(merge)
58
59
60 template(typename I0,
61 typename S0,
62 typename I1,
63 typename S1,
64 typename O,
65 typename C = less,
66 typename P0 = identity,
67 typename P1 = identity)(
68 requires sentinel_for<S0, I0> AND sentinel_for<S1, I1> AND
69 mergeable<I0, I1, O, C, P0, P1>)
70 constexpr merge_result<I0, I1, O> RANGES_FUNC(merge)(I0 begin0,
71 S0 end0,
72 I1 begin1,
73 S1 end1,
74 O out,
75 C pred = C{},
76 P0 proj0 = P0{},
77 P1 proj1 = P1{}) //
78 {
79 for(; begin0 != end0 && begin1 != end1; ++out)
80 {
81 if(invoke(pred, invoke(proj1, *begin1), invoke(proj0, *begin0)))
82 {
83 *out = *begin1;
84 ++begin1;
85 }
86 else
87 {
88 *out = *begin0;
89 ++begin0;
90 }
91 }
92 auto t0 = ranges::copy(begin0, end0, out);
93 auto t1 = ranges::copy(begin1, end1, t0.out);
94 return {t0.in, t1.in, t1.out};
95 }
96
98 template(typename Rng0,
99 typename Rng1,
100 typename O,
101 typename C = less,
102 typename P0 = identity,
103 typename P1 = identity)(
104 requires range<Rng0> AND range<Rng1> AND
105 mergeable<iterator_t<Rng0>, iterator_t<Rng1>, O, C, P0, P1>)
106 constexpr merge_result<borrowed_iterator_t<Rng0>, borrowed_iterator_t<Rng1>, O>
107 RANGES_FUNC(merge)(Rng0 && rng0,
108 Rng1 && rng1,
109 O out,
110 C pred = C{},
111 P0 proj0 = P0{},
112 P1 proj1 = P1{})
113 {
114 return (*this)(begin(rng0),
115 end(rng0),
116 begin(rng1),
117 end(rng1),
118 std::move(out),
119 std::move(pred),
120 std::move(proj0),
121 std::move(proj1));
122 }
123
124 RANGES_FUNC_END(merge)
125
126 namespace cpp20
127 {
128 using ranges::merge;
129 using ranges::merge_result;
130 } // namespace cpp20
132} // namespace ranges
133
134#include <range/v3/detail/epilogue.hpp>
135
136#endif
The range concept.
The sentinel_for concept.
decltype(begin(declval(Rng &))) iterator_t
Definition access.hpp:698
typename Fn::template invoke< Args... > invoke
Evaluate the invocable Fn with the arguments Args.
Definition meta.hpp:541
Definition identity.hpp:25
Definition comparisons.hpp:50