Compare commits

..

No commits in common. "038ef8f439cf45e6e3eb4343c651ebd62a1e6ccb" and "e942e957126e19a166513334db50e212c7e632c2" have entirely different histories.

2 changed files with 32 additions and 42 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0)
project(task1)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

View File

@ -6,7 +6,6 @@
#include <mutex>
#include <future>
#include <ranges>
#include <span>
namespace algo {
@ -294,36 +293,37 @@ 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)) {
while (l < left.end() && r < right.end()) {
if (cmp(*l, *r)) {
*mid = *l;
l++;
} else {
*mid = *r;
r++;
}
mid++;
}
while (l < left.end()) {
*mid = *l;
mid++;
l++;
} else {
}
while (r < right.end()) {
*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> &output, Comparator cmp, int depth, int &num_threads,
std::mutex &mut) {
static auto split(std::vector<T> &data, std::vector<T> &outdata, Comparator cmp, int depth, int &num_threads, std::mutex &mut) {
if (data.size() <= 1) {
return;
}
@ -332,36 +332,26 @@ namespace algo {
std::advance(mid, std::distance(data.begin(), data.end()) / 2);
std::vector<T> left(data.begin(), mid);
std::vector<T> right(mid, data.end());
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()));
if (depth < num_threads) {
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); });
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); });
left_thread.join();
right_thread.join();
} else {
split(left, output, cmp, depth + 1, num_threads, mut);
split(right, output, cmp, depth + 1, num_threads, mut);
split(left, outdata, cmp, depth + 1, num_threads, mut);
split(right, outdata, cmp, depth + 1, num_threads, 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++;
}
}
mid = outdata.begin();
std::advance(mid, std::distance(outdata.begin(), outdata.end())/2);
merge(mid, left, right, cmp, mut);
}
public: