tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
set_intersection.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_INTERSECTION_HPP
5#define TETL_ALGORITHM_SET_INTERSECTION_HPP
6
7#include <etl/_functional/less.hpp>
8
9namespace etl {
10
11/// \ingroup algorithm
12/// @{
13
14/// \brief Constructs a sorted range beginning at `dest` consisting of elements
15/// that are found in both sorted ranges `[first1, last1)` and `[first2,
16/// last2)`. If some element is found `m` times in `[first1, last1)` and n times
17/// in `[first2, last2)`, the first `min(m, n)` elements will be copied from the
18/// first range to the destination range. The order of equivalent elements is
19/// preserved. The resulting range cannot overlap with either of the input
20/// ranges.
21template <typename InputIt1, typename InputIt2, typename OutputIt, typename Compare>
22constexpr auto
23set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt dest, Compare comp)
24 -> OutputIt
25{
26 while (first1 != last1 and first2 != last2) {
27 if (comp(*first1, *first2)) {
28 ++first1;
29 } else {
30 if (not comp(*first2, *first1)) {
31 *dest++ = *first1++;
32 }
33 ++first2;
34 }
35 }
36 return dest;
37}
38
39template <typename InputIt1, typename InputIt2, typename OutputIt>
40constexpr auto set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt dest)
41 -> OutputIt
42{
43 return etl::set_intersection(first1, last1, first2, last2, dest, etl::less());
44}
45
46/// @}
47
48} // namespace etl
49
50#endif // TETL_ALGORITHM_SET_INTERSECTION_HPP
constexpr auto set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt dest) -> OutputIt
Constructs a sorted range beginning at dest consisting of elements that are found in both sorted rang...
Definition set_intersection.hpp:40
constexpr auto set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt dest, Compare comp) -> OutputIt
Constructs a sorted range beginning at dest consisting of elements that are found in both sorted rang...
Definition set_intersection.hpp:23
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15