Compare commits

...

2 Commits

Author SHA1 Message Date
038ef8f439 experiments 2023-11-01 21:38:09 +01:00
11190a0eee 1 2023-11-01 21:04:12 +01:00
2 changed files with 42 additions and 32 deletions

View File

@ -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)

View File

@ -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: