File indexing completed on 2025-12-16 10:27:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #ifndef RANGES_V3_ALGORITHM_AUX_MERGE_N_HPP
0029 #define RANGES_V3_ALGORITHM_AUX_MERGE_N_HPP
0030
0031 #include <tuple>
0032
0033 #include <range/v3/range_fwd.hpp>
0034
0035 #include <range/v3/algorithm/copy_n.hpp>
0036 #include <range/v3/algorithm/result_types.hpp>
0037 #include <range/v3/functional/comparisons.hpp>
0038 #include <range/v3/functional/identity.hpp>
0039 #include <range/v3/functional/invoke.hpp>
0040 #include <range/v3/iterator/operations.hpp>
0041 #include <range/v3/iterator/traits.hpp>
0042 #include <range/v3/range/access.hpp>
0043 #include <range/v3/range/concepts.hpp>
0044 #include <range/v3/range/traits.hpp>
0045 #include <range/v3/utility/static_const.hpp>
0046
0047 #include <range/v3/detail/prologue.hpp>
0048
0049 namespace ranges
0050 {
0051 namespace aux
0052 {
0053 template<typename I0, typename I1, typename O>
0054 using merge_n_result = detail::in1_in2_out_result<I0, I1, O>;
0055
0056 struct merge_n_fn
0057 {
0058 template(typename I0, typename I1, typename O, typename C = less,
0059 typename P0 = identity, typename P1 = identity)(
0060 requires mergeable<I0, I1, O, C, P0, P1>)
0061 merge_n_result<I0, I1, O> operator()(I0 begin0,
0062 iter_difference_t<I0> n0,
0063 I1 begin1,
0064 iter_difference_t<I1> n1,
0065 O out,
0066 C r = C{},
0067 P0 p0 = P0{},
0068 P1 p1 = P1{}) const
0069 {
0070 using T = merge_n_result<I0, I1, O>;
0071 auto n0orig = n0;
0072 auto n1orig = n1;
0073 auto b0 = uncounted(begin0);
0074 auto b1 = uncounted(begin1);
0075 while(true)
0076 {
0077 if(0 == n0)
0078 {
0079 auto res = copy_n(b1, n1, out);
0080 begin0 = recounted(begin0, b0, n0orig);
0081 begin1 = recounted(begin1, res.in, n1orig);
0082 return T{begin0, begin1, res.out};
0083 }
0084 if(0 == n1)
0085 {
0086 auto res = copy_n(b0, n0, out);
0087 begin0 = recounted(begin0, res.in, n0orig);
0088 begin1 = recounted(begin1, b1, n1orig);
0089 return T{begin0, begin1, res.out};
0090 }
0091 if(invoke(r, invoke(p1, *b1), invoke(p0, *b0)))
0092 {
0093 *out = *b1;
0094 ++b1;
0095 ++out;
0096 --n1;
0097 }
0098 else
0099 {
0100 *out = *b0;
0101 ++b0;
0102 ++out;
0103 --n0;
0104 }
0105 }
0106 }
0107 };
0108
0109 RANGES_INLINE_VARIABLE(merge_n_fn, merge_n)
0110 }
0111 }
0112
0113 #include <range/v3/detail/epilogue.hpp>
0114
0115 #endif