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