| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "chetverikova_e_shell_sort_simple_merge/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <thread> | ||
| 6 | #include <utility> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "chetverikova_e_shell_sort_simple_merge/common/include/common.hpp" | ||
| 10 | #include "util/include/util.hpp" | ||
| 11 | |||
| 12 | namespace chetverikova_e_shell_sort_simple_merge { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | ChetverikovaEShellSortSimpleMergeSTL::ChetverikovaEShellSortSimpleMergeSTL(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetInput() = in; |
| 17 | GetOutput().clear(); | ||
| 18 | 24 | } | |
| 19 | |||
| 20 | 24 | bool ChetverikovaEShellSortSimpleMergeSTL::ValidationImpl() { | |
| 21 | 24 | return !(GetInput().empty()); | |
| 22 | } | ||
| 23 | |||
| 24 | 24 | bool ChetverikovaEShellSortSimpleMergeSTL::PreProcessingImpl() { | |
| 25 | 24 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | void ChetverikovaEShellSortSimpleMergeSTL::ShellSort(std::vector<int> &data) { |
| 29 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | if (data.empty()) { |
| 30 | return; | ||
| 31 | } | ||
| 32 | |||
| 33 | size_t n = data.size(); | ||
| 34 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 60 times.
|
152 | for (size_t gap = n / 2; gap > 0; gap /= 2) { |
| 35 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 92 times.
|
444 | for (size_t i = gap; i < n; i++) { |
| 36 | 352 | int temp = data[i]; | |
| 37 | size_t j = i; | ||
| 38 | |||
| 39 |
4/4✓ Branch 0 taken 448 times.
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 228 times.
✓ Branch 3 taken 220 times.
|
580 | while (j >= gap && data[j - gap] > temp) { |
| 40 | 228 | data[j] = data[j - gap]; | |
| 41 | j -= gap; | ||
| 42 | } | ||
| 43 | |||
| 44 | 352 | data[j] = temp; | |
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | bool ChetverikovaEShellSortSimpleMergeSTL::RunImpl() { |
| 50 | const auto &input = GetInput(); | ||
| 51 | auto &output = GetOutput(); | ||
| 52 | |||
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (input.empty()) { |
| 54 | output.clear(); | ||
| 55 | ✗ | return true; | |
| 56 | } | ||
| 57 | |||
| 58 | 24 | const size_t num_threads = ppc::util::GetNumThreads(); | |
| 59 | 24 | const size_t n = input.size(); | |
| 60 | |||
| 61 | 24 | const size_t block_size = (n + num_threads - 1) / num_threads; | |
| 62 | 24 | std::vector<std::vector<int>> blocks(num_threads); | |
| 63 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<std::thread> threads(num_threads); |
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (size_t tid = 0; tid < num_threads; ++tid) { |
| 66 | 60 | threads[tid] = std::thread([&, tid]() { | |
| 67 | 60 | size_t start = tid * block_size; | |
| 68 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 46 times.
|
60 | size_t end = std::min(start + block_size, n); |
| 69 | |||
| 70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | if (start >= n) { |
| 71 | ✗ | return; | |
| 72 | } | ||
| 73 | |||
| 74 | 60 | std::vector<int> local; | |
| 75 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | local.reserve(end - start); |
| 76 | |||
| 77 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 60 times.
|
316 | for (size_t i = start; i < end; ++i) { |
| 78 |
1/2✓ Branch 0 taken 256 times.
✗ Branch 1 not taken.
|
256 | local.push_back(input[i]); |
| 79 | } | ||
| 80 | |||
| 81 | 60 | ShellSort(local); | |
| 82 | |||
| 83 | 60 | blocks[tid] = std::move(local); | |
| 84 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | }); |
| 85 | } | ||
| 86 | |||
| 87 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (auto &th : threads) { |
| 88 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | th.join(); |
| 89 | } | ||
| 90 | |||
| 91 | std::vector<int> result = std::move(blocks[0]); | ||
| 92 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 24 times.
|
60 | for (size_t i = 1; i < num_threads; ++i) { |
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (blocks[i].empty()) { |
| 94 | ✗ | continue; | |
| 95 | } | ||
| 96 | |||
| 97 |
1/4✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
36 | std::vector<int> tmp(result.size() + blocks[i].size()); |
| 98 | |||
| 99 |
1/2✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
|
36 | std::merge(result.begin(), result.end(), blocks[i].begin(), blocks[i].end(), tmp.begin()); |
| 100 | |||
| 101 | result.swap(tmp); | ||
| 102 | } | ||
| 103 | |||
| 104 | output = std::move(result); | ||
| 105 | return true; | ||
| 106 | 24 | } | |
| 107 | |||
| 108 | 24 | bool ChetverikovaEShellSortSimpleMergeSTL::PostProcessingImpl() { | |
| 109 | 24 | return true; | |
| 110 | } | ||
| 111 | |||
| 112 | } // namespace chetverikova_e_shell_sort_simple_merge | ||
| 113 |