| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "trofimov_n_hoar_sort_batcher/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <vector> | ||
| 5 | |||
| 6 | #include "oneapi/tbb/task_group.h" | ||
| 7 | #include "trofimov_n_hoar_sort_batcher/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace trofimov_n_hoar_sort_batcher { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | ✗ | int HoarePartition(std::vector<int> &arr, int left, int right) { | |
| 14 | ✗ | const int pivot = arr[left + ((right - left) / 2)]; | |
| 15 | ✗ | int i = left - 1; | |
| 16 | ✗ | int j = right + 1; | |
| 17 | |||
| 18 | while (true) { | ||
| 19 | ✗ | while (arr[++i] < pivot) { | |
| 20 | } | ||
| 21 | |||
| 22 | ✗ | while (arr[--j] > pivot) { | |
| 23 | } | ||
| 24 | |||
| 25 | ✗ | if (i >= j) { | |
| 26 | ✗ | return j; | |
| 27 | } | ||
| 28 | |||
| 29 | std::swap(arr[i], arr[j]); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | 42 | void QuickSortTbbTask(std::vector<int> &arr, int left, int right, int depth_limit) { | |
| 34 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (left >= right) { |
| 35 | 42 | return; | |
| 36 | } | ||
| 37 | |||
| 38 | constexpr int kSequentialThreshold = 1024; | ||
| 39 | |||
| 40 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
42 | if ((right - left) < kSequentialThreshold || depth_limit <= 0) { |
| 41 | 42 | std::sort(arr.begin() + left, arr.begin() + right + 1); | |
| 42 | 42 | return; | |
| 43 | } | ||
| 44 | |||
| 45 | ✗ | const int split = HoarePartition(arr, left, right); | |
| 46 | |||
| 47 | oneapi::tbb::task_group tg; | ||
| 48 | ✗ | if ((split - left) > kSequentialThreshold) { | |
| 49 | ✗ | tg.run([&arr, left, split, depth_limit]() { QuickSortTbbTask(arr, left, split, depth_limit - 1); }); | |
| 50 | } else { | ||
| 51 | ✗ | std::sort(arr.begin() + left, arr.begin() + split + 1); | |
| 52 | } | ||
| 53 | |||
| 54 | ✗ | if ((right - (split + 1)) > kSequentialThreshold) { | |
| 55 | ✗ | tg.run([&arr, split, right, depth_limit]() { QuickSortTbbTask(arr, split + 1, right, depth_limit - 1); }); | |
| 56 | } else { | ||
| 57 | ✗ | std::sort(arr.begin() + split + 1, arr.begin() + right + 1); | |
| 58 | } | ||
| 59 | |||
| 60 | ✗ | tg.wait(); | |
| 61 | } | ||
| 62 | |||
| 63 | } // namespace | ||
| 64 | |||
| 65 |
1/2✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
|
50 | TrofimovNHoarSortBatcherTBB::TrofimovNHoarSortBatcherTBB(const InType &in) { |
| 66 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 67 |
1/2✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
|
50 | GetInput() = in; |
| 68 | 50 | } | |
| 69 | |||
| 70 | 50 | bool TrofimovNHoarSortBatcherTBB::ValidationImpl() { | |
| 71 | 50 | return true; | |
| 72 | } | ||
| 73 | |||
| 74 | 50 | bool TrofimovNHoarSortBatcherTBB::PreProcessingImpl() { | |
| 75 | 50 | GetOutput() = GetInput(); | |
| 76 | 50 | return true; | |
| 77 | } | ||
| 78 | |||
| 79 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 8 times.
|
50 | bool TrofimovNHoarSortBatcherTBB::RunImpl() { |
| 80 | auto &data = GetOutput(); | ||
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 8 times.
|
50 | if (data.size() > 1) { |
| 83 | 42 | QuickSortTbbTask(data, 0, static_cast<int>(data.size()) - 1, 4); | |
| 84 | } | ||
| 85 | |||
| 86 | 50 | return true; | |
| 87 | } | ||
| 88 | |||
| 89 | 50 | bool TrofimovNHoarSortBatcherTBB::PostProcessingImpl() { | |
| 90 | 50 | return true; | |
| 91 | } | ||
| 92 | |||
| 93 | } // namespace trofimov_n_hoar_sort_batcher | ||
| 94 |