tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
set_union.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_SET_UNION_HPP
5#define TETL_ALGORITHM_SET_UNION_HPP
6
7#include <etl/_algorithm/copy.hpp>
8#include <etl/_functional/less.hpp>
9
10namespace etl {
11
12/// \ingroup algorithm
13/// @{
14
15/// \brief Constructs a sorted union beginning at destination consisting of the
16/// set of elements present in one or both sorted ranges `[first1, last1)` and
17/// `[first2, last2)`. The resulting range cannot overlap with either of the
18/// input ranges.
19template <typename InputIt1, typename InputIt2, typename OutputIt, typename Compare>
20constexpr auto
21set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp)
22 -> OutputIt
23{
24 for (; first1 != last1; ++destination) {
25 if (first2 == last2) {
26 return etl::copy(first1, last1, destination);
27 }
28 if (comp(*first2, *first1)) {
29 *destination = *first2++;
30 continue;
31 }
32
33 *destination = *first1;
34 if (not comp(*first1, *first2)) {
35 ++first2;
36 }
37 ++first1;
38 }
39 return etl::copy(first2, last2, destination);
40}
41
42template <typename InputIt1, typename InputIt2, typename OutputIt>
43constexpr auto set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
44 -> OutputIt
45{
46 return etl::set_union(first1, last1, first2, last2, destination, etl::less());
47}
48
49/// @}
50
51} // namespace etl
52
53#endif // TETL_ALGORITHM_SET_UNION_HPP
constexpr auto set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) -> OutputIt
Constructs a sorted union beginning at destination consisting of the set of elements present in one o...
Definition set_union.hpp:43
constexpr auto set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) -> OutputIt
Constructs a sorted union beginning at destination consisting of the set of elements present in one o...
Definition set_union.hpp:21
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15