tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
unique_copy.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_UNIQUE_COPY_HPP
5#define TETL_ALGORITHM_UNIQUE_COPY_HPP
6
7#include <etl/_functional/equal_to.hpp>
8
9namespace etl {
10
11/// \ingroup algorithm
12/// @{
13
14/// Copies the elements from the range `[first, last)`, to another range
15/// beginning at d_first in such a way that there are no consecutive equal
16/// elements. Only the first element of each group of equal elements is copied.
17///
18/// Elements are compared using the given binary predicate pred. The behavior
19/// is undefined if it is not an equivalence relation.
20template <typename InputIt, typename OutputIt, typename Predicate>
21constexpr auto unique_copy(InputIt first, InputIt last, OutputIt destination, Predicate pred) -> OutputIt
22{
23 if (first != last) {
24 *destination = *first;
25
26 while (++first != last) {
27 if (not pred(*destination, *first)) {
28 *++destination = *first;
29 }
30 }
31
32 ++destination;
33 }
34
35 return destination;
36}
37
38template <typename InputIt, typename OutputIt>
39constexpr auto unique_copy(InputIt first, InputIt last, OutputIt destination) -> OutputIt
40{
41 return etl::unique_copy(first, last, destination, etl::equal_to());
42}
43
44/// @}
45
46} // namespace etl
47
48#endif // TETL_ALGORITHM_UNIQUE_COPY_HPP
constexpr auto unique_copy(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Definition unique_copy.hpp:39
constexpr auto unique_copy(InputIt first, InputIt last, OutputIt destination, Predicate pred) -> OutputIt
Definition unique_copy.hpp:21
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator== on type T....
Definition equal_to.hpp:15