tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
inplace_merge.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2023 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_INPLACE_MERGE_HPP
5#define TETL_ALGORITHM_INPLACE_MERGE_HPP
6
7#include <etl/_algorithm/move_backward.hpp>
8#include <etl/_functional/less.hpp>
9#include <etl/_utility/move.hpp>
10
11namespace etl {
12
13/// Merges two consecutive sorted ranges [first, middle) and
14/// [middle, last) into one sorted range [first, last).
15///
16/// A sequence [first, last) is said to be sorted with respect to
17/// a comparator comp if for any iterator it pointing to the sequence
18/// and any non-negative integer n such that it + n is a valid
19/// iterator pointing to an element of the sequence, comp(*(it + n), *it)
20/// evaluates to false.
21///
22/// https://en.cppreference.com/w/cpp/algorithm/inplace_merge
23///
24/// \ingroup algorithm
25template <typename BidirIt, typename Compare>
26constexpr auto inplace_merge(BidirIt begin, BidirIt mid, BidirIt end, Compare comp) -> void
27{
28 auto left = begin;
29 auto right = mid;
30 while (left != mid and right != end) {
31 if (comp(*right, *left)) {
32 auto value = etl::move(*right);
33 etl::move_backward(left, mid, mid + 1);
34 *left = etl::move(value);
35 ++right;
36 ++mid;
37 } else {
38 ++left;
39 }
40 }
41}
42
43/// \ingroup algorithm
44template <typename BidirIt>
45constexpr auto inplace_merge(BidirIt first, BidirIt mid, BidirIt last) -> void
46{
47 etl::inplace_merge(first, mid, last, etl::less());
48}
49
50} // namespace etl
51
52#endif // TETL_ALGORITHM_INPLACE_MERGE_HPP
constexpr auto inplace_merge(BidirIt first, BidirIt mid, BidirIt last) -> void
Definition inplace_merge.hpp:45
constexpr auto inplace_merge(BidirIt begin, BidirIt mid, BidirIt end, Compare comp) -> void
Merges two consecutive sorted ranges [first, middle) and [middle, last) into one sorted range [first,...
Definition inplace_merge.hpp:26
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15