aca-tasks/task1/mergesort_mt.h
2023-11-03 11:41:58 +01:00

54 lines
1.1 KiB
C++

#include <vector>
#include <span>
#include <thread>
#include <mutex>
template<typename T, typename C>
class MergeSorterMT {
C comp;
std::recursive_mutex mut;
auto merge(std::span<T> &output, std::span<T> left, std::span<T> right) -> void {
std::vector<T> buf;
buf.reserve(left.size() + right.size());
auto l = left.begin();
auto r = right.begin();
auto o = buf.begin();
while (l < left.end() && r < right.end()) {
if (cmp(*l, *r)) {
buf.insert(o, *l);
l++;
} else {
buf.insert(o, *r);
r++;
}
o++;
}
while (l < left.end()) {
buf.insert(o, *l);
o++;
l++;
}
while (r < right.end()) {
buf.insert(o, *r);
o++;
r++;
}
{
std::lock_guard<std::recursive_mutex> lock(mut);
std::move(buf.begin(), buf.end(), output.begin());
}
}
auto split(std::vector<T> &data, C cmp, int depth, int &max_depth, std::mutex &mut) -> void {
}
};