tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
partition.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_PARTITION_HPP
5#define TETL_ALGORITHM_PARTITION_HPP
6
7#include <etl/_algorithm/find_if_not.hpp>
8#include <etl/_algorithm/iter_swap.hpp>
9#include <etl/_iterator/next.hpp>
10
11namespace etl {
12
13/// \brief Reorders the elements in the range `[first, last)` in such a way that
14/// all elements for which the predicate p returns true precede the elements for
15/// which predicate p returns false. Relative order of the elements is not
16/// preserved.
17/// \ingroup algorithm
18template <typename ForwardIt, typename Predicate>
19constexpr auto partition(ForwardIt first, ForwardIt last, Predicate p) -> ForwardIt
20{
21 first = etl::find_if_not(first, last, p);
22 if (first == last) {
23 return first;
24 }
25
26 for (ForwardIt i = etl::next(first); i != last; ++i) {
27 if (p(*i)) {
28 etl::iter_swap(i, first);
29 ++first;
30 }
31 }
32 return first;
33}
34
35} // namespace etl
36
37#endif // TETL_ALGORITHM_PARTITION_HPP
constexpr auto partition(ForwardIt first, ForwardIt last, Predicate p) -> ForwardIt
Reorders the elements in the range [first, last) in such a way that all elements for which the predic...
Definition partition.hpp:19
Definition adjacent_find.hpp:9