tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
unique.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2020 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_UNIQUE_HPP
5#define TETL_ALGORITHM_UNIQUE_HPP
6
7#include <etl/_functional/equal_to.hpp>
8#include <etl/_utility/move.hpp>
9
10namespace etl {
11
12/// \ingroup algorithm
13/// @{
14
15/// Eliminates all except the first element from every consecutive group
16/// of equivalent elements from the range `[first, last)` and returns a
17/// past-the-end iterator for the new logical end of the range.
18template <typename ForwardIt, typename Predicate>
19constexpr auto unique(ForwardIt first, ForwardIt last, Predicate pred) -> ForwardIt
20{
21 if (first == last) {
22 return last;
23 }
24
25 auto result = first;
26 while (++first != last) {
27 if (not pred(*result, *first) and ++result != first) {
28 *result = etl::move(*first);
29 }
30 }
31 return ++result;
32}
33
34template <typename ForwardIt>
35constexpr auto unique(ForwardIt first, ForwardIt last) -> ForwardIt
36{
37 return etl::unique(first, last, etl::equal_to());
38}
39
40/// @}
41
42} // namespace etl
43
44#endif // TETL_ALGORITHM_UNIQUE_HPP
constexpr auto unique(ForwardIt first, ForwardIt last, Predicate pred) -> ForwardIt
Definition unique.hpp:19
constexpr auto unique(ForwardIt first, ForwardIt last) -> ForwardIt
Definition unique.hpp:35
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator== on type T....
Definition equal_to.hpp:15