| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "shelenkova_m_shell_sort_simple_merge/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/tbb.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "shelenkova_m_shell_sort_simple_merge/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace shelenkova_m_shell_sort_simple_merge { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 36 times.
|
144 | void ShellSort(std::vector<int> &data) { |
| 17 | const size_t n = data.size(); | ||
| 18 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 36 times.
|
144 | if (n <= 1) { |
| 19 | return; | ||
| 20 | } | ||
| 21 | |||
| 22 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 108 times.
|
216 | for (size_t gap = n / 2; gap > 0; gap /= 2) { |
| 23 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 108 times.
|
244 | for (size_t i = gap; i < n; ++i) { |
| 24 | 136 | int temp = data[i]; | |
| 25 | size_t j = i; | ||
| 26 |
4/4✓ Branch 0 taken 162 times.
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 62 times.
|
236 | while (j >= gap && data[j - gap] > temp) { |
| 27 | 100 | data[j] = data[j - gap]; | |
| 28 | j -= gap; | ||
| 29 | } | ||
| 30 | 136 | data[j] = temp; | |
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | } // namespace | ||
| 36 | |||
| 37 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | ShelenkovaMShellSortSimpleMergeTBB::ShelenkovaMShellSortSimpleMergeTBB(const InType &in) { |
| 38 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 39 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | GetInput() = in; |
| 40 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | GetOutput() = in; |
| 41 | 52 | } | |
| 42 | |||
| 43 | 52 | bool ShelenkovaMShellSortSimpleMergeTBB::ValidationImpl() { | |
| 44 | 52 | return !GetInput().empty(); | |
| 45 | } | ||
| 46 | |||
| 47 | 52 | bool ShelenkovaMShellSortSimpleMergeTBB::PreProcessingImpl() { | |
| 48 | 52 | GetOutput() = GetInput(); | |
| 49 | 52 | return true; | |
| 50 | } | ||
| 51 | |||
| 52 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 4 times.
|
52 | bool ShelenkovaMShellSortSimpleMergeTBB::RunImpl() { |
| 53 | const std::vector<int> &input = GetInput(); | ||
| 54 | std::vector<int> &output = GetOutput(); | ||
| 55 | |||
| 56 | 52 | const size_t n = input.size(); | |
| 57 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 4 times.
|
52 | if (n <= 1) { |
| 58 | return true; | ||
| 59 | } | ||
| 60 | |||
| 61 | 48 | const int raw_concurrency = tbb::this_task_arena::max_concurrency(); | |
| 62 | 48 | const size_t num_threads = std::min(n, static_cast<size_t>(std::max(1, raw_concurrency))); | |
| 63 | 48 | const size_t block_size = (n + num_threads - 1) / num_threads; | |
| 64 | |||
| 65 | 48 | std::vector<std::vector<int>> blocks(num_threads); | |
| 66 | |||
| 67 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
224 | tbb::parallel_for(tbb::blocked_range<size_t>(0, num_threads), [&](const tbb::blocked_range<size_t> &r) { |
| 68 |
2/2✓ Branch 0 taken 176 times.
✓ Branch 1 taken 176 times.
|
352 | for (size_t block_id = r.begin(); block_id < r.end(); ++block_id) { |
| 69 | 176 | const size_t start = block_id * block_size; | |
| 70 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 144 times.
|
176 | if (start >= n) { |
| 71 | 32 | continue; | |
| 72 | } | ||
| 73 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 112 times.
|
144 | const size_t end = std::min(start + block_size, n); |
| 74 | 144 | blocks[block_id].assign(input.begin() + static_cast<std::ptrdiff_t>(start), | |
| 75 | 144 | input.begin() + static_cast<std::ptrdiff_t>(end)); | |
| 76 | 144 | ShellSort(blocks[block_id]); | |
| 77 | } | ||
| 78 | 176 | }); | |
| 79 | |||
| 80 | 48 | std::vector<int> result; | |
| 81 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | result.reserve(n); |
| 82 |
2/2✓ Branch 0 taken 176 times.
✓ Branch 1 taken 48 times.
|
224 | for (size_t i = 0; i < num_threads; ++i) { |
| 83 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 144 times.
|
176 | if (blocks[i].empty()) { |
| 84 | 32 | continue; | |
| 85 | } | ||
| 86 |
1/4✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
144 | std::vector<int> tmp(result.size() + blocks[i].size()); |
| 87 | 144 | std::merge(result.begin(), result.end(), blocks[i].begin(), blocks[i].end(), tmp.begin()); | |
| 88 | result = std::move(tmp); | ||
| 89 | } | ||
| 90 | |||
| 91 | output = std::move(result); | ||
| 92 | return std::ranges::is_sorted(output); | ||
| 93 | 48 | } | |
| 94 | |||
| 95 | 52 | bool ShelenkovaMShellSortSimpleMergeTBB::PostProcessingImpl() { | |
| 96 | 52 | return !GetOutput().empty(); | |
| 97 | } | ||
| 98 | |||
| 99 | } // namespace shelenkova_m_shell_sort_simple_merge | ||
| 100 |