tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
stable_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_STABLE_PARTITION_HPP
5#define TETL_ALGORITHM_STABLE_PARTITION_HPP
6
7#include <etl/_algorithm/rotate.hpp>
8
9namespace etl {
10
11/// \brief Reorders the elements in the range `[first, last)` in such a way
12/// that all elements for which the predicate p returns true precede the
13/// elements for which predicate p returns false. Relative order of the
14/// elements is preserved.
15/// \ingroup algorithm
16template <typename BidirIt, typename Predicate>
17constexpr auto stable_partition(BidirIt f, BidirIt l, Predicate p) -> BidirIt
18{
19 auto const n = l - f;
20 if (n == 0) {
21 return f;
22 }
23 if (n == 1) {
24 return f + p(*f);
25 }
26 auto const m = f + (n / 2);
27 return etl::rotate(etl::stable_partition(f, m, p), m, etl::stable_partition(m, l, p));
28}
29
30} // namespace etl
31
32#endif // TETL_ALGORITHM_STABLE_PARTITION_HPP
constexpr auto stable_partition(BidirIt f, BidirIt l, Predicate p) -> BidirIt
Reorders the elements in the range [first, last) in such a way that all elements for which the predic...
Definition stable_partition.hpp:17
Definition adjacent_find.hpp:9