Move algo to class

This commit is contained in:
Robin Dietzel 2023-11-01 10:19:25 +01:00
parent ab129bc6c8
commit 78562a45ba
2 changed files with 62 additions and 57 deletions

View File

@ -16,7 +16,7 @@ an arbitrary number of processors, as Amdahls and Gustafson-Barsis laws
require. require.
Assuming that this equal division is possible, estimate α, i.e., the part of the Assuming that this equal division is possible, estimate α, i.e., the part of the
program that can be parallelized, by using a profiler like gprof or valgrind to program that can be parallelized, by using a profiler like gprof or valgrind to
measure the duration of mergesorts execution relative to the overall execution measure the duration of sorts execution relative to the overall execution
time. Use this number to estimate the predicted speedup for your program. time. Use this number to estimate the predicted speedup for your program.
Does α depend on the size of the input? If it does, how should you modify Does α depend on the size of the input? If it does, how should you modify
your predictions and their graphical illustration? your predictions and their graphical illustration?
@ -52,7 +52,7 @@ int main(int argc, char *argv[]) {
auto t1 = std::chrono::high_resolution_clock::now(); auto t1 = std::chrono::high_resolution_clock::now();
algo::mergesort(dataset.begin(), dataset.end(), [](int32_t a, int32_t b) { algo::MergeSort_v1::sort(dataset.begin(), dataset.end(), [](int32_t a, int32_t b) {
return (a > b); return (a > b);
}); });

View File

@ -5,8 +5,10 @@
namespace algo { namespace algo {
class MergeSort_v1 {
private:
template<typename Iterator, typename Comparator> template<typename Iterator, typename Comparator>
void merge(Iterator start, Iterator middle, Iterator end, Comparator cmp, Iterator output_start) { static auto merge(Iterator start, Iterator middle, Iterator end, Comparator cmp, Iterator output_start) -> void {
Iterator start_m = start; Iterator start_m = start;
Iterator begin = output_start; Iterator begin = output_start;
Iterator start2 = middle + 1; Iterator start2 = middle + 1;
@ -44,7 +46,7 @@ namespace algo {
} }
template<typename Container, typename Iterator, typename Comparator> template<typename Container, typename Iterator, typename Comparator>
void ms_split(Container &output_vec, Iterator start, Iterator end, Comparator cmp, Iterator output_start) { static auto ms_split(Container &output_vec, Iterator start, Iterator end, Comparator cmp, Iterator output_start) -> void {
Iterator mid = start; Iterator mid = start;
Iterator begin = output_start; Iterator begin = output_start;
@ -66,10 +68,13 @@ namespace algo {
} }
} }
public:
template<typename Iterator, typename Comparator> template<typename Iterator, typename Comparator>
void mergesort(Iterator start, Iterator end, Comparator cmp) { static auto sort(Iterator start, Iterator end, Comparator cmp) -> void {
using valtype = typename std::iterator_traits<Iterator>::value_type; using valtype = typename std::iterator_traits<Iterator>::value_type;
std::vector<valtype> temporary_dataset(std::distance(start, end)); std::vector<valtype> temporary_dataset(std::distance(start, end));
ms_split(temporary_dataset, start, end - 1, cmp, temporary_dataset.begin()); ms_split(temporary_dataset, start, end - 1, cmp, temporary_dataset.begin());
} }
};
} }