| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "melnik_i_radix_sort_int/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <utility> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "melnik_i_radix_sort_int/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace melnik_i_radix_sort_int { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | constexpr int kBitsPerDigit = 8; | ||
| 15 | constexpr int kBuckets = 1 << kBitsPerDigit; | ||
| 16 | |||
| 17 | } // namespace | ||
| 18 | |||
| 19 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | MelnikIRadixSortIntSEQ::MelnikIRadixSortIntSEQ(const InType &in) { |
| 20 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 21 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | GetInput() = in; |
| 22 | GetOutput() = {}; | ||
| 23 | 56 | } | |
| 24 | |||
| 25 | 56 | bool MelnikIRadixSortIntSEQ::ValidationImpl() { | |
| 26 | 56 | return !GetInput().empty(); | |
| 27 | } | ||
| 28 | |||
| 29 | 56 | bool MelnikIRadixSortIntSEQ::PreProcessingImpl() { | |
| 30 | 56 | GetOutput() = GetInput(); | |
| 31 | 56 | return !GetOutput().empty(); | |
| 32 | } | ||
| 33 | |||
| 34 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | bool MelnikIRadixSortIntSEQ::RunImpl() { |
| 35 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | if (GetOutput().empty()) { |
| 36 | return false; | ||
| 37 | } | ||
| 38 | 56 | RadixSort(GetOutput()); | |
| 39 | 56 | return !GetOutput().empty(); | |
| 40 | } | ||
| 41 | |||
| 42 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | bool MelnikIRadixSortIntSEQ::PostProcessingImpl() { |
| 43 | 56 | return std::ranges::is_sorted(GetOutput()); | |
| 44 | } | ||
| 45 | |||
| 46 | ✗ | int MelnikIRadixSortIntSEQ::GetMaxValue(const OutType &data) { | |
| 47 | 56 | return *std::ranges::max_element(data); | |
| 48 | } | ||
| 49 | |||
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | void MelnikIRadixSortIntSEQ::CountingSort(OutType &data, int exp, int offset) { |
| 51 | 56 | const auto n = static_cast<int>(data.size()); | |
| 52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | if (n == 0) { |
| 53 | ✗ | return; | |
| 54 | } | ||
| 55 | |||
| 56 | 56 | OutType output(n); | |
| 57 | 56 | std::array<int, kBuckets> count{}; | |
| 58 | count.fill(0); | ||
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 56 times.
|
344 | for (int i = 0; i < n; i++) { |
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
|
288 | int digit = ((data[i] + offset) / exp) % kBuckets; |
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
|
288 | count.at(digit)++; |
| 63 | } | ||
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 14280 times.
✓ Branch 1 taken 56 times.
|
14336 | for (int i = 1; i < kBuckets; i++) { |
| 66 | 14280 | count.at(i) += count.at(i - 1); | |
| 67 | } | ||
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 56 times.
|
344 | for (int i = n - 1; i >= 0; i--) { |
| 70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
|
288 | int digit = ((data[i] + offset) / exp) % kBuckets; |
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
|
288 | output[count.at(digit) - 1] = data[i]; |
| 72 | 288 | count.at(digit)--; | |
| 73 | } | ||
| 74 | |||
| 75 | data = std::move(output); | ||
| 76 | } | ||
| 77 | |||
| 78 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | void MelnikIRadixSortIntSEQ::RadixSort(OutType &data) { |
| 79 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | if (data.empty()) { |
| 80 | return; | ||
| 81 | } | ||
| 82 | |||
| 83 | int max_val = GetMaxValue(data); | ||
| 84 | 56 | int min_val = *std::ranges::min_element(data); | |
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 16 times.
|
56 | if (min_val >= 0) { |
| 87 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 40 times.
|
80 | for (int exp = 1; max_val / exp > 0; exp <<= kBitsPerDigit) { |
| 88 | 40 | CountingSort(data, exp, 0); | |
| 89 | } | ||
| 90 | return; | ||
| 91 | } | ||
| 92 | |||
| 93 | 16 | int offset = -min_val; | |
| 94 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
|
32 | for (int exp = 1; (max_val + offset) / exp > 0 || (min_val + offset) / exp > 0; exp <<= kBitsPerDigit) { |
| 95 | 16 | CountingSort(data, exp, offset); | |
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | } // namespace melnik_i_radix_sort_int | ||
| 100 |