Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:27:52

0001 /// \file
0002 // Range v3 library
0003 //
0004 //  Copyright Eric Niebler 2014-present
0005 //
0006 //  Use, modification and distribution is subject to the
0007 //  Boost Software License, Version 1.0. (See accompanying
0008 //  file LICENSE_1_0.txt or copy at
0009 //  http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // Project home: https://github.com/ericniebler/range-v3
0012 //
0013 // Copyright (c) 2009 Alexander Stepanov and Paul McJones
0014 //
0015 // Permission to use, copy, modify, distribute and sell this software
0016 // and its documentation for any purpose is hereby granted without
0017 // fee, provided that the above copyright notice appear in all copies
0018 // and that both that copyright notice and this permission notice
0019 // appear in supporting documentation. The authors make no
0020 // representations about the suitability of this software for any
0021 // purpose. It is provided "as is" without express or implied
0022 // warranty.
0023 //
0024 // Algorithms from
0025 // Elements of Programming
0026 // by Alexander Stepanov and Paul McJones
0027 // Addison-Wesley Professional, 2009
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     } // namespace aux
0111 } // namespace ranges
0112 
0113 #include <range/v3/detail/epilogue.hpp>
0114 
0115 #endif