Back to home page

EIC code displayed by LXR

 
 

    


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

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 
0029 #ifndef RANGES_V3_ALGORITHM_MERGE_HPP
0030 #define RANGES_V3_ALGORITHM_MERGE_HPP
0031 
0032 #include <tuple>
0033 
0034 #include <range/v3/range_fwd.hpp>
0035 
0036 #include <range/v3/algorithm/copy.hpp>
0037 #include <range/v3/algorithm/result_types.hpp>
0038 #include <range/v3/functional/comparisons.hpp>
0039 #include <range/v3/functional/identity.hpp>
0040 #include <range/v3/functional/invoke.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/dangling.hpp>
0045 #include <range/v3/range/traits.hpp>
0046 #include <range/v3/utility/static_const.hpp>
0047 
0048 #include <range/v3/detail/prologue.hpp>
0049 
0050 namespace ranges
0051 {
0052     /// \addtogroup group-algorithms
0053     /// @{
0054     template<typename I0, typename I1, typename O>
0055     using merge_result = detail::in1_in2_out_result<I0, I1, O>;
0056 
0057     RANGES_FUNC_BEGIN(merge)
0058 
0059         /// \brief function template \c merge
0060         template(typename I0,
0061                  typename S0,
0062                  typename I1,
0063                  typename S1,
0064                  typename O,
0065                  typename C = less,
0066                  typename P0 = identity,
0067                  typename P1 = identity)(
0068             requires sentinel_for<S0, I0> AND sentinel_for<S1, I1> AND
0069                 mergeable<I0, I1, O, C, P0, P1>)
0070         constexpr merge_result<I0, I1, O> RANGES_FUNC(merge)(I0 begin0,
0071                                                              S0 end0,
0072                                                              I1 begin1,
0073                                                              S1 end1,
0074                                                              O out,
0075                                                              C pred = C{},
0076                                                              P0 proj0 = P0{},
0077                                                              P1 proj1 = P1{}) //
0078         {
0079             for(; begin0 != end0 && begin1 != end1; ++out)
0080             {
0081                 if(invoke(pred, invoke(proj1, *begin1), invoke(proj0, *begin0)))
0082                 {
0083                     *out = *begin1;
0084                     ++begin1;
0085                 }
0086                 else
0087                 {
0088                     *out = *begin0;
0089                     ++begin0;
0090                 }
0091             }
0092             auto t0 = ranges::copy(begin0, end0, out);
0093             auto t1 = ranges::copy(begin1, end1, t0.out);
0094             return {t0.in, t1.in, t1.out};
0095         }
0096 
0097         /// \overload
0098         template(typename Rng0,
0099                  typename Rng1,
0100                  typename O,
0101                  typename C = less,
0102                  typename P0 = identity,
0103                  typename P1 = identity)(
0104             requires range<Rng0> AND range<Rng1> AND
0105                 mergeable<iterator_t<Rng0>, iterator_t<Rng1>, O, C, P0, P1>)
0106         constexpr merge_result<borrowed_iterator_t<Rng0>, borrowed_iterator_t<Rng1>, O>
0107         RANGES_FUNC(merge)(Rng0 && rng0,
0108                            Rng1 && rng1,
0109                            O out,
0110                            C pred = C{},
0111                            P0 proj0 = P0{},
0112                            P1 proj1 = P1{})
0113         {
0114             return (*this)(begin(rng0),
0115                            end(rng0),
0116                            begin(rng1),
0117                            end(rng1),
0118                            std::move(out),
0119                            std::move(pred),
0120                            std::move(proj0),
0121                            std::move(proj1));
0122         }
0123 
0124     RANGES_FUNC_END(merge)
0125 
0126     namespace cpp20
0127     {
0128         using ranges::merge;
0129         using ranges::merge_result;
0130     } // namespace cpp20
0131     /// @}
0132 } // namespace ranges
0133 
0134 #include <range/v3/detail/epilogue.hpp>
0135 
0136 #endif