2024-03-12 20:32阅读: 5评论: 0推荐: 0

基于 concept 的归并排序

基于 concept 的归并排序

template <std::random_access_iterator RandomIter,
std::random_access_iterator RandomTempIter, typename Comp>
requires requires(Comp comp, RandomIter it, RandomTempIter tmp_it) {
{ comp(*it, *it) } -> std::same_as<bool>;
{ comp(*tmp_it, *tmp_it) } -> std::same_as<bool>;
}
constexpr auto MergeSort(RandomIter begin, RandomIter end,
RandomTempIter temp_begin, RandomTempIter temp_end,
Comp &&comp) -> void {
if (begin + 1 == end) {
return;
}
auto mid = begin + (end - begin) / 2;
auto temp_mid = temp_begin + (temp_end - temp_begin) / 2;
MergeSort(begin, mid, temp_begin, temp_mid, std::forward<Comp>(comp));
MergeSort(mid, end, temp_mid, temp_end, std::forward<Comp>(comp));
auto left_it = begin, right_it = mid;
auto tit = temp_begin;
while (left_it < mid && right_it < end) {
if (comp(*left_it, *right_it)) {
*tit = std::move(*left_it);
++left_it;
} else {
*tit = std::move(*right_it);
++right_it;
}
++tit;
}
while (left_it < mid) {
*tit = std::move(*left_it);
++tit;
++left_it;
}
while (right_it < end) {
*tit = std::move(*right_it);
++tit;
++right_it;
}
while (begin < end) {
*begin = std::move(*temp_begin);
++begin;
++temp_begin;
}
}
template <std::random_access_iterator Iter, typename Comp>
requires requires(Comp comp, Iter it) {
{ comp(*it, *it) } -> std::same_as<bool>;
}
constexpr auto MergeSort(Iter begin, Iter end, Comp &&comp) -> void {
std::vector<typename std::iterator_traits<Iter>::value_type> buffer(end -
begin);
MergeSort(begin, end, std::begin(buffer), std::end(buffer),
std::forward<Comp>(comp));
}

本文作者:フランドール·スカーレット

本文链接:https://www.cnblogs.com/FlandreScarlet/p/18069214

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   フランドール·スカーレット  阅读(5)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Scarborough Fair 山田タマル
  2. 2 Faster Than Light Paradox Interactive,Andreas Waldetoft
  3. 3 世界は可愛く出来ている 上海アリス幻樂団
Scarborough Fair - 山田タマル
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Pual Simon

作曲 : Pual Simon

作词 : イングランド民謡

作曲:加藤达也

Are you going to Scarborough Fair?

Parsley, sage, rosemary and thyme

Remember me to one who lives there

He once was a true love of mine

Tell him to make me a cambric shirt

Tell him to make me a cambric shirt

Parsley, sage, rosemary and thyme

Without no seams nor needlework

Then he'll be a true love of mine.

Tell him to find me an acre of land

Tell him to find me an acre of land

Parsley, sage, rosemary and thyme

Between the salt water and the sea strand

Then he'll be a true love of mine

Are you going to Scarborough Fair?