Compare commits
2 Commits
e942e95712
...
038ef8f439
Author | SHA1 | Date | |
---|---|---|---|
038ef8f439 | |||
11190a0eee |
@ -1,7 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(task1)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <mutex>
|
||||
#include <future>
|
||||
#include <ranges>
|
||||
#include <span>
|
||||
|
||||
namespace algo {
|
||||
|
||||
@ -293,37 +294,36 @@ namespace algo {
|
||||
static auto
|
||||
merge(typename std::vector<T>::iterator mid, std::vector<T> &left, std::vector<T> &right,
|
||||
Comparator cmp, std::mutex &mut) {
|
||||
{
|
||||
std::lock_guard lock(mut);
|
||||
|
||||
auto l = left.begin();
|
||||
auto r = right.begin();
|
||||
auto l = left.begin();
|
||||
auto r = right.begin();
|
||||
|
||||
while (l < left.end() && r < right.end()) {
|
||||
if (cmp(*l, *r)) {
|
||||
*mid = *l;
|
||||
l++;
|
||||
} else {
|
||||
*mid = *r;
|
||||
r++;
|
||||
}
|
||||
mid++;
|
||||
}
|
||||
while (l < left.end()) {
|
||||
while (l < left.end() && r < right.end()) {
|
||||
if (cmp(*l, *r)) {
|
||||
*mid = *l;
|
||||
mid++;
|
||||
l++;
|
||||
}
|
||||
while (r < right.end()) {
|
||||
} else {
|
||||
*mid = *r;
|
||||
mid++;
|
||||
r++;
|
||||
}
|
||||
mid++;
|
||||
}
|
||||
while (l < left.end()) {
|
||||
*mid = *l;
|
||||
mid++;
|
||||
l++;
|
||||
}
|
||||
while (r < right.end()) {
|
||||
*mid = *r;
|
||||
mid++;
|
||||
r++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<typename T, typename Comparator>
|
||||
static auto split(std::vector<T> &data, std::vector<T> &outdata, Comparator cmp, int depth, int &num_threads, std::mutex &mut) {
|
||||
static auto split(std::vector<T> &data, std::vector<T> &output, Comparator cmp, int depth, int &num_threads,
|
||||
std::mutex &mut) {
|
||||
if (data.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
@ -332,26 +332,36 @@ namespace algo {
|
||||
std::advance(mid, std::distance(data.begin(), data.end()) / 2);
|
||||
|
||||
|
||||
|
||||
|
||||
std::vector<T> left(std::make_move_iterator(data.begin()), std::make_move_iterator(mid));
|
||||
std::vector<T> right(std::make_move_iterator(mid), std::make_move_iterator(data.end()));
|
||||
std::vector<T> left(data.begin(), mid);
|
||||
std::vector<T> right(mid, data.end());
|
||||
|
||||
if (depth < num_threads) {
|
||||
std::thread left_thread([&]() { split(left, outdata, cmp, depth + 1, num_threads, mut); });
|
||||
std::thread right_thread([&]() { split(right, outdata, cmp, depth + 1, num_threads, mut); });
|
||||
std::thread left_thread([&]() { split(left, output, cmp, depth + 1, num_threads, mut); });
|
||||
std::thread right_thread([&]() { split(right, output, cmp, depth + 1, num_threads, mut); });
|
||||
|
||||
left_thread.join();
|
||||
right_thread.join();
|
||||
} else {
|
||||
split(left, outdata, cmp, depth + 1, num_threads, mut);
|
||||
split(right, outdata, cmp, depth + 1, num_threads, mut);
|
||||
split(left, output, cmp, depth + 1, num_threads, mut);
|
||||
split(right, output, cmp, depth + 1, num_threads, mut);
|
||||
}
|
||||
|
||||
|
||||
mid = outdata.begin();
|
||||
std::advance(mid, std::distance(outdata.begin(), outdata.end())/2);
|
||||
merge(mid, left, right, cmp, mut);
|
||||
mid = output.begin();
|
||||
std::advance(mid, std::distance(output.begin(), output.end()) / 2);
|
||||
|
||||
std::vector<T> buf;
|
||||
buf.reserve(data.size());
|
||||
merge(buf.begin(), left, right, cmp, mut);
|
||||
{
|
||||
std::lock_guard lock(mut);
|
||||
auto bufi = buf.begin();
|
||||
for(auto &element : output) {
|
||||
element = *bufi;
|
||||
bufi++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
Loading…
Reference in New Issue
Block a user