tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
merge.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_MERGE_HPP
5#define TETL_ALGORITHM_MERGE_HPP
6
7#include <etl/_algorithm/copy.hpp>
8#include <etl/_functional/less.hpp>
9
10namespace etl {
11
12/// \brief Merges two sorted ranges `[first1, last1)` and `[first2, last2)` into
13/// one sorted range beginning at `destination`.
14/// \ingroup algorithm
15template <typename InputIt1, typename InputIt2, typename OutputIt, typename Compare>
16constexpr auto
17merge(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) -> OutputIt
18{
19 for (; first1 != last1; ++destination) {
20 if (first2 == last2) {
21 return etl::copy(first1, last1, destination);
22 }
23 if (comp(*first2, *first1)) {
24 *destination = *first2;
25 ++first2;
26 } else {
27 *destination = *first1;
28 ++first1;
29 }
30 }
31 return etl::copy(first2, last2, destination);
32}
33
34/// \ingroup algorithm
35template <typename InputIt1, typename InputIt2, typename OutputIt>
36constexpr auto merge(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) -> OutputIt
37{
38 return etl::merge(first1, last1, first2, last2, destination, etl::less());
39}
40
41} // namespace etl
42
43#endif // TETL_ALGORITHM_MERGE_HPP
constexpr auto merge(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) -> OutputIt
Definition merge.hpp:36
constexpr auto merge(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) -> OutputIt
Merges two sorted ranges [first1, last1) and [first2, last2) into one sorted range beginning at desti...
Definition merge.hpp:17
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15