| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kamaletdinov_r_bitwise_int/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <functional> | ||
| 7 | #include <thread> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "kamaletdinov_r_bitwise_int/common/include/common.hpp" | ||
| 11 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace kamaletdinov_r_bitwise_int { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | 200 | void CountingSortByDigitSTL(std::vector<int> &arr, int exp) { | |
| 18 | 200 | int n = static_cast<int>(arr.size()); | |
| 19 | 200 | std::vector<int> output(n); | |
| 20 | 200 | std::array<int, 10> count = {}; | |
| 21 | |||
| 22 |
2/2✓ Branch 0 taken 31960 times.
✓ Branch 1 taken 200 times.
|
32160 | for (int i = 0; i < n; i++) { |
| 23 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31960 times.
|
31960 | count.at((arr[i] / exp) % 10)++; |
| 24 | } | ||
| 25 | |||
| 26 |
2/2✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 200 times.
|
2000 | for (int i = 1; i < 10; i++) { |
| 27 | 1800 | count.at(i) += count.at(i - 1); | |
| 28 | } | ||
| 29 | |||
| 30 |
2/2✓ Branch 0 taken 31960 times.
✓ Branch 1 taken 200 times.
|
32160 | for (int i = n - 1; i >= 0; i--) { |
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31960 times.
|
31960 | output[count.at((arr[i] / exp) % 10) - 1] = arr[i]; |
| 32 | 31960 | count.at((arr[i] / exp) % 10)--; | |
| 33 | } | ||
| 34 | |||
| 35 |
1/2✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
|
200 | arr = output; |
| 36 | 200 | } | |
| 37 | |||
| 38 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 8 times.
|
128 | void RadixSortPositiveSTL(std::vector<int> &arr) { |
| 39 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 8 times.
|
128 | if (arr.empty()) { |
| 40 | return; | ||
| 41 | } | ||
| 42 | |||
| 43 | 120 | int max_val = *std::ranges::max_element(arr); | |
| 44 | |||
| 45 |
1/2✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
|
200 | for (int exp = 1; max_val / exp > 0; exp *= 10) { |
| 46 | 200 | CountingSortByDigitSTL(arr, exp); | |
| 47 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 120 times.
|
200 | if (exp > max_val / 10) { |
| 48 | break; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | } // namespace | ||
| 54 | |||
| 55 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 64 times.
|
80 | void BitwiseSortSTL(std::vector<int> &arr) { |
| 56 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 64 times.
|
80 | if (arr.size() <= 1) { |
| 57 | 16 | return; | |
| 58 | } | ||
| 59 | |||
| 60 | 64 | int num_threads = ppc::util::GetNumThreads(); | |
| 61 | |||
| 62 | 64 | std::vector<int> neg; | |
| 63 | 64 | std::vector<int> pos; | |
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 11064 times.
✓ Branch 1 taken 64 times.
|
11128 | for (int x : arr) { |
| 66 |
2/2✓ Branch 0 taken 5480 times.
✓ Branch 1 taken 5584 times.
|
11064 | if (x < 0) { |
| 67 |
1/2✓ Branch 1 taken 5480 times.
✗ Branch 2 not taken.
|
5480 | neg.push_back(-x); |
| 68 | } else { | ||
| 69 | pos.push_back(x); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 |
5/6✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
|
64 | if (num_threads >= 2 && !neg.empty() && !pos.empty()) { |
| 74 |
1/4✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
42 | std::thread neg_thread(RadixSortPositiveSTL, std::ref(neg)); |
| 75 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | RadixSortPositiveSTL(pos); |
| 76 |
1/2✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
|
42 | neg_thread.join(); |
| 77 | } else { | ||
| 78 |
1/2✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
|
22 | RadixSortPositiveSTL(neg); |
| 79 |
1/2✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
|
22 | RadixSortPositiveSTL(pos); |
| 80 | } | ||
| 81 | |||
| 82 | std::size_t idx = 0; | ||
| 83 |
2/2✓ Branch 0 taken 5480 times.
✓ Branch 1 taken 64 times.
|
5544 | for (int i = static_cast<int>(neg.size()) - 1; i >= 0; i--) { |
| 84 | 5480 | arr[idx++] = -neg[i]; | |
| 85 | } | ||
| 86 |
2/2✓ Branch 0 taken 5584 times.
✓ Branch 1 taken 64 times.
|
5648 | for (int p : pos) { |
| 87 | 5584 | arr[idx++] = p; | |
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | 80 | KamaletdinovRBitwiseIntSTL::KamaletdinovRBitwiseIntSTL(const InType &in) { | |
| 92 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 93 | 80 | GetInput() = in; | |
| 94 | GetOutput() = 0; | ||
| 95 | 80 | } | |
| 96 | |||
| 97 | 80 | bool KamaletdinovRBitwiseIntSTL::ValidationImpl() { | |
| 98 | 80 | return GetInput() >= 0; | |
| 99 | } | ||
| 100 | |||
| 101 | 80 | bool KamaletdinovRBitwiseIntSTL::PreProcessingImpl() { | |
| 102 | 80 | int n = GetInput(); | |
| 103 | 80 | data_.resize(n); | |
| 104 |
2/2✓ Branch 0 taken 11072 times.
✓ Branch 1 taken 80 times.
|
11152 | for (int i = 0; i < n; i++) { |
| 105 | 11072 | data_[i] = (n / 2) - i; | |
| 106 | } | ||
| 107 | 80 | return true; | |
| 108 | } | ||
| 109 | |||
| 110 | 80 | bool KamaletdinovRBitwiseIntSTL::RunImpl() { | |
| 111 | 80 | BitwiseSortSTL(data_); | |
| 112 | 80 | return true; | |
| 113 | } | ||
| 114 | |||
| 115 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 8 times.
|
80 | bool KamaletdinovRBitwiseIntSTL::PostProcessingImpl() { |
| 116 | bool sorted = std::ranges::is_sorted(data_); | ||
| 117 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
80 | GetOutput() = sorted ? GetInput() : 0; |
| 118 | 80 | return true; | |
| 119 | } | ||
| 120 | |||
| 121 | } // namespace kamaletdinov_r_bitwise_int | ||
| 122 |