Difference between revisions of "cpp/algorithm/inplace merge"
(My mistake. Put back old text.) |
m (fmt.) |
||
(21 intermediate revisions by 8 users not shown) | |||
Line 2: | Line 2: | ||
{{cpp/algorithm/navbar}} | {{cpp/algorithm/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | algorithm}} | + | {{dcl header|algorithm}} |
− | {{ | + | {{dcla|num=1|constexpr=c++26| |
template< class BidirIt > | template< class BidirIt > | ||
void inplace_merge( BidirIt first, BidirIt middle, BidirIt last ); | void inplace_merge( BidirIt first, BidirIt middle, BidirIt last ); | ||
}} | }} | ||
− | {{dcl | since=c++17 | | + | {{dcl|num=2|since=c++17| |
template< class ExecutionPolicy, class BidirIt > | template< class ExecutionPolicy, class BidirIt > | ||
− | void inplace_merge( ExecutionPolicy&& policy, BidirIt first, BidirIt middle, BidirIt last );}} | + | void inplace_merge( ExecutionPolicy&& policy, |
− | {{ | + | BidirIt first, BidirIt middle, BidirIt last ); |
− | template< class BidirIt, class Compare> | + | }} |
− | void inplace_merge( BidirIt first, BidirIt middle, BidirIt last, Compare comp ); | + | {{dcla|num=3|constexpr=c++26| |
+ | template< class BidirIt, class Compare > | ||
+ | void inplace_merge( BidirIt first, BidirIt middle, BidirIt last, | ||
+ | Compare comp ); | ||
+ | }} | ||
+ | {{dcl|num=4|since=c++17| | ||
+ | template< class ExecutionPolicy, class BidirIt, class Compare > | ||
+ | void inplace_merge( ExecutionPolicy&& policy, | ||
+ | BidirIt first, BidirIt middle, BidirIt last, | ||
+ | Compare comp ); | ||
}} | }} | ||
− | |||
− | |||
− | |||
{{dcl end}} | {{dcl end}} | ||
− | Merges two consecutive sorted ranges {{ | + | Merges two consecutive sorted ranges {{range|first|middle}} and {{range|middle|last}} into one sorted range {{range|first|last}}. |
− | @1@ | + | @1@ If {{range|first|middle}} or {{range|middle|last}} is not {{rlp|/#Requirements|sorted}} with respect to {{rev inl|until=c++20|{{c/core|operator<}}}}{{rev inl|since=c++20|{{c|std::less{}<!---->}}}}, the behavior is undefined. |
− | @3@ | + | |
− | @2,4@ Same as {{v|1,3}}, but executed according to {{ | + | @3@ If {{range|first|middle}} or {{range|middle|last}} is not sorted with respect to {{c|comp}}, the behavior is undefined. |
+ | |||
+ | @2,4@ Same as {{v|1,3}}, but executed according to {{c|policy}}. | ||
+ | @@ {{cpp/algorithm/parallel overload precondition|plural=yes}} | ||
+ | |||
+ | This merge function is stable, which means that for equivalent elements in the original two ranges, the elements from the first range (preserving their original order) precede the elements from the second range (preserving their original order). | ||
+ | |||
+ | If any of the following conditions is satisfied, the behavior is undefined: | ||
+ | * {{range|first|middle}} or {{range|middle|last}} is not a [[cpp/iterator#Ranges|valid range]]. | ||
+ | * The output range overlaps with {{range|first|middle}} or {{range|middle|last}}. | ||
+ | {{rev begin}} | ||
+ | {{rev|until=c++11| | ||
+ | * The type of {{c|*first}} is not {{named req|Swappable}}. | ||
+ | }} | ||
+ | {{rev|since=c++11| | ||
+ | * {{tt|BiditIt}} is not {{named req|ValueSwappable}}. | ||
+ | * The type of {{c|*first}} is not {{named req|MoveConstructible}}. | ||
+ | * The type of {{c|*first}} is not {{named req|MoveAssignable}}. | ||
+ | }} | ||
+ | {{rev end}} | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | first | the beginning of the first sorted range }} | + | {{par|first|the beginning of the first sorted range}} |
− | {{par | middle | the end of the first sorted range and the beginning of the second }} | + | {{par|middle|the end of the first sorted range and the beginning of the second}} |
− | {{par | last | the end of the second sorted range }} | + | {{par|last|the end of the second sorted range}} |
{{par exec pol}} | {{par exec pol}} | ||
− | {{par cmp ord | comp | p1=BidirIt}} | + | {{par cmp ord|comp|p1=BidirIt}} |
{{par hreq}} | {{par hreq}} | ||
− | {{par req named | BidirIt | BidirectionalIterator | + | {{par req named|BidirIt|BidirectionalIterator}} |
− | {{par req named | + | {{par req named|Compare|Compare}} |
{{par end}} | {{par end}} | ||
− | |||
− | |||
− | |||
===Complexity=== | ===Complexity=== | ||
− | Given {{ | + | Given {{mathjax-or|\(\scriptsize N\)|N}} as {{c|std::distance(first, last)}}: |
− | @1 | + | |
− | @ | + | @1@ Exactly {{mathjax-or|\(\scriptsize N-1\)|N-1}} comparisons using {{rev inl|until=c++20|{{c/core|operator<}}}}{{rev inl|since=c++20|{{c|std::less{}<!---->}}}} if enough additional memory is available, {{mathjax-or|\(\scriptsize O(N \cdot \log(N))\)|O(N⋅log(N))}} comparisons otherwise. |
+ | |||
+ | @2@ {{mathjax-or|\(\scriptsize O(N \cdot \log(N))\)|O(N⋅log(N))}} comparisons using {{rev inl|until=c++20|{{c/core|operator<}}}}{{rev inl|since=c++20|{{c|std::less{}<!---->}}}}. | ||
+ | |||
+ | @3@ Exactly {{mathjax-or|\(\scriptsize N-1\)|N-1}} applications of the comparison function {{c|comp}} if enough additional memory is available, {{mathjax-or|\(\scriptsize O(N \cdot \log(N))\)|O(N⋅log(N))}} applications otherwise. | ||
+ | |||
+ | @4@ {{mathjax-or|\(\scriptsize O(N \cdot \log(N))\)|O(N⋅log(N))}} applications of the comparison function {{c|comp}}. | ||
===Exceptions=== | ===Exceptions=== | ||
− | {{cpp/algorithm/ | + | {{cpp/algorithm/parallel exceptions reporting behavior|singular=no}} |
+ | |||
+ | ===Possible implementation=== | ||
+ | See the implementations in [https://github.com/gcc-mirror/gcc/blob/d9375e490072d1aae73a93949aa158fcd2a27018/libstdc%2B%2B-v3/include/bits/stl_algo.h#L2508 libstdc++] and [https://github.com/llvm-mirror/libcxx/blob/a12cb9d211019d99b5875b6d8034617cbc24c2cc/include/algorithm#L4452 libc++]. | ||
===Notes=== | ===Notes=== | ||
This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen. | This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen. | ||
+ | |||
+ | {{feature test macro|__cpp_lib_constexpr_algorithms|{{c/core|constexpr}} inplace merging {{vl|1}}, {{vl|3}}|value=202306L|std=C++26}} | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |The following code is an implementation of merge sort. | |
− | + | |code= | |
− | + | ||
− | + | ||
#include <algorithm> | #include <algorithm> | ||
+ | #include <iostream> | ||
+ | #include <vector> | ||
template<class Iter> | template<class Iter> | ||
void merge_sort(Iter first, Iter last) | void merge_sort(Iter first, Iter last) | ||
{ | { | ||
− | if (last - first > 1) { | + | if (last - first > 1) |
+ | { | ||
Iter middle = first + (last - first) / 2; | Iter middle = first + (last - first) / 2; | ||
merge_sort(first, middle); | merge_sort(first, middle); | ||
Line 74: | Line 108: | ||
std::vector<int> v{8, 2, -2, 0, 11, 11, 1, 7, 3}; | std::vector<int> v{8, 2, -2, 0, 11, 11, 1, 7, 3}; | ||
merge_sort(v.begin(), v.end()); | merge_sort(v.begin(), v.end()); | ||
− | for(auto n : v) | + | for (const auto& n : v) |
std::cout << n << ' '; | std::cout << n << ' '; | ||
− | |||
std::cout << '\n'; | std::cout << '\n'; | ||
} | } | ||
− | + | |output= | |
-2 0 1 2 3 7 8 11 11 | -2 0 1 2 3 7 8 11 11 | ||
}} | }} | ||
Line 85: | Line 118: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/algorithm/dsc merge}} | + | {{dsc inc|cpp/algorithm/dsc merge}} |
− | {{dsc inc | cpp/algorithm/dsc sort}} | + | {{dsc inc|cpp/algorithm/dsc sort}} |
− | {{dsc inc | cpp/algorithm/dsc stable_sort}} | + | {{dsc inc|cpp/algorithm/dsc stable_sort}} |
− | + | {{dsc inc|cpp/algorithm/ranges/dsc inplace_merge}} | |
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 04:12, 10 July 2024
Defined in header <algorithm>
|
||
template< class BidirIt > void inplace_merge( BidirIt first, BidirIt middle, BidirIt last ); |
(1) | (constexpr since C++26) |
template< class ExecutionPolicy, class BidirIt > void inplace_merge( ExecutionPolicy&& policy, |
(2) | (since C++17) |
template< class BidirIt, class Compare > void inplace_merge( BidirIt first, BidirIt middle, BidirIt last, |
(3) | (constexpr since C++26) |
template< class ExecutionPolicy, class BidirIt, class Compare > void inplace_merge( ExecutionPolicy&& policy, |
(4) | (since C++17) |
Merges two consecutive sorted ranges [
first,
middle)
and [
middle,
last)
into one sorted range [
first,
last)
.
[
first,
middle)
or [
middle,
last)
is not sorted with respect to operator<(until C++20)std::less{}(since C++20), the behavior is undefined.[
first,
middle)
or [
middle,
last)
is not sorted with respect to comp, the behavior is undefined.
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true. |
(until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true. |
(since C++20) |
This merge function is stable, which means that for equivalent elements in the original two ranges, the elements from the first range (preserving their original order) precede the elements from the second range (preserving their original order).
If any of the following conditions is satisfied, the behavior is undefined:
-
[
first,
middle)
or[
middle,
last)
is not a valid range. - The output range overlaps with
[
first,
middle)
or[
middle,
last)
.
|
(until C++11) |
|
(since C++11) |
Contents |
[edit] Parameters
first | - | the beginning of the first sorted range |
middle | - | the end of the first sorted range and the beginning of the second |
last | - | the end of the second sorted range |
policy | - | the execution policy to use. See execution policy for details. |
comp | - | comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than (i.e. is ordered before) the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1& a, const Type2& b); While the signature does not need to have const&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) |
Type requirements | ||
-BidirIt must meet the requirements of LegacyBidirectionalIterator.
| ||
-Compare must meet the requirements of Compare.
|
[edit] Complexity
Given N as std::distance(first, last):
[edit] Exceptions
The overloads with a template parameter named ExecutionPolicy
report errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicy
is one of the standard policies, std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined. - If the algorithm fails to allocate memory, std::bad_alloc is thrown.
[edit] Possible implementation
See the implementations in libstdc++ and libc++.
[edit] Notes
This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_constexpr_algorithms |
202306L | (C++26) | constexpr inplace merging (1), (3) |
[edit] Example
The following code is an implementation of merge sort.
#include <algorithm> #include <iostream> #include <vector> template<class Iter> void merge_sort(Iter first, Iter last) { if (last - first > 1) { Iter middle = first + (last - first) / 2; merge_sort(first, middle); merge_sort(middle, last); std::inplace_merge(first, middle, last); } } int main() { std::vector<int> v{8, 2, -2, 0, 11, 11, 1, 7, 3}; merge_sort(v.begin(), v.end()); for (const auto& n : v) std::cout << n << ' '; std::cout << '\n'; }
Output:
-2 0 1 2 3 7 8 11 11
[edit] See also
merges two sorted ranges (function template) | |
sorts a range into ascending order (function template) | |
sorts a range of elements while preserving order between equal elements (function template) | |
(C++20) |
merges two ordered ranges in-place (niebloid) |